cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bimargul...@apache.org
Subject svn commit: r611657 - in /incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic: ArrayType.java BeanType.java ObjectType.java
Date Sun, 13 Jan 2008 21:50:31 GMT
Author: bimargulies
Date: Sun Jan 13 13:50:28 2008
New Revision: 611657

URL: http://svn.apache.org/viewvc?rev=611657&view=rev
Log:
 Fix CXF-1391 and other related problems with xsi:type.

Modified:
    incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ArrayType.java
    incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/BeanType.java
    incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ObjectType.java

Modified: incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ArrayType.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ArrayType.java?rev=611657&r1=611656&r2=611657&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ArrayType.java
(original)
+++ incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ArrayType.java
Sun Jan 13 13:50:28 2008
@@ -38,6 +38,7 @@
 import org.apache.cxf.aegis.xml.MessageReader;
 import org.apache.cxf.aegis.xml.MessageWriter;
 import org.apache.cxf.common.util.SOAPConstants;
+import org.apache.cxf.common.xmlschema.XmlSchemaConstants;
 import org.jdom.Attribute;
 import org.jdom.Element;
 import org.jdom.Namespace;
@@ -49,7 +50,7 @@
  */
 public class ArrayType extends Type {
     private static final Log LOG = LogFactory.getLog(ArrayType.class);
-    
+
     private QName componentName;
     private long minOccurs;
     private long maxOccurs = Long.MAX_VALUE;
@@ -159,20 +160,22 @@
                 Array.set(array, i, objects[i]);
             }
         }
-        return array == null
-            ? values.toArray((Object[])Array.newInstance(getComponentType().getTypeClass(),
values.size()))
-            : array;
+        return array == null ? values.toArray((Object[])Array.newInstance(getComponentType().getTypeClass(),
+                                                                          values.size()))
: array;
     }
 
     @Override
