Skip to content

Commit

Permalink
Issue #2 - Cater for unusual instance where the xmlns prefix setting may
Browse files Browse the repository at this point in the history
come through to the SAXHandler, but from a namespace-prefixes=false
source, which would mean that the namespace declaration would have the
XMLNS namespace, but no xmlns: prefix.
  • Loading branch information
rolfl committed Oct 7, 2011
1 parent 9edf1b0 commit c9ced41
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
38 changes: 32 additions & 6 deletions core/src/java/org/jdom/input/SAXHandler.java
Expand Up @@ -56,12 +56,28 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT

package org.jdom.input;

import java.util.*;

import org.jdom.*;
import org.xml.sax.*;
import org.xml.sax.ext.*;
import org.xml.sax.helpers.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Attribute;
import org.jdom.DefaultJDOMFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.EntityRef;
import org.jdom.JDOMFactory;
import org.jdom.Namespace;
import org.jdom.Parent;
import org.xml.sax.Attributes;
import org.xml.sax.DTDHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;

/**
* A support class for {@link SAXBuilder}.
Expand Down Expand Up @@ -592,6 +608,16 @@ public void startElement(String namespaceURI, String localName,
int attType = getAttributeType(atts.getType(i));
String attValue = atts.getValue(i);
String attURI = atts.getURI(i);

if ("xmlns".equals(attLocalName) ||
"xmlns".equals(attPrefix) ||
"http://www.w3.org/2000/xmlns/".equals(attURI)) {
// use the actual Namespace to check too, because, in theory, a
// namespace-aware parser does not need to set the qName unless
// the namespace-prefixes feature is set as well.
continue;
}

// just one thing to sort out....
// the prefix for the namespace.
if (!"".equals(attURI) && "".equals(attPrefix)) {
Expand Down
38 changes: 34 additions & 4 deletions test/src/java/org/jdom/test/cases/input/TestSAXHandler.java
Expand Up @@ -17,6 +17,19 @@

public class TestSAXHandler extends TestCase {

/**
* We have to have our own simple implementation of Attributes2 because
* the Attributes2Impl class in Java5 has a significant bug it seems...
* <p>
* .. you can't add an Attribute to it because the internal fields
* 'specified' and 'declared' are dereferenced before they are set in
* addAttribute(). his has to be one of the most stupid bugs I have
* encountered in Java code so far....
* <p>
*
* @author rolf
*
*/
private static final class Attributes2JDOM implements Attributes2 {

// support only one attribute
Expand Down Expand Up @@ -1587,13 +1600,12 @@ public void testIllegalGetCurrentElement() {

}


public void testAndroidParserIssue2() throws SAXException {
public void testAndroidParserIssue2QNameOnly() throws SAXException {
SAXHandler handler = new SAXHandler();
handler.startDocument();
Attributes2JDOM attrs = new Attributes2JDOM();
attrs.addAttribute("", "attname", "", "CDATA", "val");
handler.startElement("", "root", "", attrs);
attrs.addAttribute("", "", "attname", "CDATA", "val");
handler.startElement("", "", "root", attrs);
handler.endElement("", "root", "");
handler.endDocument();
Document doc = handler.getDocument();
Expand All @@ -1604,4 +1616,22 @@ public void testAndroidParserIssue2() throws SAXException {
assertEquals("val", att.getValue());
}

public void testAndroidParserIssue2AttXMLNS() throws SAXException {
// test a namespace-aware parser that is not configured namespace-prefixes
// in theory, it should leave the qName empty, even for an XMLNS declaration
SAXHandler handler = new SAXHandler();
handler.startDocument();
Attributes2JDOM attrs = new Attributes2JDOM();
attrs.addAttribute("http://www.w3.org/2000/xmlns/", "pfx", "", "CDATA", "nsuri");
handler.startElement("", "", "root", attrs);
handler.endElement("", "root", "");
handler.endDocument();
Document doc = handler.getDocument();
Element root = doc.getRootElement();
assertEquals("root", root.getName());
Attribute att = root.getAttribute("pfx");
assertNull(att);
assertTrue(root.getAttributes().isEmpty());
}

}

0 comments on commit c9ced41

Please sign in to comment.