cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From andreasm...@apache.org
Subject svn commit: r454689 [2/2] - in /incubator/cxf/trunk: api/src/main/java/org/apache/cxf/phase/ common/common/src/main/java/org/apache/cxf/jaxb/ common/metacode/src/main/resources/schemas/wsdl/ rt/ws/rm/ rt/ws/rm/src/main/java/org/apache/cxf/ws/rm/impl/ r...
Date Tue, 10 Oct 2006 10:48:16 GMT
Modified: incubator/cxf/trunk/tools/xjc/cfg-test/src/test/resources/schemas/configuration/foo.xsd
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/cfg-test/src/test/resources/schemas/configuration/foo.xsd?view=diff&rev=454689&r1=454688&r2=454689
==============================================================================
--- incubator/cxf/trunk/tools/xjc/cfg-test/src/test/resources/schemas/configuration/foo.xsd
(original)
+++ incubator/cxf/trunk/tools/xjc/cfg-test/src/test/resources/schemas/configuration/foo.xsd
Tue Oct 10 03:48:13 2006
@@ -53,6 +53,7 @@
             <xs:element name="unsignedIntElem" type="xs:unsignedInt" default="19" minOccurs="0"></xs:element>
             <xs:element name="unsignedShortElem" type="xs:unsignedShort" default="20"
minOccurs="0"></xs:element>
             <xs:element name="unsignedByteElem" type="xs:unsignedByte" default="21" minOccurs="0"></xs:element>
+            <xs:element name="durationElem" type="xs:duration" default="PT3H" minOccurs="0"></xs:element>
             
             <xs:element name="point" type="tns:point" minOccurs="0"></xs:element>
             <xs:element name="address" minOccurs="0">
@@ -83,6 +84,8 @@
         <xs:attribute name="unsignedIntAttr" type="xs:unsignedInt" default="119"/>
         <xs:attribute name="unsignedShortAttr" type="xs:unsignedShort" default="120"/>
         <xs:attribute name="unsignedByteAttr" type="xs:unsignedByte" default="121"/>
+        <xs:attribute name="durationAttr" type="xs:duration" default="PT3S"/>
+        
         
         <xs:attribute name="stringAttrNoDefault" type="xs:string"/>
         <xs:attribute name="booleanAttrNoDefault" type="xs:boolean"/>
@@ -100,6 +103,8 @@
         <xs:attribute name="unsignedIntAttrNoDefault" type="xs:unsignedInt"/>
         <xs:attribute name="unsignedShortAttrNoDefault" type="xs:unsignedShort"/>
         <xs:attribute name="unsignedByteAttrNoDefault" type="xs:unsignedByte"/>
+        <xs:attribute name="durationAttrNoDefault" type="xs:duration"/>
+        
         
     </xs:complexType>
     

Modified: incubator/cxf/trunk/tools/xjc/cfg/src/main/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPlugin.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/cfg/src/main/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPlugin.java?view=diff&rev=454689&r1=454688&r2=454689
==============================================================================
--- incubator/cxf/trunk/tools/xjc/cfg/src/main/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPlugin.java
(original)
+++ incubator/cxf/trunk/tools/xjc/cfg/src/main/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPlugin.java
Tue Oct 10 03:48:13 2006
@@ -21,22 +21,16 @@
 
 import java.util.Collections;
 import java.util.List;
-import java.util.Map;
-
-import javax.xml.bind.DatatypeConverter;
-import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
-import javax.xml.namespace.NamespaceContext;
-import javax.xml.namespace.QName;
 
 import org.xml.sax.ErrorHandler;
 
+import com.sun.codemodel.JBlock;
 import com.sun.codemodel.JConditional;
 import com.sun.codemodel.JDefinedClass;
 import com.sun.codemodel.JDocComment;
 import com.sun.codemodel.JExpr;
 import com.sun.codemodel.JExpression;
 import com.sun.codemodel.JFieldRef;
-import com.sun.codemodel.JFieldVar;
 import com.sun.codemodel.JInvocation;
 import com.sun.codemodel.JMethod;
 import com.sun.codemodel.JOp;
@@ -48,14 +42,6 @@
 import com.sun.tools.xjc.outline.ClassOutline;
 import com.sun.tools.xjc.outline.FieldOutline;
 import com.sun.tools.xjc.outline.Outline;
-import com.sun.tools.xjc.util.NamespaceContextAdapter;
-import com.sun.xml.xsom.XSAttributeDecl;
-import com.sun.xml.xsom.XSAttributeUse;
-import com.sun.xml.xsom.XSElementDecl;
-import com.sun.xml.xsom.XSParticle;
-import com.sun.xml.xsom.XSTerm;
-import com.sun.xml.xsom.XSType;
-import com.sun.xml.xsom.XmlString;
 
 import org.apache.cxf.configuration.AbstractConfigurableBeanBase;
 