-    public void writeObject(Object values,
-                            MessageWriter writer,
-                            Context context) throws DatabindingException {
+    public void writeObject(Object values, MessageWriter writer, Context context) 
+        throws DatabindingException {
+        boolean forceXsiWrite = false;
         if (values == null) {
             return;
         }
 
         Type type = getComponentType();
+        if (XmlSchemaConstants.ANY_TYPE_QNAME.equals(type.getSchemaType())) {
+            forceXsiWrite = true;
+        }
 
         String ns = null;
         if (type.isAbstract()) {
@@ -189,53 +192,62 @@
 
         Class arrayType = type.getTypeClass();
 
-        int i;
-        int n;
-        if (Object.class.isAssignableFrom(arrayType)) {
-            Object[] objects = (Object[])values;
-            for (i = 0, n = objects.length; i < n; i++) {
-                writeValue(objects[i], writer, context, type, name, ns);
-            }
-        } else if (Integer.TYPE.equals(arrayType)) {
-            int[] objects = (int[])values;
-            for (i = 0, n = objects.length; i < n; i++) {
-                writeValue(new Integer(objects[i]), writer, context, type, name, ns);
-            }
-        } else if (Long.TYPE.equals(arrayType)) {
-            long[] objects = (long[])values;
-            for (i = 0, n = objects.length; i < n; i++) {
-                writeValue(new Long(objects[i]), writer, context, type, name, ns);
-            }
-        } else if (Short.TYPE.equals(arrayType)) {
-            short[] objects = (short[])values;
-            for (i = 0, n = objects.length; i < n; i++) {
-                writeValue(new Short(objects[i]), writer, context, type, name, ns);
-            }
-        } else if (Double.TYPE.equals(arrayType)) {
-            double[] objects = (double[])values;
-            for (i = 0, n = objects.length; i < n; i++) {
-                writeValue(new Double(objects[i]), writer, context, type, name, ns);
-            }
-        } else if (Float.TYPE.equals(arrayType)) {
-            float[] objects = (float[])values;
-            for (i = 0, n = objects.length; i < n; i++) {
-                writeValue(new Float(objects[i]), writer, context, type, name, ns);
-            }
-        } else if (Byte.TYPE.equals(arrayType)) {
-            byte[] objects = (byte[])values;
-            for (i = 0, n = objects.length; i < n; i++) {
-                writeValue(new Byte(objects[i]), writer, context, type, name, ns);
-            }
-        } else if (Boolean.TYPE.equals(arrayType)) {
-            boolean[] objects = (boolean[])values;
-            for (i = 0, n = objects.length; i < n; i++) {
-                writeValue(Boolean.valueOf(objects[i]), writer, context, type, name, ns);
+        boolean oldXsiWrite = context.getGlobalContext().isWriteXsiTypes();
+        try {
+            if (forceXsiWrite) {
+                context.getGlobalContext().setWriteXsiTypes(true);
             }
-        } else if (Character.TYPE.equals(arrayType)) {
-            char[] objects = (char[])values;
-            for (i = 0, n = objects.length; i < n; i++) {
-                writeValue(new Character(objects[i]), writer, context, type, name, ns);
+
+            int i;
+            int n;
+            if (Object.class.isAssignableFrom(arrayType)) {
+                Object[] objects = (Object[])values;
+                for (i = 0, n = objects.length; i < n; i++) {
+                    writeValue(objects[i], writer, context, type, name, ns);
+                }
+            } else if (Integer.TYPE.equals(arrayType)) {
+                int[] objects = (int[])values;
+                for (i = 0, n = objects.length; i < n; i++) {
+                    writeValue(new Integer(objects[i]), writer, context, type, name, ns);
+                }
+            } else if (Long.TYPE.equals(arrayType)) {
+                long[] objects = (long[])values;
+                for (i = 0, n = objects.length; i < n; i++) {
+                    writeValue(new Long(objects[i]), writer, context, type, name, ns);
+                }
+            } else if (Short.TYPE.equals(arrayType)) {
+                short[] objects = (short[])values;
+                for (i = 0, n = objects.length; i < n; i++) {
+                    writeValue(new Short(objects[i]), writer, context, type, name, ns);
+                }
+            } else if (Double.TYPE.equals(arrayType)) {
+                double[] objects = (double[])values;
+                for (i = 0, n = objects.length; i < n; i++) {
+                    writeValue(new Double(objects[i]), writer, context, type, name, ns);
+                }
+            } else if (Float.TYPE.equals(arrayType)) {
+                float[] objects = (float[])values;
+                for (i = 0, n = objects.length; i < n; i++) {
+                    writeValue(new Float(objects[i]), writer, context, type, name, ns);
+                }
+            } else if (Byte.TYPE.equals(arrayType)) {
+                byte[] objects = (byte[])values;
+                for (i = 0, n = objects.length; i < n; i++) {
+                    writeValue(new Byte(objects[i]), writer, context, type, name, ns);
+                }
+            } else if (Boolean.TYPE.equals(arrayType)) {
+                boolean[] objects = (boolean[])values;
+                for (i = 0, n = objects.length; i < n; i++) {
+                    writeValue(Boolean.valueOf(objects[i]), writer, context, type, name,
ns);
+                }
+            } else if (Character.TYPE.equals(arrayType)) {
+                char[] objects = (char[])values;
+                for (i = 0, n = objects.length; i < n; i++) {
+                    writeValue(new Character(objects[i]), writer, context, type, name, ns);
+                }
             }
+        } finally {
+            context.getGlobalContext().setWriteXsiTypes(oldXsiWrite);
         }
     }
 
@@ -248,7 +260,7 @@
         } else {
             cwriter = writer;
         }
-        
+
         if (value == null && type.isNillable()) {
             cwriter.writeXsiNil();
         } else {
@@ -264,7 +276,7 @@
             if (hasDefinedArray(root)) {
                 return;
             }
-            
+
             Element complex = new Element("complexType", SOAPConstants.XSD_PREFIX, SOAPConstants.XSD);
             complex.setAttribute(new Attribute("name", getSchemaType().getLocalPart()));
             root.addContent(complex);
@@ -276,8 +288,8 @@
             seq.addContent(element);
 
             Type componentType = getComponentType();
-            String prefix = NamespaceHelper.getUniquePrefix(root, componentType
-                .getSchemaType().getNamespaceURI());
+            String prefix = NamespaceHelper.getUniquePrefix(root, componentType.getSchemaType()
+                .getNamespaceURI());
 
             String typeName = prefix + ":" + componentType.getSchemaType().getLocalPart();
 
@@ -302,17 +314,17 @@
     }
 
     /**
-     * Since both an Array and a List can have the same type definition, double check
-     * that there isn't already a defined type already.
+     * Since both an Array and a List can have the same type definition, double
+     * check that there isn't already a defined type already.
+     * 
      * @param root
      * @return
      */
     private boolean hasDefinedArray(Element root) {
         List children = root.getChildren("complexType", Namespace.getNamespace(SOAPConstants.XSD));
         for (Iterator itr = children.iterator(); itr.hasNext();) {
-            Element e = (Element) itr.next();
-            
-            
+            Element e = (Element)itr.next();
+
             if (e.getAttributeValue("name").equals(getSchemaType().getLocalPart())) {
                 return true;
             }
@@ -368,8 +380,7 @@
             // We couldn't find the type the user specified. One is created
             // below instead.
             if (type == null) {
-                LOG.debug("Couldn't find array component type " + componentName
-                             + ". Creating one instead.");
+                LOG.debug("Couldn't find array component type " + componentName + ". Creating
one instead.");
             }
         }
 

Modified: incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/BeanType.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/BeanType.java?rev=611657&r1=611656&r2=611657&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/BeanType.java
(original)
+++ incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/BeanType.java
Sun Jan 13 13:50:28 2008
@@ -266,22 +266,39 @@
 
         return impl.getMethod(name, new Class[] {pd.getPropertyType()});
     }
-
+    
+    /**
+     * To avoid double-writing xsi:type attributes, ObjectType uses this special entrypoint.
+     * @param object
+     * @param writer
+     * @param context
+     * @param wroteXsiType
+     */
+    void writeObjectFromObjectType(Object object, MessageWriter writer, 
+                                   Context context, boolean wroteXsiType) {
+        writeObjectInternal(object, writer, context, wroteXsiType);
+    }
+    
     /**
-     * @see org.apache.cxf.aegis.type.Type#writeObject(Object,
-     *      org.apache.cxf.aegis.xml.MessageWriter,
-     *      org.apache.cxf.aegis.Context)
+     * {@inheritDoc}
      */
     @Override
     public void writeObject(Object object, MessageWriter writer, Context context)
         throws DatabindingException {
+        writeObjectInternal(object, writer, context, false);
+    }
+
+    private void writeObjectInternal(Object object, MessageWriter writer, Context context,

+                                     boolean wroteXsiType)
+        throws DatabindingException {
         if (object == null) {
             return;
         }
 
         BeanTypeInfo inf = getTypeInfo();
 
-        if (object.getClass() == getTypeClass()
+        if (!wroteXsiType 
+            && object.getClass() == getTypeClass()
             && context.isWriteXsiTypes()) {
             writer.writeXsiType(getSchemaType());
         }

Modified: incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ObjectType.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ObjectType.java?rev=611657&r1=611656&r2=611657&view=diff
==============================================================================
--- incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ObjectType.java
(original)
+++ incubator/cxf/trunk/rt/databinding/aegis/src/main/java/org/apache/cxf/aegis/type/basic/ObjectType.java
Sun Jan 13 13:50:28 2008
@@ -184,17 +184,14 @@
                 type = tm.getTypeCreator().createType(object.getClass());
                 tm.register(type);
             }
-
-            String prefix = writer.getPrefixForNamespace(type.getSchemaType().getNamespaceURI(),
-                                                         type.getSchemaType().getPrefix());
-
-            if (null == prefix || prefix.length() == 0) {
-                addXsiType(writer, type.getSchemaType().getLocalPart());
+            
+            writer.writeXsiType(type.getSchemaType());
+            boolean nextIsBeanType = type instanceof BeanType;
+            if (nextIsBeanType) {
+                ((BeanType)type).writeObjectFromObjectType(object, writer, context, true);
             } else {
-                addXsiType(writer, prefix + ":" + type.getSchemaType().getLocalPart());
+                type.writeObject(object, writer, context);
             }
-
-            type.writeObject(object, writer, context);
         }
     }
 
@@ -230,14 +227,6 @@
         return determineType(context, superclass);
     }
 
-    private void addXsiType(MessageWriter writer, String prefixedType) {
-        MessageWriter typeWriter = writer.getAttributeWriter(XSI_TYPE);
-
-        typeWriter.writeValue(prefixedType);
-
-        typeWriter.close();
-    }
-
     public boolean isReadToDocument() {
         return readToDocument;
     }
@@ -270,25 +259,21 @@
 
     @Override
     public boolean isAbstract() {
-        // TODO Auto-generated method stub
         return super.isAbstract();
     }
 
     @Override
     public boolean isNillable() {
-        // TODO Auto-generated method stub
         return super.isNillable();
     }
 
     @Override
     public boolean isWriteOuter() {
-        // TODO Auto-generated method stub
         return super.isWriteOuter();
     }
 
     @Override
     public void setNillable(boolean nillable) {
-        // TODO Auto-generated method stub
         super.setNillable(nillable);
     }
 



Mime
View raw message