Author: klape
Date: 2013-01-11 13:56:30 -0500 (Fri, 11 Jan 2013)
New Revision: 17231
Modified:
thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
Log:
[JBPAPP-10129] Adding in fixes from CXF-4729 and CXF-4680 regarding property order of
exception types
Modified:
thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java
===================================================================
---
thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java 2013-01-11
16:45:53 UTC (rev 17230)
+++
thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBContextInitializer.java 2013-01-11
18:56:30 UTC (rev 17231)
@@ -38,7 +38,6 @@
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
-import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
import javax.xml.namespace.QName;
@@ -292,11 +291,11 @@
}
XmlJavaTypeAdapter xjta =
cls.getAnnotation(XmlJavaTypeAdapter.class);
if (xjta != null) {
- Type t = Utils.getTypeFromXmlAdapter(xjta);
- if (t != null) {
- addType(t);
- }
- return;
+ Type t = Utils.getTypeFromXmlAdapter(xjta);
+ if (t != null) {
+ addType(t);
+ }
+ return;
}
} else if (classes.contains(cls)) {
return;
@@ -314,7 +313,8 @@
adapter = f.getType().getAnnotation(XmlJavaTypeAdapter.class);
}
if (adapter == null) {
- XmlJavaTypeAdapters adapters =
f.getDeclaringClass().getPackage().getAnnotation(XmlJavaTypeAdapters.class);
+ XmlJavaTypeAdapters adapters =
+
f.getDeclaringClass().getPackage().getAnnotation(XmlJavaTypeAdapters.class);
if (adapters != null) {
for (XmlJavaTypeAdapter candidate : adapters.value()) {
if (candidate != null &&
candidate.type().equals(f.getType())) {
@@ -441,4 +441,4 @@
}
return false;
}
-}
\ No newline at end of file
+}
Modified:
thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java
===================================================================
---
thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java 2013-01-11
16:45:53 UTC (rev 17230)
+++
thirdparty/cxf/branches/cxf-2.2.12-patch02_JBPAPP-10129/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBSchemaInitializer.java 2013-01-11
18:56:30 UTC (rev 17231)
@@ -26,12 +26,22 @@
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.Iterator;
+import java.util.List;
+import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
+import javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorOrder;
+import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlList;
+import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.namespace.QName;
@@ -367,7 +377,7 @@
MessagePartInfo part = fault.getMessageParts().get(0);
Class<?> cls = part.getTypeClass();
Class<?> cl2 = (Class)fault.getProperty(Class.class.getName());
- if (cls != cl2) {
+ if (cls != cl2) {
QName name = (QName)fault.getProperty("elementName");
part.setElementQName(name);
JAXBBeanInfo beanInfo = getBeanInfo(cls);
@@ -426,6 +436,8 @@
XmlSchemaSequence seq = new XmlSchemaSequence();
ct.setParticle(seq);
+ List<XmlSchemaElement> elementList = new
ArrayList<XmlSchemaElement>();
+
Method methods[] = cls.getMethods();
for (Method m : methods) {
if (m.getName().startsWith("get")
@@ -453,15 +465,38 @@
el.setSchemaTypeName(typeName);
}
- seq.getItems().add(el);
+ elementList.add(el);
} catch (Exception e) {
//not mappable
}
}
}
+ XmlAccessorOrder xmlAccessorOrder =
cls.getAnnotation(XmlAccessorOrder.class);
+ XmlType xmlTypeAnno = cls.getAnnotation(XmlType.class);
+ String[] propertyOrder = null;
+
+ if (xmlTypeAnno != null && xmlTypeAnno.propOrder().length > 0) {
+ propertyOrder = xmlTypeAnno.propOrder();
+ //TODO: handle @XmlAccessOrder
+ }
+
+ if (propertyOrder != null && propertyOrder.length ==
elementList.size()) {
+ sortItems(elementList, propertyOrder);
+ } else if (propertyOrder != null && propertyOrder.length !=
elementList.size()) {
+ LOG.log(Level.WARNING, "propOrder in @XmlType doesn't define all
schema elements: "
+ + Arrays.toString(propertyOrder));
+ }
+
+ if (xmlAccessorOrder != null &&
xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL)
+ && propertyOrder == null) {
+ sort(elementList);
+ }
+
+ for (XmlSchemaElement elem : elementList) {
+ seq.getItems().add(elem);
+ }
}
}
-
private void buildExceptionType(MessagePartInfo part, Class<?> cls) {
SchemaInfo schemaInfo = null;
@@ -471,6 +506,15 @@
break;
}
}
+ XmlAccessorOrder xmlAccessorOrder = cls.getAnnotation(XmlAccessorOrder.class);
+ XmlType xmlTypeAnno = cls.getAnnotation(XmlType.class);
+ String[] propertyOrder = null;
+
+ if (xmlTypeAnno != null && xmlTypeAnno.propOrder().length > 0) {
+ propertyOrder = xmlTypeAnno.propOrder();
+ //TODO: handle @XmlAccessOrder
+ }
+
XmlSchema schema;
if (schemaInfo == null) {
schema =
schemas.newXmlSchemaInCollection(part.getElementQName().getNamespaceURI());
@@ -516,13 +560,21 @@
ct.setParticle(seq);
String namespace = part.getElementQName().getNamespaceURI();
+ XmlAccessorType accessorType = cls.getAnnotation(XmlAccessorType.class);
+ if (accessorType == null && cls.getPackage() != null) {
+ accessorType = cls.getPackage().getAnnotation(XmlAccessorType.class);
+ }
+
XmlAccessType accessType = Utils.getXmlAccessType(cls);
+
+ List<XmlSchemaElement> elementList = new
ArrayList<XmlSchemaElement>();
+
for (Field f : Utils.getFields(cls, accessType)) {
//map field
Type type = Utils.getFieldType(f);
JAXBBeanInfo beanInfo = getBeanInfo(type);
if (beanInfo != null) {
- addElement(schema, seq, beanInfo, new QName(namespace, f.getName()),
isArray(type));
+ addElement(elementList, beanInfo, new QName(namespace, f.getName()),
isArray(type));
}
}
for (Method m : Utils.getGetters(cls, accessType)) {
@@ -533,9 +585,26 @@
int idx = m.getName().startsWith("get") ? 3 : 2;
String name = m.getName().substring(idx);
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
- addElement(schema, seq, beanInfo, new QName(namespace, name),
isArray(type));
+ addElement(elementList, beanInfo, new QName(namespace, name),
isArray(type));
}
}
+
+ if (propertyOrder != null && propertyOrder.length == elementList.size())
{
+ sortItems(elementList, propertyOrder);
+ } else if (propertyOrder != null && propertyOrder.length !=
elementList.size()) {
+ LOG.log(Level.WARNING, "propOrder in @XmlType doesn't define all
schema elements: "
+ + Arrays.toString(propertyOrder));
+ }
+
+ if (xmlAccessorOrder != null &&
xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL)
+ && propertyOrder == null) {
+ sort(elementList);
+ }
+
+ for (XmlSchemaElement elem : elementList) {
+ seq.getItems().add(elem);
+ }
+
part.setProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION",
Boolean.TRUE);
}
@@ -550,8 +619,7 @@
return false;
}
- public void addElement(XmlSchema schema,
- XmlSchemaSequence seq, JAXBBeanInfo beanInfo,
+ public void addElement(List<XmlSchemaElement> elementList, JAXBBeanInfo
beanInfo,
QName name, boolean isArray) {
XmlSchemaElement el = new XmlSchemaElement();
el.setName(name.getLocalPart());
@@ -581,7 +649,7 @@
el.setSchemaTypeName(typeName);
}
- seq.getItems().add(el);
+ elementList.add(el);
}
@@ -599,4 +667,25 @@
}
return isExist;
}
+
+ //sort to Alphabetical order
+ private void sort(final List<XmlSchemaElement> elementList) {
+ Collections.sort(elementList, new Comparator<XmlSchemaElement>() {
+ public int compare(XmlSchemaElement e1, XmlSchemaElement e2) {
+ return e1.getName().compareTo(e2.getName());
+ }
+ });
+ }
+
+ private void sortItems(List<XmlSchemaElement> elementList, String[] propOrder)
{
+ final List<String> propList = Arrays.asList(propOrder);
+
+ Collections.sort(elementList, new Comparator<XmlSchemaElement>() {
+ public int compare(XmlSchemaElement el1, XmlSchemaElement el2) {
+ int index1 = propList.indexOf(el1.getName());
+ int index2 = propList.indexOf(el2.getName());
+ return index1 - index2;
+ }
+ });
+ }
}