@@ -103,10 +89,6 @@
             JDefinedClass dc = co.implClass;
             dc._extends(AbstractConfigurableBeanBase.class);
             
-            // set default values
-
-            setDefaultValues(outline, co);      
-
             // replace default getters by getters trying the registered providers
 
             updateGetters(co, dc);
@@ -119,104 +101,6 @@
         return true;
     }
     
-    private boolean setDefaultValues(Outline outline, ClassOutline co) {
-        for (FieldOutline f : co.getDeclaredFields()) {
-
-            // Use XML schema object model to determine if field is mapped
-            // from an element or attribute with a default value
-            // and get its default value.
-
-            String fieldName = f.getPropertyInfo().getName(false);
-            XmlString xmlDefaultValue = null;
-            XSType xsType = null;    
-
-            if (f.getPropertyInfo().getSchemaComponent() instanceof XSParticle) {
-                XSParticle particle = (XSParticle)f.getPropertyInfo().getSchemaComponent();
-                XSTerm term = particle.getTerm();
-                XSElementDecl element = null;
-
-                if (term.isElementDecl()) {
-                    element = particle.getTerm().asElementDecl();
-                    xmlDefaultValue = element.getDefaultValue();                        
-                    xsType = element.getType();
-                }
-            } else if (f.getPropertyInfo().getSchemaComponent() instanceof XSAttributeUse)
{
-                XSAttributeUse attributeUse = (XSAttributeUse)f.getPropertyInfo().getSchemaComponent();
-                XSAttributeDecl decl = attributeUse.getDecl();
-                xmlDefaultValue = decl.getDefaultValue();                        
-                xsType = decl.getType();
-            }
-
-            if (null == xmlDefaultValue) {
-                continue;
-            }
-            
-            String defaultValue = xmlDefaultValue.value;
-            
-            if (null == defaultValue) {
-                continue;
-            }
-
-            JType type = f.getRawType();
-            String typeName = type.fullName();                
-
-            JDefinedClass impl = co.implClass;
-            Map<String, JFieldVar> fields = impl.fields();
-            JFieldVar var = fields.get(fieldName);
-
-            if ("java.lang.Boolean".equals(typeName)) {
-                var.init(JExpr.direct(Boolean.valueOf(defaultValue) ? "Boolean.TRUE" : "Boolean.FALSE"));
-            } else if ("java.lang.Byte".equals(typeName)) {
-                var.init(JExpr._new(type)
-                    .arg(JExpr.cast(type.unboxify(), 
-                        JExpr.lit(new Byte(Short.valueOf(defaultValue).byteValue())))));
-            } else if ("java.lang.Double".equals(typeName)) {
-                var.init(JExpr._new(type)
-                    .arg(JExpr.lit(new Double(Double.valueOf(defaultValue).doubleValue()))));
-            } else if ("java.lang.Float".equals(typeName)) {
-                var.init(JExpr._new(type)
-                         .arg(JExpr.lit(new Float(Float.valueOf(defaultValue).floatValue()))));
-            } else if ("java.lang.Integer".equals(typeName)) {
-                var.init(JExpr._new(type)
-                    .arg(JExpr.lit(new Integer(Integer.valueOf(defaultValue).intValue()))));
-            } else if ("java.lang.Long".equals(typeName)) {
-                var.init(JExpr._new(type)
-                    .arg(JExpr.lit(new Long(Long.valueOf(defaultValue).longValue()))));
-            } else if ("java.lang.Short".equals(typeName)) {
-                var.init(JExpr._new(type)
-                    .arg(JExpr.cast(type.unboxify(), 
-                        JExpr.lit(new Short(Short.valueOf(defaultValue).shortValue())))));
-            } else if ("java.lang.String".equals(type.fullName())) {
-                var.init(JExpr.lit(defaultValue));
-            } else if ("java.math.BigInteger".equals(type.fullName())) {
-                var.init(JExpr._new(type).arg(JExpr.lit(defaultValue)));
-            } else if ("java.math.BigDecimal".equals(type.fullName())) {
-                var.init(JExpr._new(type).arg(JExpr.lit(defaultValue)));
-            } else if ("byte[]".equals(type.fullName()) && xsType.isSimpleType())
{
-                while (!"anySimpleType".equals(xsType.getBaseType().getName())) {
-                    xsType = xsType.getBaseType();
-                }
-                if ("base64Binary".equals(xsType.getName())) {
-                    var.init(outline.getCodeModel().ref(DatatypeConverter.class)
-                       .staticInvoke("parseBase64Binary").arg(defaultValue));
-                } else if ("hexBinary".equals(xsType.getName())) {
-                    var.init(JExpr._new(outline.getCodeModel().ref(HexBinaryAdapter.class))
-                        .invoke("unmarshal").arg(defaultValue));
-                }
-            } else if ("javax.xml.namespace.QName".equals(typeName)) {
-                NamespaceContext nsc = new NamespaceContextAdapter(xmlDefaultValue);
-                QName qn = DatatypeConverter.parseQName(xmlDefaultValue.value, nsc);
-                var.init(JExpr._new(outline.getCodeModel().ref(QName.class))
-                    .arg(qn.getNamespaceURI())
-                    .arg(qn.getLocalPart())
-                    .arg(qn.getPrefix()));
-            }
-            // TODO: GregorianCalendar, ...
-        }
-
-        return true;
-    }
-    
     private void updateGetters(ClassOutline co, JDefinedClass dc) {
         for (FieldOutline fo : co.getDeclaredFields()) {
 
@@ -227,25 +111,13 @@
             String getterName = ("java.lang.Boolean".equals(typeName) ? "is" : "get")
                                 + fo.getPropertyInfo().getName(true);
             
-            // REVISIT: it seems that for Spring in order to inject a Boolean
-            // the getter name needs to be get<fieldName>
-            // so change it here  
-            // getterName = "get" + fo.getPropertyInfo().getName(true);
-
             // retain existing javadoc, modifiers, and name
             
             JMethod method = dc.getMethod(getterName, new JType[0]);
             JDocComment doc = method.javadoc();
             int mods = method.mods().getValue();
-            
-            // ensure type is always the wrapped type
-            
-            JType mtype = method.type();
-            
-            if (mtype.isPrimitive()) {
-                mtype = mtype.boxify();
-            }
-            
+            JType mtype = method.type(); 
+            JBlock block = method.body();
             
             // remove existing method and define new one
             
@@ -255,7 +127,6 @@
             method.javadoc().append(doc);
 
             JFieldRef fr = JExpr.ref(fieldName);
-
             
             JExpression test;
             JConditional jc;
@@ -269,14 +140,23 @@
             test = JOp.not(JOp.eq(JExpr._null(), tmp));
             jc = method.body()._if(test);
             jc._then()._return(tmp);
-                
-            test = JOp.eq(JExpr._null(), fr);
+            
+            test = JOp.not(JOp.eq(JExpr._null(), fr));
             jc = method.body()._if(test);
+            jc._then()._return(fr);
+            
             invocation = JExpr.invoke("tryFallback");
             invocation.arg(JExpr.dotclass(type.boxify()));
             invocation.arg(JExpr.lit(fieldName));
-            jc._then()._return(invocation);
-            jc._else()._return(fr);
+            // tmp = method.body().decl(type.boxify(), "_" + fieldName, invocation);
+            method.body().assign(tmp, invocation);
+            test = JOp.not(JOp.eq(JExpr._null(), tmp));
+            jc = method.body()._if(test);
+            jc._then()._return(tmp);
+            
+            // add the original block
+            
+            method.body().add(block);
 
         }
     }

