Migrated to Gradle build
This commit migrates from a Maven-based build system to a Gradle-based one. Changes include: - Removed archetype & parent - Renamed core, support, test, security and xml directories to spring-ws-core, spring-ws-test, spring-ws-security, spring-xml respectively. - Moved samples to separate project (https://github.com/spring-projects/spring-ws-samples)
This commit is contained in:
committed by
Arjen Poutsma
parent
a8c1d2ad97
commit
843ca6d2ef
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2005-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Helper class used to find the current version of JAXP. We cannot depend on the Java version, since JAXP can be
|
||||
* upgraded independently of the Java version.
|
||||
* <p/>
|
||||
* Only distinguishes between JAXP 1.0, 1.1, 1.3, and 1.4, since JAXP 1.2 was a maintenance release with no new
|
||||
* classes.
|
||||
* <p>
|
||||
* Note that Spring-WS requires JDK 1.5 as of Spring-WS 2.0, and therefore has at least JAXP 1.3 available.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class JaxpVersion {
|
||||
|
||||
/**
|
||||
* Constant identifying JAXP 1.0.
|
||||
*/
|
||||
public static final int JAXP_10 = 0;
|
||||
|
||||
/**
|
||||
* Constant identifying JAXP 1.1.
|
||||
*/
|
||||
public static final int JAXP_11 = 1;
|
||||
|
||||
/**
|
||||
* Constant identifying JAXP 1.3.
|
||||
*/
|
||||
public static final int JAXP_13 = 3;
|
||||
|
||||
/**
|
||||
* Constant identifying JAXP 1.4.
|
||||
*/
|
||||
public static final int JAXP_14 = 4;
|
||||
|
||||
private static final String JAXP_14_CLASS_NAME = "javax.xml.transform.stax.StAXSource";
|
||||
|
||||
private static int jaxpVersion;
|
||||
|
||||
static {
|
||||
ClassLoader classLoader = JaxpVersion.class.getClassLoader();
|
||||
try {
|
||||
ClassUtils.forName(JAXP_14_CLASS_NAME, classLoader);
|
||||
jaxpVersion = JAXP_14;
|
||||
}
|
||||
catch (ClassNotFoundException ex) {
|
||||
// leave 1.3 as default (it's either 1.3 or unknown)
|
||||
jaxpVersion = JAXP_13;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the JAXP version. This means we can do things like if <code>(getJaxpVersion() < JAXP_13)</code>.
|
||||
*
|
||||
* @return a code comparable to the JAXP_XX codes in this class
|
||||
* @see #JAXP_10
|
||||
* @see #JAXP_11
|
||||
* @see #JAXP_13
|
||||
* @see #JAXP_14
|
||||
*/
|
||||
public static int getJaxpVersion() {
|
||||
return jaxpVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to determine if the current JAXP version is at least 1.4 (packaged with JDK 1.6).
|
||||
*
|
||||
* @return <code>true</code> if the current JAXP version is at least JAXP 1.4
|
||||
* @see #getJaxpVersion()
|
||||
* @see #JAXP_14
|
||||
*/
|
||||
public static boolean isAtLeastJaxp14() {
|
||||
return getJaxpVersion() >= JAXP_14;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml;
|
||||
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
|
||||
/**
|
||||
* Root of the hierarchy of XML exception.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class XmlException extends NestedRuntimeException {
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the <code>XmlException</code> with the specific detail message.
|
||||
*
|
||||
* @param message the detail message
|
||||
*/
|
||||
protected XmlException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the <code>XmlException</code> with the specific detail message and exception.
|
||||
*
|
||||
* @param message the detail message
|
||||
* @param throwable the wrapped exception
|
||||
*/
|
||||
protected XmlException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.dom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.ProcessingInstruction;
|
||||
import org.w3c.dom.Text;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.Locator;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* SAX <code>ContentHandler</code> that transforms callback calls to DOM <code>Node</code>s.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see org.w3c.dom.Node
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class DomContentHandler implements ContentHandler {
|
||||
|
||||
private final Document document;
|
||||
|
||||
private final List<Element> elements = new ArrayList<Element>();
|
||||
|
||||
private final Node node;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the <code>DomContentHandler</code> with the given node.
|
||||
*
|
||||
* @param node the node to publish events to
|
||||
*/
|
||||
public DomContentHandler(Node node) {
|
||||
Assert.notNull(node, "node must not be null");
|
||||
this.node = node;
|
||||
if (node instanceof Document) {
|
||||
document = (Document) node;
|
||||
}
|
||||
else {
|
||||
document = node.getOwnerDocument();
|
||||
}
|
||||
Assert.notNull(document, "document must not be null");
|
||||
}
|
||||
|
||||
private Node getParent() {
|
||||
if (!elements.isEmpty()) {
|
||||
return elements.get(elements.size() - 1);
|
||||
}
|
||||
else {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
||||
Node parent = getParent();
|
||||
Element element = document.createElementNS(uri, qName);
|
||||
for (int i = 0; i < attributes.getLength(); i++) {
|
||||
String attrUri = attributes.getURI(i);
|
||||
String attrQname = attributes.getQName(i);
|
||||
String value = attributes.getValue(i);
|
||||
if (!attrQname.startsWith("xmlns")) {
|
||||
element.setAttributeNS(attrUri, attrQname, value);
|
||||
}
|
||||
}
|
||||
element = (Element) parent.appendChild(element);
|
||||
elements.add(element);
|
||||
}
|
||||
|
||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
||||
elements.remove(elements.size() - 1);
|
||||
}
|
||||
|
||||
public void characters(char ch[], int start, int length) throws SAXException {
|
||||
String data = new String(ch, start, length);
|
||||
Node parent = getParent();
|
||||
Node lastChild = parent.getLastChild();
|
||||
if (lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) {
|
||||
((Text) lastChild).appendData(data);
|
||||
}
|
||||
else {
|
||||
Text text = document.createTextNode(data);
|
||||
parent.appendChild(text);
|
||||
}
|
||||
}
|
||||
|
||||
public void processingInstruction(String target, String data) throws SAXException {
|
||||
Node parent = getParent();
|
||||
ProcessingInstruction pi = document.createProcessingInstruction(target, data);
|
||||
parent.appendChild(pi);
|
||||
}
|
||||
|
||||
/*
|
||||
* Unsupported
|
||||
*/
|
||||
|
||||
public void setDocumentLocator(Locator locator) {
|
||||
}
|
||||
|
||||
public void startDocument() throws SAXException {
|
||||
}
|
||||
|
||||
public void endDocument() throws SAXException {
|
||||
}
|
||||
|
||||
public void startPrefixMapping(String prefix, String uri) throws SAXException {
|
||||
}
|
||||
|
||||
public void endPrefixMapping(String prefix) throws SAXException {
|
||||
}
|
||||
|
||||
public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
|
||||
}
|
||||
|
||||
public void skippedEntity(String name) throws SAXException {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides classes that help with DOM: the Document Object Model. Mostly for internal use by the framework.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import java.beans.PropertyEditorSupport;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* PropertyEditor for <code>javax.xml.namespace.QName</code>, to populate a property of type QName from a String value.
|
||||
* <p/>
|
||||
* Expects the syntax
|
||||
* <pre>
|
||||
* localPart
|
||||
* </pre>
|
||||
* or
|
||||
* <pre>
|
||||
* {namespace}localPart
|
||||
* </pre>
|
||||
* or
|
||||
* <pre>
|
||||
* {namespace}prefix:localPart
|
||||
* </pre>
|
||||
* This resembles the <code>toString()</code> representation of <code>QName</code> itself, but allows for prefixes to be
|
||||
* specified as well.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see javax.xml.namespace.QName
|
||||
* @see javax.xml.namespace.QName#toString()
|
||||
* @see javax.xml.namespace.QName#valueOf(String)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class QNameEditor extends PropertyEditorSupport {
|
||||
|
||||
@Override
|
||||
public void setAsText(String text) throws IllegalArgumentException {
|
||||
setValue(QNameUtils.parseQNameString(text));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsText() {
|
||||
Object value = getValue();
|
||||
if (value == null || !(value instanceof QName)) {
|
||||
return "";
|
||||
}
|
||||
else {
|
||||
QName qName = (QName) value;
|
||||
String prefix = QNameUtils.getPrefix(qName);
|
||||
if (StringUtils.hasLength(qName.getNamespaceURI()) && StringUtils.hasLength(prefix)) {
|
||||
return "{" + qName.getNamespaceURI() + "}" + prefix + ":" + qName.getLocalPart();
|
||||
}
|
||||
else if (StringUtils.hasLength(qName.getNamespaceURI())) {
|
||||
return "{" + qName.getNamespaceURI() + "}" + qName.getLocalPart();
|
||||
}
|
||||
else {
|
||||
return qName.getLocalPart();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Helper class for using {@link QName}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see javax.xml.namespace.QName
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class QNameUtils {
|
||||
|
||||
/** Indicates whether {@link QName} has a prefix. The first release of the class did not have this. */
|
||||
private static boolean qNameHasPrefix;
|
||||
|
||||
static {
|
||||
try {
|
||||
QName.class.getDeclaredConstructor(new Class[]{String.class, String.class, String.class});
|
||||
qNameHasPrefix = true;
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
qNameHasPrefix = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>QName</code> with the given parameters. Sets the prefix if possible, i.e. if the
|
||||
* <code>QName(String, String, String)</code> constructor can be found. If this constructor is not available (as is
|
||||
* the case on older implementations of JAX-RPC), the prefix is ignored.
|
||||
*
|
||||
* @param namespaceUri namespace URI of the <code>QName</code>
|
||||
* @param localPart local part of the <code>QName</code>
|
||||
* @param prefix prefix of the <code>QName</code>. May be ignored.
|
||||
* @return the created <code>QName</code>
|
||||
* @see QName#QName(String,String,String)
|
||||
*/
|
||||
public static QName createQName(String namespaceUri, String localPart, String prefix) {
|
||||
if (qNameHasPrefix) {
|
||||
return new QName(namespaceUri, localPart, prefix);
|
||||
}
|
||||
else {
|
||||
return new QName(namespaceUri, localPart);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the prefix of the given <code>QName</code>. Returns the prefix if available, i.e. if the
|
||||
* <code>QName.getPrefix()</code> method can be found. If this method is not available (as is the case on older
|
||||
* implementations of JAX-RPC), an empty string is returned.
|
||||
*
|
||||
* @param qName the <code>QName</code> to return the prefix from
|
||||
* @return the prefix, if available, or an empty string
|
||||
* @see javax.xml.namespace.QName#getPrefix()
|
||||
*/
|
||||
public static String getPrefix(QName qName) {
|
||||
return qNameHasPrefix ? qName.getPrefix() : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given String as a QName
|
||||
*
|
||||
* @param text the qualified name
|
||||
* @return <code>true</code> if valid, <code>false</code> otherwise
|
||||
*/
|
||||
public static boolean validateQName(String text) {
|
||||
if (!StringUtils.hasLength(text)) {
|
||||
return false;
|
||||
}
|
||||
if (text.charAt(0) == '{') {
|
||||
int i = text.indexOf('}');
|
||||
|
||||
if (i == -1 || i == text.length() - 1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the qualified name of the given DOM Node.
|
||||
*
|
||||
* @param node the node
|
||||
* @return the qualified name of the node
|
||||
*/
|
||||
public static QName getQNameForNode(Node node) {
|
||||
if (node.getNamespaceURI() != null && node.getPrefix() != null && node.getLocalName() != null) {
|
||||
return createQName(node.getNamespaceURI(), node.getLocalName(), node.getPrefix());
|
||||
}
|
||||
else if (node.getNamespaceURI() != null && node.getLocalName() != null) {
|
||||
return new QName(node.getNamespaceURI(), node.getLocalName());
|
||||
}
|
||||
else if (node.getLocalName() != null) {
|
||||
return new QName(node.getLocalName());
|
||||
}
|
||||
else {
|
||||
// as a last resort, use the node name
|
||||
return new QName(node.getNodeName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a <code>QName</code> to a qualified name, as used by DOM and SAX. The returned string has a format of
|
||||
* <code>prefix:localName</code> if the prefix is set, or just <code>localName</code> if not.
|
||||
*
|
||||
* @param qName the <code>QName</code>
|
||||
* @return the qualified name
|
||||
*/
|
||||
public static String toQualifiedName(QName qName) {
|
||||
String prefix = getPrefix(qName);
|
||||
if (!StringUtils.hasLength(prefix)) {
|
||||
return qName.getLocalPart();
|
||||
}
|
||||
else {
|
||||
return prefix + ":" + qName.getLocalPart();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a namespace URI and DOM or SAX qualified name to a <code>QName</code>. The qualified name can have the
|
||||
* form <code>prefix:localname</code> or <code>localName</code>.
|
||||
*
|
||||
* @param namespaceUri the namespace URI
|
||||
* @param qualifiedName the qualified name
|
||||
* @return a QName
|
||||
*/
|
||||
public static QName toQName(String namespaceUri, String qualifiedName) {
|
||||
int idx = qualifiedName.indexOf(':');
|
||||
if (idx == -1) {
|
||||
return new QName(namespaceUri, qualifiedName);
|
||||
}
|
||||
else {
|
||||
return createQName(namespaceUri, qualifiedName.substring(idx + 1), qualifiedName.substring(0, idx));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given qualified name string into a <code>QName</code>. Expects the syntax <code>localPart</code>,
|
||||
* <code>{namespace}localPart</code>, or <code>{namespace}prefix:localPart</code>. This format resembles the
|
||||
* <code>toString()</code> representation of <code>QName</code> itself, but allows for prefixes to be specified as
|
||||
* well.
|
||||
*
|
||||
* @return a corresponding QName instance
|
||||
* @throws IllegalArgumentException when the given string is <code>null</code> or empty.
|
||||
*/
|
||||
public static QName parseQNameString(String qNameString) {
|
||||
Assert.hasLength(qNameString, "QName text may not be null or empty");
|
||||
if (qNameString.charAt(0) != '{') {
|
||||
return new QName(qNameString);
|
||||
}
|
||||
else {
|
||||
int endOfNamespaceURI = qNameString.indexOf('}');
|
||||
if (endOfNamespaceURI == -1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Cannot create QName from \"" + qNameString + "\", missing closing \"}\"");
|
||||
}
|
||||
int prefixSeperator = qNameString.indexOf(':', endOfNamespaceURI + 1);
|
||||
String namespaceURI = qNameString.substring(1, endOfNamespaceURI);
|
||||
if (prefixSeperator == -1) {
|
||||
return new QName(namespaceURI, qNameString.substring(endOfNamespaceURI + 1));
|
||||
}
|
||||
else {
|
||||
return createQName(namespaceURI, qNameString.substring(prefixSeperator + 1),
|
||||
qNameString.substring(endOfNamespaceURI + 1, prefixSeperator));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Copyright 2005-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.namespace.NamespaceContext;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Simple <code>javax.xml.namespace.NamespaceContext</code> implementation. Follows the standard
|
||||
* <code>NamespaceContext</code> contract, and is loadable via a <code>java.util.Map</code> or
|
||||
* <code>java.util.Properties</code> object
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class SimpleNamespaceContext implements NamespaceContext {
|
||||
|
||||
private Map<String, String> prefixToNamespaceUri = new LinkedHashMap<String, String>();
|
||||
|
||||
private Map<String, Set<String>> namespaceUriToPrefixes = new LinkedHashMap<String, Set<String>>();
|
||||
|
||||
public String getNamespaceURI(String prefix) {
|
||||
Assert.notNull(prefix, "prefix is null");
|
||||
if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
|
||||
return XMLConstants.XML_NS_URI;
|
||||
}
|
||||
else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
|
||||
return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
|
||||
}
|
||||
else if (prefixToNamespaceUri.containsKey(prefix)) {
|
||||
return prefixToNamespaceUri.get(prefix);
|
||||
}
|
||||
return XMLConstants.NULL_NS_URI;
|
||||
}
|
||||
|
||||
public String getPrefix(String namespaceUri) {
|
||||
Iterator<String> iterator = getPrefixes(namespaceUri);
|
||||
return iterator.hasNext() ? iterator.next() : null;
|
||||
}
|
||||
|
||||
public Iterator<String> getPrefixes(String namespaceUri) {
|
||||
Set<String> prefixes = getPrefixesInternal(namespaceUri);
|
||||
prefixes = Collections.unmodifiableSet(prefixes);
|
||||
return prefixes.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the bindings for this namespace context. The supplied map must consist of string key value pairs.
|
||||
*
|
||||
* @param bindings the bindings
|
||||
*/
|
||||
public void setBindings(Map<String, String> bindings) {
|
||||
for (Map.Entry<String, String> entry : bindings.entrySet()) {
|
||||
bindNamespaceUri(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds the given namespace as default namespace.
|
||||
*
|
||||
* @param namespaceUri the namespace uri
|
||||
*/
|
||||
public void bindDefaultNamespaceUri(String namespaceUri) {
|
||||
bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds the given prefix to the given namespace.
|
||||
*
|
||||
* @param prefix the namespace prefix
|
||||
* @param namespaceUri the namespace uri
|
||||
*/
|
||||
public void bindNamespaceUri(String prefix, String namespaceUri) {
|
||||
Assert.notNull(prefix, "No prefix given");
|
||||
Assert.notNull(namespaceUri, "No namespaceUri given");
|
||||
if (XMLConstants.XML_NS_PREFIX.equals(prefix)) {
|
||||
Assert.isTrue(XMLConstants.XML_NS_URI.equals(namespaceUri), "Prefix \"" + prefix +
|
||||
"\" bound to namespace \"" + namespaceUri + "\" (should be \"" + XMLConstants.XML_NS_URI + "\")");
|
||||
} else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) {
|
||||
Assert.isTrue(XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri), "Prefix \"" + prefix +
|
||||
"\" bound to namespace \"" + namespaceUri + "\" (should be \"" +
|
||||
XMLConstants.XMLNS_ATTRIBUTE_NS_URI + "\")");
|
||||
}
|
||||
else {
|
||||
prefixToNamespaceUri.put(prefix, namespaceUri);
|
||||
getPrefixesInternal(namespaceUri).add(prefix);
|
||||
}
|
||||
}
|
||||
|
||||
/** Removes all declared prefixes. */
|
||||
public void clear() {
|
||||
prefixToNamespaceUri.clear();
|
||||
namespaceUriToPrefixes.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all declared prefixes.
|
||||
*
|
||||
* @return the declared prefixes
|
||||
*/
|
||||
public Iterator<String> getBoundPrefixes() {
|
||||
Set<String> prefixes = new HashSet<String>(prefixToNamespaceUri.keySet());
|
||||
prefixes.remove(XMLConstants.DEFAULT_NS_PREFIX);
|
||||
prefixes = Collections.unmodifiableSet(prefixes);
|
||||
return prefixes.iterator();
|
||||
}
|
||||
|
||||
private Set<String> getPrefixesInternal(String namespaceUri) {
|
||||
if (XMLConstants.XML_NS_URI.equals(namespaceUri)) {
|
||||
return Collections.singleton(XMLConstants.XML_NS_PREFIX);
|
||||
}
|
||||
else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceUri)) {
|
||||
return Collections.singleton(XMLConstants.XMLNS_ATTRIBUTE);
|
||||
}
|
||||
else {
|
||||
Set<String> set = namespaceUriToPrefixes.get(namespaceUri);
|
||||
if (set == null) {
|
||||
set = new LinkedHashSet<String>();
|
||||
namespaceUriToPrefixes.put(namespaceUri, set);
|
||||
}
|
||||
return set;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given prefix from this context.
|
||||
*
|
||||
* @param prefix the prefix to be removed
|
||||
*/
|
||||
public void removeBinding(String prefix) {
|
||||
String namespaceUri = prefixToNamespaceUri.remove(prefix);
|
||||
if (namespaceUri != null) {
|
||||
Set<String> prefixes = getPrefixesInternal(namespaceUri);
|
||||
prefixes.remove(prefix);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasBinding(String prefix) {
|
||||
return prefixToNamespaceUri.containsKey(prefix);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides classes that help with XML Namespace processing. Mostly for internal use by the framework.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides classes for XML handling: version detection and a base XML exception class. Mostly for internal use by the
|
||||
framework.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.sax;
|
||||
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.DTDHandler;
|
||||
import org.xml.sax.EntityResolver;
|
||||
import org.xml.sax.ErrorHandler;
|
||||
import org.xml.sax.SAXNotRecognizedException;
|
||||
import org.xml.sax.SAXNotSupportedException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
|
||||
/**
|
||||
* Abstract base class for SAX <code>XMLReader</code> implementations. Contains properties as defined in {@link
|
||||
* XMLReader}, and does not recognize any features
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #setContentHandler(org.xml.sax.ContentHandler)
|
||||
* @see #setDTDHandler(org.xml.sax.DTDHandler)
|
||||
* @see #setEntityResolver(org.xml.sax.EntityResolver)
|
||||
* @see #setErrorHandler(org.xml.sax.ErrorHandler)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class AbstractXmlReader implements XMLReader {
|
||||
|
||||
private DTDHandler dtdHandler;
|
||||
|
||||
private ContentHandler contentHandler;
|
||||
|
||||
private EntityResolver entityResolver;
|
||||
|
||||
private ErrorHandler errorHandler;
|
||||
|
||||
private LexicalHandler lexicalHandler;
|
||||
|
||||
public ContentHandler getContentHandler() {
|
||||
return contentHandler;
|
||||
}
|
||||
|
||||
public void setContentHandler(ContentHandler contentHandler) {
|
||||
this.contentHandler = contentHandler;
|
||||
}
|
||||
|
||||
public void setDTDHandler(DTDHandler dtdHandler) {
|
||||
this.dtdHandler = dtdHandler;
|
||||
}
|
||||
|
||||
public DTDHandler getDTDHandler() {
|
||||
return dtdHandler;
|
||||
}
|
||||
|
||||
public EntityResolver getEntityResolver() {
|
||||
return entityResolver;
|
||||
}
|
||||
|
||||
public void setEntityResolver(EntityResolver entityResolver) {
|
||||
this.entityResolver = entityResolver;
|
||||
}
|
||||
|
||||
public ErrorHandler getErrorHandler() {
|
||||
return errorHandler;
|
||||
}
|
||||
|
||||
public void setErrorHandler(ErrorHandler errorHandler) {
|
||||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
protected LexicalHandler getLexicalHandler() {
|
||||
return lexicalHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws a <code>SAXNotRecognizedException</code> exception.
|
||||
*
|
||||
* @throws org.xml.sax.SAXNotRecognizedException
|
||||
* always
|
||||
*/
|
||||
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
throw new SAXNotRecognizedException(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws a <code>SAXNotRecognizedException</code> exception.
|
||||
*
|
||||
* @throws SAXNotRecognizedException always
|
||||
*/
|
||||
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
throw new SAXNotRecognizedException(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws a <code>SAXNotRecognizedException</code> exception when the given property does not signify a lexical
|
||||
* handler. The property name for a lexical handler is <code>http://xml.org/sax/properties/lexical-handler</code>.
|
||||
*/
|
||||
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
if ("http://xml.org/sax/properties/lexical-handler".equals(name)) {
|
||||
return lexicalHandler;
|
||||
}
|
||||
else {
|
||||
throw new SAXNotRecognizedException(name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws a <code>SAXNotRecognizedException</code> exception when the given property does not signify a lexical
|
||||
* handler. The property name for a lexical handler is <code>http://xml.org/sax/properties/lexical-handler</code>.
|
||||
*/
|
||||
public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
if ("http://xml.org/sax/properties/lexical-handler".equals(name)) {
|
||||
lexicalHandler = (LexicalHandler) value;
|
||||
}
|
||||
else {
|
||||
throw new SAXNotRecognizedException(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.sax;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
/**
|
||||
* Convenient utility methods for dealing with SAX.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class SaxUtils {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(SaxUtils.class);
|
||||
|
||||
/**
|
||||
* Creates a SAX <code>InputSource</code> from the given resource. Sets the system identifier to the resource's
|
||||
* <code>URL</code>, if available.
|
||||
*
|
||||
* @param resource the resource
|
||||
* @return the input source created from the resource
|
||||
* @throws IOException if an I/O exception occurs
|
||||
* @see InputSource#setSystemId(String)
|
||||
* @see #getSystemId(org.springframework.core.io.Resource)
|
||||
*/
|
||||
public static InputSource createInputSource(Resource resource) throws IOException {
|
||||
InputSource inputSource = new InputSource(resource.getInputStream());
|
||||
inputSource.setSystemId(getSystemId(resource));
|
||||
return inputSource;
|
||||
}
|
||||
|
||||
/** Retrieves the URL from the given resource as System ID. Returns <code>null</code> if it cannot be opened. */
|
||||
public static String getSystemId(Resource resource) {
|
||||
try {
|
||||
return new URI(resource.getURL().toExternalForm()).toString();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
logger.debug("Could not get System ID from [" + resource + "], ex");
|
||||
return null;
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
logger.debug("Could not get System ID from [" + resource + "], ex");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides classes that help with SAX: the Simple API for XML. Mostly for internal use by the framework.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
/**
|
||||
* Convenient subclass of {@link SAXSource} that reads from a Spring {@link Resource}. The resource to be read can be
|
||||
* set via the constructor.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ResourceSource extends SAXSource {
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the <code>ResourceSource</code> with the given resource.
|
||||
*
|
||||
* @param content the content
|
||||
*/
|
||||
public ResourceSource(Resource content) throws IOException {
|
||||
super(SaxUtils.createInputSource(content));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the <code>ResourceSource</code> with the given {@link XMLReader} and resource.
|
||||
*
|
||||
* @param content the content
|
||||
*/
|
||||
public ResourceSource(XMLReader reader, Resource content) throws IOException {
|
||||
super(reader, SaxUtils.createInputSource(content));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
/**
|
||||
* Convenient subclass of <code>StreamResult</code> that writes to a <code>StringWriter</code>. The resulting string can
|
||||
* be retrieved via <code>toString()</code>.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #toString()
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class StringResult extends StreamResult {
|
||||
|
||||
public StringResult() {
|
||||
super(new StringWriter());
|
||||
}
|
||||
|
||||
/** Returns the written XML as a string. */
|
||||
public String toString() {
|
||||
return getWriter().toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Convenient subclass of <code>StreamSource</code> that reads from a <code>StringReader</code>. The string to be read
|
||||
* can be set via the constructor.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class StringSource extends StreamSource {
|
||||
|
||||
private final String content;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the <code>StringSource</code> with the given string content.
|
||||
*
|
||||
* @param content the content
|
||||
*/
|
||||
public StringSource(String content) {
|
||||
Assert.notNull(content, "'content' must not be null");
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader getReader() {
|
||||
return new StringReader(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws {@link UnsupportedOperationException}.
|
||||
*
|
||||
* @throws UnsupportedOperationException always
|
||||
*/
|
||||
@Override
|
||||
public void setInputStream(InputStream inputStream) {
|
||||
throw new UnsupportedOperationException("setInputStream is not supported");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code null}.
|
||||
*
|
||||
* @return {@code null}
|
||||
*/
|
||||
@Override
|
||||
public InputStream getInputStream() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws {@link UnsupportedOperationException}.
|
||||
*
|
||||
* @throws UnsupportedOperationException always
|
||||
*/
|
||||
@Override
|
||||
public void setReader(Reader reader) {
|
||||
throw new UnsupportedOperationException("setReader is not supported");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Helper class for {@link Transformer} usage. Provides {@link #createTransformer()} and {@link #transform(Source,
|
||||
* Result)}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
*/
|
||||
public class TransformerHelper {
|
||||
|
||||
private volatile TransformerFactory transformerFactory;
|
||||
|
||||
private Class<? extends TransformerFactory> transformerFactoryClass;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the {@code TransformerHelper}.
|
||||
*/
|
||||
public TransformerHelper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the {@code TransformerHelper} with the specified {@link TransformerFactory}.
|
||||
*/
|
||||
public TransformerHelper(TransformerFactory transformerFactory) {
|
||||
this.transformerFactory = transformerFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the {@code TransformerHelper} with the specified {@link TransformerFactory} class.
|
||||
*/
|
||||
public TransformerHelper(Class<? extends TransformerFactory> transformerFactoryClass) {
|
||||
setTransformerFactoryClass(transformerFactoryClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the {@code TransformerFactory} class to use.
|
||||
*/
|
||||
public void setTransformerFactoryClass(Class<? extends TransformerFactory> transformerFactoryClass) {
|
||||
Assert.isAssignable(TransformerFactory.class, transformerFactoryClass);
|
||||
this.transformerFactoryClass = transformerFactoryClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate a new TransformerFactory.
|
||||
* <p/>
|
||||
* The default implementation simply calls {@link TransformerFactory#newInstance()}. If a {@link
|
||||
* #setTransformerFactoryClass transformerFactoryClass} has been specified explicitly, the default constructor of
|
||||
* the specified class will be called instead.
|
||||
* <p/>
|
||||
* Can be overridden in subclasses.
|
||||
*
|
||||
* @param transformerFactoryClass the specified factory class (if any)
|
||||
* @return the new TransactionFactory instance
|
||||
* @see #setTransformerFactoryClass
|
||||
* @see #getTransformerFactory()
|
||||
*/
|
||||
protected TransformerFactory newTransformerFactory(Class<? extends TransformerFactory> transformerFactoryClass) {
|
||||
if (transformerFactoryClass != null) {
|
||||
try {
|
||||
return transformerFactoryClass.newInstance();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new TransformerFactoryConfigurationError(ex,
|
||||
"Could not instantiate TransformerFactory [" + transformerFactoryClass + "]");
|
||||
}
|
||||
}
|
||||
else {
|
||||
return TransformerFactory.newInstance();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@code TransformerFactory}.
|
||||
*
|
||||
* @return the transformer factory
|
||||
*/
|
||||
public TransformerFactory getTransformerFactory() {
|
||||
TransformerFactory result = transformerFactory;
|
||||
if (result == null) {
|
||||
synchronized (this) {
|
||||
result = transformerFactory;
|
||||
if (result == null) {
|
||||
transformerFactory = result = newTransformerFactory(transformerFactoryClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code Transformer}. Must be called per thread, as transformers are not thread-safe.
|
||||
*
|
||||
* @return the created transformer
|
||||
* @throws TransformerConfigurationException
|
||||
* if thrown by JAXP methods
|
||||
*/
|
||||
public Transformer createTransformer() throws TransformerConfigurationException {
|
||||
return getTransformerFactory().newTransformer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the given {@link Source} to the given {@link Result}. Creates a new {@link Transformer} for every
|
||||
* call, as transformers are not thread-safe.
|
||||
*
|
||||
* @param source the source to transform from
|
||||
* @param result the result to transform to
|
||||
* @throws TransformerException if thrown by JAXP methods
|
||||
*/
|
||||
public void transform(Source source, Result result) throws TransformerException {
|
||||
Transformer transformer = createTransformer();
|
||||
transformer.transform(source, result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Convenient base class for objects that use a <code>Transformer</code>. Subclasses can call {@link
|
||||
* #createTransformer()} or {@link #transform(Source, Result)}. This should be done per thread (i.e. per incoming
|
||||
* request), because <code>Transformer</code> instances are not thread-safe.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see Transformer
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class TransformerObjectSupport {
|
||||
|
||||
/**
|
||||
* Logger available to subclasses.
|
||||
*/
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private TransformerHelper transformerHelper = new TransformerHelper();
|
||||
|
||||
/**
|
||||
* Specify the {@code TransformerFactory} class to use.
|
||||
*/
|
||||
public void setTransformerFactoryClass(Class<? extends TransformerFactory> transformerFactoryClass) {
|
||||
transformerHelper.setTransformerFactoryClass(transformerFactoryClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate a new TransformerFactory. <p>The default implementation simply calls {@link
|
||||
* TransformerFactory#newInstance()}. If a {@link #setTransformerFactoryClass "transformerFactoryClass"} has been
|
||||
* specified explicitly, the default constructor of the specified class will be called instead. <p>Can be overridden
|
||||
* in subclasses.
|
||||
*
|
||||
* @param transformerFactoryClass the specified factory class (if any)
|
||||
* @return the new TransactionFactory instance
|
||||
* @see #setTransformerFactoryClass
|
||||
* @see #getTransformerFactory()
|
||||
*/
|
||||
protected TransformerFactory newTransformerFactory(Class<? extends TransformerFactory> transformerFactoryClass) {
|
||||
return transformerHelper.newTransformerFactory(transformerFactoryClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the <code>TransformerFactory</code>.
|
||||
*/
|
||||
protected TransformerFactory getTransformerFactory() {
|
||||
return transformerHelper.getTransformerFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>Transformer</code>. Must be called per request, as transformers are not thread-safe.
|
||||
*
|
||||
* @return the created transformer
|
||||
* @throws TransformerConfigurationException
|
||||
* if thrown by JAXP methods
|
||||
*/
|
||||
protected final Transformer createTransformer() throws TransformerConfigurationException {
|
||||
return transformerHelper.createTransformer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the given {@link Source} to the given {@link Result}. Creates a new {@link Transformer} for every
|
||||
* call, as transformers are not thread-safe.
|
||||
*
|
||||
* @param source the source to transform from
|
||||
* @param result the result to transform to
|
||||
* @throws TransformerException if thrown by JAXP methods
|
||||
*/
|
||||
protected final void transform(Source source, Result result) throws TransformerException {
|
||||
transformerHelper.transform(source, result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
/*
|
||||
* Copyright 2005-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stax.StAXResult;
|
||||
import javax.xml.transform.stax.StAXSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.StaxUtils;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
|
||||
/**
|
||||
* Convenient utility methods for dealing with TrAX.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public abstract class TraxUtils {
|
||||
|
||||
/**
|
||||
* Returns the {@link Document} of the given {@link DOMSource}.
|
||||
*
|
||||
* @param source the DOM source
|
||||
* @return the document
|
||||
*/
|
||||
public static Document getDocument(DOMSource source) {
|
||||
Node node = source.getNode();
|
||||
if (node instanceof Document) {
|
||||
return (Document) node;
|
||||
}
|
||||
else if (node != null) {
|
||||
return node.getOwnerDocument();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given {@linkplain SourceCallback callback} operation on a {@link Source}. Supports both the JAXP 1.4
|
||||
* {@link StAXSource} and the Spring 3.0 {@link StaxUtils#createStaxSource StaxSource}.
|
||||
*
|
||||
* @param source source to look at
|
||||
* @param callback the callback to invoke for each kind of source
|
||||
*/
|
||||
public static void doWithSource(Source source, SourceCallback callback) throws Exception {
|
||||
if (source instanceof DOMSource) {
|
||||
callback.domSource(((DOMSource) source).getNode());
|
||||
return;
|
||||
}
|
||||
else if (StaxUtils.isStaxSource(source)) {
|
||||
XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(source);
|
||||
if (streamReader != null) {
|
||||
callback.staxSource(streamReader);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
XMLEventReader eventReader = StaxUtils.getXMLEventReader(source);
|
||||
if (eventReader != null) {
|
||||
callback.staxSource(eventReader);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (source instanceof SAXSource) {
|
||||
SAXSource saxSource = (SAXSource) source;
|
||||
callback.saxSource(saxSource.getXMLReader(), saxSource.getInputSource());
|
||||
return;
|
||||
}
|
||||
else if (source instanceof StreamSource) {
|
||||
StreamSource streamSource = (StreamSource) source;
|
||||
if (streamSource.getInputStream() != null) {
|
||||
callback.streamSource(streamSource.getInputStream());
|
||||
return;
|
||||
}
|
||||
else if (streamSource.getReader() != null) {
|
||||
callback.streamSource(streamSource.getReader());
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (StringUtils.hasLength(source.getSystemId())) {
|
||||
String systemId = source.getSystemId();
|
||||
callback.source(systemId);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Unknown Source type: " + source.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the given {@linkplain org.springframework.xml.transform.TraxUtils.ResultCallback callback} operation on a {@link javax.xml.transform.Result}. Supports both the JAXP 1.4
|
||||
* {@link javax.xml.transform.stax.StAXResult} and the Spring 3.0 {@link org.springframework.util.xml.StaxUtils#createStaxResult StaxSource}.
|
||||
*
|
||||
* @param result result to look at
|
||||
* @param callback the callback to invoke for each kind of result
|
||||
*/
|
||||
public static void doWithResult(Result result, ResultCallback callback) throws Exception {
|
||||
if (result instanceof DOMResult) {
|
||||
callback.domResult(((DOMResult) result).getNode());
|
||||
return;
|
||||
}
|
||||
else if (StaxUtils.isStaxResult(result)) {
|
||||
XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(result);
|
||||
if (streamWriter != null) {
|
||||
callback.staxResult(streamWriter);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
XMLEventWriter eventWriter = StaxUtils.getXMLEventWriter(result);
|
||||
if (eventWriter != null) {
|
||||
callback.staxResult(eventWriter);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (result instanceof SAXResult) {
|
||||
SAXResult saxSource = (SAXResult) result;
|
||||
callback.saxResult(saxSource.getHandler(), saxSource.getLexicalHandler());
|
||||
return;
|
||||
}
|
||||
else if (result instanceof StreamResult) {
|
||||
StreamResult streamSource = (StreamResult) result;
|
||||
if (streamSource.getOutputStream() != null) {
|
||||
callback.streamResult(streamSource.getOutputStream());
|
||||
return;
|
||||
}
|
||||
else if (streamSource.getWriter() != null) {
|
||||
callback.streamResult(streamSource.getWriter());
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (StringUtils.hasLength(result.getSystemId())) {
|
||||
String systemId = result.getSystemId();
|
||||
callback.result(systemId);
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Unknown Result type: " + result.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback interface invoked on each sort of {@link Source}.
|
||||
*
|
||||
* @see TraxUtils#doWithSource(Source, SourceCallback)
|
||||
*/
|
||||
public interface SourceCallback {
|
||||
|
||||
/**
|
||||
* Perform an operation on the node contained in a {@link DOMSource}.
|
||||
*
|
||||
* @param node the node
|
||||
*/
|
||||
void domSource(Node node) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the {@code XMLReader} and {@code InputSource} contained in a {@link SAXSource}.
|
||||
*
|
||||
* @param reader the reader, can be {@code null}
|
||||
* @param inputSource the input source, can be {@code null}
|
||||
*/
|
||||
void saxSource(XMLReader reader, InputSource inputSource) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the {@code XMLEventReader} contained in a JAXP 1.4 {@link StAXSource} or Spring
|
||||
* {@link StaxUtils#createStaxSource StaxSource}.
|
||||
*
|
||||
* @param eventReader the reader
|
||||
*/
|
||||
void staxSource(XMLEventReader eventReader) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the {@code XMLStreamReader} contained in a JAXP 1.4 {@link StAXSource} or Spring
|
||||
* {@link StaxUtils#createStaxSource StaxSource}.
|
||||
*
|
||||
* @param streamReader the reader
|
||||
*/
|
||||
void staxSource(XMLStreamReader streamReader) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the {@code InputStream} contained in a {@link StreamSource}.
|
||||
*
|
||||
* @param inputStream the input stream
|
||||
*/
|
||||
void streamSource(InputStream inputStream) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the {@code Reader} contained in a {@link StreamSource}.
|
||||
*
|
||||
* @param reader the reader
|
||||
*/
|
||||
void streamSource(Reader reader) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the system identifier contained in any {@link Source}.
|
||||
*
|
||||
* @param systemId the system identifier
|
||||
*/
|
||||
void source(String systemId) throws Exception;
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback interface invoked on each sort of {@link Result}.
|
||||
*
|
||||
* @see TraxUtils#doWithResult(Result, ResultCallback)
|
||||
*/
|
||||
public interface ResultCallback {
|
||||
|
||||
/**
|
||||
* Perform an operation on the node contained in a {@link DOMResult}.
|
||||
*
|
||||
* @param node the node
|
||||
*/
|
||||
void domResult(Node node) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the {@code ContentHandler} and {@code LexicalHandler} contained in a {@link
|
||||
* SAXResult}.
|
||||
*
|
||||
* @param contentHandler the content handler
|
||||
* @param lexicalHandler the lexicalHandler, can be {@code null}
|
||||
*/
|
||||
void saxResult(ContentHandler contentHandler, LexicalHandler lexicalHandler) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the {@code XMLEventWriter} contained in a JAXP 1.4 {@link StAXResult} or Spring
|
||||
* {@link StaxUtils#createStaxResult StaxResult}.
|
||||
*
|
||||
* @param eventWriter the writer
|
||||
*/
|
||||
void staxResult(XMLEventWriter eventWriter) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the {@code XMLStreamWriter} contained in a JAXP 1.4 {@link StAXResult} or Spring
|
||||
* {@link StaxUtils#createStaxResult StaxResult}.
|
||||
*
|
||||
* @param streamWriter the writer
|
||||
*/
|
||||
void staxResult(XMLStreamWriter streamWriter) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the {@code OutputStream} contained in a {@link StreamResult}.
|
||||
*
|
||||
* @param outputStream the output stream
|
||||
*/
|
||||
void streamResult(OutputStream outputStream) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the {@code Writer} contained in a {@link StreamResult}.
|
||||
*
|
||||
* @param writer the writer
|
||||
*/
|
||||
void streamResult(Writer writer) throws Exception;
|
||||
|
||||
/**
|
||||
* Perform an operation on the system identifier contained in any {@link Result}.
|
||||
*
|
||||
* @param systemId the system identifier
|
||||
*/
|
||||
void result(String systemId) throws Exception;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides classes that help with XML transformations. Mostly for internal use by the framework.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2005-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.validation.Schema;
|
||||
import javax.xml.validation.Validator;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
/**
|
||||
* Internal class that uses JAXP 1.0 features to create <code>XmlValidator</code> instances.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
abstract class Jaxp13ValidatorFactory {
|
||||
|
||||
static XmlValidator createValidator(Resource[] resources, String schemaLanguage) throws IOException {
|
||||
try {
|
||||
Schema schema = SchemaLoaderUtils.loadSchema(resources, schemaLanguage);
|
||||
return new Jaxp13Validator(schema);
|
||||
}
|
||||
catch (SAXException ex) {
|
||||
throw new XmlValidationException("Could not create Schema: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Jaxp13Validator implements XmlValidator {
|
||||
|
||||
private Schema schema;
|
||||
|
||||
public Jaxp13Validator(Schema schema) {
|
||||
this.schema = schema;
|
||||
}
|
||||
|
||||
public SAXParseException[] validate(Source source) throws IOException {
|
||||
return validate(source, null);
|
||||
}
|
||||
|
||||
public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException {
|
||||
if (errorHandler == null) {
|
||||
errorHandler = new DefaultValidationErrorHandler();
|
||||
}
|
||||
Validator validator = schema.newValidator();
|
||||
validator.setErrorHandler(errorHandler);
|
||||
try {
|
||||
validator.validate(source);
|
||||
return errorHandler.getErrors();
|
||||
}
|
||||
catch (SAXException ex) {
|
||||
throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** <code>ErrorHandler</code> implementation that stores errors and fatal errors in a list. */
|
||||
private static class DefaultValidationErrorHandler implements ValidationErrorHandler {
|
||||
|
||||
private List<SAXParseException> errors = new ArrayList<SAXParseException>();
|
||||
|
||||
public SAXParseException[] getErrors() {
|
||||
return errors.toArray(new SAXParseException[errors.size()]);
|
||||
}
|
||||
|
||||
public void warning(SAXParseException ex) throws SAXException {
|
||||
}
|
||||
|
||||
public void error(SAXParseException ex) throws SAXException {
|
||||
errors.add(ex);
|
||||
}
|
||||
|
||||
public void fatalError(SAXParseException ex) throws SAXException {
|
||||
errors.add(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.validation.Schema;
|
||||
import javax.xml.validation.SchemaFactory;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.transform.ResourceSource;
|
||||
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
/**
|
||||
* Convenient utility methods for loading of {@link Schema} objects, performing standard handling of input streams.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class SchemaLoaderUtils {
|
||||
|
||||
/**
|
||||
* Load schema from the given resource.
|
||||
*
|
||||
* @param resource the resource to load from
|
||||
* @param schemaLanguage the language of the schema. Can be <code>XMLConstants.W3C_XML_SCHEMA_NS_URI</code> or
|
||||
* <code>XMLConstants.RELAXNG_NS_URI</code>.
|
||||
* @throws IOException if loading failed
|
||||
* @throws SAXException if loading failed
|
||||
* @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI
|
||||
* @see javax.xml.XMLConstants#RELAXNG_NS_URI
|
||||
*/
|
||||
public static Schema loadSchema(Resource resource, String schemaLanguage) throws IOException, SAXException {
|
||||
return loadSchema(new Resource[]{resource}, schemaLanguage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load schema from the given resource.
|
||||
*
|
||||
* @param resources the resources to load from
|
||||
* @param schemaLanguage the language of the schema. Can be <code>XMLConstants.W3C_XML_SCHEMA_NS_URI</code> or
|
||||
* <code>XMLConstants.RELAXNG_NS_URI</code>.
|
||||
* @throws IOException if loading failed
|
||||
* @throws SAXException if loading failed
|
||||
* @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI
|
||||
* @see javax.xml.XMLConstants#RELAXNG_NS_URI
|
||||
*/
|
||||
public static Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
|
||||
Assert.notEmpty(resources, "No resources given");
|
||||
Assert.hasLength(schemaLanguage, "No schema language provided");
|
||||
Source[] schemaSources = new Source[resources.length];
|
||||
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
|
||||
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
for (int i = 0; i < resources.length; i++) {
|
||||
Assert.notNull(resources[i], "Resource is null");
|
||||
Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
|
||||
schemaSources[i] = new ResourceSource(xmlReader, resources[i]);
|
||||
}
|
||||
SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
|
||||
return schemaFactory.newSchema(schemaSources);
|
||||
}
|
||||
|
||||
/** Retrieves the URL from the given resource as System ID. Returns <code>null</code> if it cannot be opened. */
|
||||
public static String getSystemId(Resource resource) {
|
||||
try {
|
||||
return resource.getURL().toString();
|
||||
}
|
||||
catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2005-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import org.xml.sax.ErrorHandler;
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
/**
|
||||
* Subinterface of {@link ErrorHandler} that allows the registered errors to be retrieved.
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0.1
|
||||
*/
|
||||
public interface ValidationErrorHandler extends ErrorHandler {
|
||||
|
||||
/**
|
||||
* Returns the errors collected by this error handler.
|
||||
* @return the errors
|
||||
*/
|
||||
SAXParseException[] getErrors();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import org.springframework.xml.XmlException;
|
||||
|
||||
/**
|
||||
* Exception thrown when a validation error occurs
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class XmlValidationException extends XmlException {
|
||||
|
||||
public XmlValidationException(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
public XmlValidationException(String s, Throwable throwable) {
|
||||
super(s, throwable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2005-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
/**
|
||||
* Simple processor that validates a given {@link Source}. Can be created via the {@link XmlValidatorFactory}.
|
||||
* <p/>
|
||||
* Instances of this class are designed to be thread safe.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see XmlValidatorFactory#createValidator(org.springframework.core.io.Resource, String)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface XmlValidator {
|
||||
|
||||
/**
|
||||
* Validates the given {@link Source}, and returns an array of {@link SAXParseException}s as result. The array will
|
||||
* be empty if no validation errors are found.
|
||||
*
|
||||
* @param source the input document
|
||||
* @return an array of <code>SAXParseException</code>s
|
||||
* @throws IOException if the <code>source</code> cannot be read
|
||||
* @throws XmlValidationException if the <code>source</code> cannot be validated
|
||||
*/
|
||||
SAXParseException[] validate(Source source) throws IOException;
|
||||
|
||||
/**
|
||||
* Validates the given {@link Source} and {@link ValidationErrorHandler}, and returns an array of {@link
|
||||
* SAXParseException}s as result. The array will be empty if no validation errors are found.
|
||||
*
|
||||
* @param source the input document
|
||||
* @param errorHandler the error handler to use. May be {@code null}, in which case a default will be used.
|
||||
* @return an array of <code>SAXParseException</code>s
|
||||
* @throws IOException if the <code>source</code> cannot be read
|
||||
* @throws XmlValidationException if the <code>source</code> cannot be validated
|
||||
*/
|
||||
SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2005-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.validation.Validator;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.JaxpVersion;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Factory for {@link XmlValidator} objects, being aware of JAXP 1.3 {@link Validator}s, and JAXP 1.0 parsing
|
||||
* capababilities. Mainly for internal use within the framework.
|
||||
* <p/>
|
||||
* The goal of this class is to avoid runtime dependencies on JAXP 1.3 by using the best validation implementation that
|
||||
* is available. Prefers JAXP 1.3 {@link XmlValidator} implementations to a custom, SAX-based implementation.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see XmlValidator
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class XmlValidatorFactory {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(XmlValidatorFactory.class);
|
||||
|
||||
/** Constant that defines a W3C XML Schema. */
|
||||
public static final String SCHEMA_W3C_XML = "http://www.w3.org/2001/XMLSchema";
|
||||
|
||||
/** Constant that defines a RELAX NG Schema. */
|
||||
public static final String SCHEMA_RELAX_NG = "http://relaxng.org/ns/structure/1.0";
|
||||
|
||||
/**
|
||||
* Create a {@link XmlValidator} with the given schema resource and schema language type. The schema language must
|
||||
* be one of the <code>SCHEMA_XXX</code> constants.
|
||||
*
|
||||
* @param schemaResource a resource that locates the schema to validate against
|
||||
* @param schemaLanguage the language of the schema
|
||||
* @return a validator
|
||||
* @throws IOException if the schema resource cannot be read
|
||||
* @throws IllegalArgumentException if the schema language is not supported
|
||||
* @throws IllegalStateException if JAXP 1.0 cannot be located
|
||||
* @throws XmlValidationException if a <code>XmlValidator</code> cannot be created
|
||||
* @see #SCHEMA_RELAX_NG
|
||||
* @see #SCHEMA_W3C_XML
|
||||
*/
|
||||
public static XmlValidator createValidator(Resource schemaResource, String schemaLanguage) throws IOException {
|
||||
return createValidator(new Resource[]{schemaResource}, schemaLanguage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link XmlValidator} with the given schema resources and schema language type. The schema language must
|
||||
* be one of the <code>SCHEMA_XXX</code> constants.
|
||||
*
|
||||
* @param schemaResources an array of resource that locate the schemas to validate against
|
||||
* @param schemaLanguage the language of the schemas
|
||||
* @return a validator
|
||||
* @throws IOException if the schema resource cannot be read
|
||||
* @throws IllegalArgumentException if the schema language is not supported
|
||||
* @throws IllegalStateException if JAXP 1.0 cannot be located
|
||||
* @throws XmlValidationException if a <code>XmlValidator</code> cannot be created
|
||||
* @see #SCHEMA_RELAX_NG
|
||||
* @see #SCHEMA_W3C_XML
|
||||
*/
|
||||
public static XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException {
|
||||
Assert.notEmpty(schemaResources, "No resources given");
|
||||
Assert.hasLength(schemaLanguage, "No schema language provided");
|
||||
Assert.isTrue(SCHEMA_W3C_XML.equals(schemaLanguage) || SCHEMA_RELAX_NG.equals(schemaLanguage),
|
||||
"Invalid schema language: " + schemaLanguage);
|
||||
for (Resource schemaResource : schemaResources) {
|
||||
Assert.isTrue(schemaResource.exists(), "schema [" + schemaResource + "] does not exist");
|
||||
}
|
||||
if (JaxpVersion.getJaxpVersion() >= JaxpVersion.JAXP_13) {
|
||||
logger.trace("Creating JAXP 1.3 XmlValidator");
|
||||
return Jaxp13ValidatorFactory.createValidator(schemaResources, schemaLanguage);
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Could not locate JAXP 1.3.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides classes for XML validation in JAXP 1.0 and JAXP 1.3. Mostly for internal use by the framework.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.Map;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.springframework.xml.transform.TransformerObjectSupport;
|
||||
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Abstract base class for implementations of {@link XPathOperations}. Contains a namespaces property.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class AbstractXPathTemplate extends TransformerObjectSupport implements XPathOperations {
|
||||
|
||||
private Map<String, String> namespaces;
|
||||
|
||||
/** Returns namespaces used in the XPath expression. */
|
||||
public Map<String, String> getNamespaces() {
|
||||
return namespaces;
|
||||
}
|
||||
|
||||
/** Sets namespaces used in the XPath expression. Maps prefixes to namespaces. */
|
||||
public void setNamespaces(Map<String, String> namespaces) {
|
||||
this.namespaces = namespaces;
|
||||
}
|
||||
|
||||
public final void evaluate(String expression, Source context, NodeCallbackHandler callbackHandler)
|
||||
throws XPathException {
|
||||
evaluate(expression, context, new NodeCallbackHandlerNodeMapper(callbackHandler));
|
||||
}
|
||||
|
||||
/** Static inner class that adapts a {@link NodeCallbackHandler} to the interface of {@link NodeMapper}. */
|
||||
private static class NodeCallbackHandlerNodeMapper implements NodeMapper<Object> {
|
||||
|
||||
private final NodeCallbackHandler callbackHandler;
|
||||
|
||||
public NodeCallbackHandlerNodeMapper(NodeCallbackHandler callbackHandler) {
|
||||
this.callbackHandler = callbackHandler;
|
||||
}
|
||||
|
||||
public Object mapNode(Node node, int nodeNum) throws DOMException {
|
||||
callbackHandler.processNode(node);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root element of the given source.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @return the root element
|
||||
*/
|
||||
protected Element getRootElement(Source source) throws TransformerException {
|
||||
DOMResult domResult = new DOMResult();
|
||||
transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return document.getDocumentElement();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jaxen.JaxenException;
|
||||
import org.jaxen.SimpleNamespaceContext;
|
||||
import org.jaxen.XPath;
|
||||
import org.jaxen.dom.DOMXPath;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Jaxen-specific factory for creating <code>XPathExpression</code>s.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #createXPathExpression(String)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
abstract class JaxenXPathExpressionFactory {
|
||||
|
||||
/**
|
||||
* Creates a Jaxen <code>XPathExpression</code> from the given string expression.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @return the compiled <code>XPathExpression</code>
|
||||
* @throws XPathParseException when the given expression cannot be parsed
|
||||
*/
|
||||
static XPathExpression createXPathExpression(String expression) {
|
||||
try {
|
||||
XPath xpath = new DOMXPath(expression);
|
||||
return new JaxenXpathExpression(xpath);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new org.springframework.xml.xpath.XPathParseException(
|
||||
"Could not compile [" + expression + "] to a XPathExpression: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Jaxen <code>XPathExpression</code> from the given string expression and prefixes.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param namespaces the namespaces
|
||||
* @return the compiled <code>XPathExpression</code>
|
||||
* @throws XPathParseException when the given expression cannot be parsed
|
||||
*/
|
||||
public static XPathExpression createXPathExpression(String expression, Map<String, String> namespaces) {
|
||||
try {
|
||||
XPath xpath = new DOMXPath(expression);
|
||||
xpath.setNamespaceContext(new SimpleNamespaceContext(namespaces));
|
||||
return new JaxenXpathExpression(xpath);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new org.springframework.xml.xpath.XPathParseException(
|
||||
"Could not compile [" + expression + "] to a XPathExpression: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
/** Jaxen implementation of the <code>XPathExpression</code> interface. */
|
||||
private static class JaxenXpathExpression implements XPathExpression {
|
||||
|
||||
private XPath xpath;
|
||||
|
||||
private JaxenXpathExpression(XPath xpath) {
|
||||
this.xpath = xpath;
|
||||
}
|
||||
|
||||
public Node evaluateAsNode(Node node) {
|
||||
try {
|
||||
return (Node) xpath.selectSingleNode(node);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean evaluateAsBoolean(Node node) {
|
||||
try {
|
||||
return xpath.booleanValueOf(node);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
public double evaluateAsNumber(Node node) {
|
||||
try {
|
||||
return xpath.numberValueOf(node).doubleValue();
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
public String evaluateAsString(Node node) {
|
||||
try {
|
||||
return xpath.stringValueOf(node);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Node> evaluateAsNodeList(Node node) {
|
||||
try {
|
||||
return xpath.selectNodes(node);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T evaluateAsObject(Node context, NodeMapper<T> nodeMapper) throws XPathException {
|
||||
try {
|
||||
Node result = (Node) xpath.selectSingleNode(context);
|
||||
if (result != null) {
|
||||
try {
|
||||
return nodeMapper.mapNode(result, 0);
|
||||
}
|
||||
catch (DOMException ex) {
|
||||
throw new XPathException("Mapping resulted in DOMException", ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> evaluate(Node context, NodeMapper<T> nodeMapper) throws XPathException {
|
||||
try {
|
||||
List<?> nodes = xpath.selectNodes(context);
|
||||
List<T> results = new ArrayList<T>(nodes.size());
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node node = (Node) nodes.get(i);
|
||||
try {
|
||||
results.add(nodeMapper.mapNode(node, i));
|
||||
}
|
||||
catch (DOMException ex) {
|
||||
throw new XPathException("Mapping resulted in DOMException", ex);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + xpath + "] :" + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
|
||||
import org.jaxen.JaxenException;
|
||||
import org.jaxen.SimpleNamespaceContext;
|
||||
import org.jaxen.XPath;
|
||||
import org.jaxen.dom.DOMXPath;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Implementation of {@link XPathOperations} that uses Jaxen.
|
||||
* <p/>
|
||||
* Namespaces can be set using the <code>namespaces</code> property.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see <a href="http://www.jaxen.org/">Jaxen</a>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class JaxenXPathTemplate extends AbstractXPathTemplate {
|
||||
|
||||
public boolean evaluateAsBoolean(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = getRootElement(context);
|
||||
return xpath.booleanValueOf(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new XPathException("Could not transform context to DOM Node", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Node evaluateAsNode(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = getRootElement(context);
|
||||
return (Node) xpath.selectSingleNode(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new XPathException("Could not transform context to DOM Node", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Node> evaluateAsNodeList(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = getRootElement(context);
|
||||
return xpath.selectNodes(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new XPathException("Could not transform context to DOM Node", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public double evaluateAsDouble(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = getRootElement(context);
|
||||
return xpath.numberValueOf(element).doubleValue();
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new XPathException("Could not transform context to DOM Node", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public String evaluateAsString(String expression, Source context) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = getRootElement(context);
|
||||
return xpath.stringValueOf(element);
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new XPathException("Could not transform context to DOM Node", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T evaluateAsObject(String expression, Source context, NodeMapper<T> nodeMapper) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = getRootElement(context);
|
||||
Node node = (Node) xpath.selectSingleNode(element);
|
||||
if (node != null) {
|
||||
try {
|
||||
return nodeMapper.mapNode(node, 0);
|
||||
}
|
||||
catch (DOMException ex) {
|
||||
throw new XPathException("Mapping resulted in DOMException", ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new XPathException("Could not transform context to DOM Node", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> evaluate(String expression, Source context, NodeMapper<T> nodeMapper) throws XPathException {
|
||||
try {
|
||||
XPath xpath = createXPath(expression);
|
||||
Element element = getRootElement(context);
|
||||
List<?> nodes = xpath.selectNodes(element);
|
||||
List<T> results = new ArrayList<T>(nodes.size());
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node node = (Node) nodes.get(i);
|
||||
try {
|
||||
results.add(nodeMapper.mapNode(node, i));
|
||||
}
|
||||
catch (DOMException ex) {
|
||||
throw new XPathException("Mapping resulted in DOMException", ex);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
catch (JaxenException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new XPathException("Could not transform context to DOM Node", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private XPath createXPath(String expression) throws JaxenException {
|
||||
XPath xpath = new DOMXPath(expression);
|
||||
if (getNamespaces() != null && !getNamespaces().isEmpty()) {
|
||||
xpath.setNamespaceContext(new SimpleNamespaceContext(getNamespaces()));
|
||||
}
|
||||
return xpath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
|
||||
import org.springframework.xml.namespace.SimpleNamespaceContext;
|
||||
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
/**
|
||||
* JAXP 1.3-specific factory creating {@link XPathExpression} objects.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #createXPathExpression(String)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
abstract class Jaxp13XPathExpressionFactory {
|
||||
|
||||
private static XPathFactory xpathFactory = XPathFactory.newInstance();
|
||||
|
||||
/**
|
||||
* Creates a JAXP 1.3 <code>XPathExpression</code> from the given string expression.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @return the compiled <code>XPathExpression</code>
|
||||
* @throws XPathParseException when the given expression cannot be parsed
|
||||
*/
|
||||
static XPathExpression createXPathExpression(String expression) {
|
||||
try {
|
||||
XPath xpath = createXPath();
|
||||
javax.xml.xpath.XPathExpression xpathExpression = xpath.compile(expression);
|
||||
return new Jaxp13XPathExpression(xpathExpression);
|
||||
}
|
||||
catch (XPathExpressionException ex) {
|
||||
throw new org.springframework.xml.xpath.XPathParseException(
|
||||
"Could not compile [" + expression + "] to a XPathExpression: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a JAXP 1.3 <code>XPathExpression</code> from the given string expression and namespaces.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param namespaces the namespaces
|
||||
* @return the compiled <code>XPathExpression</code>
|
||||
* @throws XPathParseException when the given expression cannot be parsed
|
||||
*/
|
||||
public static XPathExpression createXPathExpression(String expression, Map<String, String> namespaces) {
|
||||
try {
|
||||
XPath xpath = createXPath();
|
||||
SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
|
||||
namespaceContext.setBindings(namespaces);
|
||||
xpath.setNamespaceContext(namespaceContext);
|
||||
javax.xml.xpath.XPathExpression xpathExpression = xpath.compile(expression);
|
||||
return new Jaxp13XPathExpression(xpathExpression);
|
||||
}
|
||||
catch (XPathExpressionException ex) {
|
||||
throw new org.springframework.xml.xpath.XPathParseException(
|
||||
"Could not compile [" + expression + "] to a XPathExpression: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static synchronized XPath createXPath() {
|
||||
return xpathFactory.newXPath();
|
||||
}
|
||||
|
||||
|
||||
/** JAXP 1.3 implementation of the <code>XPathExpression</code> interface. */
|
||||
private static class Jaxp13XPathExpression implements XPathExpression {
|
||||
|
||||
private final javax.xml.xpath.XPathExpression xpathExpression;
|
||||
|
||||
private Jaxp13XPathExpression(javax.xml.xpath.XPathExpression xpathExpression) {
|
||||
this.xpathExpression = xpathExpression;
|
||||
}
|
||||
|
||||
public String evaluateAsString(Node node) {
|
||||
return (String) evaluate(node, XPathConstants.STRING);
|
||||
}
|
||||
|
||||
public List<Node> evaluateAsNodeList(Node node) {
|
||||
NodeList nodeList = (NodeList) evaluate(node, XPathConstants.NODESET);
|
||||
return toNodeList(nodeList);
|
||||
}
|
||||
|
||||
private Object evaluate(Node node, QName returnType) {
|
||||
try {
|
||||
// XPathExpression is not thread-safe
|
||||
synchronized (xpathExpression) {
|
||||
return xpathExpression.evaluate(node, returnType);
|
||||
}
|
||||
}
|
||||
catch (XPathExpressionException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression:" + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Node> toNodeList(NodeList nodeList) {
|
||||
List<Node> result = new ArrayList<Node>(nodeList.getLength());
|
||||
for (int i = 0; i < nodeList.getLength(); i++) {
|
||||
result.add(nodeList.item(i));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public double evaluateAsNumber(Node node) {
|
||||
return (Double) evaluate(node, XPathConstants.NUMBER);
|
||||
}
|
||||
|
||||
public boolean evaluateAsBoolean(Node node) {
|
||||
return (Boolean) evaluate(node, XPathConstants.BOOLEAN);
|
||||
}
|
||||
|
||||
public Node evaluateAsNode(Node node) {
|
||||
return (Node) evaluate(node, XPathConstants.NODE);
|
||||
}
|
||||
|
||||
public <T> T evaluateAsObject(Node node, NodeMapper<T> nodeMapper) throws XPathException {
|
||||
Node result = (Node) evaluate(node, XPathConstants.NODE);
|
||||
if (result != null) {
|
||||
try {
|
||||
return nodeMapper.mapNode(result, 0);
|
||||
}
|
||||
catch (DOMException ex) {
|
||||
throw new XPathException("Mapping resulted in DOMException", ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> evaluate(Node node, NodeMapper<T> nodeMapper) throws XPathException {
|
||||
NodeList nodes = (NodeList) evaluate(node, XPathConstants.NODESET);
|
||||
List<T> results = new ArrayList<T>(nodes.getLength());
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
try {
|
||||
results.add(nodeMapper.mapNode(nodes.item(i), i));
|
||||
}
|
||||
catch (DOMException ex) {
|
||||
throw new XPathException("Mapping resulted in DOMException", ex);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
* Copyright 2005-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.xpath.XPath;
|
||||
import javax.xml.xpath.XPathConstants;
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
import javax.xml.xpath.XPathFactory;
|
||||
import javax.xml.xpath.XPathFactoryConfigurationException;
|
||||
|
||||
import org.springframework.util.xml.StaxUtils;
|
||||
import org.springframework.xml.namespace.SimpleNamespaceContext;
|
||||
import org.springframework.xml.transform.TransformerHelper;
|
||||
import org.springframework.xml.transform.TraxUtils;
|
||||
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
/**
|
||||
* Implementation of {@link XPathOperations} that uses JAXP 1.3. JAXP 1.3 is part of Java SE since 1.5.
|
||||
* <p/>
|
||||
* Namespaces can be set using the {@code namespaces} property.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #setNamespaces(java.util.Map)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class Jaxp13XPathTemplate extends AbstractXPathTemplate {
|
||||
|
||||
private XPathFactory xpathFactory;
|
||||
|
||||
public Jaxp13XPathTemplate() {
|
||||
this(XPathFactory.DEFAULT_OBJECT_MODEL_URI);
|
||||
}
|
||||
|
||||
public Jaxp13XPathTemplate(String xpathFactoryUri) {
|
||||
try {
|
||||
xpathFactory = XPathFactory.newInstance(xpathFactoryUri);
|
||||
}
|
||||
catch (XPathFactoryConfigurationException ex) {
|
||||
throw new XPathException("Could not create XPathFactory", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean evaluateAsBoolean(String expression, Source context) throws XPathException {
|
||||
Boolean result = (Boolean) evaluate(expression, context, XPathConstants.BOOLEAN);
|
||||
return result != null && result;
|
||||
}
|
||||
|
||||
public Node evaluateAsNode(String expression, Source context) throws XPathException {
|
||||
return (Node) evaluate(expression, context, XPathConstants.NODE);
|
||||
}
|
||||
|
||||
public List<Node> evaluateAsNodeList(String expression, Source context) throws XPathException {
|
||||
NodeList result = (NodeList) evaluate(expression, context, XPathConstants.NODESET);
|
||||
List<Node> nodes = new ArrayList<Node>(result.getLength());
|
||||
for (int i = 0; i < result.getLength(); i++) {
|
||||
nodes.add(result.item(i));
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
public double evaluateAsDouble(String expression, Source context) throws XPathException {
|
||||
Double result = (Double) evaluate(expression, context, XPathConstants.NUMBER);
|
||||
return result != null ? result : Double.NaN;
|
||||
}
|
||||
|
||||
public String evaluateAsString(String expression, Source context) throws XPathException {
|
||||
return (String) evaluate(expression, context, XPathConstants.STRING);
|
||||
}
|
||||
|
||||
public <T> T evaluateAsObject(String expression, Source context, NodeMapper<T> nodeMapper) throws XPathException {
|
||||
Node node = evaluateAsNode(expression, context);
|
||||
if (node != null) {
|
||||
try {
|
||||
return nodeMapper.mapNode(node, 0);
|
||||
}
|
||||
catch (DOMException ex) {
|
||||
throw new XPathException("Mapping resulted in DOMException", ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> evaluate(String expression, Source context, NodeMapper<T> nodeMapper) throws XPathException {
|
||||
NodeList nodes = (NodeList) evaluate(expression, context, XPathConstants.NODESET);
|
||||
List<T> results = new ArrayList<T>(nodes.getLength());
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
try {
|
||||
results.add(nodeMapper.mapNode(nodes.item(i), i));
|
||||
}
|
||||
catch (DOMException ex) {
|
||||
throw new XPathException("Mapping resulted in DOMException", ex);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private Object evaluate(String expression, Source context, QName returnType) throws XPathException {
|
||||
XPath xpath = createXPath();
|
||||
if (getNamespaces() != null && !getNamespaces().isEmpty()) {
|
||||
SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
|
||||
namespaceContext.setBindings(getNamespaces());
|
||||
xpath.setNamespaceContext(namespaceContext);
|
||||
}
|
||||
try {
|
||||
EvaluationCallback callback = new EvaluationCallback(xpath, expression, returnType);
|
||||
TraxUtils.doWithSource(context, callback);
|
||||
return callback.result;
|
||||
}
|
||||
catch (javax.xml.xpath.XPathException ex) {
|
||||
throw new XPathException("Could not evaluate XPath expression [" + expression + "]", ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new XPathException("Could not transform context to DOM Node", ex);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new XPathException(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized XPath createXPath() {
|
||||
return xpathFactory.newXPath();
|
||||
}
|
||||
|
||||
private static class EvaluationCallback implements TraxUtils.SourceCallback {
|
||||
|
||||
private final XPath xpath;
|
||||
|
||||
private final String expression;
|
||||
|
||||
private final QName returnType;
|
||||
|
||||
private final TransformerHelper transformerHelper = new TransformerHelper();
|
||||
|
||||
private Object result;
|
||||
|
||||
private EvaluationCallback(XPath xpath, String expression, QName returnType) {
|
||||
this.xpath = xpath;
|
||||
this.expression = expression;
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
public void domSource(Node node) throws XPathExpressionException {
|
||||
result = xpath.evaluate(expression, node, returnType);
|
||||
}
|
||||
|
||||
public void saxSource(XMLReader reader, InputSource inputSource) throws XPathExpressionException {
|
||||
inputSource(inputSource);
|
||||
}
|
||||
|
||||
public void staxSource(XMLEventReader eventReader)
|
||||
throws XPathExpressionException, XMLStreamException, TransformerException {
|
||||
Element element = getRootElement(StaxUtils.createCustomStaxSource(eventReader));
|
||||
domSource(element);
|
||||
}
|
||||
|
||||
public void staxSource(XMLStreamReader streamReader) throws TransformerException, XPathExpressionException {
|
||||
Element element = getRootElement(StaxUtils.createCustomStaxSource(streamReader));
|
||||
domSource(element);
|
||||
}
|
||||
|
||||
public void streamSource(InputStream inputStream) throws XPathExpressionException {
|
||||
inputSource(new InputSource(inputStream));
|
||||
}
|
||||
|
||||
public void streamSource(Reader reader) throws XPathExpressionException {
|
||||
inputSource(new InputSource(reader));
|
||||
}
|
||||
|
||||
public void source(String systemId) throws XPathExpressionException {
|
||||
inputSource(new InputSource(systemId));
|
||||
}
|
||||
|
||||
private void inputSource(InputSource inputSource) throws XPathExpressionException {
|
||||
result = xpath.evaluate(expression, inputSource, returnType);
|
||||
}
|
||||
|
||||
private Element getRootElement(Source source) throws TransformerException {
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformerHelper.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return document.getDocumentElement();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* An interface used by {@link XPathOperations} implementations for processing {@link Node} objects on a per-node basis.
|
||||
* Implementations of this interface perform the actual work of processing nodes, but don't need to worry about
|
||||
* exception handling.
|
||||
* <p/>
|
||||
* Consider using a {@link NodeMapper} instead if you need to map exactly result object per node, assembling them in a
|
||||
* List.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see XPathOperations#evaluate(String,javax.xml.transform.Source,NodeCallbackHandler)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface NodeCallbackHandler {
|
||||
|
||||
/**
|
||||
* Processed a single node.
|
||||
*
|
||||
* @param node the node to map
|
||||
* @throws DOMException in case of DOM errors
|
||||
*/
|
||||
void processNode(Node node) throws DOMException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* An interface used by {@link XPathOperations} implementations for mapping {@link Node} objects on a per-node basis.
|
||||
* Implementations of this interface perform the actual work of mapping each node to a result object, but don't need to
|
||||
* worry about exception handling.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see XPathOperations#evaluate(String,javax.xml.transform.Source,NodeMapper)
|
||||
* @see XPathOperations#evaluateAsObject(String,javax.xml.transform.Source,NodeMapper)
|
||||
* @see XPathExpression#evaluate(org.w3c.dom.Node,NodeMapper)
|
||||
* @see XPathExpression#evaluateAsObject(org.w3c.dom.Node,NodeMapper)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface NodeMapper<T> {
|
||||
|
||||
/**
|
||||
* Maps a single node to an arbitrary object.
|
||||
*
|
||||
* @param node the node to map
|
||||
* @param nodeNum the number of the current node
|
||||
* @return object for the current node
|
||||
* @throws DOMException in case of DOM errors
|
||||
*/
|
||||
T mapNode(Node node, int nodeNum) throws DOMException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import org.springframework.xml.XmlException;
|
||||
|
||||
/**
|
||||
* Exception thrown when an error occurs during XPath processing.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class XPathException extends XmlException {
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the <code>XPathException</code> with the specific detail message.
|
||||
*
|
||||
* @param message the detail message
|
||||
*/
|
||||
public XPathException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the <code>XPathException</code> with the specific detail message and exception.
|
||||
*
|
||||
* @param message the detail message
|
||||
* @param throwable the wrapped exception
|
||||
*/
|
||||
public XPathException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Defines the contract for a precompiled XPath expression. Concrete instances can be obtained through the {@link
|
||||
* XPathExpressionFactory}.
|
||||
* <p/>
|
||||
* Implementations of this interface are precompiled, and thus faster, but less flexible, than the XPath expressions
|
||||
* used by {@link XPathOperations} implementations.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface XPathExpression {
|
||||
|
||||
/**
|
||||
* Evaluates the given expression as a <code>boolean</code>. Returns the boolean evaluation of the expression, or
|
||||
* <code>false</code> if it is invalid.
|
||||
* <p/>
|
||||
* The return value is determined per the {@code boolean()} function defined in the XPath specification.
|
||||
* This means that an expression that selects zero nodes will return {@code false}, while an expression that
|
||||
* selects one or more nodes will return {@code true}.
|
||||
* An expression that returns a string returns {@code false} for empty strings and {@code true} for all other
|
||||
* strings.
|
||||
* An expression that returns a number returns {@code false} for zero and {@code true} for non-zero numbers.
|
||||
*
|
||||
* @param node the starting point
|
||||
* @return the result of the evaluation
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath/#function-boolean">XPath specification - boolean() function</a>
|
||||
*/
|
||||
boolean evaluateAsBoolean(Node node) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression as a {@link Node}. Returns the evaluation of the expression, or <code>null</code>
|
||||
* if it is invalid.
|
||||
*
|
||||
* @param node the starting point
|
||||
* @return the result of the evaluation
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath#node-sets">XPath specification</a>
|
||||
*/
|
||||
Node evaluateAsNode(Node node) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression, and returns all {@link Node} objects that conform to it. Returns an empty list if
|
||||
* no result could be found.
|
||||
*
|
||||
* @param node the starting point
|
||||
* @return a list of <code>Node</code>s that are selected by the expression
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath#node-sets">XPath specification</a>
|
||||
*/
|
||||
List<Node> evaluateAsNodeList(Node node) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression as a number (<code>double</code>). Returns the numeric evaluation of the
|
||||
* expression, or {@link Double#NaN} if it is invalid.
|
||||
* <p/>
|
||||
* The return value is determined per the {@code number()} function as defined in the XPath specification.
|
||||
* This means that if the expression selects multiple nodes, it will return the number value of the first node.
|
||||
*
|
||||
* @param node the starting point
|
||||
* @return the result of the evaluation
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath/#function-number">XPath specification - number() function</a>
|
||||
*/
|
||||
double evaluateAsNumber(Node node) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression as a String. Returns <code>null</code> if no result could be found.
|
||||
* <p/>
|
||||
* The return value is determined per the {@code string()} function as defined in the XPath specification.
|
||||
* This means that if the expression selects multiple nodes, it will return the string value of the first node.
|
||||
*
|
||||
* @param node the starting point
|
||||
* @return the result of the evaluation
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath/#function-string">XPath specification - string() function</a>
|
||||
*/
|
||||
String evaluateAsString(Node node) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression, mapping a single {@link Node} result to a Java object via a {@link NodeMapper}.
|
||||
*
|
||||
* @param node the starting point
|
||||
* @param nodeMapper object that will map one object per node
|
||||
* @return the single mapped object
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath#node-sets">XPath specification</a>
|
||||
*/
|
||||
<T> T evaluateAsObject(Node node, NodeMapper<T> nodeMapper) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression, mapping each result {@link Node} objects to a Java object via a {@link
|
||||
* NodeMapper}.
|
||||
*
|
||||
* @param node the starting point
|
||||
* @param nodeMapper object that will map one object per node
|
||||
* @return the result list, containing mapped objects
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath#node-sets">XPath specification</a>
|
||||
*/
|
||||
<T> List<T> evaluate(Node node, NodeMapper<T> nodeMapper) throws XPathException;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2005-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* Factory for compiled <code>XPathExpression</code>s, being aware of JAXP 1.3+ XPath functionality, and Jaxen. Mainly
|
||||
* for internal use of the framework.
|
||||
* <p/>
|
||||
* The goal of this class is to avoid runtime dependencies a specific XPath engine, simply using the best XPath
|
||||
* implementation that is available. Prefers JAXP 1.3+ XPath implementations to Jaxen.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see XPathExpression
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public abstract class XPathExpressionFactory {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(XPathExpressionFactory.class);
|
||||
|
||||
/**
|
||||
* Create a compiled XPath expression using the given string.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @return the compiled XPath expression
|
||||
* @throws IllegalStateException if neither JAXP 1.3+, or Jaxen are available
|
||||
* @throws XPathParseException if the given expression cannot be parsed
|
||||
*/
|
||||
public static XPathExpression createXPathExpression(String expression)
|
||||
throws IllegalStateException, XPathParseException {
|
||||
return createXPathExpression(expression, Collections.<String, String>emptyMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a compiled XPath expression using the given string and namespaces. The namespace map should consist of
|
||||
* string prefixes mapped to string namespaces.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param namespaces a map that binds string prefixes to string namespaces
|
||||
* @return the compiled XPath expression
|
||||
* @throws IllegalStateException if neither JAXP 1.3+, or Jaxen are available
|
||||
* @throws XPathParseException if the given expression cannot be parsed
|
||||
*/
|
||||
public static XPathExpression createXPathExpression(String expression, Map<String, String> namespaces)
|
||||
throws IllegalStateException, XPathParseException {
|
||||
Assert.hasLength(expression, "expression is empty");
|
||||
if (namespaces == null) {
|
||||
namespaces = Collections.emptyMap();
|
||||
}
|
||||
try {
|
||||
logger.trace("Creating [javax.xml.xpath.XPathExpression]");
|
||||
return Jaxp13XPathExpressionFactory.createXPathExpression(expression, namespaces);
|
||||
}
|
||||
catch (XPathException e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Spring {@link FactoryBean} for {@link XPathExpression} object. Facilitates injection of XPath expressions into
|
||||
* endpoint beans.
|
||||
* <p/>
|
||||
* Uses {@link XPathExpressionFactory} underneath, so support is provided for JAXP 1.3, and Jaxen XPaths.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see #setExpression(String)
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class XPathExpressionFactoryBean implements FactoryBean<XPathExpression>, InitializingBean {
|
||||
|
||||
private Map<String, String> namespaces;
|
||||
|
||||
private String expressionString;
|
||||
|
||||
private XPathExpression expression;
|
||||
|
||||
/** Sets the XPath expression. Setting this property is required. */
|
||||
public void setExpression(String expression) {
|
||||
expressionString = expression;
|
||||
}
|
||||
|
||||
/** Sets the namespaces for the expressions. The given properties binds string prefixes to string namespaces. */
|
||||
public void setNamespaces(Map<String, String> namespaces) {
|
||||
this.namespaces = namespaces;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws IllegalStateException, XPathParseException {
|
||||
Assert.notNull(expressionString, "expression is required");
|
||||
if (CollectionUtils.isEmpty(namespaces)) {
|
||||
expression = XPathExpressionFactory.createXPathExpression(expressionString);
|
||||
}
|
||||
else {
|
||||
expression = XPathExpressionFactory.createXPathExpression(expressionString, namespaces);
|
||||
}
|
||||
}
|
||||
|
||||
public XPathExpression getObject() throws Exception {
|
||||
return expression;
|
||||
}
|
||||
|
||||
public Class<? extends XPathExpression> getObjectType() {
|
||||
return XPathExpression.class;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.List;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Interface that specifies a basic set of XPath operations, implemented by various XPathTemplates. Contains numerous
|
||||
* evaluation methods,
|
||||
* <p/>
|
||||
* The templates that implement this interface do not use precompiled XPath expressions. Consider using the {@link
|
||||
* XPathExpressionFactory} or the {@link XPathExpressionFactoryBean} for optimal performance, but less flexibility.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see Jaxp13XPathTemplate
|
||||
* @see JaxenXPathTemplate
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface XPathOperations {
|
||||
|
||||
/**
|
||||
* Evaluates the given expression as a <code>boolean</code>. Returns the boolean evaluation of the expression, or
|
||||
* <code>false</code> if it is invalid.
|
||||
* <p/>
|
||||
* The return value is determined per the {@code boolean()} function defined in the XPath specification.
|
||||
* This means that an expression that selects zero nodes will return {@code false}, while an expression that
|
||||
* selects one or more nodes will return {@code true}.
|
||||
* An expression that returns a string returns {@code false} for empty strings and {@code true} for all other
|
||||
* strings.
|
||||
* An expression that returns a number returns {@code false} for zero and {@code true} for non-zero numbers.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param context the context starting point
|
||||
* @return the result of the evaluation
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath/#function-boolean">XPath specification - boolean() function</a>
|
||||
*/
|
||||
boolean evaluateAsBoolean(String expression, Source context) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression as a {@link Node}. Returns the evaluation of the expression, or <code>null</code>
|
||||
* if it is invalid.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param context the context starting point
|
||||
* @return the result of the evaluation
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath#node-sets">XPath specification</a>
|
||||
*/
|
||||
Node evaluateAsNode(String expression, Source context) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression as a list of {@link Node} objects. Returns the evaluation of the expression, or an
|
||||
* empty list if no results are found.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param context the context starting point
|
||||
* @return the result of the evaluation
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath#node-sets">XPath specification</a>
|
||||
*/
|
||||
List<Node> evaluateAsNodeList(String expression, Source context) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression as a <code>double</code>. Returns the evaluation of the expression, or {@link
|
||||
* Double#NaN} if it is invalid.
|
||||
* <p/>
|
||||
* The return value is determined per the {@code number()} function as defined in the XPath specification.
|
||||
* This means that if the expression selects multiple nodes, it will return the number value of the first node.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param context the context starting point
|
||||
* @return the result of the evaluation
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath/#function-number">XPath specification - number() function</a>
|
||||
*/
|
||||
double evaluateAsDouble(String expression, Source context) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression as a {@link String}. Returns the evaluation of the expression, or
|
||||
* <code>null</code> if it is invalid.
|
||||
* <p/>
|
||||
* The return value is determined per the {@code string()} function as defined in the XPath specification.
|
||||
* This means that if the expression selects multiple nodes, it will return the string value of the first node.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param context the context starting point
|
||||
* @return the result of the evaluation
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath/#function-string">XPath specification - string() function</a>
|
||||
*/
|
||||
String evaluateAsString(String expression, Source context) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression, mapping a single {@link Node} result to a Java object via a {@link NodeMapper}.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param context the context starting point
|
||||
* @param nodeMapper object that will map one object per node
|
||||
* @return the single mapped object
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath#node-sets">XPath specification</a>
|
||||
*/
|
||||
<T> T evaluateAsObject(String expression, Source context, NodeMapper<T> nodeMapper) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression, mapping each result {@link Node} objects to a Java object via a {@link
|
||||
* NodeMapper}.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param context the context starting point
|
||||
* @param nodeMapper object that will map one object per node
|
||||
* @return the result list, containing mapped objects
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath#node-sets">XPath specification</a>
|
||||
*/
|
||||
<T> List<T> evaluate(String expression, Source context, NodeMapper<T> nodeMapper) throws XPathException;
|
||||
|
||||
/**
|
||||
* Evaluates the given expression, handling the result {@link Node} objects on a per-node basis with a {@link
|
||||
* NodeCallbackHandler}.
|
||||
*
|
||||
* @param expression the XPath expression
|
||||
* @param context the context starting point
|
||||
* @param callbackHandler object that will extract results, one row at a time
|
||||
* @throws XPathException in case of XPath errors
|
||||
* @see <a href="http://www.w3.org/TR/xpath#node-sets">XPath specification</a>
|
||||
*/
|
||||
void evaluate(String expression, Source context, NodeCallbackHandler callbackHandler) throws XPathException;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2006 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
/**
|
||||
* Exception throws when a XPath expression cannot be parsed.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class XPathParseException extends XPathException {
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the <code>XPathParseException</code> with the specific detail message.
|
||||
*
|
||||
* @param message the detail message
|
||||
*/
|
||||
public XPathParseException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the <code>XPathParseException</code> with the specific detail message and
|
||||
* exception.
|
||||
*
|
||||
* @param message the detail message
|
||||
* @param throwable the wrapped exception
|
||||
*/
|
||||
public XPathParseException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides XPathTemplate implementations, and various classes for XPath evaluation using JAXP 1.3, and Jaxen.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright 2005-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.namespace.QNameUtils;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
/**
|
||||
* The default {@link XsdSchema} implementation.
|
||||
* <p/>
|
||||
* Allows a XSD to be set by the {@link #setXsd(Resource)}, or directly in the {@link #SimpleXsdSchema(Resource)
|
||||
* constructor}.
|
||||
*
|
||||
* @author Mark LaFond
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public class SimpleXsdSchema implements XsdSchema, InitializingBean {
|
||||
|
||||
private static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
|
||||
private static final String SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";
|
||||
|
||||
private static final QName SCHEMA_NAME = QNameUtils.createQName(SCHEMA_NAMESPACE, "schema", "xsd");
|
||||
|
||||
private Resource xsdResource;
|
||||
|
||||
private Element schemaElement;
|
||||
|
||||
static {
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the {@link SimpleXsdSchema} class.
|
||||
* <p/>
|
||||
* A subsequent call to the {@link #setXsd(Resource)} method is required.
|
||||
*/
|
||||
public SimpleXsdSchema() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the {@link SimpleXsdSchema} class with the specified resource.
|
||||
*
|
||||
* @param xsdResource the XSD resource; must not be <code>null</code>
|
||||
* @throws IllegalArgumentException if the supplied <code>xsdResource</code> is <code>null</code>
|
||||
*/
|
||||
public SimpleXsdSchema(Resource xsdResource) {
|
||||
Assert.notNull(xsdResource, "xsdResource must not be null");
|
||||
this.xsdResource = xsdResource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the XSD resource to be exposed by calls to this instances' {@link #getSource()} method.
|
||||
*
|
||||
* @param xsdResource the XSD resource
|
||||
*/
|
||||
public void setXsd(Resource xsdResource) {
|
||||
this.xsdResource = xsdResource;
|
||||
}
|
||||
|
||||
public String getTargetNamespace() {
|
||||
return schemaElement.getAttribute("targetNamespace");
|
||||
}
|
||||
|
||||
public Source getSource() {
|
||||
return new DOMSource(schemaElement);
|
||||
}
|
||||
|
||||
public XmlValidator createValidator() throws IOException {
|
||||
return XmlValidatorFactory.createValidator(xsdResource, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws ParserConfigurationException, IOException, SAXException {
|
||||
Assert.notNull(xsdResource, "'xsd' is required");
|
||||
Assert.isTrue(this.xsdResource.exists(), "xsd '" + this.xsdResource + "' does not exist");
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
loadSchema(documentBuilder);
|
||||
}
|
||||
|
||||
private void loadSchema(DocumentBuilder documentBuilder) throws SAXException, IOException {
|
||||
Document schemaDocument = documentBuilder.parse(SaxUtils.createInputSource(xsdResource));
|
||||
schemaElement = schemaDocument.getDocumentElement();
|
||||
Assert.isTrue(SCHEMA_NAME.getLocalPart().equals(schemaElement.getLocalName()),
|
||||
xsdResource + " has invalid root element : [" + schemaElement.getLocalName() + "] instead of [schema]");
|
||||
Assert.isTrue(SCHEMA_NAME.getNamespaceURI().equals(schemaElement.getNamespaceURI()), xsdResource +
|
||||
" has invalid root element: [" + schemaElement.getNamespaceURI() + "] instead of [" +
|
||||
SCHEMA_NAME.getNamespaceURI() + "]");
|
||||
Assert.hasText(getTargetNamespace(), xsdResource + " has no targetNamespace");
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder("SimpleXsdSchema");
|
||||
builder.append('{');
|
||||
builder.append(getTargetNamespace());
|
||||
builder.append('}');
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
|
||||
/**
|
||||
* Represents an abstraction for XSD schemas.
|
||||
*
|
||||
* @author Mark LaFond
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public interface XsdSchema {
|
||||
|
||||
/**
|
||||
* Returns the target namespace of this schema.
|
||||
*
|
||||
* @return the target namespace
|
||||
*/
|
||||
String getTargetNamespace();
|
||||
|
||||
/**
|
||||
* Returns the {@link Source} of the schema.
|
||||
*
|
||||
* @return the source of this XSD schema
|
||||
*/
|
||||
Source getSource();
|
||||
|
||||
/**
|
||||
* Creates a {@link XmlValidator} based on the schema.
|
||||
*
|
||||
* @return a validator for this schema
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
XmlValidator createValidator() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
|
||||
/**
|
||||
* Represents an abstraction for a collection of XSD schemas.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public interface XsdSchemaCollection {
|
||||
|
||||
/**
|
||||
* Returns all schemas contained in this collection.
|
||||
*
|
||||
* @return the schemas contained in this collection
|
||||
*/
|
||||
XsdSchema[] getXsdSchemas();
|
||||
|
||||
/**
|
||||
* Creates a {@link XmlValidator} based on the schemas contained in this collection.
|
||||
*
|
||||
* @return a validator for this collection
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
XmlValidator createValidator() throws IOException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd;
|
||||
|
||||
import org.springframework.xml.XmlException;
|
||||
|
||||
/**
|
||||
* Base class for all WSDL definition exceptions.
|
||||
*
|
||||
* @author Mark LaFond
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public class XsdSchemaException extends XmlException {
|
||||
|
||||
public XsdSchemaException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public XsdSchemaException(String message, Throwable throwable) {
|
||||
super(message, throwable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright 2005-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd.commons;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.springframework.beans.BeanInstantiationException;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
import org.springframework.xml.xsd.XsdSchema;
|
||||
|
||||
import org.apache.ws.commons.schema.XmlSchema;
|
||||
import org.apache.ws.commons.schema.XmlSchemaCollection;
|
||||
import org.apache.ws.commons.schema.XmlSchemaSerializer;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* Implementation of the {@link XsdSchema} interface that uses Apache WS-Commons XML Schema.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see <a href="http://ws.apache.org/commons/XmlSchema/">Commons XML Schema</a>
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public class CommonsXsdSchema implements XsdSchema {
|
||||
|
||||
private final XmlSchema schema;
|
||||
|
||||
private final XmlSchemaCollection collection;
|
||||
|
||||
/**
|
||||
* Create a new instance of the {@code CommonsXsdSchema} class with the specified {@link XmlSchema} reference.
|
||||
*
|
||||
* @param schema the Commons <code>XmlSchema</code> object; must not be <code>null</code>
|
||||
* @throws IllegalArgumentException if the supplied <code>schema</code> is <code>null</code>
|
||||
*/
|
||||
protected CommonsXsdSchema(XmlSchema schema) {
|
||||
this(schema, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the {@code CommonsXsdSchema} class with the specified {@link XmlSchema} and {@link
|
||||
* XmlSchemaCollection} reference.
|
||||
*
|
||||
* @param schema the Commons <code>XmlSchema</code> object; must not be <code>null</code>
|
||||
* @param collection the Commons <code>XmlSchemaCollection</code> object; can be <code>null</code>
|
||||
* @throws IllegalArgumentException if the supplied <code>schema</code> is <code>null</code>
|
||||
*/
|
||||
protected CommonsXsdSchema(XmlSchema schema, XmlSchemaCollection collection) {
|
||||
Assert.notNull(schema, "'schema' must not be null");
|
||||
this.schema = schema;
|
||||
this.collection = collection;
|
||||
}
|
||||
|
||||
public String getTargetNamespace() {
|
||||
return schema.getTargetNamespace();
|
||||
}
|
||||
|
||||
public QName[] getElementNames() {
|
||||
List<QName> result = new ArrayList<QName>(schema.getElements().keySet());
|
||||
return result.toArray(new QName[result.size()]);
|
||||
}
|
||||
|
||||
public Source getSource() {
|
||||
// try to use the the package-friendly XmlSchemaSerializer first, fall back to slower stream-based version
|
||||
try {
|
||||
XmlSchemaSerializer serializer = BeanUtils.instantiateClass(XmlSchemaSerializer.class);
|
||||
if (collection != null) {
|
||||
serializer.setExtReg(collection.getExtReg());
|
||||
}
|
||||
Document[] serializedSchemas = serializer.serializeSchema(schema, false);
|
||||
return new DOMSource(serializedSchemas[0]);
|
||||
}
|
||||
catch (BeanInstantiationException ex) {
|
||||
// ignore
|
||||
}
|
||||
catch (XmlSchemaSerializer.XmlSchemaSerializerException ex) {
|
||||
// ignore
|
||||
}
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
try {
|
||||
schema.write(bos);
|
||||
}
|
||||
catch (UnsupportedEncodingException ex) {
|
||||
throw new CommonsXsdSchemaException(ex.getMessage(), ex);
|
||||
}
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
|
||||
return new StreamSource(bis);
|
||||
}
|
||||
|
||||
public XmlValidator createValidator() throws IOException {
|
||||
Resource resource = new UrlResource(schema.getSourceURI());
|
||||
return XmlValidatorFactory.createValidator(resource, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
}
|
||||
|
||||
/** Returns the wrapped Commons <code>XmlSchema</code> object. */
|
||||
public XmlSchema getSchema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder("CommonsXsdSchema");
|
||||
builder.append('{');
|
||||
builder.append(getTargetNamespace());
|
||||
builder.append('}');
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
* Copyright 2005-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd.commons;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
import org.springframework.xml.xsd.XsdSchema;
|
||||
import org.springframework.xml.xsd.XsdSchemaCollection;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.ws.commons.schema.XmlSchema;
|
||||
import org.apache.ws.commons.schema.XmlSchemaCollection;
|
||||
import org.apache.ws.commons.schema.XmlSchemaExternal;
|
||||
import org.apache.ws.commons.schema.XmlSchemaImport;
|
||||
import org.apache.ws.commons.schema.XmlSchemaInclude;
|
||||
import org.apache.ws.commons.schema.XmlSchemaObject;
|
||||
import org.apache.ws.commons.schema.resolver.DefaultURIResolver;
|
||||
import org.apache.ws.commons.schema.resolver.URIResolver;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
/**
|
||||
* Implementation of the {@link XsdSchemaCollection} that uses Apache WS-Commons XML Schema.
|
||||
* <p/>
|
||||
* Setting the {@link #setInline(boolean) inline} flag to <code>true</code> will result in all referenced schemas
|
||||
* (included and imported) being merged into the referred schema. When including the schemas into a WSDL, this greatly
|
||||
* simplifies the deployment of the schemas.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see <a href="http://ws.apache.org/commons/XmlSchema/">Commons XML Schema</a>
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public class CommonsXsdSchemaCollection implements XsdSchemaCollection, InitializingBean, ResourceLoaderAware {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(CommonsXsdSchemaCollection.class);
|
||||
|
||||
private final XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
|
||||
|
||||
private final List<XmlSchema> xmlSchemas = new ArrayList<XmlSchema>();
|
||||
|
||||
private Resource[] xsdResources;
|
||||
|
||||
private boolean inline = false;
|
||||
|
||||
private URIResolver uriResolver = new ClasspathUriResolver();
|
||||
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
/**
|
||||
* Constructs a new, empty instance of the <code>CommonsXsdSchemaCollection</code>.
|
||||
* <p/>
|
||||
* A subsequent call to the {@link #setXsds(Resource[])} is required.
|
||||
*/
|
||||
public CommonsXsdSchemaCollection() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the <code>CommonsXsdSchemaCollection</code> based on the given resources.
|
||||
*
|
||||
* @param resources the schema resources to load
|
||||
*/
|
||||
public CommonsXsdSchemaCollection(Resource[] resources) {
|
||||
this.xsdResources = resources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the schema resources to be loaded.
|
||||
*
|
||||
* @param xsdResources the schema resources to be loaded
|
||||
*/
|
||||
public void setXsds(Resource[] xsdResources) {
|
||||
this.xsdResources = xsdResources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines whether included schemas should be inlined into the including schema.
|
||||
* <p/>
|
||||
* Defaults to <code>false</code>.
|
||||
*/
|
||||
public void setInline(boolean inline) {
|
||||
this.inline = inline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the WS-Commons uri resolver to use when resolving (relative) schemas.
|
||||
* <p/>
|
||||
* Default is an internal subclass of {@link DefaultURIResolver} which correctly handles schemas on the classpath.
|
||||
*/
|
||||
public void setUriResolver(URIResolver uriResolver) {
|
||||
Assert.notNull(uriResolver, "'uriResolver' must not be null");
|
||||
this.uriResolver = uriResolver;
|
||||
}
|
||||
|
||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws IOException {
|
||||
Assert.notEmpty(xsdResources, "'xsds' must not be empty");
|
||||
|
||||
schemaCollection.setSchemaResolver(uriResolver);
|
||||
|
||||
Set<XmlSchema> processedIncludes = new HashSet<XmlSchema>();
|
||||
Set<XmlSchema> processedImports = new HashSet<XmlSchema>();
|
||||
|
||||
for (Resource xsdResource : xsdResources) {
|
||||
Assert.isTrue(xsdResource.exists(), xsdResource + " does not exit");
|
||||
try {
|
||||
XmlSchema xmlSchema =
|
||||
schemaCollection.read(SaxUtils.createInputSource(xsdResource));
|
||||
xmlSchemas.add(xmlSchema);
|
||||
|
||||
if (inline) {
|
||||
inlineIncludes(xmlSchema, processedIncludes, processedImports);
|
||||
findImports(xmlSchema, processedImports, processedIncludes);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new CommonsXsdSchemaException("Schema [" + xsdResource + "] could not be loaded", ex);
|
||||
}
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("Loaded " + StringUtils.arrayToCommaDelimitedString(xsdResources));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public XsdSchema[] getXsdSchemas() {
|
||||
XsdSchema[] result = new XsdSchema[xmlSchemas.size()];
|
||||
for (int i = 0; i < xmlSchemas.size(); i++) {
|
||||
XmlSchema xmlSchema = xmlSchemas.get(i);
|
||||
result[i] = new CommonsXsdSchema(xmlSchema, schemaCollection);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public XmlValidator createValidator() throws IOException {
|
||||
Resource[] resources = new Resource[xmlSchemas.size()];
|
||||
for (int i = xmlSchemas.size() - 1; i >= 0; i--) {
|
||||
XmlSchema xmlSchema = xmlSchemas.get(i);
|
||||
String sourceUri = xmlSchema.getSourceURI();
|
||||
if (StringUtils.hasLength(sourceUri)) {
|
||||
resources[i] = new UrlResource(sourceUri);
|
||||
}
|
||||
}
|
||||
return XmlValidatorFactory.createValidator(resources, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
}
|
||||
|
||||
private void inlineIncludes(XmlSchema schema, Set<XmlSchema> processedIncludes, Set<XmlSchema> processedImports) {
|
||||
processedIncludes.add(schema);
|
||||
|
||||
List<XmlSchemaObject> schemaItems = schema.getItems();
|
||||
for (int i = 0; i < schemaItems.size(); i++) {
|
||||
XmlSchemaObject schemaObject = schemaItems.get(i);
|
||||
if (schemaObject instanceof XmlSchemaInclude) {
|
||||
XmlSchema includedSchema = ((XmlSchemaInclude) schemaObject).getSchema();
|
||||
if (!processedIncludes.contains(includedSchema)) {
|
||||
inlineIncludes(includedSchema, processedIncludes, processedImports);
|
||||
findImports(includedSchema, processedImports, processedIncludes);
|
||||
List<XmlSchemaObject> includeItems = includedSchema.getItems();
|
||||
for (XmlSchemaObject includedItem : includeItems) {
|
||||
schemaItems.add(includedItem);
|
||||
}
|
||||
}
|
||||
// remove the <include/>
|
||||
schemaItems.remove(i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void findImports(XmlSchema schema, Set<XmlSchema> processedImports, Set<XmlSchema> processedIncludes) {
|
||||
processedImports.add(schema);
|
||||
List<XmlSchemaExternal> externals = schema.getExternals();
|
||||
for (XmlSchemaExternal external : externals) {
|
||||
if (external instanceof XmlSchemaImport) {
|
||||
XmlSchemaImport schemaImport = (XmlSchemaImport) external;
|
||||
XmlSchema importedSchema = schemaImport.getSchema();
|
||||
if (!"http://www.w3.org/XML/1998/namespace".equals(schemaImport.getNamespace()) &&
|
||||
importedSchema != null && !processedImports.contains(importedSchema)) {
|
||||
inlineIncludes(importedSchema, processedIncludes, processedImports);
|
||||
findImports(importedSchema, processedImports, processedIncludes);
|
||||
xmlSchemas.add(importedSchema);
|
||||
}
|
||||
// remove the schemaLocation
|
||||
external.setSchemaLocation(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder("CommonsXsdSchemaCollection");
|
||||
builder.append('{');
|
||||
for (int i = 0; i < xmlSchemas.size(); i++) {
|
||||
XmlSchema schema = xmlSchemas.get(i);
|
||||
builder.append(schema.getTargetNamespace());
|
||||
if (i < xmlSchemas.size() - 1) {
|
||||
builder.append(',');
|
||||
}
|
||||
}
|
||||
builder.append('}');
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private class ClasspathUriResolver extends DefaultURIResolver {
|
||||
|
||||
@Override
|
||||
public InputSource resolveEntity(String namespace, String schemaLocation, String baseUri) {
|
||||
if (resourceLoader != null) {
|
||||
Resource resource = resourceLoader.getResource(schemaLocation);
|
||||
if (resource.exists()) {
|
||||
return createInputSource(resource);
|
||||
}
|
||||
else if (StringUtils.hasLength(baseUri)) {
|
||||
// let's try and find it relative to the baseUri, see SWS-413
|
||||
try {
|
||||
Resource baseUriResource = new UrlResource(baseUri);
|
||||
resource = baseUriResource.createRelative(schemaLocation);
|
||||
if (resource.exists()) {
|
||||
return createInputSource(resource);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
// let's try and find it on the classpath, see SWS-362
|
||||
String classpathLocation = ResourceLoader.CLASSPATH_URL_PREFIX + "/" + schemaLocation;
|
||||
resource = resourceLoader.getResource(classpathLocation);
|
||||
if (resource.exists()) {
|
||||
return createInputSource(resource);
|
||||
}
|
||||
}
|
||||
return super.resolveEntity(namespace, schemaLocation, baseUri);
|
||||
}
|
||||
|
||||
private InputSource createInputSource(Resource resource) {
|
||||
try {
|
||||
return SaxUtils.createInputSource(resource);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new CommonsXsdSchemaException("Could not resolve location", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd.commons;
|
||||
|
||||
import org.springframework.xml.xsd.XsdSchemaException;
|
||||
|
||||
/**
|
||||
* Commons XmlSchema version of the {@link XsdSchemaException}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 1.5.0
|
||||
*/
|
||||
public class CommonsXsdSchemaException extends XsdSchemaException {
|
||||
|
||||
public CommonsXsdSchemaException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public CommonsXsdSchemaException(String message, Throwable exception) {
|
||||
super(message, exception);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Contains a implementation of the <code>XsdSchema</code> interfaces that uses Apache WS-Commons XML Schema.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Provides an abstraction over XSD XML schemas. Contains the <code>XsdSchema</code> and related interfaces.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.dom;
|
||||
|
||||
import java.io.StringReader;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
|
||||
public class DomContentHandlerTest {
|
||||
|
||||
private static final String XML_1 = "<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" +
|
||||
"<root xmlns='namespace'>" +
|
||||
"<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>" +
|
||||
"</root>";
|
||||
|
||||
private static final String XML_2_EXPECTED = "<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" +
|
||||
"<child xmlns='namespace2' />" + "</root>";
|
||||
|
||||
private static final String XML_2_SNIPPET =
|
||||
"<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace2' />";
|
||||
|
||||
private Document expected;
|
||||
|
||||
private DomContentHandler handler;
|
||||
|
||||
private Document result;
|
||||
|
||||
private XMLReader xmlReader;
|
||||
|
||||
private DocumentBuilder documentBuilder;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
result = documentBuilder.newDocument();
|
||||
xmlReader = XMLReaderFactory.createXMLReader();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentHandlerDocumentNamespacePrefixes() throws Exception {
|
||||
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
handler = new DomContentHandler(result);
|
||||
expected = documentBuilder.parse(new InputSource(new StringReader(XML_1)));
|
||||
xmlReader.setContentHandler(handler);
|
||||
xmlReader.parse(new InputSource(new StringReader(XML_1)));
|
||||
assertXMLEqual("Invalid result", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentHandlerDocumentNoNamespacePrefixes() throws Exception {
|
||||
handler = new DomContentHandler(result);
|
||||
expected = documentBuilder.parse(new InputSource(new StringReader(XML_1)));
|
||||
xmlReader.setContentHandler(handler);
|
||||
xmlReader.parse(new InputSource(new StringReader(XML_1)));
|
||||
assertXMLEqual("Invalid result", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContentHandlerElement() throws Exception {
|
||||
Element rootElement = result.createElementNS("namespace", "root");
|
||||
result.appendChild(rootElement);
|
||||
handler = new DomContentHandler(rootElement);
|
||||
expected = documentBuilder.parse(new InputSource(new StringReader(XML_2_EXPECTED)));
|
||||
xmlReader.setContentHandler(handler);
|
||||
xmlReader.parse(new InputSource(new StringReader(XML_2_SNIPPET)));
|
||||
assertXMLEqual("Invalid result", expected, result);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class QNameEditorTest {
|
||||
|
||||
private QNameEditor editor;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
editor = new QNameEditor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamespaceLocalPartPrefix() throws Exception {
|
||||
QName qname = new QName("namespace", "localpart", "prefix");
|
||||
doTest(qname);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNamespaceLocalPart() throws Exception {
|
||||
QName qname = new QName("namespace", "localpart");
|
||||
doTest(qname);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalPart() throws Exception {
|
||||
QName qname = new QName("localpart");
|
||||
doTest(qname);
|
||||
}
|
||||
|
||||
private void doTest(QName qname) {
|
||||
editor.setValue(qname);
|
||||
String text = editor.getAsText();
|
||||
Assert.assertNotNull("getAsText returns null", text);
|
||||
editor.setAsText(text);
|
||||
QName result = (QName) editor.getValue();
|
||||
Assert.assertNotNull("getValue returns null", result);
|
||||
Assert.assertEquals("Parsed QName local part is not equal to original", qname.getLocalPart(), result.getLocalPart());
|
||||
Assert.assertEquals("Parsed QName prefix is not equal to original", qname.getPrefix(), result.getPrefix());
|
||||
Assert.assertEquals("Parsed QName namespace is not equal to original", qname.getNamespaceURI(),
|
||||
result.getNamespaceURI());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public class QNameUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testValidQNames() {
|
||||
Assert.assertTrue("Namespace QName not validated", QNameUtils.validateQName("{namespace}local"));
|
||||
Assert.assertTrue("No Namespace QName not validated", QNameUtils.validateQName("local"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidQNames() {
|
||||
Assert.assertFalse("Null QName validated", QNameUtils.validateQName(null));
|
||||
Assert.assertFalse("Empty QName validated", QNameUtils.validateQName(""));
|
||||
Assert.assertFalse("Invalid QName validated", QNameUtils.validateQName("{namespace}"));
|
||||
Assert.assertFalse("Invalid QName validated", QNameUtils.validateQName("{namespace"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetQNameForNodeNoNamespace() throws Exception {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element element = document.createElement("localname");
|
||||
QName qName = QNameUtils.getQNameForNode(element);
|
||||
Assert.assertNotNull("getQNameForNode returns null", qName);
|
||||
Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
Assert.assertFalse("Qname has invalid namespace", StringUtils.hasLength(qName.getNamespaceURI()));
|
||||
Assert.assertFalse("Qname has invalid prefix", StringUtils.hasLength(qName.getPrefix()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetQNameForNodeNoPrefix() throws Exception {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element element = document.createElementNS("namespace", "localname");
|
||||
QName qName = QNameUtils.getQNameForNode(element);
|
||||
Assert.assertNotNull("getQNameForNode returns null", qName);
|
||||
Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
Assert.assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
Assert.assertFalse("Qname has invalid prefix", StringUtils.hasLength(qName.getPrefix()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetQNameForNode() throws Exception {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element element = document.createElementNS("namespace", "prefix:localname");
|
||||
QName qName = QNameUtils.getQNameForNode(element);
|
||||
Assert.assertNotNull("getQNameForNode returns null", qName);
|
||||
Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
Assert.assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
Assert.assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToQualifiedNamePrefix() throws Exception {
|
||||
QName qName = new QName("namespace", "localName", "prefix");
|
||||
String result = QNameUtils.toQualifiedName(qName);
|
||||
Assert.assertEquals("Invalid result", "prefix:localName", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToQualifiedNameNoPrefix() throws Exception {
|
||||
QName qName = new QName("localName");
|
||||
String result = QNameUtils.toQualifiedName(qName);
|
||||
Assert.assertEquals("Invalid result", "localName", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToQNamePrefix() throws Exception {
|
||||
QName result = QNameUtils.toQName("namespace", "prefix:localName");
|
||||
Assert.assertEquals("invalid namespace", "namespace", result.getNamespaceURI());
|
||||
Assert.assertEquals("invalid prefix", "prefix", result.getPrefix());
|
||||
Assert.assertEquals("invalid localname", "localName", result.getLocalPart());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToQNameNoPrefix() throws Exception {
|
||||
QName result = QNameUtils.toQName("namespace", "localName");
|
||||
Assert.assertEquals("invalid namespace", "namespace", result.getNamespaceURI());
|
||||
Assert.assertEquals("invalid prefix", "", result.getPrefix());
|
||||
Assert.assertEquals("invalid localname", "localName", result.getLocalPart());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import javax.xml.XMLConstants;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SimpleNamespaceContextTest {
|
||||
|
||||
private SimpleNamespaceContext context;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
context = new SimpleNamespaceContext();
|
||||
context.bindNamespaceUri("prefix", "namespaceURI");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNamespaceURI() {
|
||||
Assert.assertEquals("Invalid namespaceURI for default namespace", "", context
|
||||
.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
|
||||
String defaultNamespaceUri = "defaultNamespace";
|
||||
context.bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, defaultNamespaceUri);
|
||||
Assert.assertEquals("Invalid namespaceURI for default namespace", defaultNamespaceUri, context
|
||||
.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
|
||||
Assert.assertEquals("Invalid namespaceURI for bound prefix", "namespaceURI", context.getNamespaceURI("prefix"));
|
||||
Assert.assertEquals("Invalid namespaceURI for unbound prefix", "", context.getNamespaceURI("unbound"));
|
||||
Assert.assertEquals("Invalid namespaceURI for namespace prefix", XMLConstants.XML_NS_URI, context
|
||||
.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
|
||||
Assert.assertEquals("Invalid namespaceURI for attribute prefix", XMLConstants.XMLNS_ATTRIBUTE_NS_URI, context
|
||||
.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPrefix() {
|
||||
context.bindDefaultNamespaceUri("defaultNamespaceURI");
|
||||
Assert.assertEquals("Invalid prefix for default namespace", XMLConstants.DEFAULT_NS_PREFIX, context.getPrefix("defaultNamespaceURI"));
|
||||
Assert.assertEquals("Invalid prefix for bound namespace", "prefix", context.getPrefix("namespaceURI"));
|
||||
Assert.assertNull("Invalid prefix for unbound namespace", context.getPrefix("unbound"));
|
||||
Assert.assertEquals("Invalid prefix for namespace", XMLConstants.XML_NS_PREFIX, context
|
||||
.getPrefix(XMLConstants.XML_NS_URI));
|
||||
Assert.assertEquals("Invalid prefix for attribute namespace", XMLConstants.XMLNS_ATTRIBUTE, context
|
||||
.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPrefixes() {
|
||||
context.bindDefaultNamespaceUri("defaultNamespaceURI");
|
||||
assertPrefixes("defaultNamespaceURI", XMLConstants.DEFAULT_NS_PREFIX);
|
||||
assertPrefixes("namespaceURI", "prefix");
|
||||
Assert.assertFalse("Invalid prefix for unbound namespace", context.getPrefixes("unbound").hasNext());
|
||||
assertPrefixes(XMLConstants.XML_NS_URI, XMLConstants.XML_NS_PREFIX);
|
||||
assertPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void unmodifiableGetPrefixes() {
|
||||
String namespaceUri = "namespaceUri";
|
||||
context.bindNamespaceUri("prefix1", namespaceUri);
|
||||
context.bindNamespaceUri("prefix2", namespaceUri);
|
||||
|
||||
Iterator<String> prefixes = context.getPrefixes(namespaceUri);
|
||||
prefixes.next();
|
||||
prefixes.remove();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiplePrefixes() {
|
||||
context.bindNamespaceUri("prefix1", "namespace");
|
||||
context.bindNamespaceUri("prefix2", "namespace");
|
||||
Iterator<String> iterator = context.getPrefixes("namespace");
|
||||
Assert.assertNotNull("getPrefixes returns null", iterator);
|
||||
Assert.assertTrue("iterator is empty", iterator.hasNext());
|
||||
String result = (String) iterator.next();
|
||||
Assert.assertTrue("Invalid prefix", result.equals("prefix1") || result.equals("prefix2"));
|
||||
Assert.assertTrue("iterator is empty", iterator.hasNext());
|
||||
result = (String) iterator.next();
|
||||
Assert.assertTrue("Invalid prefix", result.equals("prefix1") || result.equals("prefix2"));
|
||||
Assert.assertFalse("iterator contains more than two values", iterator.hasNext());
|
||||
}
|
||||
|
||||
private void assertPrefixes(String namespaceUri, String prefix) {
|
||||
Iterator<String> iterator = context.getPrefixes(namespaceUri);
|
||||
Assert.assertNotNull("getPrefixes returns null", iterator);
|
||||
Assert.assertTrue("iterator is empty", iterator.hasNext());
|
||||
String result = (String) iterator.next();
|
||||
Assert.assertEquals("Invalid prefix", prefix, result);
|
||||
Assert.assertFalse("iterator contains multiple values", iterator.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBoundPrefixes() throws Exception {
|
||||
Iterator<String> iterator = context.getBoundPrefixes();
|
||||
Assert.assertNotNull("getPrefixes returns null", iterator);
|
||||
Assert.assertTrue("iterator is empty", iterator.hasNext());
|
||||
String result = (String) iterator.next();
|
||||
Assert.assertEquals("Invalid prefix", "prefix", result);
|
||||
Assert.assertFalse("iterator contains multiple values", iterator.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBindings() throws Exception {
|
||||
context.setBindings(Collections.singletonMap("prefix", "namespace"));
|
||||
Assert.assertEquals("Invalid namespace uri", "namespace", context.getNamespaceURI("prefix"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveBinding() {
|
||||
context.clear();
|
||||
String prefix1 = "prefix1";
|
||||
String prefix2 = "prefix2";
|
||||
String namespaceUri = "namespaceUri";
|
||||
context.bindNamespaceUri(prefix1, namespaceUri);
|
||||
context.bindNamespaceUri(prefix2, namespaceUri);
|
||||
Iterator<String> iter = context.getPrefixes(namespaceUri);
|
||||
Assert.assertTrue("iterator is empty", iter.hasNext());
|
||||
Assert.assertEquals(prefix1, iter.next());
|
||||
Assert.assertTrue("iterator is empty", iter.hasNext());
|
||||
Assert.assertEquals(prefix2, iter.next());
|
||||
Assert.assertFalse("iterator not empty", iter.hasNext());
|
||||
|
||||
context.removeBinding(prefix1);
|
||||
|
||||
iter = context.getPrefixes(namespaceUri);
|
||||
Assert.assertTrue("iterator is empty", iter.hasNext());
|
||||
Assert.assertEquals(prefix2, iter.next());
|
||||
Assert.assertFalse("iterator not empty", iter.hasNext());
|
||||
|
||||
context.removeBinding(prefix2);
|
||||
|
||||
iter = context.getPrefixes(namespaceUri);
|
||||
Assert.assertFalse("iterator not empty", iter.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasBinding() {
|
||||
context.clear();
|
||||
String prefix = "prefix";
|
||||
Assert.assertFalse("Context has binding", context.hasBinding(prefix));
|
||||
String namespaceUri = "namespaceUri";
|
||||
context.bindNamespaceUri(prefix, namespaceUri);
|
||||
Assert.assertTrue("Context has no binding", context.hasBinding(prefix));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultNamespaceMultiplePrefixes() {
|
||||
String defaultNamespace = "http://springframework.org/spring-ws";
|
||||
context.bindDefaultNamespaceUri(defaultNamespace);
|
||||
context.bindNamespaceUri("prefix", defaultNamespace);
|
||||
Assert.assertEquals("Invalid prefix", XMLConstants.DEFAULT_NS_PREFIX, context.getPrefix(defaultNamespace));
|
||||
Iterator<String> iterator = context.getPrefixes(defaultNamespace);
|
||||
Assert.assertNotNull("getPrefixes returns null", iterator);
|
||||
Assert.assertTrue("iterator is empty", iterator.hasNext());
|
||||
String result = (String) iterator.next();
|
||||
Assert.assertTrue("Invalid prefix", result.equals(XMLConstants.DEFAULT_NS_PREFIX) || result.equals("prefix"));
|
||||
Assert.assertTrue("iterator is empty", iterator.hasNext());
|
||||
result = (String) iterator.next();
|
||||
Assert.assertTrue("Invalid prefix", result.equals(XMLConstants.DEFAULT_NS_PREFIX) || result.equals("prefix"));
|
||||
Assert.assertFalse("iterator contains more than two values", iterator.hasNext());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.sax;
|
||||
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SaxUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testGetSystemId() throws Exception {
|
||||
Resource resource = new FileSystemResource("/path with spaces/file with spaces.txt");
|
||||
String systemId = SaxUtils.getSystemId(resource);
|
||||
Assert.assertNotNull("No systemId returned", systemId);
|
||||
Assert.assertTrue("Invalid system id", systemId.endsWith("path%20with%20spaces/file%20with%20spaces.txt"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public class ResourceSourceTest {
|
||||
|
||||
@Test
|
||||
public void testStringSource() throws Exception {
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
DOMResult result = new DOMResult();
|
||||
ResourceSource source = new ResourceSource(new ClassPathResource("resourceSource.xml", getClass()));
|
||||
transformer.transform(source, result);
|
||||
Element rootElement = (Element) result.getNode().getFirstChild();
|
||||
Assert.assertEquals("Invalid local name", "content", rootElement.getLocalName());
|
||||
Assert.assertEquals("Invalid prefix", "prefix", rootElement.getPrefix());
|
||||
Assert.assertEquals("Invalid namespace", "namespace", rootElement.getNamespaceURI());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
|
||||
public class StringResultTest {
|
||||
|
||||
@Test
|
||||
public void testStringResult() throws Exception {
|
||||
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
Element element = document.createElementNS("namespace", "prefix:localName");
|
||||
document.appendChild(element);
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
StringResult result = new StringResult();
|
||||
transformer.transform(new DOMSource(document), result);
|
||||
assertXMLEqual("Invalid result", "<prefix:localName xmlns:prefix='namespace'/>", result.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
public class StringSourceTest {
|
||||
|
||||
@Test
|
||||
public void testStringSource() throws TransformerException {
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
String content = "<prefix:content xmlns:prefix='namespace'/>";
|
||||
DOMResult result = new DOMResult();
|
||||
transformer.transform(new StringSource(content), result);
|
||||
Element rootElement = (Element) result.getNode().getFirstChild();
|
||||
Assert.assertEquals("Invalid local name", "content", rootElement.getLocalName());
|
||||
Assert.assertEquals("Invalid prefix", "prefix", rootElement.getPrefix());
|
||||
Assert.assertEquals("Invalid namespace", "namespace", rootElement.getNamespaceURI());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
|
||||
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
|
||||
public class TransformerHelperTest {
|
||||
|
||||
private TransformerHelper helper;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
helper = new TransformerHelper();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultTransformerFactory() throws TransformerException, IOException, SAXException {
|
||||
doTest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customTransformerFactory() throws TransformerException, IOException, SAXException {
|
||||
helper.setTransformerFactoryClass(TransformerFactoryImpl.class);
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() throws TransformerException, SAXException, IOException {
|
||||
String xml = "<root xmlns='http://springframework.org/spring-ws'><child>text</child></root>";
|
||||
Source source = new StringSource(xml);
|
||||
Result result = new StringResult();
|
||||
|
||||
helper.transform(source, result);
|
||||
|
||||
assertXMLEqual(xml, result.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,326 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.transform;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.springframework.util.xml.StaxUtils;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.ext.DefaultHandler2;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
public class TraxUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testGetDocument() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.newDocument();
|
||||
Assert.assertSame("Invalid document", document, TraxUtils.getDocument(new DOMSource(document)));
|
||||
Element element = document.createElement("element");
|
||||
document.appendChild(element);
|
||||
Assert.assertSame("Invalid document", document, TraxUtils.getDocument(new DOMSource(element)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithDomSource() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.newDocument();
|
||||
|
||||
TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class);
|
||||
mock.domSource(document);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithSource(new DOMSource(document), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithDomResult() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.newDocument();
|
||||
|
||||
TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class);
|
||||
mock.domResult(document);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithResult(new DOMResult(document), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithSaxSource() throws Exception {
|
||||
XMLReader reader = XMLReaderFactory.createXMLReader();
|
||||
InputSource inputSource = new InputSource();
|
||||
|
||||
TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class);
|
||||
mock.saxSource(reader, inputSource);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithSource(new SAXSource(reader, inputSource), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithSaxResult() throws Exception {
|
||||
ContentHandler contentHandler = new DefaultHandler();
|
||||
LexicalHandler lexicalHandler = new DefaultHandler2();
|
||||
|
||||
TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class);
|
||||
mock.saxResult(contentHandler, lexicalHandler);
|
||||
|
||||
replay(mock);
|
||||
|
||||
SAXResult result = new SAXResult(contentHandler);
|
||||
result.setLexicalHandler(lexicalHandler);
|
||||
TraxUtils.doWithResult(result, mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithStaxSourceEventReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader("<element/>"));
|
||||
|
||||
TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class);
|
||||
mock.staxSource(eventReader);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithSource(StaxUtils.createStaxSource(eventReader), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithStaxResultEventWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter());
|
||||
|
||||
TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class);
|
||||
mock.staxResult(eventWriter);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithResult(StaxUtils.createStaxResult(eventWriter), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithStaxSourceStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader("<element/>"));
|
||||
|
||||
TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class);
|
||||
mock.staxSource(streamReader);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithSource(StaxUtils.createStaxSource(streamReader), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithStaxResultStreamWriter() throws Exception {
|
||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
|
||||
|
||||
TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class);
|
||||
mock.staxResult(streamWriter);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithResult(StaxUtils.createStaxResult(streamWriter), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithStreamSourceInputStream() throws Exception {
|
||||
byte[] xml = "<element/>".getBytes("UTF-8");
|
||||
InputStream inputStream = new ByteArrayInputStream(xml);
|
||||
|
||||
TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class);
|
||||
mock.streamSource(inputStream);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithSource(new StreamSource(inputStream), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithStreamResultOutputStream() throws Exception {
|
||||
OutputStream outputStream = new ByteArrayOutputStream();
|
||||
|
||||
TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class);
|
||||
mock.streamResult(outputStream);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithResult(new StreamResult(outputStream), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithStreamSourceReader() throws Exception {
|
||||
String xml = "<element/>";
|
||||
Reader reader = new StringReader(xml);
|
||||
|
||||
TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class);
|
||||
mock.streamSource(reader);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithSource(new StreamSource(reader), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithStreamResultWriter() throws Exception {
|
||||
Writer writer = new StringWriter();
|
||||
|
||||
TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class);
|
||||
mock.streamResult(writer);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithResult(new StreamResult(writer), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithSystemIdSource() throws Exception {
|
||||
String systemId = "http://www.springframework.org/dtd/spring-beans.dtd";
|
||||
|
||||
TraxUtils.SourceCallback mock = createMock(TraxUtils.SourceCallback.class);
|
||||
mock.source(systemId);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithSource(new StreamSource(systemId), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithSystemIdResult() throws Exception {
|
||||
String systemId = "http://www.springframework.org/dtd/spring-beans.dtd";
|
||||
|
||||
TraxUtils.ResultCallback mock = createMock(TraxUtils.ResultCallback.class);
|
||||
mock.result(systemId);
|
||||
|
||||
replay(mock);
|
||||
|
||||
TraxUtils.doWithResult(new StreamResult(systemId), mock);
|
||||
|
||||
verify(mock);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDoWithInvalidSource() throws Exception {
|
||||
Source source = new Source() {
|
||||
|
||||
public void setSystemId(String systemId) {
|
||||
}
|
||||
|
||||
public String getSystemId() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
TraxUtils.doWithSource(source, null);
|
||||
Assert.fail("IllegalArgumentException expected");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoWithInvalidResult() throws Exception {
|
||||
Result result = new Result() {
|
||||
|
||||
public void setSystemId(String systemId) {
|
||||
}
|
||||
|
||||
public String getSystemId() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
TraxUtils.doWithResult(result, null);
|
||||
Assert.fail("IllegalArgumentException expected");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright 2005-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import java.io.InputStream;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.xml.transform.ResourceSource;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
public abstract class AbstractValidatorFactoryTestCase {
|
||||
|
||||
private XmlValidator validator;
|
||||
|
||||
private InputStream validInputStream;
|
||||
|
||||
private InputStream invalidInputStream;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Resource[] schemaResource =
|
||||
new Resource[]{new ClassPathResource("schema.xsd", AbstractValidatorFactoryTestCase.class)};
|
||||
validator = createValidator(schemaResource, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
validInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("validDocument.xml");
|
||||
invalidInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("invalidDocument.xml");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
validInputStream.close();
|
||||
invalidInputStream.close();
|
||||
}
|
||||
|
||||
protected abstract XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws Exception;
|
||||
|
||||
@Test
|
||||
public void testHandleValidMessageStream() throws Exception {
|
||||
SAXParseException[] errors = validator.validate(new StreamSource(validInputStream));
|
||||
Assert.assertNotNull("Null returned for errors", errors);
|
||||
Assert.assertEquals("ValidationErrors returned", 0, errors.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateTwice() throws Exception {
|
||||
validator.validate(new StreamSource(validInputStream));
|
||||
validInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("validDocument.xml");
|
||||
validator.validate(new StreamSource(validInputStream));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleInvalidMessageStream() throws Exception {
|
||||
SAXParseException[] errors = validator.validate(new StreamSource(invalidInputStream));
|
||||
Assert.assertNotNull("Null returned for errors", errors);
|
||||
Assert.assertEquals("ValidationErrors returned", 3, errors.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleValidMessageSax() throws Exception {
|
||||
SAXParseException[] errors = validator.validate(new SAXSource(new InputSource(validInputStream)));
|
||||
Assert.assertNotNull("Null returned for errors", errors);
|
||||
Assert.assertEquals("ValidationErrors returned", 0, errors.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleInvalidMessageSax() throws Exception {
|
||||
SAXParseException[] errors = validator.validate(new SAXSource(new InputSource(invalidInputStream)));
|
||||
Assert.assertNotNull("Null returned for errors", errors);
|
||||
Assert.assertEquals("ValidationErrors returned", 3, errors.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleValidMessageDom() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
Document document = documentBuilderFactory.newDocumentBuilder()
|
||||
.parse(new InputSource(validInputStream));
|
||||
SAXParseException[] errors = validator.validate(new DOMSource(document));
|
||||
Assert.assertNotNull("Null returned for errors", errors);
|
||||
Assert.assertEquals("ValidationErrors returned", 0, errors.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandleInvalidMessageDom() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
Document document = documentBuilderFactory.newDocumentBuilder()
|
||||
.parse(new InputSource(invalidInputStream));
|
||||
SAXParseException[] errors = validator.validate(new DOMSource(document));
|
||||
Assert.assertNotNull("Null returned for errors", errors);
|
||||
Assert.assertEquals("ValidationErrors returned", 3, errors.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleSchemasValidMessage() throws Exception {
|
||||
Resource[] schemaResources = new Resource[]{
|
||||
new ClassPathResource("multipleSchemas1.xsd", AbstractValidatorFactoryTestCase.class),
|
||||
new ClassPathResource("multipleSchemas2.xsd", AbstractValidatorFactoryTestCase.class)};
|
||||
validator = createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
|
||||
Source document = new ResourceSource(
|
||||
new ClassPathResource("multipleSchemas1.xml", AbstractValidatorFactoryTestCase.class));
|
||||
SAXParseException[] errors = validator.validate(document);
|
||||
Assert.assertEquals("ValidationErrors returned", 0, errors.length);
|
||||
validator = createValidator(schemaResources, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
document = new ResourceSource(
|
||||
new ClassPathResource("multipleSchemas2.xml", AbstractValidatorFactoryTestCase.class));
|
||||
errors = validator.validate(document);
|
||||
Assert.assertEquals("ValidationErrors returned", 0, errors.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customErrorHandler() throws Exception {
|
||||
ValidationErrorHandler myHandler = new ValidationErrorHandler() {
|
||||
public SAXParseException[] getErrors() {
|
||||
return new SAXParseException[0];
|
||||
}
|
||||
|
||||
public void warning(SAXParseException exception) throws SAXException {
|
||||
}
|
||||
|
||||
public void error(SAXParseException exception) throws SAXException {
|
||||
}
|
||||
|
||||
public void fatalError(SAXParseException exception) throws SAXException {
|
||||
}
|
||||
};
|
||||
SAXParseException[] errors = validator.validate(new StreamSource(invalidInputStream), myHandler);
|
||||
Assert.assertNotNull("Null returned for errors", errors);
|
||||
Assert.assertEquals("ValidationErrors returned", 0, errors.length);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
public class Jaxp13ValidatorFactoryTest extends AbstractValidatorFactoryTestCase {
|
||||
|
||||
@Override
|
||||
protected XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException {
|
||||
return Jaxp13ValidatorFactory.createValidator(schemaResources, schemaLanguage);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.validation.Schema;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SchemaLoaderUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testLoadSchema() throws Exception {
|
||||
Resource resource = new ClassPathResource("schema.xsd", getClass());
|
||||
Schema schema = SchemaLoaderUtils.loadSchema(resource, XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Assert.assertNotNull("No schema returned", schema);
|
||||
Assert.assertFalse("Resource not closed", resource.isOpen());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadNonExistantSchema() throws Exception {
|
||||
try {
|
||||
Resource nonExistant = new ClassPathResource("bla");
|
||||
SchemaLoaderUtils.loadSchema(nonExistant, XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Assert.fail("Should have thrown an IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadNullSchema() throws Exception {
|
||||
try {
|
||||
SchemaLoaderUtils.loadSchema((Resource) null, XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Assert.fail("Should have thrown an IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadMultipleSchemas() throws Exception {
|
||||
Resource envelope = new ClassPathResource("envelope.xsd", getClass());
|
||||
Resource encoding = new ClassPathResource("encoding.xsd", getClass());
|
||||
Schema schema =
|
||||
SchemaLoaderUtils.loadSchema(new Resource[]{envelope, encoding}, XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
||||
Assert.assertNotNull("No schema returned", schema);
|
||||
Assert.assertFalse("Resource not closed", envelope.isOpen());
|
||||
Assert.assertFalse("Resource not closed", encoding.isOpen());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.validation;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
|
||||
import org.springframework.core.io.AbstractResource;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class XmlValidatorFactoryTest {
|
||||
|
||||
@Test
|
||||
public void testCreateValidator() throws Exception {
|
||||
Resource resource = new ClassPathResource("schema.xsd", AbstractValidatorFactoryTestCase.class);
|
||||
XmlValidator validator = XmlValidatorFactory.createValidator(resource, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
Assert.assertNotNull("No validator returned", validator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonExistentResource() throws Exception {
|
||||
Resource resource = new NonExistentResource();
|
||||
try {
|
||||
XmlValidatorFactory.createValidator(resource, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
Assert.fail("IllegalArgumentException expected");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidSchemaLanguage() throws Exception {
|
||||
Resource resource = new ClassPathResource("schema.xsd", AbstractValidatorFactoryTestCase.class);
|
||||
try {
|
||||
XmlValidatorFactory.createValidator(resource, "bla");
|
||||
Assert.fail("IllegalArgumentException expected");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
private static class NonExistentResource extends AbstractResource {
|
||||
|
||||
@Override
|
||||
public Resource createRelative(String relativePath) throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getFile() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getURL() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReadable() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public abstract class AbstractXPathExpressionFactoryTestCase {
|
||||
|
||||
private Document noNamespacesDocument;
|
||||
|
||||
private Document namespacesDocument;
|
||||
|
||||
private Map<String, String> namespaces = new HashMap<String, String>();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
namespaces.put("prefix1", "namespace1");
|
||||
namespaces.put("prefix2", "namespace2");
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
InputStream inputStream = getClass().getResourceAsStream("nonamespaces.xml");
|
||||
try {
|
||||
noNamespacesDocument = documentBuilder.parse(inputStream);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
inputStream = getClass().getResourceAsStream("namespaces.xml");
|
||||
try {
|
||||
namespacesDocument = documentBuilder.parse(inputStream);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsBooleanInvalidNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:otherchild", namespaces);
|
||||
boolean result = expression.evaluateAsBoolean(namespacesDocument);
|
||||
Assert.assertFalse("Invalid result [" + result + "]", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsBooleanInvalidNoNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/root/otherchild");
|
||||
boolean result = expression.evaluateAsBoolean(noNamespacesDocument);
|
||||
Assert.assertFalse("Invalid result [" + result + "]", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsBooleanNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression =
|
||||
createXPathExpression("/prefix1:root/prefix2:child/prefix2:boolean/text()", namespaces);
|
||||
boolean result = expression.evaluateAsBoolean(namespacesDocument);
|
||||
Assert.assertTrue("Invalid result", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsBooleanNoNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/root/child/boolean/text()");
|
||||
boolean result = expression.evaluateAsBoolean(noNamespacesDocument);
|
||||
Assert.assertTrue("Invalid result", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsDoubleInvalidNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:otherchild", namespaces);
|
||||
double result = expression.evaluateAsNumber(noNamespacesDocument);
|
||||
Assert.assertTrue("Invalid result [" + result + "]", Double.isNaN(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsDoubleInvalidNoNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/root/otherchild");
|
||||
double result = expression.evaluateAsNumber(noNamespacesDocument);
|
||||
Assert.assertTrue("Invalid result [" + result + "]", Double.isNaN(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsDoubleNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression =
|
||||
createXPathExpression("/prefix1:root/prefix2:child/prefix2:number/text()", namespaces);
|
||||
double result = expression.evaluateAsNumber(namespacesDocument);
|
||||
Assert.assertEquals("Invalid result", 42D, result, 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsDoubleNoNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/root/child/number/text()");
|
||||
double result = expression.evaluateAsNumber(noNamespacesDocument);
|
||||
Assert.assertEquals("Invalid result", 42D, result, 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsNodeInvalidNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:otherchild", namespaces);
|
||||
Node result = expression.evaluateAsNode(namespacesDocument);
|
||||
Assert.assertNull("Invalid result [" + result + "]", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsNodeInvalidNoNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/root/otherchild");
|
||||
Node result = expression.evaluateAsNode(noNamespacesDocument);
|
||||
Assert.assertNull("Invalid result [" + result + "]", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsNodeNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:child", namespaces);
|
||||
Node result = expression.evaluateAsNode(namespacesDocument);
|
||||
Assert.assertNotNull("Invalid result", result);
|
||||
Assert.assertEquals("Invalid localname", "child", result.getLocalName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsNodeNoNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/root/child");
|
||||
Node result = expression.evaluateAsNode(noNamespacesDocument);
|
||||
Assert.assertNotNull("Invalid result", result);
|
||||
Assert.assertEquals("Invalid localname", "child", result.getLocalName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsNodeListNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:child/*", namespaces);
|
||||
List<Node> results = expression.evaluateAsNodeList(namespacesDocument);
|
||||
Assert.assertNotNull("Invalid result", results);
|
||||
Assert.assertEquals("Invalid amount of results", 3, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsNodeListNoNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/root/child/*");
|
||||
List<Node> results = expression.evaluateAsNodeList(noNamespacesDocument);
|
||||
Assert.assertNotNull("Invalid result", results);
|
||||
Assert.assertEquals("Invalid amount of results", 3, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsStringInvalidNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:otherchild", namespaces);
|
||||
String result = expression.evaluateAsString(namespacesDocument);
|
||||
Assert.assertFalse("Invalid result [" + result + "]", StringUtils.hasText(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsStringInvalidNoNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/root/otherchild");
|
||||
String result = expression.evaluateAsString(noNamespacesDocument);
|
||||
Assert.assertFalse("Invalid result [" + result + "]", StringUtils.hasText(result));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsStringNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression =
|
||||
createXPathExpression("/prefix1:root/prefix2:child/prefix2:text/text()", namespaces);
|
||||
String result = expression.evaluateAsString(namespacesDocument);
|
||||
Assert.assertEquals("Invalid result", "text", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsStringNoNamespaces() throws IOException, SAXException {
|
||||
XPathExpression expression = createXPathExpression("/root/child/text/text()");
|
||||
String result = expression.evaluateAsString(noNamespacesDocument);
|
||||
Assert.assertEquals("Invalid result", "text", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsObject() throws Exception {
|
||||
XPathExpression expression = createXPathExpression("/root/child");
|
||||
String result = expression.evaluateAsObject(noNamespacesDocument, new NodeMapper<String>() {
|
||||
public String mapNode(Node node, int nodeNum) throws DOMException {
|
||||
return node.getLocalName();
|
||||
}
|
||||
});
|
||||
Assert.assertNotNull("Invalid result", result);
|
||||
Assert.assertEquals("Invalid localname", "child", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluate() throws Exception {
|
||||
XPathExpression expression = createXPathExpression("/root/child/*");
|
||||
List<String> results = expression.evaluate(noNamespacesDocument, new NodeMapper<String>() {
|
||||
public String mapNode(Node node, int nodeNum) throws DOMException {
|
||||
return node.getLocalName();
|
||||
}
|
||||
});
|
||||
Assert.assertNotNull("Invalid result", results);
|
||||
Assert.assertEquals("Invalid amount of results", 3, results.size());
|
||||
Assert.assertEquals("Invalid first result", "text", results.get(0));
|
||||
Assert.assertEquals("Invalid first result", "number", results.get(1));
|
||||
Assert.assertEquals("Invalid first result", "boolean", results.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidExpression() {
|
||||
try {
|
||||
createXPathExpression("\\");
|
||||
Assert.fail("No XPathParseException thrown");
|
||||
}
|
||||
catch (XPathParseException ex) {
|
||||
// Expected behaviour
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract XPathExpression createXPathExpression(String expression);
|
||||
|
||||
protected abstract XPathExpression createXPathExpression(String expression, Map<String, String> namespaces);
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stax.StAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
import org.springframework.xml.transform.ResourceSource;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public abstract class AbstractXPathTemplateTestCase {
|
||||
|
||||
XPathOperations template;
|
||||
|
||||
private Source namespaces;
|
||||
|
||||
private Source nonamespaces;
|
||||
|
||||
@Before
|
||||
public final void setUp() throws Exception {
|
||||
template = createTemplate();
|
||||
namespaces = new ResourceSource(new ClassPathResource("namespaces.xml", AbstractXPathTemplateTestCase.class));
|
||||
nonamespaces =
|
||||
new ResourceSource(new ClassPathResource("nonamespaces.xml", AbstractXPathTemplateTestCase.class));
|
||||
}
|
||||
|
||||
protected abstract XPathOperations createTemplate() throws Exception;
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsBoolean() {
|
||||
boolean result = template.evaluateAsBoolean("/root/child/boolean", nonamespaces);
|
||||
Assert.assertTrue("Invalid result", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsBooleanNamespaces() {
|
||||
boolean result = template.evaluateAsBoolean("/prefix1:root/prefix2:child/prefix2:boolean", namespaces);
|
||||
Assert.assertTrue("Invalid result", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsDouble() {
|
||||
double result = template.evaluateAsDouble("/root/child/number", nonamespaces);
|
||||
Assert.assertEquals("Invalid result", 42D, result, 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsDoubleNamespaces() {
|
||||
double result = template.evaluateAsDouble("/prefix1:root/prefix2:child/prefix2:number", namespaces);
|
||||
Assert.assertEquals("Invalid result", 42D, result, 0D);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsNode() {
|
||||
Node result = template.evaluateAsNode("/root/child", nonamespaces);
|
||||
Assert.assertNotNull("Invalid result", result);
|
||||
Assert.assertEquals("Invalid localname", "child", result.getLocalName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsNodeNamespaces() {
|
||||
Node result = template.evaluateAsNode("/prefix1:root/prefix2:child", namespaces);
|
||||
Assert.assertNotNull("Invalid result", result);
|
||||
Assert.assertEquals("Invalid localname", "child", result.getLocalName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsNodes() {
|
||||
List<Node> results = template.evaluateAsNodeList("/root/child/*", nonamespaces);
|
||||
Assert.assertNotNull("Invalid result", results);
|
||||
Assert.assertEquals("Invalid amount of results", 3, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsNodesNamespaces() {
|
||||
List<Node> results = template.evaluateAsNodeList("/prefix1:root/prefix2:child/*", namespaces);
|
||||
Assert.assertNotNull("Invalid result", results);
|
||||
Assert.assertEquals("Invalid amount of results", 3, results.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsStringNamespaces() throws IOException, SAXException {
|
||||
String result = template.evaluateAsString("/prefix1:root/prefix2:child/prefix2:text", namespaces);
|
||||
Assert.assertEquals("Invalid result", "text", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsString() throws IOException, SAXException {
|
||||
String result = template.evaluateAsString("/root/child/text", nonamespaces);
|
||||
Assert.assertEquals("Invalid result", "text", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateDomSource() throws IOException, SAXException, ParserConfigurationException {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.parse(SaxUtils.createInputSource(
|
||||
new ClassPathResource("nonamespaces.xml", AbstractXPathTemplateTestCase.class)));
|
||||
|
||||
String result = template.evaluateAsString("/root/child/text", new DOMSource(document));
|
||||
Assert.assertEquals("Invalid result", "text", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateSAXSource() throws Exception {
|
||||
InputStream in = AbstractXPathTemplateTestCase.class.getResourceAsStream("nonamespaces.xml");
|
||||
SAXSource source = new SAXSource(new InputSource(in));
|
||||
String result = template.evaluateAsString("/root/child/text", source);
|
||||
Assert.assertEquals("Invalid result", "text", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateStaxSource() throws Exception {
|
||||
InputStream in = AbstractXPathTemplateTestCase.class.getResourceAsStream("nonamespaces.xml");
|
||||
XMLStreamReader streamReader = XMLInputFactory.newFactory().createXMLStreamReader(in);
|
||||
StAXSource source = new StAXSource(streamReader);
|
||||
String result = template.evaluateAsString("/root/child/text", source);
|
||||
Assert.assertEquals("Invalid result", "text", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateStreamSourceInputStream() throws IOException, SAXException, ParserConfigurationException {
|
||||
InputStream in = AbstractXPathTemplateTestCase.class.getResourceAsStream("nonamespaces.xml");
|
||||
StreamSource source = new StreamSource(in);
|
||||
String result = template.evaluateAsString("/root/child/text", source);
|
||||
Assert.assertEquals("Invalid result", "text", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateStreamSourceSystemId() throws IOException, SAXException, ParserConfigurationException {
|
||||
URL url = AbstractXPathTemplateTestCase.class.getResource("nonamespaces.xml");
|
||||
String result = template.evaluateAsString("/root/child/text", new StreamSource(url.toString()));
|
||||
Assert.assertEquals("Invalid result", "text", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidExpression() {
|
||||
try {
|
||||
template.evaluateAsBoolean("\\", namespaces);
|
||||
Assert.fail("No XPathException thrown");
|
||||
}
|
||||
catch (XPathException ex) {
|
||||
// Expected behaviour
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateAsObject() throws Exception {
|
||||
String result = template.evaluateAsObject("/root/child", nonamespaces, new NodeMapper<String>() {
|
||||
public String mapNode(Node node, int nodeNum) throws DOMException {
|
||||
return node.getLocalName();
|
||||
}
|
||||
});
|
||||
Assert.assertNotNull("Invalid result", result);
|
||||
Assert.assertEquals("Invalid localname", "child", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluate() throws Exception {
|
||||
List<String> results = template.evaluate("/root/child/*", nonamespaces, new NodeMapper<String>() {
|
||||
public String mapNode(Node node, int nodeNum) throws DOMException {
|
||||
return node.getLocalName();
|
||||
}
|
||||
});
|
||||
Assert.assertNotNull("Invalid result", results);
|
||||
Assert.assertEquals("Invalid amount of results", 3, results.size());
|
||||
Assert.assertEquals("Invalid first result", "text", results.get(0));
|
||||
Assert.assertEquals("Invalid first result", "number", results.get(1));
|
||||
Assert.assertEquals("Invalid first result", "boolean", results.get(2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
public class JaxenXPathExpressionFactoryTest extends AbstractXPathExpressionFactoryTestCase {
|
||||
|
||||
@Override
|
||||
protected XPathExpression createXPathExpression(String expression) {
|
||||
return JaxenXPathExpressionFactory.createXPathExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected XPathExpression createXPathExpression(String expression, Map<String, String> namespaces) {
|
||||
return JaxenXPathExpressionFactory.createXPathExpression(expression, namespaces);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testEvaluateAsDoubleNoNamespaces() throws IOException, SAXException {
|
||||
// Currently not working on Jaxen 1.1 beta 8, hence the override here
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testEvaluateAsDoubleNamespaces() throws IOException, SAXException {
|
||||
// Currently not working on Jaxen 1.1 beta 8, hence the override here
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JaxenXPathTemplateTest extends AbstractXPathTemplateTestCase {
|
||||
|
||||
@Override
|
||||
protected XPathOperations createTemplate() throws Exception {
|
||||
JaxenXPathTemplate template = new JaxenXPathTemplate();
|
||||
Map<String, String> namespaces = new HashMap<String, String>();
|
||||
namespaces.put("prefix1", "namespace1");
|
||||
namespaces.put("prefix2", "namespace2");
|
||||
template.setNamespaces(namespaces);
|
||||
return template;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class Jaxp13XPathExpressionFactoryTest extends AbstractXPathExpressionFactoryTestCase {
|
||||
|
||||
@Override
|
||||
protected XPathExpression createXPathExpression(String expression) {
|
||||
return Jaxp13XPathExpressionFactory.createXPathExpression(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected XPathExpression createXPathExpression(String expression, Map<String, String> namespaces) {
|
||||
return Jaxp13XPathExpressionFactory.createXPathExpression(expression, namespaces);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Jaxp13XPathTemplateTest extends AbstractXPathTemplateTestCase {
|
||||
|
||||
@Override
|
||||
protected XPathOperations createTemplate() throws Exception {
|
||||
Jaxp13XPathTemplate template = new Jaxp13XPathTemplate();
|
||||
Map<String, String> namespaces = new HashMap<String, String>();
|
||||
namespaces.put("prefix1", "namespace1");
|
||||
namespaces.put("prefix2", "namespace2");
|
||||
template.setNamespaces(namespaces);
|
||||
return template;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class XPathExpressionFactoryBeanTest {
|
||||
|
||||
private XPathExpressionFactoryBean factoryBean;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
factoryBean = new XPathExpressionFactoryBean();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFactoryBean() throws Exception {
|
||||
factoryBean.setExpression("/root");
|
||||
factoryBean.afterPropertiesSet();
|
||||
Object result = factoryBean.getObject();
|
||||
Assert.assertNotNull("No result obtained", result);
|
||||
Assert.assertTrue("No XPathExpression returned", result instanceof XPathExpression);
|
||||
Assert.assertTrue("Not a singleton", factoryBean.isSingleton());
|
||||
Assert.assertEquals("Not a XPathExpresison", XPathExpression.class, factoryBean.getObjectType());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xpath;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class XPathExpressionFactoryTest {
|
||||
|
||||
@Test
|
||||
public void testCreateXPathExpression() throws Exception {
|
||||
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/root");
|
||||
Assert.assertNotNull("No expression returned", expression);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateEmptyXPathExpression() throws Exception {
|
||||
try {
|
||||
XPathExpressionFactory.createXPathExpression("");
|
||||
Assert.fail("Should have thrown an Exception");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
|
||||
public abstract class AbstractXsdSchemaTestCase {
|
||||
|
||||
private DocumentBuilder documentBuilder;
|
||||
|
||||
protected Transformer transformer;
|
||||
|
||||
@Before
|
||||
public final void setUp() throws Exception {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
transformer = transformerFactory.newTransformer();
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingle() throws Exception {
|
||||
Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
|
||||
XsdSchema single = createSchema(resource);
|
||||
String namespace = "http://www.springframework.org/spring-ws/single/schema";
|
||||
Assert.assertEquals("Invalid target namespace", namespace, single.getTargetNamespace());
|
||||
resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
|
||||
Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(single.getSource(), domResult);
|
||||
Document result = (Document) domResult.getNode();
|
||||
assertXMLEqual("Invalid Source returned", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncludes() throws Exception {
|
||||
Resource resource = new ClassPathResource("including.xsd", AbstractXsdSchemaTestCase.class);
|
||||
XsdSchema including = createSchema(resource);
|
||||
String namespace = "http://www.springframework.org/spring-ws/include/schema";
|
||||
Assert.assertEquals("Invalid target namespace", namespace, including.getTargetNamespace());
|
||||
resource = new ClassPathResource("including.xsd", AbstractXsdSchemaTestCase.class);
|
||||
Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(including.getSource(), domResult);
|
||||
Document result = (Document) domResult.getNode();
|
||||
assertXMLEqual("Invalid Source returned", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImports() throws Exception {
|
||||
Resource resource = new ClassPathResource("importing.xsd", AbstractXsdSchemaTestCase.class);
|
||||
XsdSchema importing = createSchema(resource);
|
||||
String namespace = "http://www.springframework.org/spring-ws/importing/schema";
|
||||
Assert.assertEquals("Invalid target namespace", namespace, importing.getTargetNamespace());
|
||||
resource = new ClassPathResource("importing.xsd", AbstractXsdSchemaTestCase.class);
|
||||
Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(importing.getSource(), domResult);
|
||||
Document result = (Document) domResult.getNode();
|
||||
assertXMLEqual("Invalid Source returned", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXmlNamespace() throws Exception {
|
||||
Resource resource = new ClassPathResource("xmlNamespace.xsd", AbstractXsdSchemaTestCase.class);
|
||||
XsdSchema importing = createSchema(resource);
|
||||
String namespace = "http://www.springframework.org/spring-ws/xmlNamespace";
|
||||
Assert.assertEquals("Invalid target namespace", namespace, importing.getTargetNamespace());
|
||||
resource = new ClassPathResource("xmlNamespace.xsd", AbstractXsdSchemaTestCase.class);
|
||||
Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(importing.getSource(), domResult);
|
||||
Document result = (Document) domResult.getNode();
|
||||
assertXMLEqual("Invalid Source returned", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateValidator() throws Exception {
|
||||
Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
|
||||
XsdSchema single = createSchema(resource);
|
||||
XmlValidator validator = single.createValidator();
|
||||
Assert.assertNotNull("No XmlValidator returned", validator);
|
||||
}
|
||||
|
||||
protected abstract XsdSchema createSchema(Resource resource) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
public class SimpleXsdSchemaTest extends AbstractXsdSchemaTestCase {
|
||||
|
||||
@Override
|
||||
protected XsdSchema createSchema(Resource resource) throws Exception {
|
||||
SimpleXsdSchema schema = new SimpleXsdSchema(resource);
|
||||
schema.afterPropertiesSet();
|
||||
return schema;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd.commons;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
import org.springframework.xml.xsd.AbstractXsdSchemaTestCase;
|
||||
import org.springframework.xml.xsd.XsdSchema;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||
|
||||
public class CommonsXsdSchemaCollectionTest {
|
||||
|
||||
private CommonsXsdSchemaCollection collection;
|
||||
|
||||
private Transformer transformer;
|
||||
|
||||
private DocumentBuilder documentBuilder;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
collection = new CommonsXsdSchemaCollection();
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
transformer = transformerFactory.newTransformer();
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setNamespaceAware(true);
|
||||
documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingle() throws Exception {
|
||||
Resource resource = new ClassPathResource("single.xsd", AbstractXsdSchemaTestCase.class);
|
||||
collection.setXsds(new Resource[]{resource});
|
||||
collection.afterPropertiesSet();
|
||||
Assert.assertEquals("Invalid amount of XSDs loaded", 1, collection.getXsdSchemas().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineComplex() throws Exception {
|
||||
Resource a = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class);
|
||||
collection.setXsds(new Resource[]{a});
|
||||
collection.setInline(true);
|
||||
collection.afterPropertiesSet();
|
||||
XsdSchema[] schemas = collection.getXsdSchemas();
|
||||
Assert.assertEquals("Invalid amount of XSDs loaded", 2, schemas.length);
|
||||
|
||||
Assert.assertEquals("Invalid target namespace", "urn:1", schemas[0].getTargetNamespace());
|
||||
Resource abc = new ClassPathResource("ABC.xsd", AbstractXsdSchemaTestCase.class);
|
||||
Document expected = documentBuilder.parse(SaxUtils.createInputSource(abc));
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(schemas[0].getSource(), domResult);
|
||||
assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
|
||||
|
||||
Assert.assertEquals("Invalid target namespace", "urn:2", schemas[1].getTargetNamespace());
|
||||
Resource cd = new ClassPathResource("CD.xsd", AbstractXsdSchemaTestCase.class);
|
||||
expected = documentBuilder.parse(SaxUtils.createInputSource(cd));
|
||||
domResult = new DOMResult();
|
||||
transformer.transform(schemas[1].getSource(), domResult);
|
||||
assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCircular() throws Exception {
|
||||
Resource resource = new ClassPathResource("circular-1.xsd", AbstractXsdSchemaTestCase.class);
|
||||
collection.setXsds(new Resource[]{resource});
|
||||
collection.setInline(true);
|
||||
collection.afterPropertiesSet();
|
||||
XsdSchema[] schemas = collection.getXsdSchemas();
|
||||
Assert.assertEquals("Invalid amount of XSDs loaded", 1, schemas.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXmlNamespace() throws Exception {
|
||||
Resource resource = new ClassPathResource("xmlNamespace.xsd", AbstractXsdSchemaTestCase.class);
|
||||
collection.setXsds(new Resource[]{resource});
|
||||
collection.setInline(true);
|
||||
collection.afterPropertiesSet();
|
||||
XsdSchema[] schemas = collection.getXsdSchemas();
|
||||
Assert.assertEquals("Invalid amount of XSDs loaded", 1, schemas.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateValidator() throws Exception {
|
||||
Resource a = new ClassPathResource("A.xsd", AbstractXsdSchemaTestCase.class);
|
||||
collection.setXsds(new Resource[]{a});
|
||||
collection.setInline(true);
|
||||
collection.afterPropertiesSet();
|
||||
|
||||
XmlValidator validator = collection.createValidator();
|
||||
Assert.assertNotNull("No XmlValidator returned", validator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidSchema() throws Exception {
|
||||
Resource invalid = new ClassPathResource("invalid.xsd", AbstractXsdSchemaTestCase.class);
|
||||
collection.setXsds(new Resource[]{invalid});
|
||||
try {
|
||||
collection.afterPropertiesSet();
|
||||
Assert.fail("CommonsXsdSchemaException expected");
|
||||
}
|
||||
catch (CommonsXsdSchemaException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIncludesAndImports() throws Exception {
|
||||
Resource hr = new ClassPathResource("hr.xsd", getClass());
|
||||
collection.setXsds(new Resource[]{hr});
|
||||
collection.setInline(true);
|
||||
collection.afterPropertiesSet();
|
||||
|
||||
XsdSchema[] schemas = collection.getXsdSchemas();
|
||||
Assert.assertEquals("Invalid amount of XSDs loaded", 2, schemas.length);
|
||||
|
||||
Assert.assertEquals("Invalid target namespace", "http://mycompany.com/hr/schemas", schemas[0].getTargetNamespace());
|
||||
Resource hr_employee = new ClassPathResource("hr_employee.xsd", getClass());
|
||||
Document expected = documentBuilder.parse(SaxUtils.createInputSource(hr_employee));
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(schemas[0].getSource(), domResult);
|
||||
assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
|
||||
|
||||
Assert.assertEquals("Invalid target namespace", "http://mycompany.com/hr/schemas/holiday", schemas[1].getTargetNamespace());
|
||||
Resource holiday = new ClassPathResource("holiday.xsd", getClass());
|
||||
expected = documentBuilder.parse(SaxUtils.createInputSource(holiday));
|
||||
domResult = new DOMResult();
|
||||
transformer.transform(schemas[1].getSource(), domResult);
|
||||
assertXMLEqual("Invalid XSD generated", expected, (Document) domResult.getNode());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2005-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.xml.xsd.commons;
|
||||
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.xml.sax.SaxUtils;
|
||||
import org.springframework.xml.xsd.AbstractXsdSchemaTestCase;
|
||||
import org.springframework.xml.xsd.XsdSchema;
|
||||
|
||||
import org.apache.ws.commons.schema.XmlSchema;
|
||||
import org.apache.ws.commons.schema.XmlSchemaCollection;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class CommonsXsdSchemaTest extends AbstractXsdSchemaTestCase {
|
||||
|
||||
@Override
|
||||
protected XsdSchema createSchema(Resource resource) throws Exception {
|
||||
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
|
||||
XmlSchema schema = schemaCollection.read(SaxUtils.createInputSource(resource));
|
||||
return new CommonsXsdSchema(schema);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXmime() throws Exception {
|
||||
Resource resource = new ClassPathResource("xmime.xsd", AbstractXsdSchemaTestCase.class);
|
||||
XsdSchema schema = createSchema(resource);
|
||||
String namespace = "urn:test";
|
||||
assertEquals("Invalid target namespace", namespace, schema.getTargetNamespace());
|
||||
Document result = (Document) ((DOMSource) schema.getSource()).getNode();
|
||||
Element schemaElement = result.getDocumentElement();
|
||||
Element elementElement = (Element) schemaElement.getFirstChild();
|
||||
assertNotNull("No expectedContentTypes found",
|
||||
elementElement.getAttributeNS("http://www.w3.org/2005/05/xmlmime", "expectedContentTypes"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
6
spring-xml/src/test/resources/log4j.properties
Normal file
6
spring-xml/src/test/resources/log4j.properties
Normal file
@@ -0,0 +1,6 @@
|
||||
log4j.rootCategory=INFO, stdout
|
||||
log4j.logger.org.springframework.xml=DEBUG
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
||||
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<prefix:content xmlns:prefix='namespace'/>
|
||||
@@ -0,0 +1,504 @@
|
||||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:tns="http://schemas.xmlsoap.org/soap/encoding/"
|
||||
targetNamespace="http://schemas.xmlsoap.org/soap/encoding/">
|
||||
|
||||
<xs:attribute name="root">
|
||||
<xs:annotation>
|
||||
<xs:documentation>
|
||||
'root' can be used to distinguish serialization roots from other
|
||||
elements that are present in a serialization but are not roots of
|
||||
a serialized value graph
|
||||
</xs:documentation>
|
||||
</xs:annotation>
|
||||
<xs:simpleType>
|
||||
<xs:restriction base='xs:boolean'>
|
||||
<xs:pattern value='0|1'/>
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
|
||||
<xs:attributeGroup name="commonAttributes">
|
||||
<xs:annotation>
|
||||
<xs:documentation>
|
||||
Attributes common to all elements that function as accessors or
|
||||
represent independent (multi-ref) values. The href attribute is
|
||||
intended to be used in a manner like CONREF. That is, the element
|
||||
content should be empty iff the href attribute appears
|
||||
</xs:documentation>
|
||||
</xs:annotation>
|
||||
<xs:attribute name="id" type="xs:ID"/>
|
||||
<xs:attribute name="href" type="xs:anyURI"/>
|
||||
<xs:anyAttribute namespace="##other" processContents="lax"/>
|
||||
</xs:attributeGroup>
|
||||
|
||||
<!-- Global Attributes. The following attributes are intended to be usable via qualified attribute names on any complex type referencing them. -->
|
||||
|
||||
<!-- Array attributes. Needed to give the type and dimensions of an array's contents, and the offset for partially-transmitted arrays. -->
|
||||
|
||||
<xs:simpleType name="arrayCoordinate">
|
||||
<xs:restriction base="xs:string"/>
|
||||
</xs:simpleType>
|
||||
|
||||
<xs:attribute name="arrayType" type="xs:string"/>
|
||||
<xs:attribute name="offset" type="tns:arrayCoordinate"/>
|
||||
|
||||
<xs:attributeGroup name="arrayAttributes">
|
||||
<xs:attribute ref="tns:arrayType"/>
|
||||
<xs:attribute ref="tns:offset"/>
|
||||
</xs:attributeGroup>
|
||||
|
||||
<xs:attribute name="position" type="tns:arrayCoordinate"/>
|
||||
|
||||
<xs:attributeGroup name="arrayMemberAttributes">
|
||||
<xs:attribute ref="tns:position"/>
|
||||
</xs:attributeGroup>
|
||||
|
||||
<xs:group name="Array">
|
||||
<xs:sequence>
|
||||
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
|
||||
</xs:sequence>
|
||||
</xs:group>
|
||||
|
||||
<xs:element name="Array" type="tns:Array"/>
|
||||
<xs:complexType name="Array">
|
||||
<xs:annotation>
|
||||
<xs:documentation>
|
||||
'Array' is a complex type for accessors identified by position
|
||||
</xs:documentation>
|
||||
</xs:annotation>
|
||||
<xs:group ref="tns:Array" minOccurs="0"/>
|
||||
<xs:attributeGroup ref="tns:arrayAttributes"/>
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:complexType>
|
||||
|
||||
<!-- 'Struct' is a complex type for accessors identified by name.
|
||||
Constraint: No element may be have the same name as any other,
|
||||
nor may any element have a maxOccurs > 1. -->
|
||||
|
||||
<xs:element name="Struct" type="tns:Struct"/>
|
||||
|
||||
<xs:group name="Struct">
|
||||
<xs:sequence>
|
||||
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
|
||||
</xs:sequence>
|
||||
</xs:group>
|
||||
|
||||
<xs:complexType name="Struct">
|
||||
<xs:group ref="tns:Struct" minOccurs="0"/>
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:complexType>
|
||||
|
||||
<!-- 'Base64' can be used to serialize binary data using base64 encoding
|
||||
as defined in RFC2045 but without the MIME line length limitation. -->
|
||||
|
||||
<xs:simpleType name="base64">
|
||||
<xs:restriction base="xs:base64Binary"/>
|
||||
</xs:simpleType>
|
||||
|
||||
<!-- Element declarations corresponding to each of the simple types in the
|
||||
XML Schemas Specification. -->
|
||||
|
||||
<xs:element name="duration" type="tns:duration"/>
|
||||
<xs:complexType name="duration">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:duration">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="dateTime" type="tns:dateTime"/>
|
||||
<xs:complexType name="dateTime">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:dateTime">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
|
||||
<xs:element name="NOTATION" type="tns:NOTATION"/>
|
||||
<xs:complexType name="NOTATION">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:QName">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
|
||||
<xs:element name="time" type="tns:time"/>
|
||||
<xs:complexType name="time">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:time">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="date" type="tns:date"/>
|
||||
<xs:complexType name="date">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:date">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="gYearMonth" type="tns:gYearMonth"/>
|
||||
<xs:complexType name="gYearMonth">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:gYearMonth">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="gYear" type="tns:gYear"/>
|
||||
<xs:complexType name="gYear">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:gYear">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="gMonthDay" type="tns:gMonthDay"/>
|
||||
<xs:complexType name="gMonthDay">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:gMonthDay">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="gDay" type="tns:gDay"/>
|
||||
<xs:complexType name="gDay">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:gDay">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="gMonth" type="tns:gMonth"/>
|
||||
<xs:complexType name="gMonth">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:gMonth">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="boolean" type="tns:boolean"/>
|
||||
<xs:complexType name="boolean">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:boolean">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="base64Binary" type="tns:base64Binary"/>
|
||||
<xs:complexType name="base64Binary">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:base64Binary">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="hexBinary" type="tns:hexBinary"/>
|
||||
<xs:complexType name="hexBinary">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:hexBinary">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="float" type="tns:float"/>
|
||||
<xs:complexType name="float">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:float">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="double" type="tns:double"/>
|
||||
<xs:complexType name="double">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:double">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="anyURI" type="tns:anyURI"/>
|
||||
<xs:complexType name="anyURI">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:anyURI">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="QName" type="tns:QName"/>
|
||||
<xs:complexType name="QName">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:QName">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
|
||||
<xs:element name="string" type="tns:string"/>
|
||||
<xs:complexType name="string">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:string">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="normalizedString" type="tns:normalizedString"/>
|
||||
<xs:complexType name="normalizedString">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:normalizedString">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="token" type="tns:token"/>
|
||||
<xs:complexType name="token">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:token">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="language" type="tns:language"/>
|
||||
<xs:complexType name="language">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:language">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="Name" type="tns:Name"/>
|
||||
<xs:complexType name="Name">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:Name">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="NMTOKEN" type="tns:NMTOKEN"/>
|
||||
<xs:complexType name="NMTOKEN">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:NMTOKEN">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="NCName" type="tns:NCName"/>
|
||||
<xs:complexType name="NCName">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:NCName">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="NMTOKENS" type="tns:NMTOKENS"/>
|
||||
<xs:complexType name="NMTOKENS">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:NMTOKENS">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="ID" type="tns:ID"/>
|
||||
<xs:complexType name="ID">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:ID">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="IDREF" type="tns:IDREF"/>
|
||||
<xs:complexType name="IDREF">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:IDREF">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="ENTITY" type="tns:ENTITY"/>
|
||||
<xs:complexType name="ENTITY">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:ENTITY">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="IDREFS" type="tns:IDREFS"/>
|
||||
<xs:complexType name="IDREFS">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:IDREFS">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="ENTITIES" type="tns:ENTITIES"/>
|
||||
<xs:complexType name="ENTITIES">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:ENTITIES">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="decimal" type="tns:decimal"/>
|
||||
<xs:complexType name="decimal">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:decimal">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="integer" type="tns:integer"/>
|
||||
<xs:complexType name="integer">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:integer">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="nonPositiveInteger" type="tns:nonPositiveInteger"/>
|
||||
<xs:complexType name="nonPositiveInteger">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:nonPositiveInteger">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="negativeInteger" type="tns:negativeInteger"/>
|
||||
<xs:complexType name="negativeInteger">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:negativeInteger">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="long" type="tns:long"/>
|
||||
<xs:complexType name="long">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:long">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="int" type="tns:int"/>
|
||||
<xs:complexType name="int">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:int">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="short" type="tns:short"/>
|
||||
<xs:complexType name="short">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:short">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="byte" type="tns:byte"/>
|
||||
<xs:complexType name="byte">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:byte">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="nonNegativeInteger" type="tns:nonNegativeInteger"/>
|
||||
<xs:complexType name="nonNegativeInteger">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:nonNegativeInteger">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="unsignedLong" type="tns:unsignedLong"/>
|
||||
<xs:complexType name="unsignedLong">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:unsignedLong">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="unsignedInt" type="tns:unsignedInt"/>
|
||||
<xs:complexType name="unsignedInt">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:unsignedInt">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="unsignedShort" type="tns:unsignedShort"/>
|
||||
<xs:complexType name="unsignedShort">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:unsignedShort">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="unsignedByte" type="tns:unsignedByte"/>
|
||||
<xs:complexType name="unsignedByte">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:unsignedByte">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="positiveInteger" type="tns:positiveInteger"/>
|
||||
<xs:complexType name="positiveInteger">
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:positiveInteger">
|
||||
<xs:attributeGroup ref="tns:commonAttributes"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="anyType"/>
|
||||
</xs:schema>
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
<?xml version='1.0' encoding='UTF-8' ?>
|
||||
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/"
|
||||
targetNamespace="http://schemas.xmlsoap.org/soap/envelope/">
|
||||
|
||||
|
||||
<!-- Envelope, header and body -->
|
||||
<xs:element name="Envelope" type="tns:Envelope"/>
|
||||
<xs:complexType name="Envelope">
|
||||
<xs:sequence>
|
||||
<xs:element ref="tns:Header" minOccurs="0"/>
|
||||
<xs:element ref="tns:Body" minOccurs="1"/>
|
||||
<xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
|
||||
</xs:sequence>
|
||||
<xs:anyAttribute namespace="##other" processContents="lax"/>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="Header" type="tns:Header"/>
|
||||
<xs:complexType name="Header">
|
||||
<xs:sequence>
|
||||
<xs:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
|
||||
</xs:sequence>
|
||||
<xs:anyAttribute namespace="##other" processContents="lax"/>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="Body" type="tns:Body"/>
|
||||
<xs:complexType name="Body">
|
||||
<xs:sequence>
|
||||
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
|
||||
</xs:sequence>
|
||||
<xs:anyAttribute namespace="##any" processContents="lax">
|
||||
<xs:annotation>
|
||||
<xs:documentation>
|
||||
Prose in the spec does not specify that attributes are allowed on the Body element
|
||||
</xs:documentation>
|
||||
</xs:annotation>
|
||||
</xs:anyAttribute>
|
||||
</xs:complexType>
|
||||
|
||||
|
||||
<!-- Global Attributes. The following attributes are intended to be usable via qualified attribute names on any complex type referencing them. -->
|
||||
<xs:attribute name="mustUnderstand">
|
||||
<xs:simpleType>
|
||||
<xs:restriction base='xs:boolean'>
|
||||
<xs:pattern value='0|1'/>
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
</xs:attribute>
|
||||
<xs:attribute name="actor" type="xs:anyURI"/>
|
||||
|
||||
<xs:simpleType name="encodingStyle">
|
||||
<xs:annotation>
|
||||
<xs:documentation>
|
||||
'encodingStyle' indicates any canonicalization conventions followed in the contents of the containing
|
||||
element. For example, the value 'http://schemas.xmlsoap.org/soap/encoding/' indicates the pattern
|
||||
described in SOAP specification
|
||||
</xs:documentation>
|
||||
</xs:annotation>
|
||||
<xs:list itemType="xs:anyURI"/>
|
||||
</xs:simpleType>
|
||||
|
||||
<xs:attribute name="encodingStyle" type="tns:encodingStyle"/>
|
||||
<xs:attributeGroup name="encodingStyle">
|
||||
<xs:attribute ref="tns:encodingStyle"/>
|
||||
</xs:attributeGroup>
|
||||
|
||||
<xs:element name="Fault" type="tns:Fault"/>
|
||||
<xs:complexType name="Fault" final="extension">
|
||||
<xs:annotation>
|
||||
<xs:documentation>
|
||||
Fault reporting structure
|
||||
</xs:documentation>
|
||||
</xs:annotation>
|
||||
<xs:sequence>
|
||||
<xs:element name="faultcode" type="xs:QName"/>
|
||||
<xs:element name="faultstring" type="xs:string"/>
|
||||
<xs:element name="faultactor" type="xs:anyURI" minOccurs="0"/>
|
||||
<xs:element name="detail" type="tns:detail" minOccurs="0"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:complexType name="detail">
|
||||
<xs:sequence>
|
||||
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
|
||||
</xs:sequence>
|
||||
<xs:anyAttribute namespace="##any" processContents="lax"/>
|
||||
</xs:complexType>
|
||||
|
||||
</xs:schema>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<product xmlns="http://www.springframework.org/spring-ws/test/validation" effDate="2006-01-01">
|
||||
<size>20</size>
|
||||
</product>
|
||||
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<multipleSchemas1 xmlns="http://www.springframework.org/spring-ws/test/multipleSchemas1">abc</multipleSchemas1>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/spring-ws/test/multipleSchemas1"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
<element name="multipleSchemas1">
|
||||
<simpleType>
|
||||
<restriction base="string">
|
||||
<pattern value="[a-c]+"/>
|
||||
</restriction>
|
||||
</simpleType>
|
||||
</element>
|
||||
|
||||
</schema>
|
||||
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<multipleSchemas2 xmlns="http://www.springframework.org/spring-ws/test/multipleSchemas2">123</multipleSchemas2>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/spring-ws/test/multipleSchemas2"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
<element name="multipleSchemas2">
|
||||
<simpleType>
|
||||
<restriction base="string">
|
||||
<pattern value="[1-3]+"/>
|
||||
</restriction>
|
||||
</simpleType>
|
||||
</element>
|
||||
|
||||
</schema>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/spring-ws/test/validation"
|
||||
xmlns:tns="http://www.springframework.org/spring-ws/test/validation" elementFormDefault="qualified">
|
||||
|
||||
<element name="product" type="tns:ProductType"/>
|
||||
|
||||
<complexType name="ProductType">
|
||||
<sequence>
|
||||
<element name="number" type="integer"/>
|
||||
<element name="size" type="tns:SizeType"/>
|
||||
</sequence>
|
||||
<attribute name="effDate" type="date"/>
|
||||
</complexType>
|
||||
|
||||
<simpleType name="SizeType">
|
||||
<restriction base="integer">
|
||||
<minInclusive value="2"/>
|
||||
<maxInclusive value="18"/>
|
||||
</restriction>
|
||||
</simpleType>
|
||||
</schema>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<product xmlns="http://www.springframework.org/spring-ws/test/validation" effDate="2006-01-01">
|
||||
<number>42</number>
|
||||
<size>10</size>
|
||||
</product>
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<root xmlns="namespace1">
|
||||
<prefix:child xmlns:prefix="namespace2">
|
||||
<prefix:text>text</prefix:text>
|
||||
<prefix:number>42.0</prefix:number>
|
||||
<prefix:boolean>1</prefix:boolean>
|
||||
</prefix:child>
|
||||
</root>
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<root>
|
||||
<child>
|
||||
<text>text</text>
|
||||
<number>42.0</number>
|
||||
<boolean>1</boolean>
|
||||
</child>
|
||||
</root>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="urn:1"
|
||||
xmlns:tns="urn:1" elementFormDefault="qualified"
|
||||
attributeFormDefault="unqualified">
|
||||
<xsd:include schemaLocation="B.xsd"/>
|
||||
<xsd:complexType name="A">
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="tns:B"/>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns0="urn:2" xmlns:tns="urn:1"
|
||||
attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:1">
|
||||
<xsd:import namespace="urn:2"/>
|
||||
<xsd:complexType name="A">
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="tns:B"/>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
<xsd:complexType name="B">
|
||||
<xsd:sequence>
|
||||
<xsd:element name="c" type="tns:C"/>
|
||||
<xsd:element name="d" type="ns0:D"/>
|
||||
</xsd:sequence>
|
||||
</xsd:complexType>
|
||||
<xsd:simpleType name="C">
|
||||
<xsd:restriction base="xsd:string"/>
|
||||
</xsd:simpleType>
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="urn:1"
|
||||
xmlns="urn:1"
|
||||
xmlns:imported="urn:2" elementFormDefault="qualified">
|
||||
<xsd:include schemaLocation="C.xsd"/>
|
||||
<xsd:import schemaLocation="D.xsd" namespace="urn:2"/>
|
||||
<xsd:complexType name="B">
|
||||
<xsd:sequence>
|
||||
<xsd:element type="C" name="c"/>
|
||||
<xsd:element type="imported:D" name="d"/>
|
||||
</xsd:sequence>
|
||||
</xsd:complexType>
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
elementFormDefault="qualified">
|
||||
<xsd:simpleType name="C">
|
||||
<xsd:restriction base="xsd:string"/>
|
||||
</xsd:simpleType>
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:2" attributeFormDefault="unqualified"
|
||||
elementFormDefault="qualified" targetNamespace="urn:2">
|
||||
<xsd:simpleType name="D">
|
||||
<xsd:restriction base="C"/>
|
||||
</xsd:simpleType>
|
||||
<xsd:simpleType name="C">
|
||||
<xsd:restriction base="xsd:string"/>
|
||||
</xsd:simpleType>
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="urn:2"
|
||||
xmlns="urn:2" elementFormDefault="qualified">
|
||||
<xsd:include schemaLocation="C.xsd"/>
|
||||
<xsd:simpleType name="D">
|
||||
<xsd:restriction base="C"/>
|
||||
</xsd:simpleType>
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="urn:1"
|
||||
xmlns:tns="urn:1" elementFormDefault="qualified"
|
||||
attributeFormDefault="unqualified">
|
||||
<xsd:include schemaLocation="circular-2.xsd"/>
|
||||
<xsd:simpleType name="A1">
|
||||
<xsd:restriction base="tns:B1"/>
|
||||
</xsd:simpleType>
|
||||
<xsd:simpleType name="A2">
|
||||
<xsd:restriction base="xsd:string"/>
|
||||
</xsd:simpleType>
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="urn:1"
|
||||
xmlns:tns="urn:1" elementFormDefault="qualified"
|
||||
attributeFormDefault="unqualified">
|
||||
<xsd:include schemaLocation="circular-3.xsd"/>
|
||||
<xsd:simpleType name="B1">
|
||||
<xsd:restriction base="tns:C1"/>
|
||||
</xsd:simpleType>
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="urn:1"
|
||||
xmlns:tns="urn:1" elementFormDefault="qualified"
|
||||
attributeFormDefault="unqualified">
|
||||
<xsd:include schemaLocation="circular-1.xsd"/>
|
||||
<xsd:simpleType name="C1">
|
||||
<xsd:restriction base="xsd:string"/>
|
||||
</xsd:simpleType>
|
||||
<xsd:simpleType name="C2">
|
||||
<xsd:restriction base="tns:A1"/>
|
||||
</xsd:simpleType>
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:hr="http://mycompany.com/hr/schemas"
|
||||
elementFormDefault="qualified"
|
||||
targetNamespace="http://mycompany.com/hr/schemas">
|
||||
<xs:complexType name="EmployeeType">
|
||||
<xs:sequence>
|
||||
<xs:element name="Number" type="xs:integer"/>
|
||||
<xs:element name="FirstName" type="xs:string"/>
|
||||
<xs:element name="LastName" type="xs:string"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:schema>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:hrh="http://mycompany.com/hr/schemas/holiday"
|
||||
attributeFormDefault="unqualified"
|
||||
elementFormDefault="qualified"
|
||||
targetNamespace="http://mycompany.com/hr/schemas/holiday">
|
||||
<xs:complexType name="HolidayType">
|
||||
<xs:sequence>
|
||||
<xs:element name="StartDate" type="xs:date"/>
|
||||
<xs:element name="EndDate" type="xs:date"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:schema>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:hr="http://mycompany.com/hr/schemas"
|
||||
xmlns:hrh="http://mycompany.com/hr/schemas/holiday"
|
||||
elementFormDefault="qualified"
|
||||
targetNamespace="http://mycompany.com/hr/schemas">
|
||||
|
||||
<xs:import namespace="http://mycompany.com/hr/schemas/holiday" schemaLocation="holiday.xsd"/>
|
||||
<xs:include schemaLocation="employee.xsd"/>
|
||||
<xs:element name="HolidayRequest">
|
||||
<xs:complexType>
|
||||
<xs:all>
|
||||
<xs:element name="Holiday" type="hrh:HolidayType"/>
|
||||
<xs:element name="Employee" type="hr:EmployeeType"/>
|
||||
</xs:all>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user