Removed Deprecated code from xml module

This commit is contained in:
Arjen Poutsma
2012-05-07 10:27:16 +00:00
parent 92666fcd2e
commit f1720a7e21
29 changed files with 29 additions and 3198 deletions

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* 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
* 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,
@@ -29,7 +29,7 @@ import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import org.springframework.xml.transform.StaxSource;
import org.springframework.util.xml.StaxUtils;
import org.junit.Assert;
import org.junit.Test;
@@ -59,7 +59,7 @@ public class PayloadRootUtilsTest {
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(contents));
Source source = new StaxSource(streamReader);
Source source = StaxUtils.createStaxSource(streamReader);
QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
Assert.assertNotNull("getQNameForNode returns null", qName);
Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
@@ -72,7 +72,7 @@ public class PayloadRootUtilsTest {
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(contents));
Source source = new StaxSource(eventReader);
Source source = StaxUtils.createStaxSource(eventReader);
QName qName = PayloadRootUtils.getPayloadRootQName(source, TransformerFactory.newInstance());
Assert.assertNotNull("getQNameForNode returns null", qName);
Assert.assertEquals("QName has invalid localname", "localname", qName.getLocalPart());

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* 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
* 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,
@@ -81,19 +81,6 @@ public abstract class JaxpVersion {
return jaxpVersion;
}
/**
* Convenience method to determine if the current JAXP version is at least 1.3 (packaged with JDK 1.5).
*
* @return <code>true</code> if the current JAXP version is at least JAXP 1.3
* @see #getJaxpVersion()
* @see #JAXP_13
* @deprecated as of Spring-WS 2.0 which requires Java 1.5+
*/
@Deprecated
public static boolean isAtLeastJaxp13() {
return getJaxpVersion() >= JAXP_13;
}
/**
* Convenience method to determine if the current JAXP version is at least 1.4 (packaged with JDK 1.6).
*

View File

@@ -1,155 +0,0 @@
/*
* 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.stream;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import org.springframework.xml.namespace.QNameUtils;
import org.springframework.xml.namespace.SimpleNamespaceContext;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
/**
* Abstract base class for SAX <code>ContentHandler</code> implementations that use StAX as a basis. All methods
* delegate to internal template methods, capable of throwing a <code>XMLStreamException</code>. Additionally, an
* namespace context is used to keep track of declared namespaces.
*
* @author Arjen Poutsma
* @since 1.0.0
* @deprecated With no concrete replacement
*/
@Deprecated
public abstract class AbstractStaxContentHandler implements ContentHandler {
private SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
/**
* @throws SAXException
*/
public final void startDocument() throws SAXException {
namespaceContext.clear();
try {
startDocumentInternal();
}
catch (XMLStreamException ex) {
throw new SAXException("Could not handle startDocument: " + ex.getMessage(), ex);
}
}
protected abstract void startDocumentInternal() throws XMLStreamException;
public final void endDocument() throws SAXException {
namespaceContext.clear();
try {
endDocumentInternal();
}
catch (XMLStreamException ex) {
throw new SAXException("Could not handle startDocument: " + ex.getMessage(), ex);
}
}
protected abstract void endDocumentInternal() throws XMLStreamException;
/**
* Binds the given prefix to the given namespaces.
*
* @see SimpleNamespaceContext#bindNamespaceUri(String,String)
*/
public final void startPrefixMapping(String prefix, String uri) {
namespaceContext.bindNamespaceUri(prefix, uri);
}
/**
* Removes the binding for the given prefix.
*
* @see SimpleNamespaceContext#removeBinding(String)
*/
public final void endPrefixMapping(String prefix) {
namespaceContext.removeBinding(prefix);
}
public final void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
try {
startElementInternal(QNameUtils.toQName(uri, qName), atts, namespaceContext);
}
catch (XMLStreamException ex) {
throw new SAXException("Could not handle startElement: " + ex.getMessage(), ex);
}
}
protected abstract void startElementInternal(QName name, Attributes atts, SimpleNamespaceContext namespaceContext)
throws XMLStreamException;
public final void endElement(String uri, String localName, String qName) throws SAXException {
try {
endElementInternal(QNameUtils.toQName(uri, qName), namespaceContext);
}
catch (XMLStreamException ex) {
throw new SAXException("Could not handle endElement: " + ex.getMessage(), ex);
}
}
protected abstract void endElementInternal(QName name, SimpleNamespaceContext namespaceContext)
throws XMLStreamException;
public final void characters(char ch[], int start, int length) throws SAXException {
try {
charactersInternal(ch, start, length);
}
catch (XMLStreamException ex) {
throw new SAXException("Could not handle characters: " + ex.getMessage(), ex);
}
}
protected abstract void charactersInternal(char[] ch, int start, int length) throws XMLStreamException;
public final void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
try {
ignorableWhitespaceInternal(ch, start, length);
}
catch (XMLStreamException ex) {
throw new SAXException("Could not handle ignorableWhitespace:" + ex.getMessage(), ex);
}
}
protected abstract void ignorableWhitespaceInternal(char[] ch, int start, int length) throws XMLStreamException;
public final void processingInstruction(String target, String data) throws SAXException {
try {
processingInstructionInternal(target, data);
}
catch (XMLStreamException ex) {
throw new SAXException("Could not handle processingInstruction: " + ex.getMessage(), ex);
}
}
protected abstract void processingInstructionInternal(String target, String data) throws XMLStreamException;
public final void skippedEntity(String name) throws SAXException {
try {
skippedEntityInternal(name);
}
catch (XMLStreamException ex) {
throw new SAXException("Could not handle skippedEntity: " + ex.getMessage(), ex);
}
}
protected abstract void skippedEntityInternal(String name) throws XMLStreamException;
}

View File

@@ -1,194 +0,0 @@
/*
* 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.stream;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException;
import org.springframework.xml.sax.AbstractXmlReader;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
/**
* Abstract base class for SAX <code>XMLReader</code> implementations that use StAX as a basis.
*
* @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
* @deprecated With no concrete replacement
*/
@Deprecated
public abstract class AbstractStaxXmlReader extends AbstractXmlReader {
private static final String NAMESPACES_FEATURE_NAME = "http://xml.org/sax/features/namespaces";
private static final String NAMESPACE_PREFIXES_FEATURE_NAME = "http://xml.org/sax/features/namespace-prefixes";
private static final String IS_STANDALONE_FEATURE_NAME = "http://xml.org/sax/features/is-standalone";
private boolean namespacesFeature = true;
private boolean namespacePrefixesFeature = false;
private Boolean isStandalone;
@Override
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
if (NAMESPACES_FEATURE_NAME.equals(name)) {
return namespacesFeature;
}
else if (NAMESPACE_PREFIXES_FEATURE_NAME.equals(name)) {
return namespacePrefixesFeature;
}
else if (IS_STANDALONE_FEATURE_NAME.equals(name)) {
if (isStandalone != null) {
return isStandalone;
}
else {
throw new SAXNotSupportedException("startDocument() callback not completed yet");
}
}
else {
return super.getFeature(name);
}
}
@Override
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
if (NAMESPACES_FEATURE_NAME.equals(name)) {
this.namespacesFeature = value;
}
else if (NAMESPACE_PREFIXES_FEATURE_NAME.equals(name)) {
this.namespacePrefixesFeature = value;
}
else {
super.setFeature(name, value);
}
}
/** Indicates whether the SAX feature <code>http://xml.org/sax/features/namespaces</code> is turned on. */
protected boolean hasNamespacesFeature() {
return namespacesFeature;
}
/** Indicates whether the SAX feature <code>http://xml.org/sax/features/namespaces-prefixes</code> is turned on. */
protected boolean hasNamespacePrefixesFeature() {
return namespacePrefixesFeature;
}
protected void setStandalone(boolean standalone) {
isStandalone = (standalone) ? Boolean.TRUE : Boolean.FALSE;
}
/**
* Parses the StAX XML reader passed at construction-time.
* <p/>
* <strong>Note</strong> that the given <code>InputSource</code> is not read, but ignored.
*
* @param ignored is ignored
* @throws SAXException A SAX exception, possibly wrapping a <code>XMLStreamException</code>
*/
public final void parse(InputSource ignored) throws SAXException {
parse();
}
/**
* Parses the StAX XML reader passed at construction-time.
* <p/>
* <strong>Note</strong> that the given system identifier is not read, but ignored.
*
* @param ignored is ignored
* @throws SAXException A SAX exception, possibly wrapping a <code>XMLStreamException</code>
*/
public final void parse(String ignored) throws SAXException {
parse();
}
private void parse() throws SAXException {
try {
parseInternal();
}
catch (XMLStreamException ex) {
Locator locator = null;
if (ex.getLocation() != null) {
locator = new StaxLocator(ex.getLocation());
}
SAXParseException saxException = new SAXParseException(ex.getMessage(), locator, ex);
if (getErrorHandler() != null) {
getErrorHandler().fatalError(saxException);
}
else {
throw saxException;
}
}
}
/**
* Sets the SAX <code>Locator</code> based on the given StAX <code>Location</code>.
*
* @param location the location
* @see ContentHandler#setDocumentLocator(org.xml.sax.Locator)
*/
protected void setLocator(Location location) {
if (getContentHandler() != null) {
getContentHandler().setDocumentLocator(new StaxLocator(location));
}
}
/** Template-method that parses the StAX reader passed at construction-time. */
protected abstract void parseInternal() throws SAXException, XMLStreamException;
/**
* Implementation of the <code>Locator</code> interface that is based on a StAX <code>Location</code>.
*
* @see Locator
* @see Location
*/
private static class StaxLocator implements Locator {
private Location location;
protected StaxLocator(Location location) {
this.location = location;
}
public String getPublicId() {
return location.getPublicId();
}
public String getSystemId() {
return location.getSystemId();
}
public int getLineNumber() {
return location.getLineNumber();
}
public int getColumnNumber() {
return location.getColumnNumber();
}
}
}

View File