Modified: incubator/cxf/trunk/tools/xjc/dv-test/pom.xml
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/dv-test/pom.xml?view=diff&rev=454689&r1=454688&r2=454689
==============================================================================
--- incubator/cxf/trunk/tools/xjc/dv-test/pom.xml (original)
+++ incubator/cxf/trunk/tools/xjc/dv-test/pom.xml Tue Oct 10 03:48:13 2006
@@ -79,6 +79,11 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-common-utilities</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 
 

Modified: incubator/cxf/trunk/tools/xjc/dv-test/src/test/java/org/apache/cxf/xjc/dv/DefaultValueTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/dv-test/src/test/java/org/apache/cxf/xjc/dv/DefaultValueTest.java?view=diff&rev=454689&r1=454688&r2=454689
==============================================================================
--- incubator/cxf/trunk/tools/xjc/dv-test/src/test/java/org/apache/cxf/xjc/dv/DefaultValueTest.java
(original)
+++ incubator/cxf/trunk/tools/xjc/dv-test/src/test/java/org/apache/cxf/xjc/dv/DefaultValueTest.java
Tue Oct 10 03:48:13 2006
@@ -19,7 +19,12 @@
 
 package org.apache.cxf.xjc.dv;
 
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
 import javax.xml.bind.DatatypeConverter;
+import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
+import javax.xml.namespace.QName;
 
 import junit.framework.TestCase;
 
@@ -36,14 +41,166 @@
         
         Foo foo = new org.apache.cxf.configuration.foo.ObjectFactory().createFoo();
 
-        // verify default attribute values
-
-        assertEquals("hello", foo.getStringAttr());
-        assertEquals(3, foo.getBase64BinaryAttr().length);
+        // verify default values
 
-        // verify default element values
- 
-         
+        assertAttributeValuesWithoutDefault(foo);
+        assertDefaultAttributeValues(foo);        
+        assertDefaultElementValues(foo);   
+    }
+
+    private void assertDefaultAttributeValues(Foo foo) {
+        assertEquals("Unexpected value for attribute stringAttr",
+                     "hello", foo.getStringAttr());
+        assertTrue("Unexpected value for attribute booleanAttr",
+                     foo.isBooleanAttr());
+        assertEquals("Unexpected value for attribute integerAttr",
+                     new BigInteger("111"), foo.getIntegerAttr());
+        assertEquals("Unexpected value for attribute intAttr",
+                     112, foo.getIntAttr());
+        assertEquals("Unexpected value for attribute longAttr",
+                     113L, foo.getLongAttr());
+        assertEquals("Unexpected value for attribute shortAttr",
+                     114, foo.getShortAttr());
+        assertEquals("Unexpected value for attribute decimalAttr",
+                     new BigDecimal("115"), foo.getDecimalAttr());
+        assertEquals("Unexpected value for attribute floatAttr",
+                     new Float(116F), foo.getFloatAttr());
+        assertEquals("Unexpected value for attribute doubleAttr",
+                     new Double(117D), foo.getDoubleAttr());
+        assertEquals("Unexpected value for attribute byteAttr",
+                     118, foo.getByteAttr());
+        
+        byte[] expected = DatatypeConverter.parseBase64Binary("wxyz");
+        byte[] effective = foo.getBase64BinaryAttr();
+        
+        assertEquals("Unexpected value for attribute base64BinaryAttr", expected.length,
effective.length);
+        for (int i = 0; i < expected.length; i++) {
+            assertEquals("Unexpected value for attribute base64BinaryAttr", expected[i],
effective[i]);
+        }
+        
+        expected = new HexBinaryAdapter().unmarshal("aaaa");
+        effective = foo.getHexBinaryAttr();
+        assertEquals("Unexpected value for attribute hexBinaryAttr", expected.length, effective.length);
+        for (int i = 0; i < expected.length; i++) {
+            assertEquals("Unexpected value for attribute hexBinaryAttr", expected[i], effective[i]);
+        }
+                
+        QName qn = foo.getQnameAttr();
+        assertEquals("Unexpected value for attribute qnameAttr",
+                     "http://www.w3.org/2001/XMLSchema", qn.getNamespaceURI());
+        assertEquals("Unexpected value for attribute qnameAttr",
+                     "schema", qn.getLocalPart());
+       
+        assertEquals("Unexpected value for attribute unsignedIntAttr",
+                     119L, foo.getUnsignedIntAttr());
+        assertEquals("Unexpected value for attribute unsignedShortAttr",
+                     120, foo.getUnsignedShortAttr());
+        assertEquals("Unexpected value for attribute unsignedByteAttr",
+                     121, foo.getUnsignedByteAttr());
+
+        assertEquals("Unexpected value for attribute durationAttr",
+                     3, foo.getDurationAttr().getSeconds());
+        assertEquals("Unexpected value for attribute durationAttr",
+                     0, foo.getDurationAttr().getHours());
+    }
+    
+    /**
+     * @param foo
+     */
+    private void assertAttributeValuesWithoutDefault(Foo foo) {
+        assertNull("Unexpected value for attribute stringAttrNoDefault",
+                     foo.getStringAttrNoDefault());
+        assertNull("Unexpected value for attribute booleanAttrNoDefault",
+                     foo.isBooleanAttrNoDefault());
+        assertNull("Unexpected value for attribute integerAttrNoDefault",
+                     foo.getIntegerAttrNoDefault());
+        assertNull("Unexpected value for attribute intAttrNoDefault",
+                     foo.getIntAttrNoDefault());
+        assertNull("Unexpected value for attribute longAttrNoDefault",
+                     foo.getLongAttrNoDefault());
+        assertNull("Unexpected value for attribute shortAttrNoDefault",
+                     foo.getShortAttrNoDefault());
+        assertNull("Unexpected value for attribute decimalAttrNoDefault",
+                     foo.getDecimalAttrNoDefault());
+        assertNull("Unexpected value for attribute floatAttrNoDefault",
+                     foo.getFloatAttrNoDefault());
+        assertNull("Unexpected value for attribute doubleAttrNoDefault",
+                     foo.getDoubleAttrNoDefault());
+        assertNull("Unexpected value for attribute byteAttrNoDefault",
+                     foo.getByteAttrNoDefault());
+        
+        assertNull("Unexpected value for attribute base64BinaryAttrNoDefault",
+                   foo.getBase64BinaryAttrNoDefault());
+        assertNull("Unexpected value for attribute hexBinaryAttrNoDefault",
+                   foo.getHexBinaryAttrNoDefault());
+        
+        assertNull("Unexpected value for attribute qnameAttrNoDefault",
+                     foo.getQnameAttrNoDefault());
+       
+        assertNull("Unexpected value for attribute unsignedIntAttrNoDefault",
+                     foo.getUnsignedIntAttrNoDefault());
+        assertNull("Unexpected value for attribute unsignedShortAttrNoDefault",
+                     foo.getUnsignedShortAttrNoDefault());
+        assertNull("Unexpected value for attribute unsignedByteAttrNoDefault",
+                     foo.getUnsignedByteAttrNoDefault());
+        assertNull("Unexpected value for attribute durationAttrNoDefault",
+                     foo.getDurationAttrNoDefault());
+    }
+    
+    private void assertDefaultElementValues(Foo foo) {
+        assertEquals("Unexpected value for element stringElem",
+                     "hello", foo.getStringElem());
+        assertTrue("Unexpected value for element booleanElem",
+                     foo.isBooleanElem());
+        assertEquals("Unexpected value for element integerElem",
+                     new BigInteger("11"), foo.getIntegerElem());
+        assertEquals("Unexpected value for element intElem",
+                     new Integer(12), foo.getIntElem());
+        assertEquals("Unexpected value for element longElem",
+                     new Long(13L), foo.getLongElem());
+        assertEquals("Unexpected value for element shortElem",
+                     new Short((short)14), foo.getShortElem());
+        assertEquals("Unexpected value for element decimalElem",
+                     new BigDecimal("15"), foo.getDecimalElem());
+        assertEquals("Unexpected value for element floatElem",
+                     new Float(16F), foo.getFloatElem());
+        assertEquals("Unexpected value for element doubleElem",
+                     new Double(17D), foo.getDoubleElem());
+        assertEquals("Unexpected value for element byteElem",
+                     new Byte((byte)18), foo.getByteElem());
+        
+        byte[] expected = DatatypeConverter.parseBase64Binary("abcdefgh");
+        byte[] effective = foo.getBase64BinaryElem();
+        
+        assertEquals("Unexpected value for element base64BinaryElem", expected.length, effective.length);
+        for (int i = 0; i < expected.length; i++) {
+            assertEquals("Unexpected value for element base64BinaryElem", expected[i], effective[i]);
+        }
+        
+        expected = new HexBinaryAdapter().unmarshal("ffff");
+        effective = foo.getHexBinaryElem();
+        assertEquals("Unexpected value for element hexBinaryElem", expected.length, effective.length);
+        for (int i = 0; i < expected.length; i++) {
+            assertEquals("Unexpected value for element hexBinaryElem", expected[i], effective[i]);
+        }
+                
+        QName qn = foo.getQnameElem();
+        assertEquals("Unexpected value for element qnameElem",
+                     "http://www.w3.org/2001/XMLSchema", qn.getNamespaceURI());
+        assertEquals("Unexpected value for element qnameElem",
+                     "string", qn.getLocalPart());
+       
+        assertEquals("Unexpected value for element unsignedIntElem",
+                     new Long(19L), foo.getUnsignedIntElem());
+        assertEquals("Unexpected value for element unsignedShortElem",
+                     new Integer(20), foo.getUnsignedShortElem());
+        assertEquals("Unexpected value for element unsignedByteElem",
+                     new Short((short)21), foo.getUnsignedByteElem());
+
+        assertEquals("Unexpected value for element durationElem",
+                     0, foo.getDurationElem().getSeconds());
+        assertEquals("Unexpected value for element durationElem",
+                     3, foo.getDurationElem().getHours());
     }
     
     

Modified: incubator/cxf/trunk/tools/xjc/dv-test/src/test/resources/schemas/configuration/foo.xsd
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/dv-test/src/test/resources/schemas/configuration/foo.xsd?view=diff&rev=454689&r1=454688&r2=454689
==============================================================================
--- incubator/cxf/trunk/tools/xjc/dv-test/src/test/resources/schemas/configuration/foo.xsd
(original)
+++ incubator/cxf/trunk/tools/xjc/dv-test/src/test/resources/schemas/configuration/foo.xsd
Tue Oct 10 03:48:13 2006
@@ -51,6 +51,7 @@
             <xs:element name="unsignedIntElem" type="xs:unsignedInt" default="19" minOccurs="0"></xs:element>
             <xs:element name="unsignedShortElem" type="xs:unsignedShort" default="20"
minOccurs="0"></xs:element>
             <xs:element name="unsignedByteElem" type="xs:unsignedByte" default="21" minOccurs="0"></xs:element>
+            <xs:element name="durationElem" type="xs:duration" default="PT3H" minOccurs="0"></xs:element>
             <xs:element name="temperature" type="tns:celsius" default="37" minOccurs="0"></xs:element>
             <xs:element name="roomTemperature" type="tns:cel" default="38" minOccurs="0"></xs:element>
             
@@ -66,8 +67,6 @@
                 </xs:sequence>
             </xs:complexType>
             </xs:element>
-            <!--
-            -->
         </xs:sequence>
 
         <xs:attribute name="stringAttr" type="xs:string" default="hello"/>
@@ -79,13 +78,14 @@
         <xs:attribute name="decimalAttr" type="xs:decimal" default="115"/>
         <xs:attribute name="floatAttr" type="xs:float" default="116"/>
         <xs:attribute name="doubleAttr" type="xs:double" default="117"/>
