tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From svkr...@apache.org
Subject svn commit: r502160 - in /incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src: main/java/org/apache/tuscany/databinding/javabeans/ test/java/org/apache/tuscany/databinding/javabeans/
Date Thu, 01 Feb 2007 08:35:24 GMT
Author: svkrish
Date: Thu Feb  1 00:35:23 2007
New Revision: 502160

URL: http://svn.apache.org/viewvc?view=rev&rev=502160
Log:
Taken care of some sourcecheck issues

Modified:
    incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/main/java/org/apache/tuscany/databinding/javabeans/JavaBean2DOMNode.java
    incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/main/java/org/apache/tuscany/databinding/javabeans/XMLTypeMapperExtension.java
    incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/test/java/org/apache/tuscany/databinding/javabeans/XMLTypeMapperExtensionTestCase.java

Modified: incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/main/java/org/apache/tuscany/databinding/javabeans/JavaBean2DOMNode.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/main/java/org/apache/tuscany/databinding/javabeans/JavaBean2DOMNode.java?view=diff&rev=502160&r1=502159&r2=502160
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/main/java/org/apache/tuscany/databinding/javabeans/JavaBean2DOMNode.java
(original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/main/java/org/apache/tuscany/databinding/javabeans/JavaBean2DOMNode.java
Thu Feb  1 00:35:23 2007
@@ -18,46 +18,34 @@
  */
 package org.apache.tuscany.databinding.javabeans;
 
-import javax.xml.parsers.ParserConfigurationException;
-
 import org.apache.tuscany.spi.databinding.PullTransformer;
 import org.apache.tuscany.spi.databinding.TransformationContext;
-import org.apache.tuscany.spi.databinding.TransformationException;
 import org.apache.tuscany.spi.databinding.Transformer;
-import org.apache.tuscany.spi.databinding.extension.DOMHelper;
 import org.apache.tuscany.spi.databinding.extension.TransformerExtension;
-import org.apache.tuscany.spi.idl.ElementInfo;
-import org.apache.tuscany.spi.idl.TypeInfo;
 import org.osoa.sca.annotations.Service;
-import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 
 /**
  * Transformer to convert data from a JavaBean object to DOM Node
  */
 @Service(Transformer.class)
-public class JavaBean2DOMNode extends TransformerExtension<Object, Node> implements
PullTransformer<Object, Node> {
+public class JavaBean2DOMNode extends TransformerExtension<Object, Node> implements
+        PullTransformer<Object, Node> {
 
     protected XMLTypeMapperExtension<Node> mapper;
-    private Document factory;
-    
+
     public JavaBean2DOMNode() {
         this.mapper = new XMLTypeMapperExtension<Node>();
-        try {
-            factory = DOMHelper.newDocument();
-        } catch (ParserConfigurationException e) {
-            throw new TransformationException(e);
-        }
     }
-    
+
     public Node transform(Object source, TransformationContext context) {
-       return mapper.toDOMNode(source, context);
+        return mapper.toDOMNode(source, context);
     }
-    
+
     public Class getTargetType() {
         return Node.class;
     }
-    
+
     public Class getSourceType() {
         return Object.class;
     }

Modified: incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/main/java/org/apache/tuscany/databinding/javabeans/XMLTypeMapperExtension.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/main/java/org/apache/tuscany/databinding/javabeans/XMLTypeMapperExtension.java?view=diff&rev=502160&r1=502159&r2=502160
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/main/java/org/apache/tuscany/databinding/javabeans/XMLTypeMapperExtension.java
(original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/main/java/org/apache/tuscany/databinding/javabeans/XMLTypeMapperExtension.java
Thu Feb  1 00:35:23 2007
@@ -37,15 +37,14 @@
 import org.apache.tuscany.spi.databinding.TransformationContext;
 import org.apache.tuscany.spi.databinding.extension.DOMHelper;
 import org.apache.tuscany.spi.databinding.extension.SimpleTypeMapperExtension;
-import org.apache.tuscany.spi.idl.ElementInfo;
 import org.apache.tuscany.spi.idl.TypeInfo;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 /**
- *  This class maps JavaBeans objects to XML data represented physically as DOM Nodes and
vice versa.  It 
- *  uses JavaBeans Introspection for this mapping.
+ * This class maps JavaBeans objects to XML data represented physically as DOM Nodes and
vice versa. 
+ * It uses JavaBeans Introspection for this mapping.
  */
 public class XMLTypeMapperExtension<T> extends SimpleTypeMapperExtension<T> {
 
@@ -56,55 +55,76 @@
     }
 
     public Node toDOMNode(Object javaObject, TransformationContext context) {
-        QName rootElementName =
-                (QName) context.getTargetDataType().getMetadata("RootElementName");
-        if ( rootElementName == null ) {
+        QName rootElementName = (QName) context.getTargetDataType().getMetadata("RootElementName");
+        if (rootElementName == null) {
             rootElementName = new QName(resolveRootElementName(javaObject.getClass()));
         }
         try {
             factory = DOMHelper.newDocument();
             Node root = DOMHelper.createElement(factory, rootElementName);
-            appendChildElements(root, resolveElementName(javaObject.getClass()), javaObject.getClass(),
javaObject, context);
+            appendChildElements(root,
+                                resolveElementName(javaObject.getClass()),
+                                javaObject.getClass(),
+                                javaObject,
+                                context);
             return root;
-        } catch ( ParserConfigurationException e ) {
+        } catch (ParserConfigurationException e) {
             Java2XMLMapperException java2xmlEx = new Java2XMLMapperException(e);
-            java2xmlEx.addContextName("tranforming to xml, a java instance of" + javaObject.getClass().getName());
+            java2xmlEx.addContextName("tranforming to xml, a java instance of"
+                    + javaObject.getClass().getName());
             throw java2xmlEx;
         }
     }
 
-    private void appendChildElements(Node parent, String elementName, Class javaType, Object
javaObject, TransformationContext context)  {
+    private void appendChildElements(Node parent,
+                                     String elementName,
+                                     Class javaType,
+                                     Object javaObject,
+                                     TransformationContext context) {
         Node elementNode = null;
-        if ( javaObject != null ) {
+        if (javaObject != null) {
             if (javaType.isPrimitive() || isSimpleJavaType(javaObject)) {
                 parent.appendChild(factory.createTextNode(toXMLLiteral(null, javaObject,
context)));
-            } else if ( javaType.isArray() ) {
+            } else if (javaType.isArray()) {
                 boolean arrayDone = false;
                 Object arrayObject = null;
-                for (int count = 0 ; !arrayDone ; ++count) {
+                for (int count = 0; !arrayDone; ++count) {
                     try {
                         arrayObject = Array.get(javaObject, count);
                         elementNode = factory.createElement(elementName);
                         parent.appendChild(elementNode);
-                        appendChildElements(elementNode, elementName, javaType.getComponentType(),
arrayObject, context);
-                    } catch (ArrayIndexOutOfBoundsException e1 ) {
+                        appendChildElements(elementNode,
+                                            elementName,
+                                            javaType.getComponentType(),
+                                            arrayObject,
+                                            context);
+                    } catch (ArrayIndexOutOfBoundsException e1) {
                         arrayDone = true;
                     }
                 }
             } else {
                 Field[] javaFields = javaType.getFields();
                 for (Field aField : javaFields) {
-                   try {
-                       if (!aField.getType().isArray()) {
-                           elementNode = factory.createElement(aField.getName());
-                           parent.appendChild(elementNode);
-                           appendChildElements(elementNode, aField.getName(), aField.getType(),
aField.get(javaObject), context);
-                       } else {
-                           appendChildElements(parent, aField.getName(), aField.getType(),
aField.get(javaObject), context);
-                       }
-                    } catch ( IllegalAccessException e ) {
+                    try {
+                        if (!aField.getType().isArray()) {
+                            elementNode = factory.createElement(aField.getName());
+                            parent.appendChild(elementNode);
+                            appendChildElements(elementNode,
+                                                aField.getName(),
+                                                aField.getType(),
+                                                aField.get(javaObject),
+                                                context);
+                        } else {
+                            appendChildElements(parent,
+                                                aField.getName(),
+                                                aField.getType(),
+                                                aField.get(javaObject),
+                                                context);
+                        }
+                    } catch (IllegalAccessException e) {
                         Java2XMLMapperException java2xmlEx = new Java2XMLMapperException(e);
-                        java2xmlEx.addContextName("tranforming " + aField.getName() + " of
" + javaType.getName());
+                        java2xmlEx.addContextName("tranforming " + aField.getName() + " of
"
+                                + javaType.getName());
                         throw java2xmlEx;
                     }
                 }
@@ -197,9 +217,9 @@
                     }
                     setterValueArray.add(createJavaObject(fieldValue, componentType, context));
                 } else {
-                    aMethod.invoke(javaInstance, new Object[] { createJavaObject(fieldValue,
+                    aMethod.invoke(javaInstance, new Object[] {createJavaObject(fieldValue,
                                                                                  paramType,
-                                                                                 context)
});
+                                                                                 context)});
                 }
                 methodNotFound = false;
             }
@@ -261,12 +281,12 @@
                 Class paramType = aMethod.getParameterTypes()[0];
                 if (paramType.getComponentType().isPrimitive()) {
                     aMethod.invoke(javaInstance,
-                                   new Object[] { createPrimitiveArray(paramType.getComponentType(),
-                                                                       arraySetters.get(aMethod))
});
+                                   new Object[] {createPrimitiveArray(paramType.getComponentType(),
+                                                                       arraySetters.get(aMethod))});
                 } else {
                     aMethod.invoke(javaInstance,
-                                   new Object[] { createNonPrimitiveArray(paramType.getComponentType(),
-                                                                          arraySetters.get(aMethod))
});
+                                   new Object[] {createNonPrimitiveArray(paramType.getComponentType(),
+                                                                          arraySetters.get(aMethod))});
                 }
             }
         }
@@ -334,29 +354,28 @@
         }
         return values;
     }
-    
+
     private String resolveRootElementName(Class javaType) {
-        if ( javaType.isArray() ) {
+        if (javaType.isArray()) {
             return javaType.getComponentType().getSimpleName() + "_collection";
         } else {
             return javaType.getSimpleName() + "_instance";
         }
     }
-    
+
     private String resolveElementName(Class javaType) {
-        if ( javaType.isArray() ) {
+        if (javaType.isArray()) {
             return javaType.getComponentType().getSimpleName();
         } else {
             return javaType.getSimpleName();
         }
     }
-    
+
     private boolean isSimpleJavaType(Object javaObject) {
-        if ( javaObject instanceof String ||
-                javaObject instanceof Float || javaObject instanceof Double || 
-                javaObject instanceof GregorianCalendar || javaObject instanceof Date ||

-                javaObject instanceof XMLGregorianCalendar || javaObject instanceof byte[]
|| 
-                javaObject instanceof QName) {
+        if (javaObject instanceof String || javaObject instanceof Float
+                || javaObject instanceof Double || javaObject instanceof GregorianCalendar
+                || javaObject instanceof Date || javaObject instanceof XMLGregorianCalendar
+                || javaObject instanceof byte[] || javaObject instanceof QName) {
             return true;
         }
         return false;

Modified: incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/test/java/org/apache/tuscany/databinding/javabeans/XMLTypeMapperExtensionTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/test/java/org/apache/tuscany/databinding/javabeans/XMLTypeMapperExtensionTestCase.java?view=diff&rev=502160&r1=502159&r2=502160
==============================================================================
--- incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/test/java/org/apache/tuscany/databinding/javabeans/XMLTypeMapperExtensionTestCase.java
(original)
+++ incubator/tuscany/java/sca/services/databinding/databinding-javabeans/src/test/java/org/apache/tuscany/databinding/javabeans/XMLTypeMapperExtensionTestCase.java
Thu Feb  1 00:35:23 2007
@@ -23,8 +23,6 @@
 import java.io.StringWriter;
 
 import javax.xml.parsers.DocumentBuilder;
-import javax.xml.transform.Result;
-import javax.xml.transform.Source;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
@@ -44,9 +42,9 @@
  * by the JavaBeans Databinding.
  */
 public class XMLTypeMapperExtensionTestCase extends TestCase {
-    
+
     XMLTypeMapperExtension<Node> extension = new XMLTypeMapperExtension<Node>();
-    
+
     /**
      * @see junit.framework.TestCase#setUp()
      */
@@ -55,92 +53,93 @@
     }
 
     public void testFieldSettings() throws Exception {
-        String samplePropertyXML = "<property name=\"prop2\" >" 
-                                            + "<integerNumber>27</integerNumber>"

-                                            + "<floatNumber>79.34</floatNumber>"

-                                            + "<doubleNumber>184.52</doubleNumber>"

-                                            + "<innerProperty>" 
-                                            + "<integerNumber>54</integerNumber>"

-                                            + "<floatNumber>158.68</floatNumber>"

-                                            + "<doubleNumber>369.04</doubleNumber>"

-                                            + "</innerProperty>" 
-                                            + "<stringArray>TestString_1</stringArray>"

-                                            + "<stringArray>TestString_2</stringArray>"

-                                            + "<boolArray>true</boolArray>" 
-                                            + "<boolArray>false</boolArray>"

-                                    + "</property>";
-        
+        String samplePropertyXML =
+                "<property name=\"prop2\" >" + "<integerNumber>27</integerNumber>"
+                        + "<floatNumber>79.34</floatNumber>"
+                        + "<doubleNumber>184.52</doubleNumber>" + "<innerProperty>"
+                        + "<integerNumber>54</integerNumber>" + "<floatNumber>158.68</floatNumber>"
+                        + "<doubleNumber>369.04</doubleNumber>" + "</innerProperty>"
+                        + "<stringArray>TestString_1</stringArray>"
+                        + "<stringArray>TestString_2</stringArray>" + "<boolArray>true</boolArray>"
+                        + "<boolArray>false</boolArray>" + "</property>";
+
         DocumentBuilder builder = DOMHelper.newDocumentBuilder();
         InputSource inputSource = new InputSource(new StringReader(samplePropertyXML));
-        Node samplePropertyNode =  builder.parse(inputSource);
+        Node samplePropertyNode = builder.parse(inputSource);
         TypeInfo typeInfo = new TypeInfo(null, false, null);
-        
+
         TransformationContext context = EasyMock.createMock(TransformationContext.class);
         DataType<Class> dataType = new DataType<Class>(null, SamplePropertyBean.class);
         EasyMock.expect(context.getTargetDataType()).andReturn(dataType).anyTimes();
         EasyMock.replay(context);
-        
+
         Object javaObject = extension.toJavaObject(typeInfo, samplePropertyNode, context);
-        
+
         assertTrue(javaObject instanceof SamplePropertyBean);
-        SamplePropertyBean samplePropBean = (SamplePropertyBean)javaObject;
+        SamplePropertyBean samplePropBean = (SamplePropertyBean) javaObject;
         assertEquals(samplePropBean.getIntegerNumber(), 27);
-        assertEquals((float)79.34, (float)samplePropBean.getFloatNumber());
-        assertEquals((double)samplePropBean.getInnerProperty().getDoubleNumber(), (double)369.04);
-        
+        assertEquals((float) 79.34, (float) samplePropBean.getFloatNumber());
+        assertEquals((double) samplePropBean.getInnerProperty().getDoubleNumber(), (double)
369.04);
+
         assertEquals(samplePropBean.getStringArray()[0], "TestString_1");
         assertEquals(samplePropBean.boolArray[0], true);
-        
-        javax.xml.transform.Transformer transformer = TransformerFactory.newInstance().newTransformer();
+
+        javax.xml.transform.Transformer transformer =
+                TransformerFactory.newInstance().newTransformer();
         Node aNode = extension.toDOMNode(javaObject, context);
         StringWriter sw = new StringWriter();
         transformer.transform(new DOMSource(aNode), new StreamResult(sw));
         String nodeString = sw.toString();
         //System.out.println(nodeString);
-        
-        assertTrue(nodeString.indexOf("<stringArray>TestString_1</stringArray>"

-                                         + "<stringArray>TestString_2</stringArray>")
!= -1);
+
+        assertTrue(nodeString.indexOf("<stringArray>TestString_1</stringArray>"
+                + "<stringArray>TestString_2</stringArray>") != -1);
         assertTrue(nodeString.indexOf("<integerNumber>27</integerNumber>") !=
-1);
-        
+
         int startIndex = nodeString.indexOf("<innerProperty>");
         int endIndex = nodeString.indexOf("</innerProperty>");
         String fragment = nodeString.substring(startIndex, endIndex);
         assertTrue(fragment.indexOf("<integerNumber>54</integerNumber>") != -1);
-        
+
         //System.out.println(sw.toString());
-        
-        
+
     }
-    
+
     public void testJava2NodeMapping() throws Exception {
         SamplePropertyBean propertyBean = new SamplePropertyBean();
         TransformationContext context = EasyMock.createMock(TransformationContext.class);
         DataType<Class> dataType = new DataType<Class>(null, SamplePropertyBean.class);
         EasyMock.expect(context.getTargetDataType()).andReturn(dataType).anyTimes();
         EasyMock.replay(context);
-        
-        javax.xml.transform.Transformer transformer = TransformerFactory.newInstance().newTransformer();
-        Object data = new int[]{10, 20, 30, 40};
+
+        javax.xml.transform.Transformer transformer =
+                TransformerFactory.newInstance().newTransformer();
+        Object data = new int[] { 10, 20, 30, 40 };
         Node aNode = extension.toDOMNode(data, context);
         StringWriter sw = new StringWriter();
         transformer.transform(new DOMSource(aNode), new StreamResult(sw));
-        
+
         //System.out.println(sw.toString());
-        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><int_collection><int>10</int><int>20</int><int>30</int><int>40</int></int_collection>",
sw.toString());
+        assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><int_collection><int>10</int><int>20</int><int>30</int><int>40</int></int_collection>",
+                     sw.toString());
     }
-    
-    public static class SamplePropertyBean  {
-        
+
+    public static class SamplePropertyBean {
+
         public float floatNumber = 50;
+
         public double doubleNumber = 75;
-        public boolean[] boolArray = null;
+
+        public boolean[] boolArray;
+
         public int integerNumber = 25;
+
         public String[] stringArray;
-        
+
         public SamplePropertyBean innerProperty;
-        
+
         public SamplePropertyBean() {
-            
+
         }
 
         public double getDoubleNumber() {
@@ -174,10 +173,10 @@
         public void setInnerProperty(SamplePropertyBean prop) {
             this.innerProperty = prop;
         }
-        
+
         public String toString() {
-            return Double.toString(integerNumber + floatNumber + doubleNumber) + " &
" 
-            + ((innerProperty == null) ? "" : innerProperty.toString());
+            return Double.toString(integerNumber + floatNumber + doubleNumber) + " &
"
+                    + ((innerProperty == null) ? "" : innerProperty.toString());
         }
 
         public String[] getStringArray() {
@@ -187,6 +186,6 @@
         public void setStringArray(String[] stringArray) {
             this.stringArray = stringArray;
         }
-        
+
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org


Mime
View raw message