cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject git commit: [CXFXJC-4] For elements with minOccurs=0, make "new FooObject()" for the field optional.
Date Thu, 22 May 2014 16:27:47 GMT
Repository: cxf-xjc-utils
Updated Branches:
  refs/heads/master f7db26be4 -> e76108057


[CXFXJC-4] For elements with minOccurs=0, make "new FooObject()" for the field optional.


Project: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/commit/e7610805
Tree: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/tree/e7610805
Diff: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/diff/e7610805

Branch: refs/heads/master
Commit: e761080573593c3e2a8b2ca5d2a751665b422a62
Parents: f7db26b
Author: Daniel Kulp <dkulp@apache.org>
Authored: Thu May 22 12:26:56 2014 -0400
Committer: Daniel Kulp <dkulp@apache.org>
Committed: Thu May 22 12:26:56 2014 -0400

----------------------------------------------------------------------
 .../org/apache/cxf/xjc/dv/DefaultValueTest.java | 27 +++++++++++--
 .../resources/schemas/configuration/foo.xsd     | 15 ++++---
 .../addon/apache_cxf/dv/DefaultValuePlugin.java | 19 +++++++++
 .../apache/cxf/xjc/dv/DefaultValuePlugin.java   | 42 ++++++++++++++++++--
 4 files changed, 90 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/blob/e7610805/dv-test/src/test/java/org/apache/cxf/xjc/dv/DefaultValueTest.java
----------------------------------------------------------------------
diff --git a/dv-test/src/test/java/org/apache/cxf/xjc/dv/DefaultValueTest.java b/dv-test/src/test/java/org/apache/cxf/xjc/dv/DefaultValueTest.java
index d654f65..2f6cab5 100644
--- a/dv-test/src/test/java/org/apache/cxf/xjc/dv/DefaultValueTest.java
+++ b/dv-test/src/test/java/org/apache/cxf/xjc/dv/DefaultValueTest.java
@@ -19,14 +19,21 @@
 
 package org.apache.cxf.xjc.dv;
 
+import java.io.StringWriter;
 import java.lang.reflect.Field;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 
+import javax.xml.XMLConstants;
 import javax.xml.bind.DatatypeConverter;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.Marshaller;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
 import javax.xml.namespace.QName;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
 
 import org.apache.cxf.configuration.foo.Foo;
 import org.apache.ws.jaxme.impl.DatatypeConverterImpl;
@@ -49,7 +56,8 @@ public class DefaultValueTest extends Assert {
 
         assertAttributeValuesWithoutDefault(foo);
         assertDefaultAttributeValues(foo);        
-        assertDefaultElementValues(foo);   
+        assertDefaultElementValues(foo);
+        assertSchemaValid(foo);
     }
 
     private void checkCXF3131(Foo foo) throws Exception {
@@ -157,10 +165,11 @@ public class DefaultValueTest extends Assert {
     }
     
     private void assertDefaultElementValues(Foo foo) {
+        assertNull(foo.getPageColor());
         assertEquals("Unexpected value for element pageColor.background", "red", 
-                     foo.getPageColor().getBackground());
+                     foo.getPageColor2().getBackground());
         assertEquals("Unexpected value for element pageColor.foreground", "blue", 
-                     foo.getPageColor().getForeground());
+                     foo.getPageColor2().getForeground());
 
         assertEquals("Unexpected value for element driving",
                      "LeftTurn", foo.getDriving().value());
@@ -219,5 +228,15 @@ public class DefaultValueTest extends Assert {
                      3, foo.getDurationElem().getHours());
     }
     