@@ -1,185 +0,0 @@
/*
* 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.stream;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.springframework.util.Assert;
/**
* Abstract base class for <code>XMLStreamReader</code>s.
*
* @author Arjen Poutsma
* @since 1.0.0
* @deprecated With no concrete replacement
*/
@Deprecated
public abstract class AbstractXmlStreamReader implements XMLStreamReader {
public String getElementText() throws XMLStreamException {
if (getEventType() != XMLStreamConstants.START_ELEMENT) {
throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
}
int eventType = next();
StringBuilder builder = new StringBuilder();
while (eventType != XMLStreamConstants.END_ELEMENT) {
if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA ||
eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
builder.append(getText());
}
else
if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT) {
// skipping
}
else if (eventType == XMLStreamConstants.END_DOCUMENT) {
throw new XMLStreamException("unexpected end of document when reading element text content",
getLocation());
}
else if (eventType == XMLStreamConstants.START_ELEMENT) {
throw new XMLStreamException("element text content may not contain START_ELEMENT", getLocation());
}
else {
throw new XMLStreamException("Unexpected event type " + eventType, getLocation());
}
eventType = next();
}
return builder.toString();
}
public String getAttributeLocalName(int index) {
return getAttributeName(index).getLocalPart();
}
public String getAttributeNamespace(int index) {
return getAttributeName(index).getNamespaceURI();
}
public String getAttributePrefix(int index) {
return getAttributeName(index).getPrefix();
}
public String getNamespaceURI() {
int eventType = getEventType();
if (eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT) {
return getName().getNamespaceURI();
}
else {
throw new IllegalStateException("parser must be on START_ELEMENT or END_ELEMENT state");
}
}
public String getNamespaceURI(String prefix) {
Assert.notNull(prefix, "No prefix given");
return getNamespaceContext().getNamespaceURI(prefix);
}
public boolean hasText() {
int eventType = getEventType();
return eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.CHARACTERS ||
eventType == XMLStreamConstants.COMMENT || eventType == XMLStreamConstants.CDATA ||
eventType == XMLStreamConstants.ENTITY_REFERENCE;
}
public String getPrefix() {
int eventType = getEventType();
if (eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT) {
return getName().getPrefix();
}
else {
throw new IllegalStateException("parser must be on START_ELEMENT or END_ELEMENT state");
}
}
public boolean hasName() {
int eventType = getEventType();
return eventType == XMLStreamConstants.START_ELEMENT || eventType == XMLStreamConstants.END_ELEMENT;
}
public boolean isWhiteSpace() {
return getEventType() == XMLStreamConstants.SPACE;
}
public boolean isStartElement() {
return getEventType() == XMLStreamConstants.START_ELEMENT;
}
public boolean isEndElement() {
return getEventType() == XMLStreamConstants.END_ELEMENT;
}
public boolean isCharacters() {
return getEventType() == XMLStreamConstants.CHARACTERS;
}
public int nextTag() throws XMLStreamException {
int eventType = next();
while (eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace() ||
eventType == XMLStreamConstants.CDATA && isWhiteSpace() || eventType == XMLStreamConstants.SPACE ||
eventType == XMLStreamConstants.PROCESSING_INSTRUCTION || eventType == XMLStreamConstants.COMMENT) {
eventType = next();
}
if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
throw new XMLStreamException("expected start or end tag", getLocation());
}
return eventType;
}
public void require(int expectedType, String namespaceURI, String localName) throws XMLStreamException {
int eventType = getEventType();
if (eventType != expectedType) {
throw new XMLStreamException("Expected [" + expectedType + "] but read [" + eventType + "]");
}
}
public String getAttributeValue(String namespaceURI, String localName) {
for (int i = 0; i < getAttributeCount(); i++) {
QName name = getAttributeName(i);
if (name.getLocalPart().equals(localName) &&
(namespaceURI == null || name.getNamespaceURI().equals(namespaceURI))) {
return getAttributeValue(i);
}
}
return null;
}
public boolean hasNext() throws XMLStreamException {
return getEventType() != END_DOCUMENT;
}
public String getLocalName() {
return getName().getLocalPart();
}
public char[] getTextCharacters() {
return getText().toCharArray();
}
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
throws XMLStreamException {
char[] source = getTextCharacters();
length = Math.min(length, source.length);
System.arraycopy(source, sourceStart, target, targetStart, length);
return length;
}
public int getTextLength() {
return getText().length();
}
}

View File

@@ -1,193 +0,0 @@
/*
* 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.stream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.util.XMLEventConsumer;
import org.springframework.util.StringUtils;
import org.springframework.xml.namespace.QNameUtils;
import org.springframework.xml.namespace.SimpleNamespaceContext;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
/**
* SAX <code>ContentHandler</code> that transforms callback calls to <code>XMLEvent</code>s and writes them to a
* <code>XMLEventConsumer</code>.
*
* @author Arjen Poutsma
* @see XMLEvent
* @see XMLEventConsumer
* @since 1.0.0
* @deprecated In favor of {@link org.springframework.util.xml.StaxUtils#createContentHandler(javax.xml.stream.XMLEventWriter)}.
*/
@Deprecated
public class StaxEventContentHandler extends AbstractStaxContentHandler {
private final XMLEventFactory eventFactory;
private final XMLEventConsumer eventConsumer;
private Locator locator;
/**
* Constructs a new instance of the <code>StaxEventContentHandler</code> that writes to the given
* <code>XMLEventConsumer</code>. A default <code>XMLEventFactory</code> will be created.
*
* @param consumer the consumer to write events to
*/
public StaxEventContentHandler(XMLEventConsumer consumer) {
eventFactory = XMLEventFactory.newInstance();
eventConsumer = consumer;
}
/**
* Constructs a new instance of the <code>StaxEventContentHandler</code> that uses the given event factory to create
* events and writes to the given <code>XMLEventConsumer</code>.
*
* @param consumer the consumer to write events to
* @param factory the factory used to create events
*/
public StaxEventContentHandler(XMLEventConsumer consumer, XMLEventFactory factory) {
eventFactory = factory;
eventConsumer = consumer;
}
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
@Override
protected void startDocumentInternal() throws XMLStreamException {
consumeEvent(eventFactory.createStartDocument());
}
@Override
protected void endDocumentInternal() throws XMLStreamException {
consumeEvent(eventFactory.createEndDocument());
}
@Override
protected void startElementInternal(QName name, Attributes atts, SimpleNamespaceContext namespaceContext)
throws XMLStreamException {
List<Attribute> attributes = getAttributes(atts);
List<Namespace> namespaces = createNamespaces(namespaceContext);
consumeEvent(eventFactory.createStartElement(name, attributes.iterator(), namespaces.iterator()));
}
@Override
protected void endElementInternal(QName name, SimpleNamespaceContext namespaceContext) throws XMLStreamException {
List<Namespace> namespaces = createNamespaces(namespaceContext);
consumeEvent(eventFactory.createEndElement(name, namespaces.iterator()));
}
@Override
protected void charactersInternal(char[] ch, int start, int length) throws XMLStreamException {
consumeEvent(eventFactory.createCharacters(new String(ch, start, length)));
}
@Override
protected void ignorableWhitespaceInternal(char[] ch, int start, int length) throws XMLStreamException {
consumeEvent(eventFactory.createIgnorableSpace(new String(ch, start, length)));
}
@Override
protected void processingInstructionInternal(String target, String data) throws XMLStreamException {
consumeEvent(eventFactory.createProcessingInstruction(target, data));
}
private void consumeEvent(XMLEvent event) throws XMLStreamException {
if (locator != null) {
eventFactory.setLocation(new SaxLocation(locator));
}
eventConsumer.add(event);
}
/** Creates and returns a list of <code>NameSpace</code> objects from the <code>NamespaceContext</code>. */
private List<Namespace> createNamespaces(SimpleNamespaceContext namespaceContext) {
List<Namespace> namespaces = new ArrayList<Namespace>();
String defaultNamespaceUri = namespaceContext.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX);
if (StringUtils.hasLength(defaultNamespaceUri)) {
namespaces.add(eventFactory.createNamespace(defaultNamespaceUri));
}
for (Iterator<String> iterator = namespaceContext.getBoundPrefixes(); iterator.hasNext();) {
String prefix = iterator.next();
String namespaceUri = namespaceContext.getNamespaceURI(prefix);
namespaces.add(eventFactory.createNamespace(prefix, namespaceUri));
}
return namespaces;
}
private List<Attribute> getAttributes(Attributes attributes) {
List<Attribute> list = new ArrayList<Attribute>();
for (int i = 0; i < attributes.getLength(); i++) {
QName name = QNameUtils.toQName(attributes.getURI(i), attributes.getQName(i));
if (!("xmlns".equals(name.getLocalPart()) || "xmlns".equals(QNameUtils.getPrefix(name)))) {
list.add(eventFactory.createAttribute(name, attributes.getValue(i)));
}
}
return list;
}
//
// No operation
//
@Override
protected void skippedEntityInternal(String name) throws XMLStreamException {
}
private static class SaxLocation implements Location {
private Locator locator;
public SaxLocation(Locator locator) {
this.locator = locator;
}
public int getLineNumber() {
return locator.getLineNumber();
}
public int getColumnNumber() {
return locator.getColumnNumber();
}
public int getCharacterOffset() {
return -1;
}
public String getPublicId() {
return locator.getPublicId();
}
public String getSystemId() {
return locator.getSystemId();
}
}
}

View File

