[Practicalxml-commits] SF.net SVN: practicalxml:[118] branches/dev-1.1/src/main/java/net/sf/ practi
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2009-08-18 19:50:47
|
Revision: 118 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=118&view=rev Author: kdgregory Date: 2009-08-18 19:50:39 +0000 (Tue, 18 Aug 2009) Log Message: ----------- refactoring Modified Paths: -------------- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Bean2XmlAppenders.java branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Bean2XmlDriver.java branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Xml2BeanDriver.java Added Paths: ----------- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/internal/DomUtilToo.java Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Bean2XmlAppenders.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Bean2XmlAppenders.java 2009-08-18 19:16:50 UTC (rev 117) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Bean2XmlAppenders.java 2009-08-18 19:50:39 UTC (rev 118) @@ -15,13 +15,11 @@ package net.sf.practicalxml.converter.bean; import java.util.EnumSet; - -import javax.xml.XMLConstants; - import org.w3c.dom.Element; import net.sf.practicalxml.DomUtil; import net.sf.practicalxml.converter.ConversionException; +import net.sf.practicalxml.converter.internal.DomUtilToo; /** @@ -95,7 +93,7 @@ protected void setType(Element elem, String type) { if (isOptionSet(Bean2XmlOptions.ADD_XSI_TYPE)) - elem.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type", type); + DomUtilToo.setXsiType(elem, type); } protected void setValue(Element elem, String value) @@ -103,7 +101,7 @@ if (value != null) DomUtil.setText(elem, value); else if (isOptionSet(Bean2XmlOptions.XSI_NIL)) - elem.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil", "true"); + DomUtilToo.setXsiNil(elem, true); } } Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Bean2XmlDriver.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Bean2XmlDriver.java 2009-08-18 19:16:50 UTC (rev 117) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Bean2XmlDriver.java 2009-08-18 19:50:39 UTC (rev 118) @@ -24,13 +24,12 @@ import java.util.EnumSet; import java.util.Map; -import javax.xml.XMLConstants; - import org.w3c.dom.Element; import net.sf.practicalxml.DomUtil; import net.sf.practicalxml.converter.ConversionException; import net.sf.practicalxml.converter.bean.Bean2XmlAppenders.*; +import net.sf.practicalxml.converter.internal.DomUtilToo; import net.sf.practicalxml.converter.internal.PrimitiveConversionHelper; @@ -145,7 +144,7 @@ if (_options.contains(Bean2XmlOptions.XSI_NIL) && !_options.contains(Bean2XmlOptions.ADD_XSI_TYPE)) { - root.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil", "false"); + DomUtilToo.setXsiNil(root, false); } } Modified: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Xml2BeanDriver.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Xml2BeanDriver.java 2009-08-18 19:16:50 UTC (rev 117) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/bean/Xml2BeanDriver.java 2009-08-18 19:50:39 UTC (rev 118) @@ -33,10 +33,9 @@ import java.util.TreeMap; import java.util.TreeSet; -import javax.xml.XMLConstants; - import net.sf.practicalxml.DomUtil; import net.sf.practicalxml.converter.ConversionException; +import net.sf.practicalxml.converter.internal.DomUtilToo; import net.sf.practicalxml.converter.internal.PrimitiveConversionHelper; import net.sf.practicalxml.internal.StringUtils; @@ -121,9 +120,8 @@ if (_options.contains(Xml2BeanOptions.REQUIRE_XSI_NIL)) { - String attr = elem.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil"); - if (!attr.equals("true")) - throw new ConversionException("missing xsi:nil", elem); + if (!DomUtilToo.getXsiNil(elem)) + throw new ConversionException("missing/false xsi:nil", elem); } return true; @@ -235,19 +233,6 @@ /** - * Returns the <code>xsi:type</code> attribute value, <code>null</code> if - * it's not set. - */ - private String getXsiType(Element elem) - { - String xsiType = elem.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); - return StringUtils.isEmpty(xsiType) - ? null - : xsiType; - } - - - /** * Examines an element's <code>xsi:type</code> attribute, if any, and * returns the Java class corresponding to it. Used when converting * collection types, which don't have type information that can be @@ -255,7 +240,7 @@ */ private Class<?> getClassFromXsiType(Element elem) { - String xsiType = getXsiType(elem); + String xsiType = DomUtilToo.getXsiType(elem); if (xsiType == null) return null; @@ -281,8 +266,8 @@ if (!_options.contains(Xml2BeanOptions.REQUIRE_XSI_TYPE)) return; - String xsiType = elem.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); - if (StringUtils.isEmpty(xsiType)) + String xsiType = DomUtilToo.getXsiType(elem); + if (xsiType == null) throw new ConversionException("missing xsi:type", elem); if (xsiType.equals(_helper.getXsdType(klass))) Added: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/internal/DomUtilToo.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/internal/DomUtilToo.java (rev 0) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/internal/DomUtilToo.java 2009-08-18 19:50:39 UTC (rev 118) @@ -0,0 +1,79 @@ +// Copyright 2008-2009 severally by the contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package net.sf.practicalxml.converter.internal; + +import javax.xml.XMLConstants; + +import org.w3c.dom.Element; + +import net.sf.practicalxml.internal.StringUtils; + + +/** + * Contains static utility methods and constants used by the conversion routines. + * These are way too specific to go into the top-level <code>DomUtil</code>, and + * are not generally useful to anyone who isn't writing a converter. But if you + * are writing a converter, you'll probably use them a lot ... + * <p> + * Note: where methods in this class reference a namespaced element or attribute + * (eg, <code>xsi:type</code>), they do not use a prefix unless explicitly noted. + * This prevents the possibility of collisions, where the same prefix is used + * elsewhere in the DOM for elements not managed by the converter. A serializer + * will pick an appropriate prefix for output. + */ +public class DomUtilToo +{ + /** + * Sets the <code>xsi:type</code> attribute to the passed value. + */ + public static void setXsiType(Element elem, String xsiType) + { + elem.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type", xsiType); + } + + + /** + * Returns the value of the <code>xsi:type</code> attribute on the passed + * element, <code>null</code> if the attribute is not set. + */ + public static String getXsiType(Element elem) + { + String xsiType = elem.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); + return StringUtils.isEmpty(xsiType) + ? null + : xsiType; + } + + + /** + * Sets the <code>xsi:nil</code> attribute to the passed value. + */ + public static void setXsiNil(Element elem, boolean isNil) + { + String value = isNil ? "true" : "false"; + elem.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil", value); + } + + + /** + * Returns the value of the <code>xsi:nil</code> attribute on the passed + * element, <code>false</code> if the attribute is not set. + */ + public static boolean getXsiNil(Element elem) + { + String attr = elem.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil"); + return attr.equals("true"); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |