[Practicalxml-commits] SF.net SVN: practicalxml:[96] branches/dev-1.1/src
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2009-07-23 23:28:39
|
Revision: 96 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=96&view=rev Author: kdgregory Date: 2009-07-23 23:28:30 +0000 (Thu, 23 Jul 2009) Log Message: ----------- checkpoint - XML to Bean conversion Added Paths: ----------- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanDriver.java branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanHandler.java branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanOptions.java branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestXml2BeanHandler.java Added: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanDriver.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanDriver.java (rev 0) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanDriver.java 2009-07-23 23:28:30 UTC (rev 96) @@ -0,0 +1,35 @@ +// 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; + +import org.w3c.dom.Element; + + +/** + * Driver class for converting an XML DOM into a Java bean, using a {@link + * Xml2BeanHandler} to manage data conversion. + */ +public class Xml2BeanDriver +{ + /** + * Fills a bean-style object from an XML element. For each child of the + * passed element, tries to find a property with the same name, invokes + * the appropriate conversion handler method, and sets the property from + * the result. + */ + public void dispatch(Element elem, Object bean) + { + } +} Added: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanHandler.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanHandler.java (rev 0) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanHandler.java 2009-07-23 23:28:30 UTC (rev 96) @@ -0,0 +1,171 @@ +// 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; + +import net.sf.practicalxml.DomUtil; +import net.sf.practicalxml.XmlUtil; + +import java.util.Arrays; +import java.util.Date; +import java.util.EnumSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import javax.xml.XMLConstants; + +import org.w3c.dom.Element; + + +/** + * Invoked by {@link Xml2BeanDriver} to convert a DOM <code>Element</code> + * into the appropriate Java object. Unlike {@link Bean2XmlHandler}, there + * will only be one instance of this object created during conversion; all + * intermediate data can be held on the stack. + */ +public class Xml2BeanHandler +{ + private EnumSet<Xml2BeanOptions> _options; + + + /** + * Public constructor, allowing various options specifications. + */ + public Xml2BeanHandler(Xml2BeanOptions... options) + { + _options = EnumSet.noneOf(Xml2BeanOptions.class); + for (Xml2BeanOptions option : options) + _options.add(option); + } + + +//---------------------------------------------------------------------------- +// Public Methods +//---------------------------------------------------------------------------- + + + public Boolean convertBoolean(Element elem) + { + String text = DomUtil.getText(elem); + return Boolean.valueOf(XmlUtil.parseXsdBoolean(text)); + } + + + public Byte convertByte(Element elem) + { + String text = DomUtil.getText(elem); + return Byte.valueOf(text); + } + + + public Character convertCharacter(Element elem) + { + String text = DomUtil.getText(elem); + if (text.length() == 0) + return '\0'; + return Character.valueOf(text.charAt(0)); + } + + + public Date convertDate(Element elem) + { + throw new UnsupportedOperationException("not implemented yet"); + } + + + public Double convertDouble(Element elem) + { + String text = DomUtil.getText(elem); + return Double.valueOf(text); + } + + + public Float convertFloat(Element elem) + { + String text = DomUtil.getText(elem); + return Float.valueOf(text); + } + + + public Integer convertInteger(Element elem) + { + String text = DomUtil.getText(elem); + return Integer.valueOf(text); + } + + + public Long convertLong(Element elem) + { + String text = DomUtil.getText(elem); + return Long.valueOf(text); + } + + + public Number convertNumber(Element elem) + { + throw new UnsupportedOperationException("not implemented yet"); + } + + + public Short convertShort(Element elem) + { + String text = DomUtil.getText(elem); + return Short.valueOf(text); + } + + + public String convertString(Element elem) + { + String text = DomUtil.getText(elem); + return text; + } + + + public List<?> convertList(Element elem) + { + throw new UnsupportedOperationException("not implemented yet"); + } + + + public Set<?> convertSet(Element elem) + { + throw new UnsupportedOperationException("not implemented yet"); + } + + + public Map<?,?> convertMap(Element elem) + { + throw new UnsupportedOperationException("not implemented yet"); + } + + + public Object convertObject(Element elem) + { + throw new UnsupportedOperationException("not implemented yet"); + } + + + public Object[] convertObjectArray(Element elem) + { + throw new UnsupportedOperationException("not implemented yet"); + } + + +//---------------------------------------------------------------------------- +// Internals +//---------------------------------------------------------------------------- + + +} Added: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanOptions.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanOptions.java (rev 0) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/Xml2BeanOptions.java 2009-07-23 23:28:30 UTC (rev 96) @@ -0,0 +1,55 @@ +// 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; + + +/** + * Options used by {@link Xml2BeanHandler} to control the way that DOM trees + * are translated to Java beans. + */ +public enum Xml2BeanOptions +{ + /** + * If present, the converter ignores elements that don't correspond to + * settable properties of the bean. + */ + IGNORE_MISSING_PROPERTIES, + + + /** + * If present, the converter will ignore any objects that cannot be + * converted (many of the JDK-provided classes fall into this category, + * because they're not bean-structured). + */ + IGNORE_UNCONVERTIBLE_OBJECTS, + + + /** + * If present, the converter requires an <code>xsi:type</code> attribute + * on each element, and will throw if it's not present. Default behavior + * uses the <code>xsi:type</code> value to choose between different setter + * methods, but otherwise ignores it. + */ + REQUIRE_XSI_TYPE, + + + /** + * If present, the converter will use a setter method taking a String, in + * preference to any other type. Default behavior is to pick the method + * with the most restrictive type (and considering numeric types as more + * restrictive than <code>String</code>). + */ + PREFER_STRING_SETTER +} Added: branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestXml2BeanHandler.java =================================================================== --- branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestXml2BeanHandler.java (rev 0) +++ branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/TestXml2BeanHandler.java 2009-07-23 23:28:30 UTC (rev 96) @@ -0,0 +1,200 @@ +// 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; + +import org.w3c.dom.Element; + + +import net.sf.practicalxml.builder.XmlBuilder; + + +public class TestXml2BeanHandler +extends AbstractBeanConverterTestCase +{ + public TestXml2BeanHandler(String name) + { + super(name); + } + + +//---------------------------------------------------------------------------- +// Support Code +//---------------------------------------------------------------------------- + + /** + * Builds a DOM tree with a single element, containing the specified + * text as a child of the root. We don't care about the element's name; + * it's the text that's important. + */ + private Element createElement(String data) + { + return XmlBuilder.element("data", XmlBuilder.text(data)) + .toDOM().getDocumentElement(); + } + + +//---------------------------------------------------------------------------- +// Test Cases +//---------------------------------------------------------------------------- + + + public void testConvertBoolean() throws Exception + { + Xml2BeanHandler handler = new Xml2BeanHandler(); + + Element data1 = createElement("true"); + assertEquals(Boolean.TRUE, handler.convertBoolean(data1)); + + Element data2 = createElement("1"); + assertEquals(Boolean.TRUE, handler.convertBoolean(data2)); + + Element data3 = createElement("false"); + assertEquals(Boolean.FALSE, handler.convertBoolean(data3)); + + Element data4 = createElement("0"); + assertEquals(Boolean.FALSE, handler.convertBoolean(data4)); + + // FIXME - test failure + } + + + public void testConvertByte() throws Exception + { + Xml2BeanHandler handler = new Xml2BeanHandler(); + + Element data1 = createElement("123"); + assertEquals(Byte.valueOf((byte)123), handler.convertByte(data1)); + + Element data2 = createElement("-123"); + assertEquals(Byte.valueOf((byte)-123), handler.convertByte(data2)); + + // FIXME - test failure + } + + + public void testConvertCharacter() throws Exception + { + Xml2BeanHandler handler = new Xml2BeanHandler(); + + Element data1 = createElement("A"); + assertEquals(Character.valueOf('A'), handler.convertCharacter(data1)); + + Element data2 = createElement("\uFB01"); + assertEquals(Character.valueOf('\uFB01'), handler.convertCharacter(data2)); + + Element data3 = createElement(""); + assertEquals(Character.valueOf('\0'), handler.convertCharacter(data3)); + + // FIXME - test failure -- multi-character? + } + + +// public void testConvertDate() throws Exception +// { +// } + + + public void testConvertDouble() throws Exception + { + Xml2BeanHandler handler = new Xml2BeanHandler(); + + Element data = createElement("1234567890.125"); + assertEquals(Double.valueOf(1234567890.125), handler.convertDouble(data)); + + // FIXME - test failure + } + + + public void testConvertFloat() throws Exception + { + Xml2BeanHandler handler = new Xml2BeanHandler(); + + Element data = createElement("1234.5"); + assertEquals(Float.valueOf((float)1234.5), handler.convertFloat(data)); + + // FIXME - test failure + } + + + public void testConvertInteger() throws Exception + { + Xml2BeanHandler handler = new Xml2BeanHandler(); + + Element data = createElement("123456789"); + assertEquals(Integer.valueOf(123456789), handler.convertInteger(data)); + + // FIXME - test failure + } + + + public void testConvertLong() throws Exception + { + Xml2BeanHandler handler = new Xml2BeanHandler(); + + Element data = createElement("1234567890123456"); + assertEquals(Long.valueOf(1234567890123456L), handler.convertLong(data)); + + // FIXME - test failure + } + + +// public void testConvertNumber() throws Exception +// { +// } + + + public void testConvertShort() throws Exception + { + Xml2BeanHandler handler = new Xml2BeanHandler(); + + Element data = createElement("12345"); + assertEquals(Short.valueOf((short)12345), handler.convertShort(data)); + + // FIXME - test failure + } + + + public void testConvertString() throws Exception + { + Xml2BeanHandler handler = new Xml2BeanHandler(); + + Element data = createElement("foo"); + assertEquals("foo", handler.convertString(data)); + } + +// public void testConvertList() throws Exception +// { +// } +// +// +// public void testConvertSet() throws Exception +// { +// } +// +// +// public void testConvertMap() throws Exception +// { +// } +// +// +// public void testConvertObject() throws Exception +// { +// } +// +// +// public void testConvertObjectArray() throws Exception +// { +// } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |