Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add revised version of the JDOMStreamReader together with some implem…

…entation details for JDOM integration, and a round-trip test
  • Loading branch information...
commit 9b019b13ed0d6c7839dc23911d940ef0d5736dc2 1 parent c67fdb2
@rolfl rolfl authored
View
5 core/src/java/org/jdom2/jaxb/JDOMNamespaceContext.java
@@ -62,6 +62,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
import org.jdom2.JDOMConstants;
import org.jdom2.Namespace;
+import org.jdom2.internal.ArrayCopy;
/**
* A Read-Only {@link NamespaceContext} that describes namespaces found
@@ -76,11 +77,11 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* Create a read-only representation of the input namespace list.
* @param namespaces the Namespace instances to represent.
*/
- public JDOMNamespaceContext(final List<Namespace> namespaces){
+ public JDOMNamespaceContext(final Namespace[] namespaces){
if (namespaces == null) {
throw new IllegalArgumentException("Cannot process a null Namespace list");
}
- this.namespacearray = namespaces.toArray(new Namespace[0]);
+ this.namespacearray = ArrayCopy.copyOf(namespaces, namespaces.length);
for (int i = 1; i < namespacearray.length; i++) {
final Namespace n = namespacearray[i];
if (n == null) {
View
762 core/src/java/org/jdom2/jaxb/JDOMStreamReader.java
@@ -55,13 +55,17 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
package org.jdom2.jaxb;
import java.util.Iterator;
+import java.util.NoSuchElementException;
+
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
+import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
+
import org.jdom2.Attribute;
import org.jdom2.Content;
import org.jdom2.DocType;
@@ -70,7 +74,15 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
import org.jdom2.EntityRef;
import org.jdom2.Namespace;
import org.jdom2.ProcessingInstruction;
-import org.jdom2.Text;
+import org.jdom2.Verifier;
+import org.jdom2.internal.ArrayCopy;
+import org.jdom2.output.Format;
+import org.jdom2.output.Format.TextMode;
+import org.jdom2.output.XMLOutputter;
+import org.jdom2.output.support.AbstractOutputProcessor;
+import org.jdom2.output.support.FormatStack;
+import org.jdom2.output.support.Walker;
+import org.jdom2.util.NamespaceStack;
/**
* An {@link XMLStreamReader} implementation that reads the XML document
@@ -80,92 +92,317 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* events as it encounters them in the DOM.
* @author gordon burgett https://github.com/gburgett
*/
-public class JDOMStreamReader implements XMLStreamReader {
-
+public class JDOMStreamReader extends AbstractOutputProcessor implements XMLStreamReader {
+
+ private static final class DocumentWalker implements Walker {
+
+ private final Content[] data;
+ private int pos = 0;
+
+ public DocumentWalker(final Document doc) {
+ data = doc.getContent().toArray(new Content[doc.getContentSize()]);
+ }
+
+ @Override
+ public boolean isAllText() {
+ return false;
+ }
+
+ @Override
+ public boolean isAllWhitespace() {
+ return false;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return pos < data.length;
+ }
+
+ @Override
+ public Content next() {
+ return data[pos++];
+ }
+
+ @Override
+ public String text() {
+ return null;
+ }
+
+ @Override
+ public boolean isCDATA() {
+ return false;
+ }
+
+ }
+
+ private final FormatStack formatstack;
+ private final NamespaceStack nsstack = new NamespaceStack();
+
private Document document;
- private Element root;
- private DomWalkingContentIterator rootIterator;
- private ReaderState state = ReaderState.RS_START_DOCUMENT;
- private int currentEvt = START_DOCUMENT;
+ private String curi = null, clocalname = null, cprefix = null,
+ ctext = null, ctarget = null, cdata = null;
+ private Element [] emtstack = new Element[32];
+ private Walker[] stack = new Walker[32];
+ private int depth = 0;
- private Content getCurrentContent(){
- Content ret = rootIterator.getCurrentContent();
- if(ret == null){
- return root;
- }
- return ret;
- }
+ private int currentEvt = START_DOCUMENT;
/**
* Create a new JDOMStreamReader that outputs a JDOM Document as an XMLStream.
* @param document the document to output.
+ * @param format The output format to use.
*/
- public JDOMStreamReader(Document document){
+ public JDOMStreamReader(Document document, Format format){
this.document = document;
- this.root = document.getRootElement();
+ this.formatstack = new FormatStack(format);
+ stack[0] = new DocumentWalker(document);
+ }
+
+ /**
+ * Create a new JDOMStreamReader that outputs a JDOM Document as an XMLStream using
+ * the Format.getRawFormat() format.
+ * @param document the document to output.
+ */
+ public JDOMStreamReader(Document document) {
+ this(document, Format.getRawFormat());
}
@Override
- public Object getProperty(String name) throws IllegalArgumentException {
- return null;
+ public boolean hasNext() throws XMLStreamException {
+ return depth >= 0;
}
@Override
public int next() throws XMLStreamException {
- switch(state){
- case RS_START_DOCUMENT:
- state = ReaderState.RS_START_ROOT_ELEMENT;
- return currentEvt = START_DOCUMENT;
-
- case RS_START_ROOT_ELEMENT:
- state = ReaderState.RS_WALKING_TREE;
- this.rootIterator = new DomWalkingContentIterator(root);
- return currentEvt = START_ELEMENT;
-
- case RS_WALKING_TREE:
- if(this.rootIterator.hasNext()){
- return currentEvt = this.rootIterator.next();
- }
- state = ReaderState.RS_END_ROOT_ELEMENT;
- return currentEvt = END_ELEMENT;
-
- case RS_END_ROOT_ELEMENT:
- state = ReaderState.RS_END_DOCUMENT;
- return currentEvt = END_DOCUMENT;
-
- default:
- throw new IllegalStateException("Reader does not have next");
- }
+ if (depth < 0) {
+ throw new NoSuchElementException("No more data available.");
+ }
+
+ curi = null;
+ clocalname = null;
+ cprefix = null;
+ ctext = null;
+ ctarget = null;
+ cdata = null;
+
+ if (currentEvt == END_ELEMENT) {
+ nsstack.pop();
+ formatstack.pop();
+ emtstack[depth + 1] = null;
+ }
+
+ // confirm next walker item.
+ if (!stack[depth].hasNext()) {
+ // no more items at this level.
+ stack[depth] = null;
+ // we kill the element stack at the end of the END_ELEMENT event.
+ // emtstack[depth] = null
+ depth--;
+ return currentEvt = (depth < 0 ? END_DOCUMENT : END_ELEMENT);
+ }
+
+ final Content c = stack[depth].next();
+ if (c == null) {
+ // formatted text or CDATA.
+ ctext = stack[depth].text();
+ return currentEvt = stack[depth].isCDATA() ? CDATA : CHARACTERS;
+ }
+
+ switch (c.getCType()) {
+ case CDATA:
+ ctext = c.getValue();
+ return currentEvt = CDATA;
+ case Text:
+ ctext = c.getValue();
+ return currentEvt = CHARACTERS;
+ case Comment:
+ ctext = c.getValue();
+ return currentEvt = COMMENT;
+ case DocType:
+ // format doctype appropriately.
+ XMLOutputter xout = new XMLOutputter();
+ ctext = xout.outputString((DocType)c);
+ return currentEvt = DTD;
+ case EntityRef:
+ clocalname = ((EntityRef)c).getName();
+ ctext = "";
+ return currentEvt = ENTITY_REFERENCE;
+ case ProcessingInstruction:
+ final ProcessingInstruction pi = (ProcessingInstruction)c;
+ ctarget = pi.getTarget();
+ cdata = pi.getData();
+ return currentEvt = PROCESSING_INSTRUCTION;
+ case Element:
+ // great
+ // we deal with Element outside the switch statement.
+ break;
+ default:
+ throw new IllegalStateException("Unexpected content " + c);
+ }
+ // OK, we break out here if we are an Element start.
+ final Element emt = (Element)c;
+ clocalname = emt.getName();
+ cprefix = emt.getNamespacePrefix();
+ curi = emt.getNamespaceURI();
+
+ nsstack.push(emt);
+ formatstack.push();
+ final String space = emt.getAttributeValue("space", Namespace.XML_NAMESPACE);
+ // Check for xml:space and adjust format settings
+ if ("default".equals(space)) {
+ formatstack.setTextMode(formatstack.getDefaultMode());
+ }
+ else if ("preserve".equals(space)) {
+ formatstack.setTextMode(TextMode.PRESERVE);
+ }
+
+ depth++;
+ if (depth >= stack.length) {
+ stack = ArrayCopy.copyOf(stack, depth + 32);
+ emtstack = ArrayCopy.copyOf(emtstack, depth+32);
+ }
+
+ emtstack[depth] = emt;
+ stack[depth] = buildWalker(formatstack, emt.getContent(), false);
+
+ return currentEvt = START_ELEMENT;
+
+ }
+
+ @Override
+ public int getEventType() {
+ return currentEvt;
+ }
+
+ @Override
+ public boolean isStartElement() {
+ return currentEvt == START_ELEMENT;
+ }
+
+ @Override
+ public boolean isEndElement() {
+ return currentEvt == END_ELEMENT;
+ }
+
+ @Override
+ public boolean isCharacters() {
+ return currentEvt == CHARACTERS;
+ }
+
+ @Override
+ public boolean isWhiteSpace() {
+ switch (currentEvt) {
+ case SPACE :
+ return true;
+ case CDATA:
+ case CHARACTERS:
+ return Verifier.isAllXMLWhitespace(ctext);
+ default:
+ return false;
+ }
}
@Override
public void require(int type, String namespaceURI, String localName) throws XMLStreamException {
- if(type != currentEvt){
- throw new XMLStreamException("required event " + type + " but got event " + currentEvt);
+ if(type != getEventType()){
+ throw new XMLStreamException("required event " + type + " but got event " + getEventType());
}
if(localName != null){
- if(!localName.equals(getLocalName())){
- throw new XMLStreamException("required name " + localName + " but got name " + getLocalName());
+ if(!localName.equals(clocalname)){
+ throw new XMLStreamException("required name " + localName + " but got name " + clocalname);
}
}
if(namespaceURI != null){
- if(!namespaceURI.equals(getNamespaceURI())){
- throw new XMLStreamException("required namespace " + namespaceURI + " but got namespace " + getNamespaceURI());
+ if(!namespaceURI.equals(curi)){
+ throw new XMLStreamException("required namespace " + namespaceURI + " but got namespace " + curi);
}
}
}
@Override
+ public QName getName() {
+ switch (currentEvt) {
+ case START_ELEMENT:
+ case END_ELEMENT:
+ final Element emt = emtstack[depth];
+ return new QName(emt.getNamespaceURI(), emt.getName(), emt.getNamespacePrefix());
+ default:
+ throw new IllegalStateException("getName not supported for event " + currentEvt);
+ }
+ }
+
+ @Override
+ public String getLocalName() {
+ switch (currentEvt) {
+ case START_ELEMENT:
+ case END_ELEMENT:
+ case ENTITY_REFERENCE:
+ return clocalname;
+ default:
+ throw new IllegalStateException("getLocalName not supported for event " + currentEvt);
+ }
+ }
+
+ @Override
+ public boolean hasName() {
+ return currentEvt == START_ELEMENT || currentEvt == END_ELEMENT;
+ }
+
+ @Override
+ public String getNamespaceURI() {
+ switch (currentEvt) {
+ case START_ELEMENT:
+ case END_ELEMENT:
+ return curi;
+ default:
+ throw new IllegalStateException("getNamespaceURI not supported for event " + currentEvt);
+ }
+ }
+
+ @Override
+ public String getPrefix() {
+ switch (currentEvt) {
+ case START_ELEMENT:
+ case END_ELEMENT:
+ return cprefix;
+ default:
+ throw new IllegalStateException("getPrefix not supported for event " + currentEvt);
+ }
+ }
+
+ @Override
+ public String getPITarget() {
+ switch (currentEvt) {
+ case PROCESSING_INSTRUCTION:
+ return ctarget;
+ default:
+ throw new IllegalStateException("getPITarget not supported for event " + currentEvt);
+ }
+ }
+
+ @Override
+ public String getPIData() {
+ switch (currentEvt) {
+ case PROCESSING_INSTRUCTION:
+ return cdata;
+ default:
+ throw new IllegalStateException("getPIData not supported for event " + currentEvt);
+ }
+ }
+
+
+ @Override
public String getElementText() throws XMLStreamException {
+ // copied from documentation.
if(getEventType() != XMLStreamConstants.START_ELEMENT) {
- throw new XMLStreamException("parser must be on START_ELEMENT to read next text", getLocation());
+ throw new XMLStreamException("parser must be on START_ELEMENT to read next text");
}
int eventType = next();
- StringBuffer buf = new StringBuffer();
+ StringBuilder buf = new StringBuilder();
while(eventType != XMLStreamConstants.END_ELEMENT ) {
if(eventType == XMLStreamConstants.CHARACTERS
|| eventType == XMLStreamConstants.CDATA
@@ -209,65 +446,38 @@ public int nextTag() throws XMLStreamException {
}
@Override
- public boolean hasNext() throws XMLStreamException {
- return !(state == ReaderState.RS_END_DOCUMENT ||
- state == ReaderState.RS_CLOSED);
- }
-
- @Override
public void close() throws XMLStreamException {
- this.state = ReaderState.RS_CLOSED;
+ currentEvt = END_DOCUMENT;
+ while (depth >= 0) {
+ stack[depth] = null;
+ emtstack[depth] = null;
+ depth--;
+ }
+ cdata = null;
+ clocalname = null;
+ cprefix = null;
+ ctarget = null;
+ ctext = null;
+ curi = null;
this.document = null;
- this.root = null;
- this.rootIterator = null;
}
@Override
public String getNamespaceURI(String prefix) {
- if("xml".equalsIgnoreCase(prefix)){
- return Namespace.XML_NAMESPACE.getURI();
- }
- if("xmlns".equalsIgnoreCase(prefix)){
- return "http://www.w3.org/2000/xmlns/";
- }
-
- Content c = getCurrentContent();
- for(Namespace ns : c.getNamespacesInScope()){
- if(ns.getPrefix().equals(prefix)){
- return ns.getURI();
- }
- }
-
- return null;
- }
-
- @Override
- public boolean isStartElement() {
- return currentEvt == START_ELEMENT;
- }
-
- @Override
- public boolean isEndElement() {
- return currentEvt == END_ELEMENT;
- }
-
- @Override
- public boolean isCharacters() {
- return currentEvt == CHARACTERS;
- }
-
- @Override
- public boolean isWhiteSpace() {
- return currentEvt == SPACE;
+ final Namespace ns = nsstack.getNamespaceForPrefix(prefix);
+ return ns == null ? null : ns.getURI();
}
@Override
public String getAttributeValue(String namespaceURI, String localName) {
- if(currentEvt != START_ELEMENT && currentEvt != ATTRIBUTE){
+ if(currentEvt != START_ELEMENT){
throw new IllegalStateException("getAttributeCount not supported for event " + currentEvt);
}
- Element e = (Element)getCurrentContent();
+ final Element e = emtstack[depth];
+ if (!e.hasAttributes()) {
+ return null;
+ }
if(namespaceURI != null){
return e.getAttributeValue(localName, Namespace.getNamespace(namespaceURI));
@@ -285,12 +495,10 @@ public String getAttributeValue(String namespaceURI, String localName) {
@Override
public int getAttributeCount() {
- if(currentEvt != START_ELEMENT && currentEvt != ATTRIBUTE){
+ if(currentEvt != START_ELEMENT){
throw new IllegalStateException("getAttributeCount not supported for event " + currentEvt);
}
-
- Element e = (Element)getCurrentContent();
- return e.getAttributes().size();
+ return emtstack[depth].getAttributesSize();
}
@Override
@@ -299,8 +507,7 @@ public QName getAttributeName(int index) {
throw new IllegalStateException("getAttributeCount not supported for event " + currentEvt);
}
- Element e = (Element)getCurrentContent();
- Attribute a = e.getAttributes().get(index);
+ final Attribute a = emtstack[depth].getAttributes().get(index);
String ns = a.getNamespaceURI();
if("".equals(ns))
@@ -319,13 +526,8 @@ public String getAttributeNamespace(int index) {
throw new IllegalStateException("getAttributeCount not supported for event " + currentEvt);
}
- Element e = (Element)getCurrentContent();
- Attribute a = e.getAttributes().get(index);
- String ret = a.getNamespaceURI();
- if("".equals(ret)){
- return null;
- }
- return ret;
+ final Attribute a = emtstack[depth].getAttributes().get(index);
+ return a.getNamespaceURI();
}
@Override
@@ -334,8 +536,7 @@ public String getAttributeLocalName(int index) {
throw new IllegalStateException("getAttributeCount not supported for event " + currentEvt);
}
- Element e = (Element)getCurrentContent();
- Attribute a = e.getAttributes().get(index);
+ final Attribute a = emtstack[depth].getAttributes().get(index);
return a.getName();
}
@@ -345,13 +546,8 @@ public String getAttributePrefix(int index) {
throw new IllegalStateException("getAttributeCount not supported for event " + currentEvt);
}
- Element e = (Element)getCurrentContent();
- Attribute a = e.getAttributes().get(index);
- String ret = a.getNamespacePrefix();
- if(ret == null || "".equals(ret)){
- return XMLConstants.DEFAULT_NS_PREFIX;
- }
- return ret;
+ final Attribute a = emtstack[depth].getAttributes().get(index);
+ return a.getNamespacePrefix();
}
@Override
@@ -360,9 +556,7 @@ public String getAttributeType(int index) {
throw new IllegalStateException("getAttributeCount not supported for event " + currentEvt);
}
- Element e = (Element)getCurrentContent();
- Attribute a = e.getAttributes().get(index);
-
+ final Attribute a = emtstack[depth].getAttributes().get(index);
return a.getAttributeType().name();
}
@@ -372,8 +566,7 @@ public String getAttributeValue(int index) {
throw new IllegalStateException("getAttributeCount not supported for event " + currentEvt);
}
- Element e = (Element)getCurrentContent();
- Attribute a = e.getAttributes().get(index);
+ final Attribute a = emtstack[depth].getAttributes().get(index);
return a.getValue();
}
@@ -383,9 +576,7 @@ public boolean isAttributeSpecified(int index) {
throw new IllegalStateException("getAttributeCount not supported for event " + currentEvt);
}
- Element e = (Element)getCurrentContent();
- Attribute a = e.getAttributes().get(index);
-
+ final Attribute a = emtstack[depth].getAttributes().get(index);
return a.isSpecified();
}
@@ -393,28 +584,39 @@ public boolean isAttributeSpecified(int index) {
public int getNamespaceCount() {
switch(currentEvt){
case START_ELEMENT:
- case NAMESPACE:
case END_ELEMENT:
- Element e = (Element)getCurrentContent();
- return e.getNamespacesIntroduced().size();
+ final Iterator<?> it = nsstack.addedForward().iterator();
+ int cnt = 0;
+ while (it.hasNext()) {
+ cnt++;
+ it.next();
+ }
+ return cnt;
}
throw new IllegalStateException("getNamespaceCount not supported for event " + currentEvt);
}
+
+ private final Namespace getNamespaceByIndex(int index) {
+ final Iterator<Namespace> it = nsstack.addedForward().iterator();
+ int cnt = 0;
+ while (it.hasNext()) {
+ if (cnt == index) {
+ return it.next();
+ }
+ it.next();
+ cnt++;
+ }
+ throw new NoSuchElementException("No Namespace with index " + index +
+ " (there are only " + cnt + ").");
+ }
@Override
public String getNamespacePrefix(int index) {
switch(currentEvt){
case START_ELEMENT:
- case NAMESPACE:
case END_ELEMENT:
- Element e = (Element)getCurrentContent();
- Namespace ns = e.getNamespacesIntroduced().get(index);
- String ret = ns.getPrefix();
- if(ret == null || "".equals(ret)){
- ret = XMLConstants.DEFAULT_NS_PREFIX;
- }
- return ret;
+ return getNamespaceByIndex(index).getPrefix();
}
throw new IllegalStateException("getNamespacePrefix not supported for event " + currentEvt);
@@ -427,13 +629,7 @@ public String getNamespaceURI(int index) {
case START_ELEMENT:
case NAMESPACE:
case END_ELEMENT:
- Element e = (Element)getCurrentContent();
- Namespace ns = e.getNamespacesIntroduced().get(index);
- String ret = ns.getURI();
- if("".equals(ret)){
- ret = null;
- }
- return ret;
+ return getNamespaceByIndex(index).getURI();
}
throw new IllegalStateException("getNamespaceURI not supported for event " + currentEvt);
@@ -441,31 +637,32 @@ public String getNamespaceURI(int index) {
@Override
public NamespaceContext getNamespaceContext() {
- if(state == ReaderState.RS_START_DOCUMENT || state == ReaderState.RS_END_DOCUMENT || state == ReaderState.RS_CLOSED){
- throw new IllegalStateException("getNamespaceCount not supported for event " + currentEvt);
- }
-
- Content c = getCurrentContent();
- return new org.jdom2.jaxb.JDOMNamespaceContext(c.getNamespacesInScope());
+ return new JDOMNamespaceContext(nsstack.getScope());
}
@Override
- public int getEventType() {
- return currentEvt;
+ public boolean hasText() {
+ switch(currentEvt){
+ case CDATA:
+ case CHARACTERS:
+ case COMMENT:
+ case DTD:
+ case ENTITY_REFERENCE:
+ return true;
+ default:
+ return false;
+ }
}
@Override
public String getText() {
- final Content c = getCurrentContent();
- switch(c.getCType()){
+ switch(currentEvt){
case CDATA:
- case Text:
- case Comment:
- return c.getValue();
-
- case DocType:
- return ((DocType)c).getInternalSubset();
-
+ case CHARACTERS:
+ case COMMENT:
+ case DTD:
+ case ENTITY_REFERENCE:
+ return ctext;
default:
throw new IllegalStateException("getText not valid for event type " + currentEvt);
}
@@ -515,15 +712,6 @@ public String getEncoding() {
}
@Override
- public boolean hasText() {
- return currentEvt == CHARACTERS ||
- currentEvt == DTD ||
- currentEvt == ENTITY_REFERENCE ||
- currentEvt == COMMENT ||
- currentEvt == SPACE;
- }
-
- @Override
public Location getLocation() {
return new Location(){
@Override
@@ -554,72 +742,6 @@ public String getSystemId() {
};
}
- @Override
- public QName getName() {
- if(currentEvt != START_ELEMENT && currentEvt != END_ELEMENT){
- throw new IllegalStateException("getName not supported for event " + currentEvt);
- }
-
- Element e = (Element)getCurrentContent();
- String ns = e.getNamespaceURI();
- if("".equals(ns))
- ns = null;
-
- String prefix = e.getNamespacePrefix();
- if(prefix == null || "".equals(prefix))
- prefix = XMLConstants.DEFAULT_NS_PREFIX;
-
- return new QName(ns, e.getName(), prefix);
- }
-
- @Override
- public String getLocalName() {
- switch(currentEvt){
- case START_ELEMENT:
- case END_ELEMENT:
- Element e = (Element)getCurrentContent();
- return e.getName();
-
- case ENTITY_REFERENCE:
- EntityRef er = (EntityRef)getCurrentContent();
- return er.getName();
- }
-
- throw new IllegalStateException("getLocalName not supported for event " + currentEvt);
- }
-
- @Override
- public boolean hasName() {
- return currentEvt == START_ELEMENT || currentEvt == END_ELEMENT;
- }
-
- @Override
- public String getNamespaceURI() {
- if(currentEvt != START_ELEMENT && currentEvt != END_ELEMENT){
- throw new IllegalStateException("getNamespaceURI not supported for event " + currentEvt);
- }
-
- Element e = (Element)getCurrentContent();
- String ret = e.getNamespaceURI();
- if("".equals(ret)){
- ret = null;
- }
- return ret;
- }
-
- @Override
- public String getPrefix() {
- if(currentEvt != START_ELEMENT && currentEvt != END_ELEMENT){
- throw new IllegalStateException("getName not supported for event " + currentEvt);
- }
-
- Element e = (Element)getCurrentContent();
- String ret = e.getNamespacePrefix();
- if(ret == null || "".equals(ret))
- ret = XMLConstants.DEFAULT_NS_PREFIX;
-
- return ret;
- }
@Override
public String getVersion() {
@@ -647,145 +769,35 @@ public String getCharacterEncodingScheme() {
}
@Override
- public String getPITarget() {
- if(currentEvt != PROCESSING_INSTRUCTION){
- throw new IllegalStateException("getPITarget not supported for event " + currentEvt);
- }
-
- ProcessingInstruction pi = (ProcessingInstruction)getCurrentContent();
- return pi.getTarget();
- }
-
- @Override
- public String getPIData() {
- if(currentEvt != PROCESSING_INSTRUCTION){
- throw new IllegalStateException("getPIData not supported for event " + currentEvt);
- }
-
- ProcessingInstruction pi = (ProcessingInstruction)getCurrentContent();
- return pi.getData();
+ public Object getProperty(final String name) throws IllegalArgumentException {
+ if (name == null) {
+ throw new IllegalArgumentException("Property name is not allowed to be null");
+ }
+ if (XMLInputFactory.ALLOCATOR.equals(name)) {
+ return null;
+ }
+ if (XMLInputFactory.IS_COALESCING.equals(name)) {
+ return formatstack.getDefaultMode() != TextMode.PRESERVE;
+ }
+ if (XMLInputFactory.IS_NAMESPACE_AWARE.equals(name)) {
+ return Boolean.TRUE;
+ }
+ if (XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES.equals(name)) {
+ return Boolean.FALSE;
+ }
+ if (XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES.equals(name)) {
+ return Boolean.FALSE;
+ }
+ if (XMLInputFactory.IS_VALIDATING.equals(name)) {
+ return Boolean.TRUE;
+ }
+ if (XMLInputFactory.REPORTER.equals(name)) {
+ return null;
+ }
+ if (XMLInputFactory.RESOLVER.equals(name)) {
+ return null;
+ }
+ return null;
}
-
- /**
- * An Iterator that recursively walks DOM elements and returns
- * {@link XMLStreamConstants XML stream events}.
- */
- private class DomWalkingContentIterator implements Iterator<Integer> {
-
- private int contentIndex = 0;
-
- private DomWalkingContentIterator subIterator;
-
- private Content currentContent;
- public Content getCurrentContent(){
- Content ret = null;
- if(subIterator != null){
- ret = subIterator.getCurrentContent();
- }
-
- if(ret == null){
- return currentContent;
- }
-
- return ret;
- }
-
- private Element toWalk;
-
- public DomWalkingContentIterator(Element toWalk){
- this.toWalk = toWalk;
- }
-
- @Override
- public boolean hasNext() {
- //if we're walking a recursive sub-iterator
- if(subIterator != null){
- //we either have sub-iterator content or we have END_ELEMENT
- return true;
- }
-
- //if we have content remaining
- if(toWalk.getContentSize() > contentIndex){
- return true;
- }
-
- return false;
- }
-
- @Override
- public Integer next() {
- int next;
- if(subIterator != null){
- //walk the sub-element
- if(subIterator.hasNext()){
- next = subIterator.next();
- return next;
- }
- subIterator = null;
- return END_ELEMENT;
- }
-
- if(this.toWalk.getContentSize() > contentIndex){
- Content c = this.toWalk.getContent(contentIndex++);
- this.currentContent = c;
- if(c.getCType() == Content.CType.Element){
- subIterator = new DomWalkingContentIterator((Element)c);
- return START_ELEMENT;
- }
-
- return getEventType(c);
- }
-
- throw new IllegalStateException("Iterator does not have next");
- }
- @Override
- public void remove() {
- throw new UnsupportedOperationException("Remove is not supported.");
- }
-
-
- }
-
-
- private int getEventType(Content c){
- switch(c.getCType()){
- case CDATA:
- return CDATA;
-
- case Comment:
- return COMMENT;
-
- case DocType:
- return DTD;
-
- case EntityRef:
- //TODO: properly expand entity refs
- throw new UnsupportedOperationException("Entity references not yet supported");
-
- case ProcessingInstruction:
- return PROCESSING_INSTRUCTION;
-
- case Text:
- if(((Text)c).getText().trim().length() == 0){
- return SPACE;
- }
- return CHARACTERS;
-
- default:
- throw new UnsupportedOperationException("No event type available for content type " + c.getCType());
- }
- }
-
- /**
- * The current state of the reader
- */
- private enum ReaderState{
- RS_START_DOCUMENT,
- RS_START_ROOT_ELEMENT,
- RS_WALKING_TREE,
- RS_END_ROOT_ELEMENT,
- RS_END_DOCUMENT,
- RS_CLOSED
- }
}
View
2  core/src/java/org/jdom2/jaxb/JDOMStreamWriter.java
@@ -713,7 +713,7 @@ public void flush() throws XMLStreamException {
public NamespaceContext getNamespaceContext() {
final List<Namespace> namespaces = getCurrentNamespaces();
- return new org.jdom2.jaxb.JDOMNamespaceContext(namespaces);
+ return new JDOMNamespaceContext(namespaces.toArray(new Namespace[namespaces.size()]));
}
@Override
View
368 core/src/java/org/jdom2/output/StAXAsStreamReader.java
@@ -0,0 +1,368 @@
+/*--
+
+ Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions, and the disclaimer that follows
+ these conditions in the documentation and/or other materials
+ provided with the distribution.
+
+ 3. The name "JDOM" must not be used to endorse or promote products
+ derived from this software without prior written permission. For
+ written permission, please contact <request_AT_jdom_DOT_org>.
+
+ 4. Products derived from this software may not be called "JDOM", nor
+ may "JDOM" appear in their name, without prior written permission
+ from the JDOM Project Management <request_AT_jdom_DOT_org>.
+
+ In addition, we request (but do not require) that you include in the
+ end-user documentation provided with the redistribution and/or in the
+ software itself an acknowledgement equivalent to the following:
+ "This product includes software developed by the
+ JDOM Project (http://www.jdom.org/)."
+ Alternatively, the acknowledgment may be graphical using the logos
+ available at http://www.jdom.org/images/logos.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many
+ individuals on behalf of the JDOM Project and was originally
+ created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
+ Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
+ on the JDOM Project, please see <http://www.jdom.org/>.
+
+ */
+
+package org.jdom2.output;
+
+import javax.xml.stream.XMLStreamReader;
+
+import org.jdom2.Document;
+import org.jdom2.jaxb.JDOMStreamReader;
+import org.jdom2.output.support.AbstractStAXAsStreamProcessor;
+import org.jdom2.output.support.StAXAsStreamProcessor;
+
+/**
+ * Represents a JDOM document as a StAX StreamReader that can be read from.
+ * <p>
+ * The StAXAsStreamReader can manage many styles of document formatting, from
+ * untouched to 'pretty' printed. The default is to output the document content
+ * exactly as created, but this can be changed by setting a new Format object:
+ * <ul>
+ * <li>For pretty-print output, use
+ * <code>{@link Format#getPrettyFormat()}</code>.
+ * <li>For whitespace-normalised output, use
+ * <code>{@link Format#getCompactFormat()}</code>.
+ * <li>For unmodified-format output, use
+ * <code>{@link Format#getRawFormat()}</code>.
+ * </ul>
+ * <p>
+ * There is only one <code>{@link #output output(Document)}</code> method that exposes
+ * a JDOM Document as a StAX Stream.
+ * <p>
+ * If changing the {@link Format} settings are insufficient for your output
+ * needs you can customise this StAXAsStreamReader further by setting a different
+ * {@link StAXAsStreamProcessor} with the
+ * {@link #setStAXAsStreamProcessor(StAXAsStreamProcessor)} method or an appropriate
+ * constructor. A fully-enabled Abstract class
+ * {@link AbstractStAXAsStreamProcessor} is available to be further extended to
+ * your needs if all you want to do is tweak some details.
+ *
+ * @author Rolf Lear
+ */
+
+public final class StAXAsStreamReader implements Cloneable {
+
+ /*
+ * =====================================================================
+ * Static content.
+ * =====================================================================
+ */
+
+ /**
+ * Create a final and static instance of the StAXAsStreamProcessor The
+ * final part is important because it improves performance.
+ * <p>
+ * The JDOM user can change the actual XMLOutputProcessor with the
+ * {@link StAXAsStreamReader#setStAXAsStreamProcessor(StAXAsStreamProcessor)} method.
+ *
+ * @author rolf
+ */
+ private static final class DefaultStAXAsStreamProcessor implements StAXAsStreamProcessor {
+
+ private static final class MyXMLStreamReader extends JDOMStreamReader {
+ public MyXMLStreamReader(Document doc, Format format) {
+ super(doc, format);
+ }
+ }
+
+ @Override
+ public XMLStreamReader buildReader(Document doc, Format format) {
+ return new MyXMLStreamReader(doc, format);
+ }
+
+ }
+
+ /**
+ * This constant XMLOutputProcessor is used for all non-customised
+ * XMLOutputters
+ */
+ private static final DefaultStAXAsStreamProcessor DEFAULTPROCESSOR =
+ new DefaultStAXAsStreamProcessor();
+
+ /*
+ * =====================================================================
+ * Instance content.
+ * =====================================================================
+ */
+
+ // For normal output
+ private Format myFormat = null;
+
+ // The actual XMLOutputProcessor to delegate to.
+ private StAXAsStreamProcessor myProcessor = null;
+
+ /*
+ * =====================================================================
+ * Constructors
+ * =====================================================================
+ */
+
+ /**
+ * This will create an <code>XMLOutputter</code> with the specified format
+ * characteristics.
+ * <p>
+ * <b>Note:</b> the format object is cloned internally before use. If you
+ * want to modify the Format after constructing the XMLOutputter you can
+ * modify the Format instance {@link #getFormat()} returns.
+ *
+ * @param format
+ * The Format instance to use. This instance will be cloned() and as
+ * a consequence, changes made to the specified format instance
+ * <b>will not</b> be reflected in this XMLOutputter. A null input
+ * format indicates that XMLOutputter should use the default
+ * {@link Format#getRawFormat()}
+ * @param processor
+ * The XMLOutputProcessor to delegate output to. If null the
+ * XMLOutputter will use the default XMLOutputProcessor.
+ */
+ public StAXAsStreamReader(Format format, StAXAsStreamProcessor processor) {
+ myFormat = format == null ? Format.getRawFormat() : format.clone();
+ myProcessor = processor == null ? DEFAULTPROCESSOR : processor;
+ }
+
+ /**
+ * This will create an <code>XMLOutputter</code> with a default
+ * {@link Format} and {@link StAXAsStreamProcessor}.
+ */
+ public StAXAsStreamReader() {
+ this(null, null);
+ }
+
+ /**
+ * This will create an <code>XMLOutputter</code> with the same
+ * customisations set in the given <code>XMLOutputter</code> instance. Note
+ * that <code>XMLOutputter two = one.clone();</code> would work equally
+ * well.
+ *
+ * @param that
+ * the XMLOutputter to clone
+ */
+ public StAXAsStreamReader(StAXAsStreamReader that) {
+ this(that.myFormat, null);
+ }
+
+ /**
+ * This will create an <code>XMLOutputter</code> with the specified format
+ * characteristics.
+ * <p>
+ * <b>Note:</b> the format object is cloned internally before use.
+ *
+ * @param format
+ * The Format instance to use. This instance will be cloned() and as
+ * a consequence, changes made to the specified format instance
+ * <b>will not</b> be reflected in this XMLOutputter. A null input
+ * format indicates that XMLOutputter should use the default
+ * {@link Format#getRawFormat()}
+ */
+ public StAXAsStreamReader(Format format) {
+ this(format, null);
+ }
+
+ /**
+ * This will create an <code>XMLOutputter</code> with the specified
+ * XMLOutputProcessor.
+ *
+ * @param processor
+ * The XMLOutputProcessor to delegate output to. If null the
+ * XMLOutputter will use the default XMLOutputProcessor.
+ */
+ public StAXAsStreamReader(StAXAsStreamProcessor processor) {
+ this(null, processor);
+ }
+
+ /*
+ * =======================================================================
+ * API - Settings...
+ * =======================================================================
+ */
+
+ /**
+ * Sets the new format logic for the XMLOutputter. Note the Format object is
+ * cloned internally before use.
+ *
+ * @see #getFormat()
+ * @param newFormat
+ * the format to use for subsequent output
+ */
+ public void setFormat(Format newFormat) {
+ this.myFormat = newFormat.clone();
+ }
+
+ /**
+ * Returns the current format in use by the XMLOutputter. Note the Format
+ * object returned is <b>not</b> a clone of the one used internally, thus,
+ * an XMLOutputter instance is able to have it's Format changed by changing
+ * the settings on the Format instance returned by this method.
+ *
+ * @return the current Format instance used by this XMLOutputter.
+ */
+ public Format getFormat() {
+ return myFormat;
+ }
+
+ /**
+ * Returns the current XMLOutputProcessor instance in use by the
+ * StAXAsStreamReader.
+ *
+ * @return the current XMLOutputProcessor instance.
+ */
+ public StAXAsStreamProcessor getStAXAsStreamProcessor() {
+ return myProcessor;
+ }
+
+ /**
+ * Sets a new StAXAsStreamProcessor instance for this StAXAsStreamReader. Note the
+ * processor object is expected to be thread-safe.
+ *
+ * @param processor
+ * the new XMLOutputProcesor to use for output
+ */
+ public void setStAXAsStreamProcessor(StAXAsStreamProcessor processor) {
+ this.myProcessor = processor;
+ }
+
+ /*
+ * =======================================================================
+ * API - Output to STREAM Methods ... All methods defer to the WRITER
+ * equivalents
+ * =======================================================================
+ */
+
+ /**
+ * This will expose the <code>{@link Document}</code> as a StAX XMLStreamReader.
+ *
+ * @param doc
+ * <code>Document</code> to format.
+ * @return The XMLStreamReader representing the input Document.
+ * @throws NullPointerException
+ * if the specified content is null.
+ */
+ public final XMLStreamReader output(Document doc) {
+ return myProcessor.buildReader(doc, myFormat.clone());
+ }
+
+ /*
+ * ========================================================================
+ * Basic Support methods.
+ * ========================================================================
+ */
+
+ /**
+ * Returns a cloned copy of this XMLOutputter.
+ */
+ @Override
+ public StAXAsStreamReader clone() {
+ // Implementation notes: Since all state of an XMLOutputter is
+ // embodied in simple private instance variables, Object.clone
+ // can be used. Note that since Object.clone is totally
+ // broken, we must catch an exception that will never be
+ // thrown.
+ try {
+ return (StAXAsStreamReader) super.clone();
+ } catch (java.lang.CloneNotSupportedException e) {
+ // even though this should never ever happen, it's still
+ // possible to fool Java into throwing a
+ // CloneNotSupportedException. If that happens, we
+ // shouldn't swallow it.
+ throw new RuntimeException(e.toString());
+ }
+ }
+
+ /**
+ * Return a string listing of the settings for this XMLOutputter instance.
+ *
+ * @return a string listing the settings for this XMLOutputter instance
+ */
+ @Override
+ public String toString() {
+ StringBuilder buffer = new StringBuilder();
+ buffer.append("StAXAsStreamReader[omitDeclaration = ");
+ buffer.append(myFormat.omitDeclaration);
+ buffer.append(", ");
+ buffer.append("encoding = ");
+ buffer.append(myFormat.encoding);
+ buffer.append(", ");
+ buffer.append("omitEncoding = ");
+ buffer.append(myFormat.omitEncoding);
+ buffer.append(", ");
+ buffer.append("indent = '");
+ buffer.append(myFormat.indent);
+ buffer.append("'");
+ buffer.append(", ");
+ buffer.append("expandEmptyElements = ");
+ buffer.append(myFormat.expandEmptyElements);
+ buffer.append(", ");
+ buffer.append("lineSeparator = '");
+ for (char ch : myFormat.lineSeparator.toCharArray()) {
+ switch (ch) {
+ case '\r':
+ buffer.append("\\r");
+ break;
+ case '\n':
+ buffer.append("\\n");
+ break;
+ case '\t':
+ buffer.append("\\t");
+ break;
+ default:
+ buffer.append("[" + ((int) ch) + "]");
+ break;
+ }
+ }
+ buffer.append("', ");
+ buffer.append("textMode = ");
+ buffer.append(myFormat.mode + "]");
+ return buffer.toString();
+ }
+
+}
View
88 core/src/java/org/jdom2/output/support/AbstractStAXAsStreamProcessor.java
@@ -0,0 +1,88 @@
+/*--
+
+ Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions, and the disclaimer that follows
+ these conditions in the documentation and/or other materials
+ provided with the distribution.
+
+ 3. The name "JDOM" must not be used to endorse or promote products
+ derived from this software without prior written permission. For
+ written permission, please contact <request_AT_jdom_DOT_org>.
+
+ 4. Products derived from this software may not be called "JDOM", nor
+ may "JDOM" appear in their name, without prior written permission
+ from the JDOM Project Management <request_AT_jdom_DOT_org>.
+
+ In addition, we request (but do not require) that you include in the
+ end-user documentation provided with the redistribution and/or in the
+ software itself an acknowledgement equivalent to the following:
+ "This product includes software developed by the
+ JDOM Project (http://www.jdom.org/)."
+ Alternatively, the acknowledgment may be graphical using the logos
+ available at http://www.jdom.org/images/logos.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many
+ individuals on behalf of the JDOM Project and was originally
+ created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
+ Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
+ on the JDOM Project, please see <http://www.jdom.org/>.
+
+ */
+
+package org.jdom2.output.support;
+
+import javax.xml.stream.XMLStreamReader;
+
+import org.jdom2.Document;
+import org.jdom2.jaxb.JDOMStreamReader;
+import org.jdom2.output.Format;
+
+/**
+ * A complete (but still abstract) implementation of a class that produces
+ * XMLStreamReaders based on the class AbstractXMLStreamReader. Users who need
+ * to make changes to the output format that are not possible with just the
+ * Format class, may need to extend this class to create instances of their own
+ * XMLStreamReader. Their own implementation could in turn extend the AbstractXMLReader
+ * class which contains the bulk of the StAX Logic.
+ *
+ * @author Rolf Lear
+ *
+ */
+public class AbstractStAXAsStreamProcessor implements StAXAsStreamProcessor {
+
+ private static final class MyXMLStreamReader extends JDOMStreamReader {
+
+ public MyXMLStreamReader(Document doc, Format format) {
+ super(doc, format);
+ }
+ }
+
+ @Override
+ public XMLStreamReader buildReader(Document doc, Format format) {
+ return new MyXMLStreamReader(doc, format);
+ }
+
+}
View
85 core/src/java/org/jdom2/output/support/StAXAsStreamProcessor.java
@@ -0,0 +1,85 @@
+/*--
+
+ Copyright (C) 2000-2012 Jason Hunter & Brett McLaughlin.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions, and the disclaimer that follows
+ these conditions in the documentation and/or other materials
+ provided with the distribution.
+
+ 3. The name "JDOM" must not be used to endorse or promote products
+ derived from this software without prior written permission. For
+ written permission, please contact <request_AT_jdom_DOT_org>.
+
+ 4. Products derived from this software may not be called "JDOM", nor
+ may "JDOM" appear in their name, without prior written permission
+ from the JDOM Project Management <request_AT_jdom_DOT_org>.
+
+ In addition, we request (but do not require) that you include in the
+ end-user documentation provided with the redistribution and/or in the
+ software itself an acknowledgement equivalent to the following:
+ "This product includes software developed by the
+ JDOM Project (http://www.jdom.org/)."
+ Alternatively, the acknowledgment may be graphical using the logos
+ available at http://www.jdom.org/images/logos.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many
+ individuals on behalf of the JDOM Project and was originally
+ created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
+ Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
+ on the JDOM Project, please see <http://www.jdom.org/>.
+
+ */
+
+package org.jdom2.output.support;
+
+import javax.xml.stream.XMLStreamReader;
+
+import org.jdom2.Document;
+import org.jdom2.jaxb.JDOMStreamReader;
+import org.jdom2.output.Format;
+
+/**
+ * A simple interface that allows the implementation of a StAX XMLStreamReader
+ * instance for representing a JDOM Document. If a user needs to create a custom
+ * way to output as an XMLStreamReader they can implement their own XMLStreamReader
+ * class (perhaps by extending {@link JDOMStreamReader}) and then creating an
+ * implementation of this class that creates their own custom XMLStreamReader
+ * version. The implementation of this class could then be given to the
+ * StAXAsStreamReader.
+ *
+ * @author Rolf Lear
+ *
+ */
+public interface StAXAsStreamProcessor {
+ /**
+ * Return an implementation of an XMLStreamReader that represents
+ * a JDOM Document.
+ *
+ * @param doc The Document to represent.
+ * @param format The Format to apply to the document.
+ * @return The XMLStreamReader that expresses the JDOM Document.
+ */
+ public XMLStreamReader buildReader(Document doc, Format format);
+}
View
25 test/src/java/org/jdom2/test/cases/jaxb/TestJDOMNamespaceContext.java
@@ -1,9 +1,10 @@
package org.jdom2.test.cases.jaxb;
-import static org.junit.Assert.*;
-import static org.jdom2.test.util.UnitTestUtil.*;
+import static org.jdom2.test.util.UnitTestUtil.checkException;
+import static org.jdom2.test.util.UnitTestUtil.failNoException;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
-import java.util.Arrays;
import java.util.Iterator;
import org.junit.Test;
@@ -30,7 +31,7 @@
@Test
public void testJDOMNamespaceContext() {
- final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsok));
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(nsok);
assertEquals("urla", nsc.getNamespaceURI("a"));
try {
new JDOMNamespaceContext(null);
@@ -39,13 +40,13 @@ public void testJDOMNamespaceContext() {
checkException(IllegalArgumentException.class, e);
}
try {
- new JDOMNamespaceContext(Arrays.asList(nsduppfx));
+ new JDOMNamespaceContext(nsduppfx);
failNoException(IllegalArgumentException.class);
} catch (Exception e) {
checkException(IllegalArgumentException.class, e);
}
try {
- new JDOMNamespaceContext(Arrays.asList(nsnullmember));
+ new JDOMNamespaceContext(nsnullmember);
failNoException(IllegalArgumentException.class);
} catch (Exception e) {
checkException(IllegalArgumentException.class, e);
@@ -54,7 +55,7 @@ public void testJDOMNamespaceContext() {
@Test
public void testGetNamespaceURIWithDef() {
- final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsok));
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(nsok);
assertEquals("defns", nsc.getNamespaceURI(""));
assertEquals(Namespace.XML_NAMESPACE.getURI(), nsc.getNamespaceURI(Namespace.XML_NAMESPACE.getPrefix()));
assertEquals(JDOMConstants.NS_URI_XMLNS, nsc.getNamespaceURI(JDOMConstants.NS_PREFIX_XMLNS));
@@ -73,7 +74,7 @@ public void testGetNamespaceURIWithDef() {
@Test
public void testGetNamespaceURIWithoutDef() {
- final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsmay));
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(nsmay);
assertEquals("", nsc.getNamespaceURI(""));
assertEquals(Namespace.XML_NAMESPACE.getURI(), nsc.getNamespaceURI(Namespace.XML_NAMESPACE.getPrefix()));
assertEquals(JDOMConstants.NS_URI_XMLNS, nsc.getNamespaceURI(JDOMConstants.NS_PREFIX_XMLNS));
@@ -92,7 +93,7 @@ public void testGetNamespaceURIWithoutDef() {
@Test
public void testGetPrefixWithDef() {
- final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsok));
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(nsok);
assertEquals("", nsc.getPrefix("defns"));
assertEquals(null, nsc.getPrefix(Namespace.NO_NAMESPACE.getURI()));
assertEquals(Namespace.XML_NAMESPACE.getPrefix(), nsc.getPrefix(Namespace.XML_NAMESPACE.getURI()));
@@ -109,7 +110,7 @@ public void testGetPrefixWithDef() {
@Test
public void testGetPrefixWithoutDef() {
- final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsmay));
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(nsmay);
assertEquals(null, nsc.getPrefix("defns"));
assertEquals(null, nsc.getPrefix(Namespace.NO_NAMESPACE.getURI()));
assertEquals(Namespace.XML_NAMESPACE.getPrefix(), nsc.getPrefix(Namespace.XML_NAMESPACE.getURI()));
@@ -120,7 +121,7 @@ public void testGetPrefixWithoutDef() {
@Test
public void testGetPrefixesWithDef() {
- final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsok));
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(nsok);
checkIteratorEquals(nsc.getPrefixes("defns"), "");
checkIteratorEquals(nsc.getPrefixes(Namespace.NO_NAMESPACE.getURI()));
checkIteratorEquals(nsc.getPrefixes(Namespace.XML_NAMESPACE.getURI()), Namespace.XML_NAMESPACE.getPrefix());
@@ -137,7 +138,7 @@ public void testGetPrefixesWithDef() {
@Test
public void testGetPrefixesWithoutDef() {
- final JDOMNamespaceContext nsc = new JDOMNamespaceContext(Arrays.asList(nsmay));
+ final JDOMNamespaceContext nsc = new JDOMNamespaceContext(nsmay);
checkIteratorEquals(nsc.getPrefixes("defns"));
checkIteratorEquals(nsc.getPrefixes(Namespace.NO_NAMESPACE.getURI()));
checkIteratorEquals(nsc.getPrefixes(Namespace.XML_NAMESPACE.getURI()), Namespace.XML_NAMESPACE.getPrefix());
View
22 test/src/java/org/jdom2/test/cases/output/TestStAXWriterReader.java
@@ -0,0 +1,22 @@
+package org.jdom2.test.cases.output;
+
+import org.jdom2.Document;
+import org.jdom2.JDOMException;
+import org.jdom2.input.StAXStreamBuilder;
+import org.jdom2.output.StAXAsStreamReader;
+
+@SuppressWarnings("javadoc")
+public class TestStAXWriterReader extends AbstractTestRoundTrip {
+
+ @Override
+ Document roundTrip(final Document doc) {
+ final StAXStreamBuilder sb = new StAXStreamBuilder();
+ final StAXAsStreamReader sasr = new StAXAsStreamReader();
+ try {
+ return sb.build(sasr.output(doc));
+ } catch (JDOMException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.