-        <xs:attribute name="byteAttr" type="xs:byte" default="108"/>
+        <xs:attribute name="byteAttr" type="xs:byte" default="118"/>
         <xs:attribute name="qnameAttr" type="xs:QName" default="xs:schema"/>
         <xs:attribute name="base64BinaryAttr" type="xs:base64Binary" default="wxyz"/>
         <xs:attribute name="hexBinaryAttr" type="xs:hexBinary" default="aaaa"/>
         <xs:attribute name="unsignedIntAttr" type="xs:unsignedInt" default="119"/>
         <xs:attribute name="unsignedShortAttr" type="xs:unsignedShort" default="120"/>
         <xs:attribute name="unsignedByteAttr" type="xs:unsignedByte" default="121"/>
+        <xs:attribute name="durationAttr" type="xs:duration" default="PT3S"/>
         
         <xs:attribute name="stringAttrNoDefault" type="xs:string"/>
         <xs:attribute name="booleanAttrNoDefault" type="xs:boolean"/>
@@ -103,6 +103,7 @@
         <xs:attribute name="unsignedIntAttrNoDefault" type="xs:unsignedInt"/>
         <xs:attribute name="unsignedShortAttrNoDefault" type="xs:unsignedShort"/>
         <xs:attribute name="unsignedByteAttrNoDefault" type="xs:unsignedByte"/>
+        <xs:attribute name="durationAttrNoDefault" type="xs:duration"/>
     </xs:complexType>
     
     <xs:complexType name="point">

Modified: incubator/cxf/trunk/tools/xjc/dv/pom.xml
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/dv/pom.xml?view=diff&rev=454689&r1=454688&r2=454689
==============================================================================
--- incubator/cxf/trunk/tools/xjc/dv/pom.xml (original)
+++ incubator/cxf/trunk/tools/xjc/dv/pom.xml Tue Oct 10 03:48:13 2006
@@ -38,6 +38,11 @@
 
     <dependencies>
         <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-common-utilities</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
             <groupId>com.sun.xml.bind</groupId>
             <artifactId>jaxb-xjc</artifactId>
         </dependency>

Modified: incubator/cxf/trunk/tools/xjc/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java?view=diff&rev=454689&r1=454688&r2=454689
==============================================================================
--- incubator/cxf/trunk/tools/xjc/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java
(original)
+++ incubator/cxf/trunk/tools/xjc/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java
Tue Oct 10 03:48:13 2006
@@ -19,7 +19,8 @@
 
 package org.apache.cxf.xjc.dv;
 
-import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.xml.bind.DatatypeConverter;
 import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
@@ -28,9 +29,14 @@
 
 import org.xml.sax.ErrorHandler;
 
+import com.sun.codemodel.JConditional;
 import com.sun.codemodel.JDefinedClass;
+import com.sun.codemodel.JDocComment;
 import com.sun.codemodel.JExpr;
-import com.sun.codemodel.JFieldVar;
+import com.sun.codemodel.JExpression;
+import com.sun.codemodel.JFieldRef;
+import com.sun.codemodel.JMethod;
+import com.sun.codemodel.JOp;
 import com.sun.codemodel.JType;
 import com.sun.tools.xjc.Options;
 import com.sun.tools.xjc.Plugin;
@@ -38,18 +44,25 @@
 import com.sun.tools.xjc.outline.FieldOutline;
 import com.sun.tools.xjc.outline.Outline;
 import com.sun.tools.xjc.util.NamespaceContextAdapter;