@@ -1,294 +0,0 @@
/*
* 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.stream;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.Comment;
import javax.xml.stream.events.DTD;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.EntityDeclaration;
import javax.xml.stream.events.EntityReference;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.events.NotationDeclaration;
import javax.xml.stream.events.ProcessingInstruction;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.springframework.util.StringUtils;
import org.springframework.xml.namespace.QNameUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
/**
* SAX <code>XMLReader</code> that reads from a StAX <code>XMLEventReader</code>. Consumes <code>XMLEvents</code> from
* an <code>XMLEventReader</code>, and calls the corresponding methods on the SAX callback interfaces.
*
* @author Arjen Poutsma
* @see XMLEventReader
* @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
* @deprecated In favor of {@link org.springframework.util.xml.StaxUtils#createXMLReader(javax.xml.stream.XMLEventReader)}
*/
@Deprecated
public class StaxEventXmlReader extends AbstractStaxXmlReader {
private final XMLEventReader reader;
/**
* Constructs a new instance of the <code>StaxEventXmlReader</code> that reads from the given
* <code>XMLEventReader</code>. The supplied event reader must be in <code>XMLStreamConstants.START_DOCUMENT</code>
* or <code>XMLStreamConstants.START_ELEMENT</code> state.
*
* @param reader the <code>XMLEventReader</code> to read from
* @throws IllegalStateException if the reader is not at the start of a document or element
*/
public StaxEventXmlReader(XMLEventReader reader) {
try {
XMLEvent event = reader.peek();
if (event == null || !(event.isStartDocument() || event.isStartElement())) {
throw new IllegalStateException("XMLEventReader not at start of document or element");
}
}
catch (XMLStreamException ex) {
throw new IllegalStateException("Could not read first element: " + ex.getMessage());
}
this.reader = reader;
}
@Override
protected void parseInternal() throws SAXException, XMLStreamException {
boolean documentStarted = false;
boolean documentEnded = false;
int elementDepth = 0;
while (reader.hasNext() && elementDepth >= 0) {
XMLEvent event = reader.nextEvent();
if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
handleStartDocument();
documentStarted = true;
}
switch (event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
elementDepth++;
handleStartElement(event.asStartElement());
break;
case XMLStreamConstants.END_ELEMENT:
elementDepth--;
if (elementDepth >= 0) {
handleEndElement(event.asEndElement());
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
handleProcessingInstruction((ProcessingInstruction) event);
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
handleCharacters(event.asCharacters());
break;
case XMLStreamConstants.START_DOCUMENT:
setLocator(event.getLocation());
handleStartDocument();
documentStarted = true;
break;
case XMLStreamConstants.END_DOCUMENT:
handleEndDocument();
documentEnded = true;
break;
case XMLStreamConstants.NOTATION_DECLARATION:
handleNotationDeclaration((NotationDeclaration) event);
break;
case XMLStreamConstants.ENTITY_DECLARATION:
handleEntityDeclaration((EntityDeclaration) event);
break;
case XMLStreamConstants.COMMENT:
handleComment((Comment) event);
break;
case XMLStreamConstants.DTD:
handleDtd((DTD) event);
break;
case XMLStreamConstants.ENTITY_REFERENCE:
handleEntityReference((EntityReference) event);
break;
}
}
if (!documentEnded) {
handleEndDocument();
}
}
private void handleStartElement(StartElement startElement) throws SAXException {
if (getContentHandler() != null) {
QName qName = startElement.getName();
if (hasNamespacesFeature()) {
for (Iterator<?> i = startElement.getNamespaces(); i.hasNext();) {
Namespace namespace = (Namespace) i.next();
getContentHandler().startPrefixMapping(namespace.getPrefix(), namespace.getNamespaceURI());
}
getContentHandler().startElement(qName.getNamespaceURI(), qName.getLocalPart(),
QNameUtils.toQualifiedName(qName), getAttributes(startElement));
}
else {
getContentHandler()
.startElement("", "", QNameUtils.toQualifiedName(qName), getAttributes(startElement));
}
}
}
private void handleCharacters(Characters characters) throws SAXException {
char[] data = characters.getData().toCharArray();
if (getContentHandler() != null && characters.isIgnorableWhiteSpace()) {
getContentHandler().ignorableWhitespace(data, 0, data.length);
return;
}
if (characters.isCData() && getLexicalHandler() != null) {
getLexicalHandler().startCDATA();
}
if (getContentHandler() != null) {
getContentHandler().characters(data, 0, data.length);
}
if (characters.isCData() && getLexicalHandler() != null) {
getLexicalHandler().endCDATA();
}
}
private void handleEndDocument() throws SAXException {
if (getContentHandler() != null) {
getContentHandler().endDocument();
}
}
private void handleEndElement(EndElement endElement) throws SAXException {
if (getContentHandler() != null) {
QName qName = endElement.getName();
if (hasNamespacesFeature()) {
getContentHandler()
.endElement(qName.getNamespaceURI(), qName.getLocalPart(), QNameUtils.toQualifiedName(qName));
for (Iterator<?> i = endElement.getNamespaces(); i.hasNext();) {
Namespace namespace = (Namespace) i.next();
getContentHandler().endPrefixMapping(namespace.getPrefix());
}
}
else {
getContentHandler().endElement("", "", QNameUtils.toQualifiedName(qName));
}
}
}
private void handleNotationDeclaration(NotationDeclaration declaration) throws SAXException {
if (getDTDHandler() != null) {
getDTDHandler().notationDecl(declaration.getName(), declaration.getPublicId(), declaration.getSystemId());
}
}
private void handleEntityDeclaration(EntityDeclaration entityDeclaration) throws SAXException {
if (getDTDHandler() != null) {
getDTDHandler().unparsedEntityDecl(entityDeclaration.getName(), entityDeclaration.getPublicId(),
entityDeclaration.getSystemId(), entityDeclaration.getNotationName());
}
}
private void handleProcessingInstruction(ProcessingInstruction pi) throws SAXException {
if (getContentHandler() != null) {
getContentHandler().processingInstruction(pi.getTarget(), pi.getData());
}
}
private void handleStartDocument() throws SAXException {
if (getContentHandler() != null) {
getContentHandler().startDocument();
}
}
private void handleComment(Comment comment) throws SAXException {
if (getLexicalHandler() != null) {
char[] ch = comment.getText().toCharArray();
getLexicalHandler().comment(ch, 0, ch.length);
}
}
private void handleDtd(DTD dtd) throws SAXException {
if (getLexicalHandler() != null) {
javax.xml.stream.Location location = dtd.getLocation();
getLexicalHandler().startDTD(null, location.getPublicId(), location.getSystemId());
}
if (getLexicalHandler() != null) {
getLexicalHandler().endDTD();
}
}
private void handleEntityReference(EntityReference reference) throws SAXException {
if (getLexicalHandler() != null) {
getLexicalHandler().startEntity(reference.getName());
}
if (getLexicalHandler() != null) {
getLexicalHandler().endEntity(reference.getName());
}
}
private Attributes getAttributes(StartElement event) {
AttributesImpl attributes = new AttributesImpl();
for (Iterator<?> i = event.getAttributes(); i.hasNext();) {
Attribute attribute = (Attribute) i.next();
QName qName = attribute.getName();
String namespace = qName.getNamespaceURI();
if (namespace == null || !hasNamespacesFeature()) {
namespace = "";
}
String type = attribute.getDTDType();
if (type == null) {
type = "CDATA";
}
attributes.addAttribute(namespace, qName.getLocalPart(), QNameUtils.toQualifiedName(qName), type,
attribute.getValue());
}
if (hasNamespacePrefixesFeature()) {
for (Iterator<?> i = event.getNamespaces(); i.hasNext();) {
Namespace namespace = (Namespace) i.next();
String prefix = namespace.getPrefix();
String namespaceUri = namespace.getNamespaceURI();
String qName;
if (StringUtils.hasLength(prefix)) {
qName = "xmlns:" + prefix;
}
else {
qName = "xmlns";
}
attributes.addAttribute("", "", qName, "CDATA", namespaceUri);
}
}
return attributes;
}
}

View File

@@ -1,114 +0,0 @@
/*
* 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.stream;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.springframework.util.StringUtils;
import org.springframework.xml.namespace.QNameUtils;
import org.springframework.xml.namespace.SimpleNamespaceContext;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
/**
* SAX <code>ContentHandler</code> that writes to a <code>XMLStreamWriter</code>.
*
* @author Arjen Poutsma
* @see XMLStreamWriter
* @since 1.0.0
* @deprecated In favor of {@link org.springframework.util.xml.StaxUtils#createContentHandler(javax.xml.stream.XMLStreamWriter)}
*/
@Deprecated
public class StaxStreamContentHandler extends AbstractStaxContentHandler {
private final XMLStreamWriter streamWriter;
/**
* Constructs a new instance of the <code>StaxStreamContentHandler</code> that writes to the given
* <code>XMLStreamWriter</code>.
*
* @param streamWriter the stream writer to write to
*/
public StaxStreamContentHandler(XMLStreamWriter streamWriter) {
this.streamWriter = streamWriter;
}
public void setDocumentLocator(Locator locator) {
}
@Override
protected void charactersInternal(char[] ch, int start, int length) throws XMLStreamException {
streamWriter.writeCharacters(ch, start, length);
}
@Override
protected void endDocumentInternal() throws XMLStreamException {
streamWriter.writeEndDocument();
}
@Override
protected void endElementInternal(QName name, SimpleNamespaceContext namespaceContext) throws XMLStreamException {
streamWriter.writeEndElement();
}
@Override
protected void ignorableWhitespaceInternal(char[] ch, int start, int length) throws XMLStreamException {
streamWriter.writeCharacters(ch, start, length);
}
@Override
protected void processingInstructionInternal(String target, String data) throws XMLStreamException {
streamWriter.writeProcessingInstruction(target, data);
}
@Override
protected void skippedEntityInternal(String name) {
}
@Override
protected void startDocumentInternal() throws XMLStreamException {
streamWriter.writeStartDocument();
}
@Override
protected void startElementInternal(QName name, Attributes attributes, SimpleNamespaceContext namespaceContext)
throws XMLStreamException {
streamWriter.writeStartElement(QNameUtils.getPrefix(name), name.getLocalPart(), name.getNamespaceURI());
String defaultNamespaceUri = namespaceContext.getNamespaceURI("");
if (StringUtils.hasLength(defaultNamespaceUri)) {
streamWriter.writeNamespace("", defaultNamespaceUri);
streamWriter.setDefaultNamespace(defaultNamespaceUri);
}
for (Iterator<String> iterator = namespaceContext.getBoundPrefixes(); iterator.hasNext();) {
String prefix = iterator.next();
streamWriter.writeNamespace(prefix, namespaceContext.getNamespaceURI(prefix));
streamWriter.setPrefix(prefix, namespaceContext.getNamespaceURI(prefix));
}
for (int i = 0; i < attributes.getLength(); i++) {
QName attrName = QNameUtils.toQName(attributes.getURI(i), attributes.getQName(i));
String attrPrefix = QNameUtils.getPrefix(attrName);
if (!("xmlns".equals(attrName.getLocalPart()) || "xmlns".equals(attrPrefix))) {
streamWriter.writeAttribute(attrPrefix, attrName.getNamespaceURI(), attrName.getLocalPart(),
attributes.getValue(i));
}
}
}
}

View File

