cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1523043 - in /cxf/trunk/rt/databinding/jaxb/src: main/java/org/apache/cxf/jaxb/ test/java/org/apache/cxf/jaxb/
Date Fri, 13 Sep 2013 18:18:03 GMT
Author: dkulp
Date: Fri Sep 13 18:18:02 2013
New Revision: 1523043

URL: http://svn.apache.org/r1523043
Log:
[CXF-5189] Make some more attemps at mapping some of the fields, particular those with XmlAttribute.

Modified:
    cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
    cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
    cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java
    cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/OrderException.java

Modified: cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java?rev=1523043&r1=1523042&r2=1523043&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
(original)
+++ cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
Fri Sep 13 18:18:02 2013
@@ -34,6 +34,7 @@ import java.util.HashSet;
 import java.util.Set;
 
 import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlSeeAlso;
@@ -380,10 +381,13 @@ class JAXBContextInitializer extends Ser
      */
     static boolean isFieldAccepted(Field field, XmlAccessType accessType) {
         // We only accept non static fields which are not marked @XmlTransient or has transient
modifier
-        if (Modifier.isStatic(field.getModifiers()) || field.isAnnotationPresent(XmlTransient.class)
+        if (field.isAnnotationPresent(XmlTransient.class)
             || Modifier.isTransient(field.getModifiers())) {
             return false;
         }
+        if (Modifier.isStatic(field.getModifiers())) {
+            return field.isAnnotationPresent(XmlAttribute.class);
+        }
         if (accessType == XmlAccessType.PUBLIC_MEMBER 
             && !Modifier.isPublic(field.getModifiers())) {
             return false;

Modified: cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java?rev=1523043&r1=1523042&r2=1523043&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
(original)
+++ cxf/trunk/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
Fri Sep 13 18:18:02 2013
@@ -72,6 +72,7 @@ import javax.xml.stream.util.StreamReade
 import javax.xml.transform.stream.StreamResult;
 
 import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
@@ -82,6 +83,7 @@ import org.apache.cxf.common.logging.Log
 import org.apache.cxf.common.util.ReflectionUtil;
 import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.service.model.MessagePartInfo;
 import org.apache.cxf.service.model.SchemaInfo;
@@ -352,26 +354,62 @@ public final class JAXBEncoderDecoder {
             Class<?> cls = part.getTypeClass();
             XmlAccessType accessType = Utils.getXmlAccessType(cls);
             String namespace = part.getElementQName().getNamespaceURI();
+            String attNs = namespace;
             
             SchemaInfo sch = part.getMessageInfo().getOperation().getInterface()
                 .getService().getSchema(namespace);
             if (sch == null) {
                 LOG.warning("Schema associated with " + namespace + " is null");
                 namespace = null;
-            } else if (!sch.isElementFormQualified()) {
-                namespace = null;
+                attNs = null;
+            } else {
+                if (!sch.isElementFormQualified()) {
+                    namespace = null;
+                }
+                if (!sch.isAttributeFormQualified()) {
+                    attNs = null;
+                }
             }
             List<Member> combinedMembers = new ArrayList<Member>();
 
-
             for (Field f : Utils.getFields(cls, accessType)) {
                 XmlAttribute at = f.getAnnotation(XmlAttribute.class);
                 if (at == null) {
                     combinedMembers.add(f);
+                } else {
+                    QName fname = new QName(attNs, StringUtils.isEmpty(at.name()) ? f.getName()
: at.name());
+                    ReflectionUtil.setAccessible(f);
+                    Object o = Utils.getFieldValue(f, elValue);
+                    Document doc = DOMUtils.newDocument();
+                    writeObject(marshaller, doc, newJAXBElement(fname, String.class, o));
+                    
+                    if (attNs != null) {
+                        writer.writeAttribute(attNs, fname.getLocalPart(),
+                                              DOMUtils.getAllContent(doc.getDocumentElement()));
+                    } else {
+                        writer.writeAttribute(fname.getLocalPart(), DOMUtils.getAllContent(doc.getDocumentElement()));
+                    }
                 }
             }
             for (Method m : Utils.getGetters(cls, accessType)) {
-                combinedMembers.add(m);
+                if (!m.isAnnotationPresent(XmlAttribute.class)) {
+                    combinedMembers.add(m);
+                } else {
+                    int idx = m.getName().startsWith("get") ? 3 : 2;
+                    String name = m.getName().substring(idx);
+                    name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
+                    XmlAttribute at = m.getAnnotation(XmlAttribute.class);
+                    QName mname = new QName(namespace, StringUtils.isEmpty(at.name()) ? name
: at.name());
+                    Document doc = DOMUtils.newDocument();
+                    Object o = Utils.getMethodValue(m, elValue); 
+                    writeObject(marshaller, doc, newJAXBElement(mname, String.class, o));
+                    if (attNs != null) {
+                        writer.writeAttribute(attNs, mname.getLocalPart(),
+                                              DOMUtils.getAllContent(doc.getDocumentElement()));
+                    } else {
+                        writer.writeAttribute(mname.getLocalPart(), DOMUtils.getAllContent(doc.getDocumentElement()));
+                    }
+                }
             }
 
             XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);

Modified: cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java?rev=1523043&r1=1523042&r2=1523043&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java
(original)
+++ cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java
Fri Sep 13 18:18:02 2013
@@ -473,6 +473,7 @@ public class JAXBEncoderDecoderTest exte
         assertTrue(c < d);
         assertTrue(d < e);
         assertTrue(bout.toString().indexOf("transientValue") < 0);
+        assertTrue(bout.toString(), bout.toString().indexOf("mappedField=\"MappedField\"")
> 0);
     }
     
     @Test

Modified: cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/OrderException.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/OrderException.java?rev=1523043&r1=1523042&r2=1523043&view=diff
==============================================================================
--- cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/OrderException.java (original)
+++ cxf/trunk/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/OrderException.java Fri
Sep 13 18:18:02 2013
@@ -20,14 +20,18 @@ package org.apache.cxf.jaxb;
 
 import javax.xml.bind.annotation.XmlAccessOrder;
 import javax.xml.bind.annotation.XmlAccessorOrder;
+import javax.xml.bind.annotation.XmlAttribute;
 
 @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
 public class OrderException extends Exception {
       
     private static final long serialVersionUID = 1L;
-
-    private transient int transientValue;
     
+    @XmlAttribute(name = "mappedField")
+    private static final String MAPPED_FIELD = "MappedField";
+    
+    private transient int transientValue;
+
     private String info1;
 
     private String info2;
@@ -37,8 +41,9 @@ public class OrderException extends Exce
     private int intVal;
 
     private String detail;
-       
-
+    
+    
+    
     public OrderException(String message) {
         super(message);
     }
@@ -94,6 +99,10 @@ public class OrderException extends Exce
     void setTransientValue(int transientValue) {
         this.transientValue = transientValue;
     }
+    
+    public String mappedField() {
+        return MAPPED_FIELD;
+    }
 
 
 }



Mime
View raw message