+import com.sun.xml.xsom.XSAttributeDecl;
+import com.sun.xml.xsom.XSAttributeUse;
 import com.sun.xml.xsom.XSElementDecl;
 import com.sun.xml.xsom.XSParticle;
 import com.sun.xml.xsom.XSTerm;
 import com.sun.xml.xsom.XSType;
 import com.sun.xml.xsom.XmlString;
 
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.jaxb.DatatypeFactory;
+
 /**
  * Modifies the JAXB code model to initialise fields mapped from schema elements 
  * with their default value.
  */
 public class DefaultValuePlugin extends Plugin {
     
+    private static final Logger LOG = LogUtils.getL7dLogger(DatatypeFactory.class);
+    
     public DefaultValuePlugin() {
     }
 
@@ -62,7 +75,7 @@
     }
 
     public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) {
-        System.out.println("Running default value plugin.");
+        LOG.fine("Running default value plugin.");
         for (ClassOutline co : outline.getClasses()) {
             for (FieldOutline f : co.getDeclaredFields()) {
 
@@ -70,9 +83,9 @@
                 // from an element (attributes default values are handled
                 // natively) and get its default value.
 
-                String fieldName = f.getPropertyInfo().getName(false);
                 XmlString xmlDefaultValue = null;
                 XSType xsType = null;
+                boolean isElement = false;
 
                 if (f.getPropertyInfo().getSchemaComponent() instanceof XSParticle) {
                     XSParticle particle = (XSParticle)f.getPropertyInfo().getSchemaComponent();
@@ -83,77 +96,132 @@
                         element = particle.getTerm().asElementDecl();
                         xmlDefaultValue = element.getDefaultValue();                    
   
                         xsType = element.getType();
+                        isElement = true;
                     }
+                } else if (f.getPropertyInfo().getSchemaComponent() instanceof XSAttributeUse)
{
+                    XSAttributeUse attributeUse = (XSAttributeUse)f.getPropertyInfo().getSchemaComponent();
+                    XSAttributeDecl decl = attributeUse.getDecl();
+                    xmlDefaultValue = decl.getDefaultValue();                        
+                    xsType = decl.getType();
                 }
 
-                if (null == xmlDefaultValue) {
+                if (null == xmlDefaultValue || null == xmlDefaultValue.value) {
                     continue;
                 }
                 
-                String defaultValue = xmlDefaultValue.value;
+                JExpression dvExpr = 
+                    getDefaultValueExpression(f, co, outline, xsType, isElement, xmlDefaultValue);
                 
-                if (null == defaultValue) {
+                if (null == dvExpr) {
                     continue;
                 }
-
-                JType type = f.getRawType();
-                String typeName = type.fullName();                
-
-                JDefinedClass impl = co.implClass;
-                Map<String, JFieldVar> fields = impl.fields();
-                JFieldVar var = fields.get(fieldName);
- 
-                if ("java.lang.Boolean".equals(typeName)) {
-                    var.init(JExpr.direct(Boolean.valueOf(defaultValue) ? "Boolean.TRUE"
: "Boolean.FALSE"));
-                } else if ("java.lang.Byte".equals(typeName)) {
-                    var.init(JExpr._new(type)
-                        .arg(JExpr.cast(type.unboxify(), 
-                            JExpr.lit(new Byte(Short.valueOf(defaultValue).byteValue())))));
-                } else if ("java.lang.Double".equals(typeName)) {
-                    var.init(JExpr._new(type)
-                        .arg(JExpr.lit(new Double(Double.valueOf(defaultValue).doubleValue()))));
-                } else if ("java.lang.Float".equals(typeName)) {
-                    var.init(JExpr._new(type)
-                             .arg(JExpr.lit(new Float(Float.valueOf(defaultValue).floatValue()))));
-                } else if ("java.lang.Integer".equals(typeName)) {
-                    var.init(JExpr._new(type)
-                        .arg(JExpr.lit(new Integer(Integer.valueOf(defaultValue).intValue()))));
-                } else if ("java.lang.Long".equals(typeName)) {
-                    var.init(JExpr._new(type)
-                        .arg(JExpr.lit(new Long(Long.valueOf(defaultValue).longValue()))));
-                } else if ("java.lang.Short".equals(typeName)) {
-                    var.init(JExpr._new(type)
-                        .arg(JExpr.cast(type.unboxify(), 
-                            JExpr.lit(new Short(Short.valueOf(defaultValue).shortValue())))));
-                } else if ("java.lang.String".equals(type.fullName())) {
-                    var.init(JExpr.lit(defaultValue));
-                } else if ("java.math.BigInteger".equals(type.fullName())) {
-                    var.init(JExpr._new(type).arg(JExpr.lit(defaultValue)));
-                } else if ("java.math.BigDecimal".equals(type.fullName())) {
-                    var.init(JExpr._new(type).arg(JExpr.lit(defaultValue)));
-                } else if ("byte[]".equals(type.fullName()) && xsType.isSimpleType())
{
-                    while (!"anySimpleType".equals(xsType.getBaseType().getName())) {
-                        xsType = xsType.getBaseType();
-                    }
-                    if ("base64Binary".equals(xsType.getName())) {
-                        var.init(outline.getCodeModel().ref(DatatypeConverter.class)
-                           .staticInvoke("parseBase64Binary").arg(defaultValue));
-                    } else if ("hexBinary".equals(xsType.getName())) {
-                        var.init(JExpr._new(outline.getCodeModel().ref(HexBinaryAdapter.class))
-                            .invoke("unmarshal").arg(defaultValue));
-                    }
-                } else if ("javax.xml.namespace.QName".equals(typeName)) {
-                    NamespaceContext nsc = new NamespaceContextAdapter(xmlDefaultValue);
-                    QName qn = DatatypeConverter.parseQName(xmlDefaultValue.value, nsc);
-                    var.init(JExpr._new(outline.getCodeModel().ref(QName.class))
-                        .arg(qn.getNamespaceURI())
-                        .arg(qn.getLocalPart())
-                        .arg(qn.getPrefix()));
-                }
-                // TODO: GregorianCalendar, ...
+                
+                updateGetter(co, f, co.implClass, dvExpr);               
+                
             }
         }
 
         return true;
     }
+    
+    
+    JExpression getDefaultValueExpression(FieldOutline f,
+                                          ClassOutline co,
+                                          Outline outline,
+                                          XSType xsType,
+                                          boolean isElement,
+                                          XmlString xmlDefaultValue
+                                          ) {
+        JType type = f.getRawType();
+        String typeName = type.fullName();
+        String defaultValue = xmlDefaultValue.value;
+
+        JExpression dv = null;
+        
+        if ("java.lang.Boolean".equals(typeName) && isElement) {
+            dv = JExpr.direct(Boolean.valueOf(defaultValue) ? "Boolean.TRUE" : "Boolean.FALSE");
+        } else if ("java.lang.Byte".equals(typeName) && isElement) {
+            dv = JExpr._new(type)
+                .arg(JExpr.cast(type.unboxify(), 
+                    JExpr.lit(new Byte(Short.valueOf(defaultValue).byteValue()))));
+        } else if ("java.lang.Double".equals(typeName) && isElement) {
+            dv = JExpr._new(type)
+                .arg(JExpr.lit(new Double(Double.valueOf(defaultValue).doubleValue())));
+        } else if ("java.lang.Float".equals(typeName) && isElement) {
+            dv = JExpr._new(type)
+                     .arg(JExpr.lit(new Float(Float.valueOf(defaultValue).floatValue())));
+        } else if ("java.lang.Integer".equals(typeName) && isElement) {
+            dv = JExpr._new(type)
+                .arg(JExpr.lit(new Integer(Integer.valueOf(defaultValue).intValue())));
+        } else if ("java.lang.Long".equals(typeName) && isElement) {
+            dv = JExpr._new(type)
+                .arg(JExpr.lit(new Long(Long.valueOf(defaultValue).longValue())));
+        } else if ("java.lang.Short".equals(typeName) && isElement) {
+            dv = JExpr._new(type)
+                .arg(JExpr.cast(type.unboxify(), 
+                    JExpr.lit(new Short(Short.valueOf(defaultValue).shortValue()))));
+        } else if ("java.lang.String".equals(type.fullName()) && isElement) {
+            dv = JExpr.lit(defaultValue);
+        } else if ("java.math.BigInteger".equals(type.fullName()) && isElement) {
+            dv = JExpr._new(type).arg(JExpr.lit(defaultValue));
+        } else if ("java.math.BigDecimal".equals(type.fullName()) && isElement) {
+            dv = JExpr._new(type).arg(JExpr.lit(defaultValue));
+        } else if ("byte[]".equals(type.fullName()) && xsType.isSimpleType() &&
isElement) {
+            while (!"anySimpleType".equals(xsType.getBaseType().getName())) {
+                xsType = xsType.getBaseType();
+            }
+            if ("base64Binary".equals(xsType.getName())) {
+                dv = outline.getCodeModel().ref(DatatypeConverter.class)
+                   .staticInvoke("parseBase64Binary").arg(defaultValue);
+            } else if ("hexBinary".equals(xsType.getName())) {
+                dv = JExpr._new(outline.getCodeModel().ref(HexBinaryAdapter.class))
+                    .invoke("unmarshal").arg(defaultValue);
+            }
+        } else if ("javax.xml.namespace.QName".equals(typeName)) {
+            NamespaceContext nsc = new NamespaceContextAdapter(xmlDefaultValue);
+            QName qn = DatatypeConverter.parseQName(xmlDefaultValue.value, nsc);
+            dv = JExpr._new(outline.getCodeModel().ref(QName.class))
+                .arg(qn.getNamespaceURI())
+                .arg(qn.getLocalPart())
+                .arg(qn.getPrefix());
+        } else if ("javax.xml.datatype.Duration".equals(typeName)) {
+            dv = outline.getCodeModel().ref(org.apache.cxf.jaxb.DatatypeFactory.class)
+            .staticInvoke("createDuration").arg(defaultValue);
+        }
+        // TODO: GregorianCalendar, ...
+        return dv;
+    }
+    
+    private void updateGetter(ClassOutline co, FieldOutline fo, JDefinedClass dc, JExpression
dvExpr) {
+
+        String fieldName = fo.getPropertyInfo().getName(false);
+        JType type = fo.getRawType();
+        String typeName = type.fullName();
+
+        String getterName = ("java.lang.Boolean".equals(typeName) ? "is" : "get")
+                            + fo.getPropertyInfo().getName(true);
+
+        JMethod method = dc.getMethod(getterName, new JType[0]);
+        JDocComment doc = method.javadoc();
+        int mods = method.mods().getValue();
+        JType mtype = method.type();
+
+        if (LOG.isLoggable(Level.FINE)) {
+            LOG.fine("Updating getter: " + getterName);
+        }
+        // remove existing method and define new one
+
+        dc.methods().remove(method);
+
+        method = dc.method(mods, mtype, getterName);
+        method.javadoc().append(doc);
+
+        JFieldRef fr = JExpr.ref(fieldName);
+
+        JExpression test = JOp.eq(JExpr._null(), fr);
+        JConditional jc =  method.body()._if(test);
+        jc._then()._return(dvExpr);
+        jc._else()._return(fr);
+    }
+
 }



Mime
View raw message