@@ -1,292 +0,0 @@
/*
* 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.stream;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.springframework.util.StringUtils;
import org.springframework.xml.namespace.QNameUtils;
import org.springframework.xml.namespace.SimpleNamespaceContext;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
/**
* SAX <code>XMLReader</code> that reads from a StAX <code>XMLStreamReader</code>. Reads from an
* <code>XMLStreamReader</code>, and calls the corresponding methods on the SAX callback interfaces.
*
* @author Arjen Poutsma
* @see XMLStreamReader
* @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
* @deprecated In favor of {@link org.springframework.util.xml.StaxUtils#createXMLReader(javax.xml.stream.XMLStreamReader)}
*/
@Deprecated
public class StaxStreamXmlReader extends AbstractStaxXmlReader {
private final XMLStreamReader reader;
private final SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
/**
* Constructs a new instance of the <code>StaxStreamXmlReader</code> that reads from the given
* <code>XMLStreamReader</code>. The supplied stream reader must be in <code>XMLStreamConstants.START_DOCUMENT</code>
* or <code>XMLStreamConstants.START_ELEMENT</code> state.
*
* @param reader the <code>XMLEventReader</code> to read from
* @throws IllegalStateException if the reader is not at the start of a document or element
*/
public StaxStreamXmlReader(XMLStreamReader reader) {
int event = reader.getEventType();
if (!(event == XMLStreamConstants.START_DOCUMENT || event == XMLStreamConstants.START_ELEMENT)) {
throw new IllegalStateException("XMLEventReader not at start of document or element");
}
this.reader = reader;
}
@Override
protected void parseInternal() throws SAXException, XMLStreamException {
boolean documentStarted = false;
boolean documentEnded = false;
int elementDepth = 0;
int eventType = reader.getEventType();
while (true) {
if (eventType != XMLStreamConstants.START_DOCUMENT && eventType != XMLStreamConstants.END_DOCUMENT &&
!documentStarted) {
handleStartDocument();
documentStarted = true;
}
switch (eventType) {
case XMLStreamConstants.START_ELEMENT:
elementDepth++;
handleStartElement();
break;
case XMLStreamConstants.END_ELEMENT:
elementDepth--;
if (elementDepth >= 0) {
handleEndElement();
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
handleProcessingInstruction();
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CDATA:
handleCharacters();
break;
case XMLStreamConstants.START_DOCUMENT:
setLocator(reader.getLocation());
handleStartDocument();
documentStarted = true;
break;
case XMLStreamConstants.END_DOCUMENT:
handleEndDocument();
documentEnded = true;
break;
case XMLStreamConstants.COMMENT:
handleComment();
break;
case XMLStreamConstants.DTD:
handleDtd();
break;
case XMLStreamConstants.ENTITY_REFERENCE:
handleEntityReference();
break;
}
if (reader.hasNext() && elementDepth >= 0) {
eventType = reader.next();
}
else {
break;
}
}
if (!documentEnded) {
handleEndDocument();
}
}
private void handleStartDocument() throws SAXException {
if (getContentHandler() != null) {
getContentHandler().startDocument();
if (reader.standaloneSet()) {
setStandalone(reader.isStandalone());
}
}
}
private void handleStartElement() throws SAXException {
if (getContentHandler() != null) {
QName qName = reader.getName();
if (hasNamespacesFeature()) {
for (int i = 0; i < reader.getNamespaceCount(); i++) {
String prefix = reader.getNamespacePrefix(i);
if (prefix == null) {
prefix = "";
}
startPrefixMapping(prefix, reader.getNamespaceURI(i));
}
for (int i = 0; i < reader.getAttributeCount(); i++) {
String prefix = reader.getAttributePrefix(i);
if (prefix == null) {
prefix = "";
}
String namespace = reader.getAttributeNamespace(i);
if (namespace == null) {
continue;
}
startPrefixMapping(prefix, namespace);
}
getContentHandler()
.startElement(qName.getNamespaceURI(), qName.getLocalPart(), QNameUtils.toQualifiedName(qName),
getAttributes());
}
else {
getContentHandler().startElement("", "", QNameUtils.toQualifiedName(qName), getAttributes());
}
}
}
private void startPrefixMapping(String prefix, String namespace) throws SAXException {
if (!namespaceContext.getNamespaceURI(prefix).equals(namespace)) {
getContentHandler().startPrefixMapping(prefix, namespace);
namespaceContext.bindNamespaceUri(prefix, namespace);
}
}
private void handleEndElement() throws SAXException {
if (getContentHandler() != null) {
QName qName = reader.getName();
if (hasNamespacesFeature()) {
getContentHandler()
.endElement(qName.getNamespaceURI(), qName.getLocalPart(), QNameUtils.toQualifiedName(qName));
for (int i = 0; i < reader.getNamespaceCount(); i++) {
String prefix = reader.getNamespacePrefix(i);
if (prefix == null) {
prefix = "";
}
endPrefixMapping(prefix);
}
}
else {
getContentHandler().endElement("", "", QNameUtils.toQualifiedName(qName));
}
}
}
private void endPrefixMapping(String prefix) throws SAXException {
if (namespaceContext.hasBinding(prefix)) {
getContentHandler().endPrefixMapping(prefix);
namespaceContext.removeBinding(prefix);
}
}
private void handleCharacters() throws SAXException {
if (getContentHandler() != null && reader.isWhiteSpace()) {
getContentHandler()
.ignorableWhitespace(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength());
return;
}
if (XMLStreamConstants.CDATA == reader.getEventType() && getLexicalHandler() != null) {
getLexicalHandler().startCDATA();
}
if (getContentHandler() != null) {
getContentHandler().characters(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength());
}
if (XMLStreamConstants.CDATA == reader.getEventType() && getLexicalHandler() != null) {
getLexicalHandler().endCDATA();
}
}
private void handleComment() throws SAXException {
if (getLexicalHandler() != null) {
getLexicalHandler().comment(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength());
}
}
private void handleDtd() throws SAXException {
if (getLexicalHandler() != null) {
javax.xml.stream.Location location = reader.getLocation();
getLexicalHandler().startDTD(null, location.getPublicId(), location.getSystemId());
}
if (getLexicalHandler() != null) {
getLexicalHandler().endDTD();
}
}
private void handleEntityReference() throws SAXException {
if (getLexicalHandler() != null) {
getLexicalHandler().startEntity(reader.getLocalName());
}
if (getLexicalHandler() != null) {
getLexicalHandler().endEntity(reader.getLocalName());
}
}
private void handleEndDocument() throws SAXException {
if (getContentHandler() != null) {
getContentHandler().endDocument();
}
}
private void handleProcessingInstruction() throws SAXException {
if (getContentHandler() != null) {
getContentHandler().processingInstruction(reader.getPITarget(), reader.getPIData());
}
}
private Attributes getAttributes() {
AttributesImpl attributes = new AttributesImpl();
for (int i = 0; i < reader.getAttributeCount(); i++) {
String namespace = reader.getAttributeNamespace(i);
if (namespace == null || !hasNamespacesFeature()) {
namespace = "";
}
String type = reader.getAttributeType(i);
if (type == null) {
type = "CDATA";
}
attributes.addAttribute(namespace, reader.getAttributeLocalName(i),
QNameUtils.toQualifiedName(reader.getAttributeName(i)), type, reader.getAttributeValue(i));
}
if (hasNamespacePrefixesFeature()) {
for (int i = 0; i < reader.getNamespaceCount(); i++) {
String prefix = reader.getNamespacePrefix(i);
String namespaceUri = reader.getNamespaceURI(i);
String qName;
if (StringUtils.hasLength(prefix)) {
qName = "xmlns:" + prefix;
}
else {
qName = "xmlns";
}
attributes.addAttribute("", "", qName, "CDATA", namespaceUri);
}
}
return attributes;
}
}

View File

@@ -1,256 +0,0 @@
/*
* 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.stream;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.events.ProcessingInstruction;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.XMLEvent;
/**
* Implementation of the <code>XMLStreamReader</code> interface that wraps a <code>XMLEventReader</code>. Useful,
* because the StAX <code>XMLInputFactory</code> allows one to create a event reader from a stream reader, but not
* vice-versa.
*
* @author Arjen Poutsma
* @since 1.0.0
* @deprecated In favor of {@link org.springframework.util.xml.StaxUtils#createEventStreamReader(XMLEventReader)}
*/
@Deprecated
public class XmlEventStreamReader extends AbstractXmlStreamReader {
private XMLEvent event;
private final XMLEventReader eventReader;
public XmlEventStreamReader(XMLEventReader eventReader) throws XMLStreamException {
this.eventReader = eventReader;
event = eventReader.nextEvent();
}
public boolean isStandalone() {
if (event.isStartDocument()) {
return ((StartDocument) event).isStandalone();
}
else {
throw new IllegalStateException();
}
}
public String getVersion() {
if (event.isStartDocument()) {
return ((StartDocument) event).getVersion();
}
else {
throw new IllegalStateException();
}
}
public int getTextStart() {
return 0;
}
public String getText() {
if (event.isCharacters()) {
return event.asCharacters().getData();
}
else {
throw new IllegalStateException();
}
}
public String getPITarget() {
if (event.isProcessingInstruction()) {
return ((ProcessingInstruction) event).getTarget();
}
else {
throw new IllegalStateException();
}
}
public String getPIData() {
if (event.isProcessingInstruction()) {
return ((ProcessingInstruction) event).getData();
}
else {
throw new IllegalStateException();
}
}
public int getNamespaceCount() {
Iterator<?> namespaces;
if (event.isStartElement()) {
namespaces = event.asStartElement().getNamespaces();
}
else if (event.isEndElement()) {
namespaces = event.asEndElement().getNamespaces();
}
else {
throw new IllegalStateException();
}
return countIterator(namespaces);
}
public NamespaceContext getNamespaceContext() {
if (event.isStartElement()) {
return event.asStartElement().getNamespaceContext();
}
else {
throw new IllegalStateException();
}
}
public QName getName() {
if (event.isStartElement()) {
return event.asStartElement().getName();
}
else if (event.isEndElement()) {
return event.asEndElement().getName();
}
else {
throw new IllegalStateException();
}
}
public Location getLocation() {
return event.getLocation();
}
public int getEventType() {
return event.getEventType();
}
public String getEncoding() {
return null;
}
public String getCharacterEncodingScheme() {
return null;
}
public int getAttributeCount() {
if (!event.isStartElement()) {
throw new IllegalStateException();
}
Iterator<?> attributes = event.asStartElement().getAttributes();
return countIterator(attributes);
}
public void close() throws XMLStreamException {
eventReader.close();
}
public QName getAttributeName(int index) {
return getAttribute(index).getName();
}
public String getAttributeType(int index) {
return getAttribute(index).getDTDType();
}
public String getAttributeValue(int index) {
return getAttribute(index).getValue();
}
public String getNamespacePrefix(int index) {
return getNamespace(index).getPrefix();
}
public String getNamespaceURI(int index) {
return getNamespace(index).getNamespaceURI();
}
public Object getProperty(String name) throws IllegalArgumentException {
return eventReader.getProperty(name);
}
public boolean isAttributeSpecified(int index) {
return getAttribute(index).isSpecified();
}
public int next() throws XMLStreamException {
event = eventReader.nextEvent();
return event.getEventType();
}
public boolean standaloneSet() {
if (event.isStartDocument()) {
return ((StartDocument) event).standaloneSet();
}
else {
throw new IllegalStateException();
}
}
private int countIterator(Iterator<?> iterator) {
int count = 0;
while (iterator.hasNext()) {
iterator.next();
count++;
}
return count;
}
private Attribute getAttribute(int index) {
if (!event.isStartElement()) {
throw new IllegalStateException();
}
int count = 0;
Iterator<?> attributes = event.asStartElement().getAttributes();
while (attributes.hasNext()) {
Attribute attribute = (Attribute) attributes.next();
if (count == index) {
return attribute;
}
else {
count++;
}
}
throw new IllegalArgumentException();
}
private Namespace getNamespace(int index) {
Iterator<?> namespaces;
if (event.isStartElement()) {
namespaces = event.asStartElement().getNamespaces();
}
else if (event.isEndElement()) {
namespaces = event.asEndElement().getNamespaces();
}
else {
throw new IllegalStateException();
}
int count = 0;
while (namespaces.hasNext()) {
Namespace namespace = (Namespace) namespaces.next();
if (count == index) {
return namespace;
}
else {
count++;
}
}
throw new IllegalArgumentException();
}
}

View File

@@ -1,5 +0,0 @@
<html>
<body>
Provides classes that help with StAX: the Streaming API for XML. Mostly for internal use by the framework.
</body>
</html>

View File

@@ -1,121 +0,0 @@
/*
* 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.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.sax.SAXResult;
import org.springframework.xml.stream.StaxEventContentHandler;
import org.springframework.xml.stream.StaxStreamContentHandler;
import org.xml.sax.ContentHandler;
/**
* Implementation of the <code>Result</code> tagging interface for StAX writers. Can be constructed with a
* <code>XMLEventConsumer</code> or a <code>XMLStreamWriter</code>.
* <p/>
* This class is necessary because there is no implementation of <code>Source</code> for StaxReaders in JAXP 1.3. There
* is a <code>StAXResult</code> in JAXP 1.4 (JDK 1.6), but this class is kept around for back-ward compatibility
* reasons.
* <p/>
* Even though <code>StaxResult</code> extends from <code>SAXResult</code>, calling the methods of
* <code>SAXResult</code> is <strong>not supported</strong>. In general, the only supported operation on this class is
* to use the <code>ContentHandler</code> obtained via {@link #getHandler()} to parse an input source using an
* <code>XMLReader</code>. Calling {@link #setHandler(org.xml.sax.ContentHandler)} will result in
* <code>UnsupportedOperationException</code>s.
*
* @author Arjen Poutsma
* @see XMLEventWriter
* @see XMLStreamWriter
* @see javax.xml.transform.Transformer
* @since 1.0.0
* @deprecated In favor of {@link org.springframework.util.xml.StaxUtils#createStaxResult(XMLEventWriter)} and {@link
* org.springframework.util.xml.StaxUtils#createStaxResult(XMLStreamWriter)}
*/
@Deprecated
public class StaxResult extends SAXResult {
private XMLEventWriter eventWriter;
private XMLStreamWriter streamWriter;
/**
* Constructs a new instance of the <code>StaxResult</code> with the specified <code>XMLStreamWriter</code>.
*
* @param streamWriter the <code>XMLStreamWriter</code> to write to
*/
public StaxResult(XMLStreamWriter streamWriter) {
super.setHandler(new StaxStreamContentHandler(streamWriter));
this.streamWriter = streamWriter;
}
/**
* Constructs a new instance of the <code>StaxResult</code> with the specified <code>XMLEventWriter</code>.
*
* @param eventWriter the <code>XMLEventWriter</code> to write to
*/
public StaxResult(XMLEventWriter eventWriter) {
super.setHandler(new StaxEventContentHandler(eventWriter));
this.eventWriter = eventWriter;
}
/**
* Constructs a new instance of the <code>StaxResult</code> with the specified <code>XMLEventWriter</code> and
* <code>XMLEventFactory</code>.
*
* @param eventWriter the <code>XMLEventWriter</code> to write to
* @param eventFactory the <code>XMLEventFactory</code> to use for creating events
*/
public StaxResult(XMLEventWriter eventWriter, XMLEventFactory eventFactory) {
super.setHandler(new StaxEventContentHandler(eventWriter, eventFactory));
this.eventWriter = eventWriter;
}
/**
* Returns the <code>XMLEventWriter</code> used by this <code>StaxResult</code>. If this <code>StaxResult</code> was
* created with an <code>XMLStreamWriter</code>, the result will be <code>null</code>.
*
* @return the StAX event writer used by this result
* @see #StaxResult(javax.xml.stream.XMLEventWriter)
*/
public XMLEventWriter getXMLEventWriter() {
return eventWriter;
}
/**
* Returns the <code>XMLStreamWriter</code> used by this <code>StaxResult</code>. If this <code>StaxResult</code>
* was created with an <code>XMLEventConsumer</code>, the result will be <code>null</code>.
*
* @return the StAX stream writer used by this result
* @see #StaxResult(javax.xml.stream.XMLStreamWriter)
*/
public XMLStreamWriter getXMLStreamWriter() {
return streamWriter;
}
/**
* Throws a <code>UnsupportedOperationException</code>.
*
* @throws UnsupportedOperationException always
*/
@Override
public void setHandler(ContentHandler handler) {
throw new UnsupportedOperationException("setHandler is not supported");
}
}

View File

@@ -1,125 +0,0 @@
/*
* 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.stream.XMLEventReader;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.sax.SAXSource;
import org.springframework.xml.stream.StaxEventXmlReader;
import org.springframework.xml.stream.StaxStreamXmlReader;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
/**
* Implementation of the <code>Source</code> tagging interface for StAX readers. Can be constructed with a
* <code>XMLEventReader</code> or a <code>XMLStreamReader</code>.
* <p/>
* This class is necessary because there is no implementation of <code>Source</code> for StAX Readers in JAXP 1.3. There
* is a <code>StAXSource</code> in JAXP 1.4 (JDK 1.6), but this class is kept around for back-ward compatibility
* reasons.
* <p/>
* Even though <code>StaxSource</code> extends from <code>SAXSource</code>, calling the methods of
* <code>SAXSource</code> is <strong>not supported</strong>. In general, the only supported operation on this class is
* to use the <code>XMLReader</code> obtained via {@link #getXMLReader()} to parse the input source obtained via {@link
* #getInputSource()}. Calling {@link #setXMLReader(org.xml.sax.XMLReader)} or {@link
* #setInputSource(org.xml.sax.InputSource)} will result in <code>UnsupportedOperationException</code>s.
*
* @author Arjen Poutsma
* @see XMLEventReader
* @see XMLStreamReader
* @see javax.xml.transform.Transformer
* @since 1.0.0
* @deprecated In favor of {@link org.springframework.util.xml.StaxUtils#createStaxSource(XMLEventReader)} and {@link
* org.springframework.util.xml.StaxUtils#createStaxSource(XMLStreamReader)}
*/
@Deprecated
public class StaxSource extends SAXSource {
private XMLEventReader eventReader;
private XMLStreamReader streamReader;
/**
* Constructs a new instance of the <code>StaxSource</code> with the specified <code>XMLStreamReader</code>. The
* supplied stream reader must be in <code>XMLStreamConstants.START_DOCUMENT</code> or
* <code>XMLStreamConstants.START_ELEMENT</code> state.
*
* @param streamReader the <code>XMLStreamReader</code> to read from
* @throws IllegalStateException if the reader is not at the start of a document or element
*/
public StaxSource(XMLStreamReader streamReader) {
super(new StaxStreamXmlReader(streamReader), new InputSource());
this.streamReader = streamReader;
}
/**
* Constructs a new instance of the <code>StaxSource</code> with the specified <code>XMLEventReader</code>. The
* supplied event reader must be in <code>XMLStreamConstants.START_DOCUMENT</code> or
* <code>XMLStreamConstants.START_ELEMENT</code> state.
*
* @param eventReader the <code>XMLEventReader</code> to read from
* @throws IllegalStateException if the reader is not at the start of a document or element
*/
public StaxSource(XMLEventReader eventReader) {
super(new StaxEventXmlReader(eventReader), new InputSource());
this.eventReader = eventReader;
}
/**
* Returns the <code>XMLEventReader</code> used by this <code>StaxSource</code>. If this <code>StaxSource</code> was
* created with an <code>XMLStreamReader</code>, the result will be <code>null</code>.
*
* @return the StAX event reader used by this source
* @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader)
*/
public XMLEventReader getXMLEventReader() {
return eventReader;
}
/**
* Returns the <code>XMLStreamReader</code> used by this <code>StaxSource</code>. If this <code>StaxSource</code>
* was created with an <code>XMLEventReader</code>, the result will be <code>null</code>.
*
* @return the StAX event reader used by this source
* @see StaxSource#StaxSource(javax.xml.stream.XMLEventReader)
*/
public XMLStreamReader getXMLStreamReader() {
return streamReader;
}
/**
* Throws a <code>UnsupportedOperationException</code>.
*
* @throws UnsupportedOperationException always
*/
@Override
public void setInputSource(InputSource inputSource) {
throw new UnsupportedOperationException("setInputSource is not supported");
}
/**
* Throws a <code>UnsupportedOperationException</code>.
*
* @throws UnsupportedOperationException always
*/
@Override
public void setXMLReader(XMLReader reader) {
throw new UnsupportedOperationException("setXMLReader is not supported");
}
}

View File

@@ -22,7 +22,6 @@ import java.io.Reader;
import java.io.Writer;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
@@ -54,131 +53,6 @@ import org.xml.sax.ext.LexicalHandler;
*/
public abstract class TraxUtils {
/**
* Indicates whether the given {@link Source} is a StAX Source.
*
* @return <code>true</code> if <code>source</code> is a Spring-WS {@link StaxSource} or JAXP 1.4 {@link
* StAXSource}; <code>false</code> otherwise.
* @deprecated In favor of {@link StaxUtils#isStaxSource(Source)}
*/
@Deprecated
public static boolean isStaxSource(Source source) {
if (source instanceof StaxSource) {
return true;
}
return StaxUtils.isStaxSource(source);
}
/**
* Indicates whether the given {@link Result} is a StAX Result.
*
* @return <code>true</code> if <code>result</code> is a Spring-WS {@link StaxResult} or JAXP 1.4 {@link
* StAXResult}; <code>false</code> otherwise.
* @deprecated In favor of {@link StaxUtils#isStaxResult(Result)}
*/
@Deprecated
public static boolean isStaxResult(Result result) {
if (result instanceof StaxResult) {
return true;
}
return StaxUtils.isStaxResult(result);
}
/**
* Returns the {@link XMLStreamReader} for the given StAX Source.
*
* @param source a Spring-WS {@link StaxSource} or {@link StAXSource}
* @return the {@link XMLStreamReader}
* @throws IllegalArgumentException if <code>source</code> is neither a Spring-WS {@link StaxSource} or {@link
* StAXSource}
* @deprecated In favor of {@link StaxUtils#getXMLStreamReader(Source)}
*/
@Deprecated
public static XMLStreamReader getXMLStreamReader(Source source) {
if (source instanceof StaxSource) {
return ((StaxSource) source).getXMLStreamReader();
}
return StaxUtils.getXMLStreamReader(source);
}
/**
* Returns the {@link XMLEventReader} for the given StAX Source.
*
* @param source a Spring-WS {@link StaxSource} or {@link StAXSource}
* @return the {@link XMLEventReader}
* @throws IllegalArgumentException if <code>source</code> is neither a Spring-WS {@link StaxSource} or {@link
* StAXSource}
* @deprecated In favor of {@link StaxUtils#getXMLEventReader(Source)}
*/
@Deprecated
public static XMLEventReader getXMLEventReader(Source source) {
if (source instanceof StaxSource) {
return ((StaxSource) source).getXMLEventReader();
}
return StaxUtils.getXMLEventReader(source);
}
/**
* Returns the {@link XMLStreamWriter} for the given StAX Result.
*
* @param result a Spring-WS {@link StaxResult} or {@link StAXResult}
* @return the {@link XMLStreamReader}
* @throws IllegalArgumentException if <code>source</code> is neither a Spring-WS {@link StaxResult} or {@link
* StAXResult}
* @deprecated In favor of {@link StaxUtils#getXMLStreamWriter(Result)}
*/
@Deprecated
public static XMLStreamWriter getXMLStreamWriter(Result result) {
if (result instanceof StaxResult) {
return ((StaxResult) result).getXMLStreamWriter();
}
return StaxUtils.getXMLStreamWriter(result);
}
/**
* Returns the {@link XMLEventWriter} for the given StAX Result.
*
* @param result a Spring-WS {@link StaxResult} or {@link StAXResult}
* @return the {@link XMLStreamReader}
* @throws IllegalArgumentException if <code>source</code> is neither a Spring-WS {@link StaxResult} or {@link
* StAXResult}
* @deprecated In favor of {@link StaxUtils#getXMLEventWriter(Result)}
*/
@Deprecated
public static XMLEventWriter getXMLEventWriter(Result result) {
if (result instanceof StaxResult) {
return ((StaxResult) result).getXMLEventWriter();
}
return StaxUtils.getXMLEventWriter(result);
}
/**
* Creates a StAX {@link Source} for the given {@link XMLStreamReader}. Returns a {@link StAXSource} under JAXP 1.4
* or higher, or a {@link StaxSource} otherwise.
*
* @param streamReader the StAX stream reader
* @return a source wrapping <code>streamReader</code>
* @deprecated In favor of {@link StaxUtils#createStaxSource(XMLStreamReader)}
*/
@Deprecated
public static Source createStaxSource(XMLStreamReader streamReader) {
return StaxUtils.createStaxSource(streamReader);
}
/**
* Creates a StAX {@link Source} for the given {@link XMLEventReader}. Returns a {@link StAXSource} under JAXP 1.4
* or higher, or a {@link StaxSource} otherwise.
*
* @param eventReader the StAX event reader
* @return a source wrapping <code>eventReader</code>
* @throws XMLStreamException in case of StAX errors
* @deprecated In favor of {@link StaxUtils#createStaxSource(XMLEventReader)}
*/
@Deprecated
public static Source createStaxSource(XMLEventReader eventReader) throws XMLStreamException {
return StaxUtils.createStaxSource(eventReader);
}
/**
* Returns the {@link Document} of the given {@link DOMSource}.
*
@@ -210,14 +84,14 @@ public abstract class TraxUtils {
callback.domSource(((DOMSource) source).getNode());
return;
}
else if (isStaxSource(source)) {
XMLStreamReader streamReader = getXMLStreamReader(source);
else if (StaxUtils.isStaxSource(source)) {
XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(source);
if (streamReader != null) {
callback.staxSource(streamReader);
return;
}
else {
XMLEventReader eventReader = getXMLEventReader(source);
XMLEventReader eventReader = StaxUtils.getXMLEventReader(source);
if (eventReader != null) {
callback.staxSource(eventReader);
return;
@@ -261,14 +135,14 @@ public abstract class TraxUtils {
callback.domResult(((DOMResult) result).getNode());
return;
}
else if (isStaxResult(result)) {
XMLStreamWriter streamWriter = getXMLStreamWriter(result);
else if (StaxUtils.isStaxResult(result)) {
XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(result);
if (streamWriter != null) {
callback.staxResult(streamWriter);
return;
}
else {
XMLEventWriter eventWriter = getXMLEventWriter(result);
XMLEventWriter eventWriter = StaxUtils.getXMLEventWriter(result);
if (eventWriter != null) {
callback.staxResult(eventWriter);
return;
@@ -324,7 +198,7 @@ public abstract class TraxUtils {
/**
* Perform an operation on the {@code XMLEventReader} contained in a JAXP 1.4 {@link StAXSource} or Spring
* {@link StaxSource}.
* {@link StaxUtils#createStaxSource StaxSource}.
*
* @param eventReader the reader
*/
@@ -332,7 +206,7 @@ public abstract class TraxUtils {
/**
* Perform an operation on the {@code XMLStreamReader} contained in a JAXP 1.4 {@link StAXSource} or Spring
* {@link StaxSource}.
* {@link StaxUtils#createStaxSource StaxSource}.
*
* @param streamReader the reader
*/
@@ -387,7 +261,7 @@ public abstract class TraxUtils {
/**
* Perform an operation on the {@code XMLEventWriter} contained in a JAXP 1.4 {@link StAXResult} or Spring
* {@link StaxResult}.
* {@link StaxUtils#createStaxResult StaxResult}.
*
* @param eventWriter the writer
*/
@@ -395,7 +269,7 @@ public abstract class TraxUtils {
/**
* Perform an operation on the {@code XMLStreamWriter} contained in a JAXP 1.4 {@link StAXResult} or Spring
* {@link StaxResult}.
* {@link StaxUtils#createStaxResult StaxResult}.
*
* @param streamWriter the writer
*/

View File

@@ -1,189 +0,0 @@
/*
* 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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.core.io.Resource;
import org.springframework.xml.sax.SaxUtils;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Internal class that uses JAXP 1.0 features to create <code>XmlValidator</code> instances.
*
* @author Arjen Poutsma
* @since 1.0.0
* @deprecated in favor of {@link Jaxp13ValidatorFactory}
*/
@Deprecated
abstract class Jaxp10ValidatorFactory {
private static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
private static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
static XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException {
InputSource[] inputSources = new InputSource[schemaResources.length];
for (int i = 0; i < schemaResources.length; i++) {
inputSources[i] = SaxUtils.createInputSource(schemaResources[i]);
}
return new Jaxp10Validator(inputSources, schemaLanguage);
}
private static class Jaxp10Validator implements XmlValidator {
private SAXParserFactory parserFactory;
private TransformerFactory transformerFactory;
private InputSource[] schemaInputSources;
private String schemaLanguage;
private Jaxp10Validator(InputSource[] schemaInputSources, String schemaLanguage) {
this.schemaInputSources = schemaInputSources;
this.schemaLanguage = schemaLanguage;
transformerFactory = TransformerFactory.newInstance();
parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
parserFactory.setValidating(true);
}
public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler)
throws IOException {
return validate(source);
}
public SAXParseException[] validate(Source source) throws IOException {
SAXParser parser = createSAXParser();
DefaultValidationErrorHandler errorHandler = new DefaultValidationErrorHandler();
try {
if (source instanceof SAXSource) {
validateSAXSource((SAXSource) source, parser, errorHandler);
}
else if (source instanceof StreamSource) {
validateStreamSource((StreamSource) source, parser, errorHandler);
}
else if (source instanceof DOMSource) {
validateDOMSource((DOMSource) source, parser, errorHandler);
}
else {
throw new IllegalArgumentException("Source [" + source.getClass().getName() +
"] is neither SAXSource, DOMSource, nor StreamSource");
}
return errorHandler.getErrors();
}
catch (SAXException ex) {
throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex);
}
}
private void validateDOMSource(DOMSource domSource, SAXParser parser, DefaultValidationErrorHandler errorHandler)
throws IOException, SAXException {
try {
// Sadly, JAXP 1.0 DOM doesn't implement DOM level 3, so we cannot use Document.normalizeDocument()
// Instead, we write the Document to a Stream, and validate that
Transformer transformer = transformerFactory.newTransformer();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
transformer.transform(domSource, new StreamResult(outputStream));
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
validateStreamSource(new StreamSource(inputStream), parser, errorHandler);
}
catch (TransformerException ex) {
throw new XmlValidationException("Could not validate DOM source: " + ex.getMessage(), ex);
}
}
private void validateStreamSource(StreamSource streamSource,
SAXParser parser,
DefaultValidationErrorHandler errorHandler) throws SAXException, IOException {
if (streamSource.getInputStream() != null) {
parser.parse(streamSource.getInputStream(), errorHandler);
}
else if (streamSource.getReader() != null) {
parser.parse(new InputSource(streamSource.getReader()), errorHandler);
}
else {
throw new IllegalArgumentException("StreamSource contains neither InputStream nor Reader");
}
}
private void validateSAXSource(SAXSource source, SAXParser parser, DefaultValidationErrorHandler errorHandler)
throws SAXException, IOException {
parser.parse(source.getInputSource(), errorHandler);
}
private SAXParser createSAXParser() {
try {
SAXParser parser = parserFactory.newSAXParser();
parser.setProperty(SCHEMA_LANGUAGE, schemaLanguage);
parser.setProperty(SCHEMA_SOURCE, schemaInputSources);
return parser;
}
catch (ParserConfigurationException ex) {
throw new XmlValidationException("Could not create SAXParser: " + ex.getMessage(), ex);
}
catch (SAXException ex) {
throw new XmlValidationException("Could not create SAXParser: " + ex.getMessage(), ex);
}
}
}
/** <code>DefaultHandler</code> extension that stores errors and fatal errors in a list. */
private static class DefaultValidationErrorHandler extends DefaultHandler {
private List<SAXParseException> errors = new ArrayList<SAXParseException>();
private SAXParseException[] getErrors() {
return errors.toArray(new SAXParseException[errors.size()]);
}
@Override
public void warning(SAXParseException ex) throws SAXException {
}
@Override
public void error(SAXParseException ex) throws SAXException {
errors.add(ex);
}
@Override
public void fatalError(SAXParseException ex) throws SAXException {
errors.add(ex);
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* 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
* 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,
@@ -33,8 +33,8 @@ 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.StaxSource;
import org.springframework.xml.transform.TransformerHelper;
import org.springframework.xml.transform.TraxUtils;
@@ -183,12 +183,12 @@ public class Jaxp13XPathTemplate extends AbstractXPathTemplate {
public void staxSource(XMLEventReader eventReader)
throws XPathExpressionException, XMLStreamException, TransformerException {
Element element = getRootElement(new StaxSource(eventReader));
Element element = getRootElement(StaxUtils.createCustomStaxSource(eventReader));
domSource(element);
}
public void staxSource(XMLStreamReader streamReader) throws TransformerException, XPathExpressionException {
Element element = getRootElement(new StaxSource(streamReader));
Element element = getRootElement(StaxUtils.createCustomStaxSource(streamReader));
domSource(element);
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* 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
* 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,
@@ -20,8 +20,6 @@ import java.util.Collections;
import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.xml.JaxpVersion;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -41,12 +39,6 @@ public abstract class XPathExpressionFactory {
private static final Log logger = LogFactory.getLog(XPathExpressionFactory.class);
private static boolean jaxp13Available = JaxpVersion.isAtLeastJaxp13();
private static boolean jaxenAvailable =
ClassUtils.isPresent("org.jaxen.XPath", XPathExpressionFactory.class.getClassLoader());
/**
* Create a compiled XPath expression using the given string.
*
@@ -76,24 +68,13 @@ public abstract class XPathExpressionFactory {
if (namespaces == null) {
namespaces = Collections.emptyMap();
}
if (jaxp13Available) {
try {
logger.trace("Creating [javax.xml.xpath.XPathExpression]");
return Jaxp13XPathExpressionFactory.createXPathExpression(expression, namespaces);
}
catch (XPathException e) {
throw e;
}
catch (Throwable e) {
jaxp13Available = false;
}
try {
logger.trace("Creating [javax.xml.xpath.XPathExpression]");
return Jaxp13XPathExpressionFactory.createXPathExpression(expression, namespaces);
}
if (jaxenAvailable) {
logger.trace("Creating [org.jaxen.XPath]");
return JaxenXPathExpressionFactory.createXPathExpression(expression, namespaces);
catch (XPathException e) {
throw e;
}
throw new IllegalStateException(
"Could not create XPathExpression: could not locate JAXP 1.3, or Jaxen on the class path");
}

View File

@@ -1,68 +0,0 @@
/*
* 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.stream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.stream.XMLStreamException;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
@SuppressWarnings("Since15")
public abstract class AbstractStaxContentHandlerTestCase {
private static final String XML_CONTENT_HANDLER =
"<?xml version='1.0' encoding='UTF-8'?><?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2' prefix:attr='value'>content</prefix:child></root>";
private XMLReader xmlReader;
@Before
public void setUp() throws Exception {
xmlReader = XMLReaderFactory.createXMLReader();
}
@Test
public void testContentHandler() throws Exception {
StringWriter stringWriter = new StringWriter();
AbstractStaxContentHandler handler = createStaxContentHandler(stringWriter);
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(new StringReader(XML_CONTENT_HANDLER)));
assertXMLEqual("Invalid result", XML_CONTENT_HANDLER, stringWriter.toString());
}
@Test
public void testContentHandlerNamespacePrefixes() throws Exception {
StringWriter stringWriter = new StringWriter();
AbstractStaxContentHandler handler = createStaxContentHandler(stringWriter);
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(new StringReader(XML_CONTENT_HANDLER)));
assertXMLEqual("Invalid result", XML_CONTENT_HANDLER, stringWriter.toString());
}
protected abstract AbstractStaxContentHandler createStaxContentHandler(Writer writer) throws XMLStreamException;
}

View File

@@ -1,372 +0,0 @@
/*
* 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.stream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.xml.sax.SaxUtils;
import org.easymock.AbstractMatcher;
import org.easymock.MockControl;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.XMLReaderFactory;
@SuppressWarnings("Since15")
public abstract class AbstractStaxXmlReaderTestCase {
protected static XMLInputFactory inputFactory;
private Resource testContentHandler;
private XMLReader standardReader;
private MockControl contentHandlerControl;
private ContentHandler contentHandler;
@Before
public void setUp() throws Exception {
inputFactory = XMLInputFactory.newInstance();
standardReader = XMLReaderFactory.createXMLReader();
contentHandlerControl = MockControl.createStrictControl(ContentHandler.class);
contentHandlerControl.setDefaultMatcher(new SaxArgumentMatcher());
ContentHandler contentHandlerMock = (ContentHandler) contentHandlerControl.getMock();
contentHandler = new CopyingContentHandler(contentHandlerMock);
standardReader.setContentHandler(contentHandler);
testContentHandler = new ClassPathResource("testContentHandler.xml", getClass());
}
@Test
public void testContentHandlerNamespacesNoPrefixes() throws SAXException, IOException, XMLStreamException {
standardReader.setFeature("http://xml.org/sax/features/namespaces", true);
standardReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
standardReader.parse(SaxUtils.createInputSource(testContentHandler));
contentHandlerControl.replay();
AbstractStaxXmlReader staxXmlReader = createStaxXmlReader(testContentHandler.getInputStream());
staxXmlReader.setFeature("http://xml.org/sax/features/namespaces", true);
staxXmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
staxXmlReader.setContentHandler(contentHandler);
staxXmlReader.parse(new InputSource());
contentHandlerControl.verify();
}
@Test
public void testContentHandlerNamespacesPrefixes() throws SAXException, IOException, XMLStreamException {
standardReader.setFeature("http://xml.org/sax/features/namespaces", true);
standardReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
standardReader.parse(SaxUtils.createInputSource(testContentHandler));
contentHandlerControl.replay();
AbstractStaxXmlReader staxXmlReader = createStaxXmlReader(testContentHandler.getInputStream());
staxXmlReader.setFeature("http://xml.org/sax/features/namespaces", true);
staxXmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
staxXmlReader.setContentHandler(contentHandler);
staxXmlReader.parse(new InputSource());
contentHandlerControl.verify();
}
@Test
public void testContentHandlerNoNamespacesPrefixes() throws SAXException, IOException, XMLStreamException {
standardReader.setFeature("http://xml.org/sax/features/namespaces", false);
standardReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
standardReader.parse(SaxUtils.createInputSource(testContentHandler));
contentHandlerControl.replay();
AbstractStaxXmlReader staxXmlReader = createStaxXmlReader(testContentHandler.getInputStream());
staxXmlReader.setFeature("http://xml.org/sax/features/namespaces", false);
staxXmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
staxXmlReader.setContentHandler(contentHandler);
staxXmlReader.parse(new InputSource());
contentHandlerControl.verify();
}
@Test
public void testLexicalHandler() throws SAXException, IOException, XMLStreamException {
MockControl lexicalHandlerControl = MockControl.createStrictControl(LexicalHandler.class);
lexicalHandlerControl.setDefaultMatcher(new SaxArgumentMatcher());
LexicalHandler lexicalHandlerMock = (LexicalHandler) lexicalHandlerControl.getMock();
LexicalHandler lexicalHandler = new CopyingLexicalHandler(lexicalHandlerMock);
Resource testLexicalHandlerXml = new ClassPathResource("testLexicalHandler.xml", getClass());
standardReader.setContentHandler(null);
standardReader.setProperty("http://xml.org/sax/properties/lexical-handler", lexicalHandler);
standardReader.parse(SaxUtils.createInputSource(testLexicalHandlerXml));
lexicalHandlerControl.replay();
inputFactory.setProperty("javax.xml.stream.isCoalescing", Boolean.FALSE);
inputFactory.setProperty("http://java.sun.com/xml/stream/properties/report-cdata-event", Boolean.TRUE);
inputFactory.setProperty("javax.xml.stream.isReplacingEntityReferences", Boolean.FALSE);
inputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", Boolean.FALSE);
AbstractStaxXmlReader staxXmlReader = createStaxXmlReader(testLexicalHandlerXml.getInputStream());
staxXmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", lexicalHandler);
staxXmlReader.parse(new InputSource());
lexicalHandlerControl.verify();
}
protected abstract AbstractStaxXmlReader createStaxXmlReader(InputStream inputStream) throws XMLStreamException;
/** Easymock <code>ArgumentMatcher</code> implementation that matches SAX arguments. */
protected static class SaxArgumentMatcher extends AbstractMatcher {
@Override
public boolean matches(Object[] expected, Object[] actual) {
if (expected == actual) {
return true;
}
if (expected == null || actual == null) {
return false;
}
if (expected.length != actual.length) {
throw new IllegalArgumentException("Expected and actual arguments must have the same size");
}
if (expected.length == 3 && expected[0] instanceof char[] && expected[1] instanceof Integer &&
expected[2] instanceof Integer) {
// handling of the character(char[], int, int) methods
String expectedString = new String((char[]) expected[0], (Integer) expected[1],
(Integer) expected[2]);
String actualString = new String((char[]) actual[0], (Integer) actual[1],
(Integer) actual[2]);
return expectedString.equals(actualString);
}
else if (expected.length == 1 && (expected[0] instanceof Locator)) {
return true;
}
else {
return super.matches(expected, actual);
}
}
@Override
protected boolean argumentMatches(Object expected, Object actual) {
if (expected instanceof char[]) {
return Arrays.equals((char[]) expected, (char[]) actual);
}
else if (expected instanceof Attributes) {
Attributes expectedAttributes = (Attributes) expected;
Attributes actualAttributes = (Attributes) actual;
if (expectedAttributes.getLength() != actualAttributes.getLength()) {
return false;
}
for (int i = 0; i < expectedAttributes.getLength(); i++) {
boolean found = false;
for (int j = 0; j < actualAttributes.getLength(); j++) {
if (expectedAttributes.getURI(i).equals(actualAttributes.getURI(j)) &&
expectedAttributes.getQName(i).equals(actualAttributes.getQName(j)) &&
expectedAttributes.getType(i).equals(actualAttributes.getType(j)) &&
expectedAttributes.getValue(i).equals(actualAttributes.getValue(j))) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}
else {
return super.argumentMatches(expected, actual);
}
}
@Override
public String toString(Object[] arguments) {
if (arguments != null && arguments.length == 3 && arguments[0] instanceof char[] &&
arguments[1] instanceof Integer && arguments[2] instanceof Integer) {
return new String((char[]) arguments[0], (Integer) arguments[1],
(Integer) arguments[2]);
}
else {
return super.toString(arguments);
}
}
@Override
protected String argumentToString(Object argument) {
if (argument instanceof char[]) {
char[] array = (char[]) argument;
StringBuilder builder = new StringBuilder();
for (char anArray : array) {
builder.append(anArray);
}
return builder.toString();
}
else if (argument instanceof Attributes) {
Attributes attributes = (Attributes) argument;
StringBuilder builder = new StringBuilder("[");
for (int i = 0; i < attributes.getLength(); i++) {
if (attributes.getURI(i).length() != 0) {
builder.append('{');
builder.append(attributes.getURI(i));
builder.append('}');
}
// if (attributes.getLocalName(i).length() != 0) {
// buffer.append('[');
// buffer.append(attributes.getLocalName(i));
// buffer.append(']');
// }
if (attributes.getQName(i).length() != 0) {
builder.append(attributes.getQName(i));
}
builder.append('=');
builder.append(attributes.getValue(i));
if (i < attributes.getLength() - 1) {
builder.append(", ");
}
}
builder.append(']');
return builder.toString();
}
else if (argument instanceof Locator) {
Locator locator = (Locator) argument;
StringBuilder builder = new StringBuilder("[");
builder.append(locator.getLineNumber());
builder.append(',');
builder.append(locator.getColumnNumber());
builder.append(']');
return builder.toString();
}
else {
return super.argumentToString(argument);
}
}
}
private static class CopyingContentHandler implements ContentHandler {
private final ContentHandler wrappee;
private CopyingContentHandler(ContentHandler wrappee) {
this.wrappee = wrappee;
}
public void setDocumentLocator(Locator locator) {
wrappee.setDocumentLocator(locator);
}
public void startDocument() throws SAXException {
wrappee.startDocument();
}
public void endDocument() throws SAXException {
wrappee.endDocument();
}
public void startPrefixMapping(String prefix, String uri) throws SAXException {
wrappee.startPrefixMapping(prefix, uri);
}
public void endPrefixMapping(String prefix) throws SAXException {
wrappee.endPrefixMapping(prefix);
}
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
wrappee.startElement(uri, localName, qName, new AttributesImpl(attributes));
}
public void endElement(String uri, String localName, String qName) throws SAXException {
wrappee.endElement(uri, localName, qName);
}
public void characters(char ch[], int start, int length) throws SAXException {
wrappee.characters(copy(ch), start, length);
}
public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
}
public void processingInstruction(String target, String data) throws SAXException {
wrappee.processingInstruction(target, data);
}
public void skippedEntity(String name) throws SAXException {
wrappee.skippedEntity(name);
}
}
private static class CopyingLexicalHandler implements LexicalHandler {
private final LexicalHandler wrappee;
private CopyingLexicalHandler(LexicalHandler wrappee) {
this.wrappee = wrappee;
}
public void startDTD(String name, String publicId, String systemId) throws SAXException {
wrappee.startDTD("element", publicId, systemId);
}
public void endDTD() throws SAXException {
wrappee.endDTD();
}
public void startEntity(String name) throws SAXException {
wrappee.startEntity(name);
}
public void endEntity(String name) throws SAXException {
wrappee.endEntity(name);
}
public void startCDATA() throws SAXException {
wrappee.startCDATA();
}
public void endCDATA() throws SAXException {
wrappee.endCDATA();
}
public void comment(char ch[], int start, int length) throws SAXException {
wrappee.comment(copy(ch), start, length);
}
}
private static char[] copy(char[] ch) {
char[] copy = new char[ch.length];
System.arraycopy(ch, 0, copy, 0, ch.length);
return copy;
}
}

View File

@@ -1,31 +0,0 @@
/*
* 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.stream;
import java.io.Writer;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
@SuppressWarnings("Since15")
public class StaxEventContentHandlerTest extends AbstractStaxContentHandlerTestCase {
@Override
protected AbstractStaxContentHandler createStaxContentHandler(Writer writer) throws XMLStreamException {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
return new StaxEventContentHandler(outputFactory.createXMLEventWriter(writer));
}
}

View File

@@ -1,64 +0,0 @@
/*
* 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.stream;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import org.easymock.MockControl;
import org.junit.Test;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.AttributesImpl;
@SuppressWarnings("Since15")
public class StaxEventXmlReaderTest extends AbstractStaxXmlReaderTestCase {
public static final String CONTENT = "<root xmlns='http://springframework.org/spring-ws'><child/></root>";
@Override
protected AbstractStaxXmlReader createStaxXmlReader(InputStream inputStream) throws XMLStreamException {
return new StaxEventXmlReader(inputFactory.createXMLEventReader(inputStream));
}
@Test
public void testPartial() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(CONTENT));
eventReader.nextTag(); // skip to root
StaxEventXmlReader xmlReader = new StaxEventXmlReader(eventReader);
MockControl mockControl = MockControl.createStrictControl(ContentHandler.class);
mockControl.setDefaultMatcher(new SaxArgumentMatcher());
ContentHandler contentHandlerMock = (ContentHandler) mockControl.getMock();
contentHandlerMock.startDocument();
contentHandlerMock.startElement("http://springframework.org/spring-ws", "child", "child", new AttributesImpl());
contentHandlerMock.endElement("http://springframework.org/spring-ws", "child", "child");
contentHandlerMock.endDocument();
xmlReader.setContentHandler(contentHandlerMock);
mockControl.replay();
xmlReader.parse(new InputSource());
mockControl.verify();
}
}

View File

@@ -1,31 +0,0 @@
/*
* 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.stream;
import java.io.Writer;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
@SuppressWarnings("Since15")
public class StaxStreamContentHandlerTest extends AbstractStaxContentHandlerTestCase {
@Override
protected AbstractStaxContentHandler createStaxContentHandler(Writer writer) throws XMLStreamException {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
return new StaxStreamContentHandler(outputFactory.createXMLStreamWriter(writer));
}
}

View File

@@ -1,72 +0,0 @@
/*
* 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.stream;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.easymock.MockControl;
import org.junit.Test;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.AttributesImpl;
import static org.junit.Assert.assertEquals;
@SuppressWarnings("Since15")
public class StaxStreamXmlReaderTest extends AbstractStaxXmlReaderTestCase {
public static final String CONTENT = "<root xmlns='http://springframework.org/spring-ws'><child/></root>";
@Override
protected AbstractStaxXmlReader createStaxXmlReader(InputStream inputStream) throws XMLStreamException {
return new StaxStreamXmlReader(inputFactory.createXMLStreamReader(inputStream));
}
@Test
public void testPartial() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(CONTENT));
streamReader.nextTag(); // skip to root
assertEquals("Invalid element", new QName("http://springframework.org/spring-ws", "root"),
streamReader.getName());
streamReader.nextTag(); // skip to child
assertEquals("Invalid element", new QName("http://springframework.org/spring-ws", "child"),
streamReader.getName());
StaxStreamXmlReader xmlReader = new StaxStreamXmlReader(streamReader);
MockControl mockControl = MockControl.createStrictControl(ContentHandler.class);
mockControl.setDefaultMatcher(new SaxArgumentMatcher());
ContentHandler contentHandlerMock = (ContentHandler) mockControl.getMock();
contentHandlerMock.startDocument();
contentHandlerMock.startElement("http://springframework.org/spring-ws", "child", "child", new AttributesImpl());
contentHandlerMock.endElement("http://springframework.org/spring-ws", "child", "child");
contentHandlerMock.endDocument();
xmlReader.setContentHandler(contentHandlerMock);
mockControl.replay();
xmlReader.parse(new InputSource());
mockControl.verify();
}
}

View File

@@ -1,64 +0,0 @@
/*
* 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.stream;
import java.io.StringReader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import org.springframework.xml.transform.StaxSource;
import org.springframework.xml.transform.StringResult;
import org.junit.Before;
import org.junit.Test;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
@SuppressWarnings("Since15")
public class XmlEventStreamReaderTest {
private static final String XML =
"<?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2'>content</prefix:child></root>";
private XmlEventStreamReader streamReader;
@Before
public void setUp() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
streamReader = new XmlEventStreamReader(eventReader);
}
@Test
public void testReadAll() throws Exception {
while (streamReader.hasNext()) {
streamReader.next();
}
}
@Test
public void testReadCorrect() throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StaxSource source = new StaxSource(streamReader);
StringResult result = new StringResult();
transformer.transform(source, result);
assertXMLEqual(XML, result.toString());
}
}

View File

@@ -1,73 +0,0 @@
/*
* 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.StringWriter;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
@SuppressWarnings("Since15")
public class StaxResultTest {
private static final String XML = "<root xmlns='namespace'><child/></root>";
private Transformer transformer;
private XMLOutputFactory inputFactory;
@Before
public void setUp() throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
inputFactory = XMLOutputFactory.newInstance();
}
@Test
public void testStreamWriterSource() throws Exception {
StringWriter stringWriter = new StringWriter();
XMLStreamWriter streamWriter = inputFactory.createXMLStreamWriter(stringWriter);
Source source = new StringSource(XML);
StaxResult result = new StaxResult(streamWriter);
Assert.assertEquals("Invalid streamWriter returned", streamWriter, result.getXMLStreamWriter());
Assert.assertNull("EventWriter returned", result.getXMLEventWriter());
transformer.transform(source, result);
assertXMLEqual("Invalid result", XML, stringWriter.toString());
}
@Test
public void testEventWriterSource() throws Exception {
StringWriter stringWriter = new StringWriter();
XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter);
Source source = new StringSource(XML);
StaxResult result = new StaxResult(eventWriter);
Assert.assertEquals("Invalid eventWriter returned", eventWriter, result.getXMLEventWriter());
Assert.assertNull("StreamWriter returned", result.getXMLStreamWriter());
transformer.transform(source, result);
assertXMLEqual("Invalid result", XML, stringWriter.toString());
}
}

View File

@@ -1,70 +0,0 @@
/*
* 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.StringReader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
@SuppressWarnings("Since15")
public class StaxSourceTest {
private static final String XML = "<root xmlns='namespace'><child/></root>";
private Transformer transformer;
private XMLInputFactory inputFactory;
@Before
public void setUp() throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
inputFactory = XMLInputFactory.newInstance();
}
@Test
public void testStreamReaderSource() throws Exception {
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
StaxSource source = new StaxSource(streamReader);
Assert.assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
Assert.assertNull("EventReader returned", source.getXMLEventReader());
Result result = new StringResult();
transformer.transform(source, result);
assertXMLEqual("Invalid result", XML, result.toString());
}
@Test
public void testEventReaderSource() throws Exception {
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
StaxSource source = new StaxSource(eventReader);
Assert.assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
Assert.assertNull("StreamReader returned", source.getXMLStreamReader());
Result result = new StringResult();
transformer.transform(source, result);
assertXMLEqual("Invalid result", XML, result.toString());
}
}

View File

@@ -1,34 +0,0 @@
/*
* 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 org.springframework.core.io.Resource;
public class Jaxp10ValidatorFactoryTest extends AbstractValidatorFactoryTestCase {
@Override
protected XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException {
return Jaxp10ValidatorFactory.createValidator(schemaResources, schemaLanguage);
}
@Override
public void customErrorHandler() throws Exception {
// Not supported on JAXP 1.0
}
}

View File

@@ -1 +0,0 @@
<h:hello xmlns:h="http://www.greeting.com/hello/" id="a1" h:person="David"><goodbye xmlns="http://www.greeting.com/goodbye/" h:person="Arjen"/></h:hello>

View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE element
[<!ENTITY entity "entity">]>
<!--Comment-->
<element>
<cdata><![CDATA[cdata]]></cdata>
<entity>&entity;</entity>
</element>