[Practicalxml-commits] SF.net SVN: practicalxml:[106] branches/dev-1.1/src/main/java/net/sf/ pract
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2009-08-14 20:20:14
|
Revision: 106 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=106&view=rev Author: kdgregory Date: 2009-08-14 20:20:09 +0000 (Fri, 14 Aug 2009) Log Message: ----------- attach dummy xsi:nil to root element to prevent repetitive namespace defs by serializer Modified Paths: -------------- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean2xml/Bean2XmlDriver.java Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean2xml/Bean2XmlDriver.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean2xml/Bean2XmlDriver.java 2009-08-14 20:05:39 UTC (rev 105) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean2xml/Bean2XmlDriver.java 2009-08-14 20:20:09 UTC (rev 106) @@ -24,6 +24,8 @@ import java.util.EnumSet; import java.util.Map; +import javax.xml.XMLConstants; + import org.w3c.dom.Element; import net.sf.practicalxml.DomUtil; @@ -76,6 +78,7 @@ public Element convert(Object obj, String nsUri, String rootName) { Element root = DomUtil.newDocument(nsUri, rootName); + doXsiNamespaceHack(root); convert(obj, "", new DirectAppender(root, _options)); return root; } @@ -123,6 +126,32 @@ } + /** + * Introduces the XML Schema Instance namespace into the DOM tree using a + * meaningless attribute. The Xerces serializer does not attempt to promote + * namespace definitions above the subtree in which they first appear, which + * means that the XSI definition could be repeated many times throughout the + * serialized tree, adding bulk to the serialized representation. + * <p> + * By putting "nil=false" at the root element, we will keep the serializer + * from inserting all these definitions. This has to happen <em>before</em> + * any actual conversion, in case some bozo passes <code>null</code> to + * the top-level conversion routine. + * <p> + * Note that we only do this if <code>xsi:nil</code> is enabled by itself. + * If <code>xsi:type</code> is enabled, the converter will attach that + * attribute to the root instead, thereby establishing the namespace context. + */ + private void doXsiNamespaceHack(Element root) + { + if (_options.contains(Bean2XmlOptions.XSI_NIL) + && !_options.contains(Bean2XmlOptions.XSI_TYPE)) + { + root.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil", "false"); + } + } + + private boolean tryToConvertAsPrimitiveOrNull( Object obj, Class<?> klass, String name, Appender appender) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |