Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue #106 - setPrefix call before startElement.

  • Loading branch information...
commit 101f2e1494b827ef96859ce565045e8c0afeddee 1 parent 2f5eb75
@rolfl rolfl authored
View
32 core/src/java/org/jdom2/output/support/AbstractStAXStreamProcessor.java
@@ -603,13 +603,15 @@ else if ("preserve".equals(space)) {
if (expandit) {
Namespace ns = element.getNamespace();
- if (ns == Namespace.NO_NAMESPACE) {
- out.writeStartElement(element.getName());
- } else if ("".equals(ns.getPrefix())) {
- out.writeStartElement(ns.getURI(), element.getName());
- } else {
- out.writeStartElement(ns.getPrefix(), element.getName(), ns.getURI());
- }
+// out.setPrefix(ns.getPrefix(), ns.getURI());
+ out.writeStartElement(ns.getPrefix(), element.getName(), ns.getURI());
+// if (ns == Namespace.NO_NAMESPACE) {
+// out.writeStartElement(element.getName());
+// } else if ("".equals(ns.getPrefix())) {
+// out.writeStartElement(ns.getURI(), element.getName());
+// } else {
+// out.writeStartElement(ns.getPrefix(), element.getName(), ns.getURI());
+// }
// Print the element's namespace, if appropriate
for (final Namespace nsd : nstack.addedForward()) {
@@ -659,13 +661,15 @@ else if ("preserve".equals(space)) {
// and whiteonly == true
Namespace ns = element.getNamespace();
- if (ns == Namespace.NO_NAMESPACE) {
- out.writeEmptyElement(element.getName());
- } else if ("".equals(ns.getPrefix())) {
- out.writeEmptyElement("", element.getName(), ns.getURI());
- } else {
- out.writeEmptyElement(ns.getPrefix(), element.getName(), ns.getURI());
- }
+// out.setPrefix(ns.getPrefix(), ns.getURI());
+ out.writeEmptyElement(ns.getPrefix(), element.getName(), ns.getURI());
+// if (ns == Namespace.NO_NAMESPACE) {
+// out.writeEmptyElement(element.getName());
+// } else if ("".equals(ns.getPrefix())) {
+// out.writeEmptyElement("", element.getName(), ns.getURI());
+// } else {
+// out.writeEmptyElement(ns.getPrefix(), element.getName(), ns.getURI());
+// }
// Print the element's namespace, if appropriate
for (final Namespace nsd : nstack.addedForward()) {
View
36 test/src/java/org/jdom2/test/cases/output/TestStAXStreamOutputter.java
@@ -8,7 +8,6 @@
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
-import java.io.CharArrayReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
@@ -47,10 +46,11 @@
import org.jdom2.input.stax.DefaultStAXFilter;
import org.jdom2.output.Format;
import org.jdom2.output.Format.TextMode;
-import org.jdom2.output.support.AbstractStAXStreamProcessor;
-import org.jdom2.output.support.StAXStreamProcessor;
import org.jdom2.output.SAXOutputter;
import org.jdom2.output.StAXStreamOutputter;
+import org.jdom2.output.support.AbstractStAXStreamProcessor;
+import org.jdom2.output.support.StAXStreamProcessor;
+import org.jdom2.test.util.UnitTestUtil;
@SuppressWarnings("javadoc")
public final class TestStAXStreamOutputter extends AbstractTestOutputter {
@@ -547,6 +547,11 @@ public void testToString() {
*/
private void roundTripDocument(Document doc) {
+ roundTripDocument(doc, true);
+ roundTripDocument(doc, false);
+ }
+
+ private void roundTripDocument(Document doc, boolean repairing) {
StAXStreamOutputter xout = new StAXStreamOutputter(Format.getRawFormat());
// create a String representation of the input.
if (doc.hasRootElement()) {
@@ -555,32 +560,24 @@ private void roundTripDocument(Document doc) {
try {
StringWriter sw = new StringWriter();
+ soutfactory.setProperty("javax.xml.stream.isRepairingNamespaces", repairing);
XMLStreamWriter xsw = soutfactory.createXMLStreamWriter(sw);
xout.output(doc, xsw);
xsw.close();
- String expect = sw.toString();
-
+ Document expect = new SAXBuilder().build(new StringReader(sw.toString()));
// convert the input to a SAX Stream
+ UnitTestUtil.compare(doc, expect);
StAXStreamBuilder sbuilder = new StAXStreamBuilder();
- char[] chars = expect.toCharArray();
- CharArrayReader car = new CharArrayReader(chars);
+ StringReader car = new StringReader(sw.toString());
XMLStreamReader xsr = sinfactory.createXMLStreamReader(car);
Document backagain = sbuilder.build(xsr);
xsr.close();
- // get a String representation of the round-trip.
- if (backagain.hasRootElement()) {
- normalizeAttributes(backagain.getRootElement());
- }
- StringWriter swb = new StringWriter();
- xsw = soutfactory.createXMLStreamWriter(swb);
- xout.output(backagain, xsw);
- String actual = swb.toString();
+ UnitTestUtil.compare(expect, backagain);
- assertEquals(expect, actual);
} catch (Exception e) {
failException("Failed to round-trip the document with exception: "
+ e.getMessage(), e);
@@ -741,6 +738,13 @@ public void testRTOutputDocumentSimple() {
roundTripDocument(doc);
}
+ @Test
+ public void testRTOutputDocumentDefaultNS() {
+ Element root = new Element("root", "http://jdom/noprefix");
+ Document doc = new Document(root);
+ roundTripDocument(doc);
+ }
+
@Test
@Ignore
// TODO
View
18 test/src/java/org/jdom2/test/util/UnitTestUtil.java
@@ -15,10 +15,10 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
-import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jdom2.Attribute;
@@ -210,16 +210,12 @@ public static final void normalizeAttributes(Element emt) {
if (!emt.hasAttributes()) {
return;
}
- TreeMap<String,Attribute> sorted = new TreeMap<String, Attribute>();
- List<?> atts = emt.getAttributes();
- for (Object o : atts.toArray()) {
- Attribute a = (Attribute)o;
- sorted.put(a.getQualifiedName(), a);
- a.detach();
- }
- for (Attribute a : sorted.values()) {
- emt.setAttribute(a);
- }
+ emt.sortAttributes(new Comparator<Attribute>() {
+ @Override
+ public int compare(Attribute o1, Attribute o2) {
+ return o1.getQualifiedName().compareTo(o2.getQualifiedName());
+ }
+ });
for (Object o : emt.getChildren()) {
normalizeAttributes((Element)o);
}
Please sign in to comment.
Something went wrong with that request. Please try again.