提交 9aafa1c6 编写于 作者: A Arjen Poutsma

Various StAX improvements.

上级 b72cca54
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -20,7 +20,6 @@ import javax.xml.namespace.QName; ...@@ -20,7 +20,6 @@ import javax.xml.namespace.QName;
import javax.xml.stream.Location; import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.Locator; import org.xml.sax.Locator;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
...@@ -108,17 +107,6 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader { ...@@ -108,17 +107,6 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
return this.namespacePrefixesFeature; return this.namespacePrefixesFeature;
} }
/**
* Sett 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));
}
}
/** /**
* Convert a <code>QName</code> to a qualified name, as used by DOM and SAX. * Convert a <code>QName</code> to a qualified name, as used by DOM and SAX.
* The returned string has a format of <code>prefix:localName</code> if the * The returned string has a format of <code>prefix:localName</code> if the
......
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -18,6 +18,7 @@ package org.springframework.util.xml; ...@@ -18,6 +18,7 @@ package org.springframework.util.xml;
import java.util.Iterator; import java.util.Iterator;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
...@@ -31,11 +32,13 @@ import javax.xml.stream.events.EntityReference; ...@@ -31,11 +32,13 @@ import javax.xml.stream.events.EntityReference;
import javax.xml.stream.events.Namespace; import javax.xml.stream.events.Namespace;
import javax.xml.stream.events.NotationDeclaration; import javax.xml.stream.events.NotationDeclaration;
import javax.xml.stream.events.ProcessingInstruction; import javax.xml.stream.events.ProcessingInstruction;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement; import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent; import javax.xml.stream.events.XMLEvent;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.ext.Locator2;
import org.xml.sax.helpers.AttributesImpl; import org.xml.sax.helpers.AttributesImpl;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -86,10 +89,14 @@ class StaxEventXMLReader extends AbstractStaxXMLReader { ...@@ -86,10 +89,14 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
while (reader.hasNext() && elementDepth >= 0) { while (reader.hasNext() && elementDepth >= 0) {
XMLEvent event = reader.nextEvent(); XMLEvent event = reader.nextEvent();
if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) { if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
handleStartDocument(); handleStartDocument(event);
documentStarted = true; documentStarted = true;
} }
switch (event.getEventType()) { switch (event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
handleStartDocument(event);
documentStarted = true;
break;
case XMLStreamConstants.START_ELEMENT: case XMLStreamConstants.START_ELEMENT:
elementDepth++; elementDepth++;
handleStartElement(event.asStartElement()); handleStartElement(event.asStartElement());
...@@ -108,11 +115,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader { ...@@ -108,11 +115,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
case XMLStreamConstants.CDATA: case XMLStreamConstants.CDATA:
handleCharacters(event.asCharacters()); handleCharacters(event.asCharacters());
break; break;
case XMLStreamConstants.START_DOCUMENT:
setLocator(event.getLocation());
handleStartDocument();
documentStarted = true;
break;
case XMLStreamConstants.END_DOCUMENT: case XMLStreamConstants.END_DOCUMENT:
handleEndDocument(); handleEndDocument();
documentEnded = true; documentEnded = true;
...@@ -140,6 +142,51 @@ class StaxEventXMLReader extends AbstractStaxXMLReader { ...@@ -140,6 +142,51 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
} }
private void handleStartDocument(final XMLEvent event) throws SAXException {
if (getContentHandler() != null) {
final Location location = event.getLocation();
getContentHandler().setDocumentLocator(new Locator2() {
public int getColumnNumber() {
return location.getColumnNumber();
}
public int getLineNumber() {
return location.getLineNumber();
}
public String getPublicId() {
return location.getPublicId();
}
public String getSystemId() {
return location.getSystemId();
}
public String getXMLVersion() {
if (event.isStartDocument()) {
StartDocument startDocument = (StartDocument) event;
String version = startDocument.getVersion();
return StringUtils.hasLength(version) ? version : "1.0";
}
return null;
}
public String getEncoding() {
if (event.isStartDocument()) {
StartDocument startDocument = (StartDocument) event;
if (startDocument.encodingSet()) {
return startDocument.getCharacterEncodingScheme();
}
}
return null;
}
});
getContentHandler().startDocument();
}
}
private void handleStartElement(StartElement startElement) throws SAXException { private void handleStartElement(StartElement startElement) throws SAXException {
if (getContentHandler() != null) { if (getContentHandler() != null) {
QName qName = startElement.getName(); QName qName = startElement.getName();
...@@ -174,12 +221,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader { ...@@ -174,12 +221,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
} }
} }
private void handleEndDocument() throws SAXException {
if (getContentHandler() != null) {
getContentHandler().endDocument();
}
}
private void handleEndElement(EndElement endElement) throws SAXException { private void handleEndElement(EndElement endElement) throws SAXException {
if (getContentHandler() != null) { if (getContentHandler() != null) {
QName qName = endElement.getName(); QName qName = endElement.getName();
...@@ -197,6 +238,12 @@ class StaxEventXMLReader extends AbstractStaxXMLReader { ...@@ -197,6 +238,12 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
} }
} }
private void handleEndDocument() throws SAXException {
if (getContentHandler() != null) {
getContentHandler().endDocument();
}
}
private void handleNotationDeclaration(NotationDeclaration declaration) throws SAXException { private void handleNotationDeclaration(NotationDeclaration declaration) throws SAXException {
if (getDTDHandler() != null) { if (getDTDHandler() != null) {
getDTDHandler().notationDecl(declaration.getName(), declaration.getPublicId(), declaration.getSystemId()); getDTDHandler().notationDecl(declaration.getName(), declaration.getPublicId(), declaration.getSystemId());
...@@ -216,12 +263,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader { ...@@ -216,12 +263,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
} }
} }
private void handleStartDocument() throws SAXException {
if (getContentHandler() != null) {
getContentHandler().startDocument();
}
}
private void handleComment(Comment comment) throws SAXException { private void handleComment(Comment comment) throws SAXException {
if (getLexicalHandler() != null) { if (getLexicalHandler() != null) {
char[] ch = comment.getText().toCharArray(); char[] ch = comment.getText().toCharArray();
......
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -17,12 +17,14 @@ ...@@ -17,12 +17,14 @@
package org.springframework.util.xml; package org.springframework.util.xml;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamReader;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.xml.sax.ext.Locator2;
import org.xml.sax.helpers.AttributesImpl; import org.xml.sax.helpers.AttributesImpl;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -91,7 +93,6 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader { ...@@ -91,7 +93,6 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
handleCharacters(); handleCharacters();
break; break;
case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.START_DOCUMENT:
setLocator(reader.getLocation());
handleStartDocument(); handleStartDocument();
documentStarted = true; documentStarted = true;
break; break;
...@@ -123,6 +124,35 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader { ...@@ -123,6 +124,35 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
private void handleStartDocument() throws SAXException { private void handleStartDocument() throws SAXException {
if (getContentHandler() != null) { if (getContentHandler() != null) {
final Location location = reader.getLocation();
getContentHandler().setDocumentLocator(new Locator2() {
public int getColumnNumber() {
return location.getColumnNumber();
}
public int getLineNumber() {
return location.getLineNumber();
}
public String getPublicId() {
return location.getPublicId();
}
public String getSystemId() {
return location.getSystemId();
}
public String getXMLVersion() {
String version = reader.getVersion();
return StringUtils.hasLength(version) ? version : "1.0";
}
public String getEncoding() {
return reader.getEncoding();
}
});
getContentHandler().startDocument(); getContentHandler().startDocument();
if (reader.standaloneSet()) { if (reader.standaloneSet()) {
setStandalone(reader.isStandalone()); setStandalone(reader.isStandalone());
......
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -61,7 +61,7 @@ class XMLEventStreamReader extends AbstractXMLStreamReader { ...@@ -61,7 +61,7 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
return ((StartDocument) event).getVersion(); return ((StartDocument) event).getVersion();
} }
else { else {
throw new IllegalStateException(); return null;
} }
} }
......
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -18,16 +18,21 @@ package org.springframework.util.xml; ...@@ -18,16 +18,21 @@ package org.springframework.util.xml;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import static org.custommonkey.xmlunit.XMLAssert.*; import static org.custommonkey.xmlunit.XMLAssert.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class StaxSourceTests { public class StaxSourceTests {
...@@ -37,15 +42,20 @@ public class StaxSourceTests { ...@@ -37,15 +42,20 @@ public class StaxSourceTests {
private XMLInputFactory inputFactory; private XMLInputFactory inputFactory;
private DocumentBuilder documentBuilder;
@Before @Before
public void createsetUp() throws Exception { public void setUp() throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance(); TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer(); transformer = transformerFactory.newTransformer();
inputFactory = XMLInputFactory.newInstance(); inputFactory = XMLInputFactory.newInstance();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
} }
@Test @Test
public void streamReaderSource() throws Exception { public void streamReaderSourceToStreamResult() throws Exception {
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML)); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
StaxSource source = new StaxSource(streamReader); StaxSource source = new StaxSource(streamReader);
assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader()); assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
...@@ -56,7 +66,20 @@ public class StaxSourceTests { ...@@ -56,7 +66,20 @@ public class StaxSourceTests {
} }
@Test @Test
public void eventReaderSource() throws Exception { public void streamReaderSourceToDOMResult() throws Exception {
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
StaxSource source = new StaxSource(streamReader);
assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
assertNull("EventReader returned", source.getXMLEventReader());
Document expected = documentBuilder.parse(new InputSource(new StringReader(XML)));
Document result = documentBuilder.newDocument();
transformer.transform(source, new DOMResult(result));
assertXMLEqual("Invalid result", expected, result);
}
@Test
public void eventReaderSourceToStreamResult() throws Exception {
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML)); XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
StaxSource source = new StaxSource(eventReader); StaxSource source = new StaxSource(eventReader);
assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader()); assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
...@@ -65,4 +88,17 @@ public class StaxSourceTests { ...@@ -65,4 +88,17 @@ public class StaxSourceTests {
transformer.transform(source, new StreamResult(writer)); transformer.transform(source, new StreamResult(writer));
assertXMLEqual("Invalid result", XML, writer.toString()); assertXMLEqual("Invalid result", XML, writer.toString());
} }
@Test
public void eventReaderSourceToDOMResult() throws Exception {
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
StaxSource source = new StaxSource(eventReader);
assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
assertNull("StreamReader returned", source.getXMLStreamReader());
Document expected = documentBuilder.parse(new InputSource(new StringReader(XML)));
Document result = documentBuilder.newDocument();
transformer.transform(source, new DOMResult(result));
assertXMLEqual("Invalid result", expected, result);
}
} }
\ No newline at end of file
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -24,7 +24,7 @@ import javax.xml.stream.XMLStreamException; ...@@ -24,7 +24,7 @@ import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamReader;
import org.easymock.MockControl; import org.easymock.MockControl;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.xml.sax.ContentHandler; import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
...@@ -55,6 +55,8 @@ public class StaxStreamXMLReaderTests extends AbstractStaxXMLReaderTestCase { ...@@ -55,6 +55,8 @@ public class StaxStreamXMLReaderTests extends AbstractStaxXMLReaderTestCase {
mockControl.setDefaultMatcher(new SaxArgumentMatcher()); mockControl.setDefaultMatcher(new SaxArgumentMatcher());
ContentHandler contentHandlerMock = (ContentHandler) mockControl.getMock(); ContentHandler contentHandlerMock = (ContentHandler) mockControl.getMock();
contentHandlerMock.setDocumentLocator(null);
mockControl.setMatcher(MockControl.ALWAYS_MATCHER);
contentHandlerMock.startDocument(); contentHandlerMock.startDocument();
contentHandlerMock.startElement("http://springframework.org/spring-ws", "child", "child", new AttributesImpl()); contentHandlerMock.startElement("http://springframework.org/spring-ws", "child", "child", new AttributesImpl());
contentHandlerMock.endElement("http://springframework.org/spring-ws", "child", "child"); contentHandlerMock.endElement("http://springframework.org/spring-ws", "child", "child");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册