Thread: [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. |
From: Auto-Generated S. C. M. <pra...@li...> - 2009-08-15 18:13:31
|
Revision: 107 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=107&view=rev Author: kdgregory Date: 2009-08-15 18:13:24 +0000 (Sat, 15 Aug 2009) Log Message: ----------- comment change, private var name change 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:20:09 UTC (rev 106) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean2xml/Bean2XmlDriver.java 2009-08-15 18:13:24 UTC (rev 107) @@ -35,22 +35,20 @@ /** - * Driver class for converting bean data to an XML representation. In normal - * usage, an instance of this class is constructed with the desired conversion - * options, then {@link #convert} is called with the compound object to be - * converted. The driver may be reused for multiple conversions, and is - * thread-safe. + * Driver class for converting a Java bean into an XML DOM. Normal usage is + * to create a single instance of this class with desired options, then use + * it for multiple conversions. This class is thread-safe. */ public class Bean2XmlDriver { + private ConversionHelper _helper; private EnumSet<Bean2XmlOptions> _options = EnumSet.noneOf(Bean2XmlOptions.class); - private ConversionHelper _primitiveHelper; public Bean2XmlDriver(Bean2XmlOptions... options) { + _helper = new ConversionHelper(shouldUseXsdFormatting()); for (Bean2XmlOptions option : options) _options.add(option); - _primitiveHelper = new ConversionHelper(shouldUseXsdFormatting()); } @@ -158,10 +156,10 @@ if (obj != null) klass = obj.getClass(); - String objType = _primitiveHelper.getXsdType(klass); + String objType = _helper.getXsdType(klass); if ((obj == null) || (objType != null)) { - appender.appendValue(name, objType, _primitiveHelper.stringify(obj)); + appender.appendValue(name, objType, _helper.stringify(obj)); return true; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: Auto-Generated S. C. M. <pra...@li...> - 2009-08-15 18:57:45
|
Revision: 108 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=108&view=rev Author: kdgregory Date: 2009-08-15 18:57:35 +0000 (Sat, 15 Aug 2009) Log Message: ----------- fixed bug caused by re-arranging code in ctor 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-15 18:13:24 UTC (rev 107) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean2xml/Bean2XmlDriver.java 2009-08-15 18:57:35 UTC (rev 108) @@ -46,9 +46,9 @@ public Bean2XmlDriver(Bean2XmlOptions... options) { - _helper = new ConversionHelper(shouldUseXsdFormatting()); for (Bean2XmlOptions option : options) _options.add(option); + _helper = new ConversionHelper(shouldUseXsdFormatting()); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |