Index: src/java/com/ctc/wstx/api/CommonConfig.java =================================================================== --- src/java/com/ctc/wstx/api/CommonConfig.java (revision 1096) +++ src/java/com/ctc/wstx/api/CommonConfig.java (working copy) @@ -49,6 +49,8 @@ final static int PROP_SUPPORTS_XML11 = 3; final static int PROP_SUPPORT_XMLID = 4; + + final static int PROP_RETURN_NULL_FOR_DEFAULT_NAMESPACE = 5; /** * Map to use for converting from String property ids to enumeration @@ -70,6 +72,8 @@ sStdProperties.put(XMLStreamProperties.XSP_SUPPORT_XMLID, DataUtil.Integer(PROP_SUPPORT_XMLID)); + sStdProperties.put(XMLStreamProperties.RETURN_NULL_FOR_DEFAULT_NAMESPACE, + DataUtil.Integer(PROP_RETURN_NULL_FOR_DEFAULT_NAMESPACE)); /* 23-Apr-2008, tatus: Additional interoperability property, * one that Sun implementation uses. Can map tor Stax2 @@ -77,6 +81,7 @@ */ sStdProperties.put("http://java.sun.com/xml/stream/properties/implementation-name", DataUtil.Integer(PROP_IMPL_NAME)); + } protected CommonConfig() { } @@ -194,6 +199,10 @@ return true; } + protected boolean returnNullForDefaultNamespace() { + return Boolean.getBoolean(XMLStreamProperties.RETURN_NULL_FOR_DEFAULT_NAMESPACE); + } + protected abstract Object getProperty(int id); protected abstract boolean setProperty(String propName, int id, Object value); @@ -227,6 +236,8 @@ return doesSupportXml11() ? Boolean.TRUE : Boolean.FALSE; case PROP_SUPPORT_XMLID: return doesSupportXmlId() ? Boolean.TRUE : Boolean.FALSE; + case PROP_RETURN_NULL_FOR_DEFAULT_NAMESPACE: + return returnNullForDefaultNamespace() ? Boolean.TRUE : Boolean.FALSE; default: // sanity check, should never happen throw new IllegalStateException("Internal error: no handler for property with internal id "+id+"."); } Index: src/java/com/ctc/wstx/sr/BasicStreamReader.java =================================================================== --- src/java/com/ctc/wstx/sr/BasicStreamReader.java (revision 1096) +++ src/java/com/ctc/wstx/sr/BasicStreamReader.java (working copy) @@ -33,6 +33,7 @@ import org.codehaus.stax2.DTDInfo; import org.codehaus.stax2.LocationInfo; import org.codehaus.stax2.XMLStreamLocation2; +import org.codehaus.stax2.XMLStreamProperties; import org.codehaus.stax2.XMLStreamReader2; import org.codehaus.stax2.typed.TypedXMLStreamException; import org.codehaus.stax2.validation.*; @@ -62,6 +63,7 @@ extends StreamScanner implements StreamReaderImpl, DTDInfo, LocationInfo { + /* /////////////////////////////////////////////////////////////////////// // Constants @@ -367,6 +369,8 @@ * have straight-forward static rules). */ protected int mVldContent = XMLValidator.CONTENT_ALLOW_ANY_TEXT; + + protected boolean returnNullForDefaultNamespace; /* /////////////////////////////////////////////////////////////////////// @@ -469,6 +473,9 @@ input.initInputLocation(this, mCurrDepth); elemStack.connectReporter(this); + + Object value = getProperty(XMLStreamProperties.RETURN_NULL_FOR_DEFAULT_NAMESPACE); + returnNullForDefaultNamespace = (value instanceof Boolean) && ((Boolean) value).booleanValue(); } protected static InputElementStack createElementStack(ReaderConfig cfg) @@ -767,6 +774,9 @@ } // Internally it's marked as null, externally need to see "" String p = mElementStack.getLocalNsPrefix(index); + if (returnNullForDefaultNamespace) { + return p; + } return (p == null) ? XmlConsts.ATTR_NO_PREFIX : p; } Index: src/java/com/ctc/wstx/sw/BaseStreamWriter.java =================================================================== --- src/java/com/ctc/wstx/sw/BaseStreamWriter.java (revision 1096) +++ src/java/com/ctc/wstx/sw/BaseStreamWriter.java (working copy) @@ -32,6 +32,7 @@ import org.codehaus.stax2.DTDInfo; import org.codehaus.stax2.XMLStreamLocation2; +import org.codehaus.stax2.XMLStreamProperties; import org.codehaus.stax2.XMLStreamReader2; import org.codehaus.stax2.ri.Stax2WriterImpl; import org.codehaus.stax2.validation.*; @@ -203,6 +204,8 @@ * errors that validators might have trouble dealing with). */ protected String mDtdRootElem = null; + + protected boolean returnNullForDefaultNamespace; /* //////////////////////////////////////////////////// @@ -224,6 +227,9 @@ mCfgAutomaticEmptyElems = (flags & OutputConfigFlags.CFG_AUTOMATIC_EMPTY_ELEMENTS) != 0; mCfgCDataAsText = (flags & OutputConfigFlags.CFG_OUTPUT_CDATA_AS_TEXT) != 0; mCfgCopyDefaultAttrs = (flags & OutputConfigFlags.CFG_COPY_DEFAULT_ATTRS) != 0; + + Object value = getProperty(XMLStreamProperties.RETURN_NULL_FOR_DEFAULT_NAMESPACE); + returnNullForDefaultNamespace = (value instanceof Boolean) && ((Boolean) value).booleanValue(); } /* Index: src/java/com/ctc/wstx/sw/SimpleNsStreamWriter.java =================================================================== --- src/java/com/ctc/wstx/sw/SimpleNsStreamWriter.java (revision 1096) +++ src/java/com/ctc/wstx/sw/SimpleNsStreamWriter.java (working copy) @@ -72,8 +72,8 @@ throwOutputError(ErrorConsts.WERR_ATTR_NO_ELEM); } String prefix = mCurrElem.getExplicitPrefix(nsURI); - if (prefix == null) { - throwOutputError("Unbound namespace URI '"+nsURI+"'"); + if (!returnNullForDefaultNamespace && prefix == null) { + throwOutputError("Unbound namespace URI '" + nsURI + "'"); } doWriteAttr(localName, nsURI, prefix, value); } @@ -160,7 +160,7 @@ { QName name = elem.getName(); Iterator it = elem.getNamespaces(); - + while (it.hasNext()) { Namespace ns = (Namespace) it.next(); // First need to 'declare' namespace: @@ -201,10 +201,10 @@ writeNamespace(prefix, ns.getNamespaceURI()); } } - + // And finally, need to output attributes as well: - + it = elem.getAttributes(); while (it.hasNext()) { Attribute attr = (Attribute) it.next(); @@ -228,7 +228,7 @@ // Need a prefix... String prefix = mCurrElem.getPrefix(nsURI); if (prefix == null) { - throw new XMLStreamException("Unbound namespace URI '"+nsURI+"'"); + throw new XMLStreamException("ivanivanivan00002 Unbound namespace URI '"+nsURI+"'"); } checkStartElement(localName, prefix); if (mValidator != null) { @@ -295,17 +295,17 @@ } } } - + writeStartElement(elemStack.getPrefix(), elemStack.getLocalName(), elemStack.getNsURI()); - + if (nsCount > 0) { // And then output actual namespace declarations: for (int i = 0; i < nsCount; ++i) { String prefix = elemStack.getLocalNsPrefix(i); String uri = elemStack.getLocalNsURI(i); - + if (prefix == null || prefix.length() == 0) { // default NS writeDefaultNamespace(uri); } else { @@ -313,12 +313,12 @@ } } } - + /* And then let's just output attributes, if any (whether to copy * implicit, aka "default" attributes, is configurable) */ int attrCount = mCfgCopyDefaultAttrs ? - attrCollector.getCount() : + attrCollector.getCount() : attrCollector.getSpecifiedCount(); if (attrCount > 0) { Index: src/java/org/codehaus/stax2/XMLStreamProperties.java =================================================================== --- src/java/org/codehaus/stax2/XMLStreamProperties.java (revision 1096) +++ src/java/org/codehaus/stax2/XMLStreamProperties.java (working copy) @@ -38,6 +38,12 @@ */ public final static String XSP_SUPPORTS_XML11 = "org.codehaus.stax2.supportsXml11"; + /** + * This read-only property indicates whether null is returned for default name space prefix; + * Boolean.TRUE indicates it does, Boolean.FALSE that it does not. + */ + public final static String RETURN_NULL_FOR_DEFAULT_NAMESPACE = "org.codehaus.stax2.default_namespace.return_null"; + // // // Re-declared properties from XMLInputFactory /** Index: src/test/wstxtest/TestDefaultNamespacePrefix.java =================================================================== --- src/test/wstxtest/TestDefaultNamespacePrefix.java (revision 0) +++ src/test/wstxtest/TestDefaultNamespacePrefix.java (revision 0) @@ -0,0 +1,35 @@ + +package wstxtest; + +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.events.XMLEvent; + +import org.codehaus.stax2.XMLStreamProperties; + +/** + * + * + * @version $Rev$ $Date$ + */ +public class TestDefaultNamespacePrefix extends BaseWstxTest { + + public void testDefultNamespacePrefix() throws Exception { + String XML = "foo"; + System.setProperty("org.codehaus.stax2.default_namespace.return_null", "true"); + XMLInputFactory factory = getInputFactory(); + XMLStreamReader r = factory.createXMLStreamReader(new StringReader(XML)); + while (r.hasNext()) { + r.next(); + if ((r.getEventType() == XMLEvent.START_ELEMENT) && (r.getLocalName().equals("blah"))) { + String prefix = r.getNamespacePrefix(0); + if (prefix != null) { + throw new Exception("Null value is not returned for the default namespace prefix while " + XMLStreamProperties.RETURN_NULL_FOR_DEFAULT_NAMESPACE + " is set true"); + } + break; + } + } + } +} Property changes on: src\test\wstxtest\TestDefaultNamespacePrefix.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Date Revision Added: svn:eol-style + native