Skip to content
Browse files

Fixes #60 - NO_NAMESPACE attribute Namespace being pushed out in SAXO…

…utputter
  • Loading branch information...
1 parent 7031432 commit 1c453a92c3cd8e33fd628e920f2c31785dcde539 @rolfl rolfl committed
View
6 core/src/java/org/jdom/output/SAXOutputter.java
@@ -972,6 +972,12 @@ private Attributes startPrefixMapping(Element element,
while (itr.hasNext()) {
Attribute att = (Attribute)itr.next();
ns = att.getNamespace();
+ if (ns == Namespace.NO_NAMESPACE) {
+ // Issue #60
+ // no-prefix attributes are always in the NO_NAMESPACE
+ // namespace. This prefix mapping is implied for Attributes.
+ continue;
+ }
String prefix = ns.getPrefix();
String uri = namespaces.getURI(prefix);
if (!ns.getURI().equals(uri)) {
View
56 test/src/java/org/jdom/test/cases/output/TestSAXOutputter.java
@@ -0,0 +1,56 @@
+package org.jdom.test.cases.output;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.DefaultHandler2;
+
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.JDOMException;
+import org.jdom.Namespace;
+import org.jdom.output.SAXOutputter;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Rolf Lear
+ *
+ */
+public class TestSAXOutputter extends TestCase {
+
+ /**
+ * @throws JDOMException if there's a problem
+ */
+ public void testNoNamespaceIssue60 () throws JDOMException {
+ Document doc = new Document();
+ Namespace ns = Namespace.getNamespace("myurl");
+ Element root = new Element("root", ns);
+ Element child = new Element("child", ns);
+ root.addContent(child);
+ doc.setRootElement(root);
+ final String[] count = new String[1];
+
+ child.setAttribute("att", "val");
+
+ ContentHandler ch = new DefaultHandler2() {
+ public void startPrefixMapping(String pfx, String uri)
+ throws SAXException {
+ if ("".equals(pfx) && "".equals(uri)) {
+ fail("Should not be firing xmlns=\"\"");
+ }
+ if (!"".equals(pfx)) {
+ fail("we should not have prefix " + pfx);
+ }
+ if (count[0] != null) {
+ fail("we should not have multiple mappings " + pfx + " -> " + uri);
+ }
+ count[0] = uri;
+ }
+ };
+ SAXOutputter saxout = new SAXOutputter(ch);
+ saxout.output(doc);
+ assertTrue("myurl".equals(count[0]));
+ }
+
+
+}

0 comments on commit 1c453a9

Please sign in to comment.
Something went wrong with that request. Please try again.