-    
+    private void assertSchemaValid(Foo foo) throws Exception {
+        Marshaller marshaller = JAXBContext.newInstance(Foo.class).createMarshaller();
+        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        Schema schema = sf.newSchema(getClass().getResource("/schemas/configuration/foo.xsd"));
+        marshaller.setSchema(schema);
+        
+        JAXBElement<Foo> fooElement = 
+                new org.apache.cxf.configuration.foo.ObjectFactory().createFooElement(foo);
+        
+        marshaller.marshal(fooElement, new StringWriter());
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/blob/e7610805/dv-test/src/test/resources/schemas/configuration/foo.xsd
----------------------------------------------------------------------
diff --git a/dv-test/src/test/resources/schemas/configuration/foo.xsd b/dv-test/src/test/resources/schemas/configuration/foo.xsd
index da1780f..7256fbc 100644
--- a/dv-test/src/test/resources/schemas/configuration/foo.xsd
+++ b/dv-test/src/test/resources/schemas/configuration/foo.xsd
@@ -42,15 +42,16 @@
     </xs:simpleType>
 
     <xs:complexType name="pageColor">
-	<xs:sequence>
-	    <xs:element name="background" type="xs:string" default="red"/>
-	    <xs:element name="foreground" type="xs:string" default="blue"/>
+        <xs:sequence>
+            <xs:element name="background" type="xs:string" default="red" minOccurs="0"/>
+            <xs:element name="foreground" type="xs:string" default="blue" minOccurs="0"/>
     	</xs:sequence>
     </xs:complexType>
 
     <xs:complexType name="foo">        
         <xs:sequence>
-	    <xs:element name="pageColor" type="tns:pageColor" minOccurs="0"></xs:element>
+	        <xs:element name="pageColor2" type="tns:pageColor"></xs:element>
+	        <xs:element name="pageColor" type="tns:pageColor" minOccurs="0"></xs:element>
             <xs:element name="driving" type="tns:drivingDecision" default="LeftTurn" minOccurs="0"></xs:element>
             <xs:element name="StringElem" type="xs:string" default="hello" minOccurs="0"></xs:element>
             <xs:element name="BooleanElem" type="xs:boolean" default="true" minOccurs="0"></xs:element>
@@ -128,6 +129,8 @@
             <xs:element name="X" type="xs:int" default="0" minOccurs="0"></xs:element>
             <xs:element name="Y" type="xs:int" default="0" minOccurs="0"></xs:element>
         </xs:sequence>
-    </xs:complexType>    
-        
+    </xs:complexType>
+    
+    <xs:element name="fooElement" type="tns:foo"/>
+    
 </xs:schema>

http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/blob/e7610805/dv/src/main/java/com/sun/tools/xjc/addon/apache_cxf/dv/DefaultValuePlugin.java
----------------------------------------------------------------------
diff --git a/dv/src/main/java/com/sun/tools/xjc/addon/apache_cxf/dv/DefaultValuePlugin.java
b/dv/src/main/java/com/sun/tools/xjc/addon/apache_cxf/dv/DefaultValuePlugin.java
index ed41eda..3c0751d 100644
--- a/dv/src/main/java/com/sun/tools/xjc/addon/apache_cxf/dv/DefaultValuePlugin.java
+++ b/dv/src/main/java/com/sun/tools/xjc/addon/apache_cxf/dv/DefaultValuePlugin.java
@@ -19,9 +19,12 @@
 
 package com.sun.tools.xjc.addon.apache_cxf.dv;
 
+import java.io.IOException;
+
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.SAXException;
 
+import com.sun.tools.xjc.BadCommandLineException;
 import com.sun.tools.xjc.Options;
 import com.sun.tools.xjc.Plugin;
 import com.sun.tools.xjc.outline.Outline;
@@ -58,4 +61,20 @@ public class DefaultValuePlugin extends Plugin {
     public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) throws SAXException
{
         return impl.run(outline, opt, errorHandler);
     }
+    
+    public void onActivated(Options opts) throws BadCommandLineException {
+        impl.onActivated(opts);
+    }
+
+    
+    /* (non-Javadoc)
+     * @see com.sun.tools.xjc.Plugin#parseArgument(com.sun.tools.xjc.Options, java.lang.String[],
int)
+     */
+    @Override
+    public int parseArgument(Options opt, String[] args, int index) 
+        throws BadCommandLineException, IOException {
+        
+        return impl.parseArgument(opt, args, index, this);
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/blob/e7610805/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java
----------------------------------------------------------------------
diff --git a/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java b/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java
index f12069e..166c799 100644
--- a/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java
+++ b/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.xjc.dv;
 
+import java.io.IOException;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -44,6 +45,7 @@ import com.sun.codemodel.JOp;
 import com.sun.codemodel.JTryBlock;
 import com.sun.codemodel.JType;
 import com.sun.codemodel.JVar;
+import com.sun.tools.xjc.BadCommandLineException;
 import com.sun.tools.xjc.Options;
 import com.sun.tools.xjc.outline.ClassOutline;
 import com.sun.tools.xjc.outline.FieldOutline;
@@ -64,6 +66,8 @@ import com.sun.xml.xsom.XmlString;
 public class DefaultValuePlugin {
     
     private static final Logger LOG = Logger.getLogger(DefaultValuePlugin.class.getName());
//NOPMD
+    private boolean complexTypes;
+    private boolean active;
     
     public DefaultValuePlugin() {
     }
@@ -73,9 +77,30 @@ public class DefaultValuePlugin {
     }
 
     public String getUsage() {
-        return "  -Xdv                 : Initialize fields mapped from elements with their
default values";
+        return   "  -Xdv                 : Initialize fields mapped from elements with their
default values\n"
+               + "  -Xdv:optional        : Initialize fields mapped from elements with their
default values\n"
+               + "                         for elements with minOccurs=0 but with complexTypes
containing \n"
+               + "                         fields with default values.";
     }
 
+    public int parseArgument(Options opt, String[] args, int index, com.sun.tools.xjc.Plugin
plugin) 
+        throws BadCommandLineException, IOException {
+        int ret = 0;
+        
+        if (args[index].startsWith("-Xdv")) {
+            ret = 1;                    
+            if (args[index].equals("-Xts:optional")) {
+                complexTypes = true;
+            }
+            if (!opt.activePlugins.contains(plugin)) {
+                opt.activePlugins.add(plugin);
+            }
+            active = true;
+        }
+        return ret;
+    }
+
+    
     private boolean containsDefaultValue(Outline outline, FieldOutline field) {
         ClassOutline fClass = null;
         for (ClassOutline classOutline : outline.getClasses()) {
@@ -101,6 +126,9 @@ public class DefaultValuePlugin {
     }
 
     public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) {
+        if (!active) {
+            return true;
+        }
         LOG.fine("Running default value plugin.");
         for (ClassOutline co : outline.getClasses()) {
             for (FieldOutline f : co.getDeclaredFields()) {
@@ -112,8 +140,9 @@ public class DefaultValuePlugin {
                 XSType xsType = null;
                 boolean isElement = false;
                 boolean isRequiredAttr = true;
+                XSParticle particle = null;
                 if (f.getPropertyInfo().getSchemaComponent() instanceof XSParticle) {
-                    XSParticle particle = (XSParticle)f.getPropertyInfo().getSchemaComponent();
+                    particle = (XSParticle)f.getPropertyInfo().getSchemaComponent();
                     XSTerm term = particle.getTerm();
                     XSElementDecl element = null;
 
@@ -132,7 +161,10 @@ public class DefaultValuePlugin {
                 }
 
                 
-                if (xsType != null && xsType.isComplexType() && containsDefaultValue(outline,
f)) {
+                if (xsType != null 
+                    && xsType.isComplexType()
+                    && ((containsDefaultValue(outline, f) && complexTypes)
+                        || (particle != null && particle.getMinOccurs() != 0))) {
                     String varName = f.getPropertyInfo().getName(false);
                     JFieldVar var = co.implClass.fields().get(varName);
                     if (var != null) {
@@ -388,4 +420,8 @@ public class DefaultValuePlugin {
         
     }
 
+    public void onActivated(Options opts) {
+        active = true;
+    }
+
 }


Mime
View raw message