You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2008/05/29 19:24:19 UTC
svn commit: r661410 [1/2] - in /incubator/tuscany/java/sca/modules:
core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/processor/
core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/
databinding-axiom...
Author: rfeng
Date: Thu May 29 10:24:18 2008
New Revision: 661410
URL: http://svn.apache.org/viewvc?rev=661410&view=rev
Log:
Improve the databinding wrapper handling, especially for JAXB
Added:
incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DataConverter.java (with props)
incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBPropertyDescriptor.java (with props)
incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTypeHelper.java
- copied, changed from r660979, incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/JAXBTypeHelper.java
incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperException.java (with props)
incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHelper.java (with props)
incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/XMLRootElementUtil.java (with props)
Removed:
incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/JAXBTypeHelper.java
Modified:
incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/processor/DataBindingJavaInterfaceProcessor.java
incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/Input2InputTransformer.java
incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/Output2OutputTransformer.java
incubator/tuscany/java/sca/modules/databinding-axiom/src/main/java/org/apache/tuscany/sca/databinding/axiom/OMElementWrapperHandler.java
incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java
incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHandler.java
incubator/tuscany/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/JAXBReflectionTestCase.java
incubator/tuscany/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/JAXBWrapperHandlerTestCase.java
incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOContextHelper.java
incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDODataBinding.java
incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOWrapperHandler.java
incubator/tuscany/java/sca/modules/databinding/pom.xml
incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DataBinding.java
incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DefaultDataBindingExtensionPoint.java
incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/WrapperHandler.java
incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/BaseDataBinding.java
incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/xml/DOMWrapperHandler.java
incubator/tuscany/java/sca/modules/interface-wsdl-java2wsdl/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/interface2wsdl/Interface2WSDLGenerator.java
incubator/tuscany/java/sca/modules/interface/src/main/java/org/apache/tuscany/sca/interfacedef/Operation.java
incubator/tuscany/java/sca/modules/interface/src/main/java/org/apache/tuscany/sca/interfacedef/impl/OperationImpl.java
incubator/tuscany/java/sca/modules/interface/src/main/java/org/apache/tuscany/sca/interfacedef/util/WrapperInfo.java
Modified: incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/processor/DataBindingJavaInterfaceProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/processor/DataBindingJavaInterfaceProcessor.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/processor/DataBindingJavaInterfaceProcessor.java (original)
+++ incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/processor/DataBindingJavaInterfaceProcessor.java Thu May 29 10:24:18 2008
@@ -43,7 +43,8 @@
* @version $Rev$ $Date$
*/
public class DataBindingJavaInterfaceProcessor implements JavaInterfaceVisitor {
- private DataBindingExtensionPoint dataBindingRegistry;
+ private static final String JAXB_DATABINDING = "javax.xml.bind.JAXBElement";
+ private DataBindingExtensionPoint dataBindingRegistry;
public DataBindingJavaInterfaceProcessor(DataBindingExtensionPoint dataBindingRegistry) {
super();
@@ -85,7 +86,7 @@
continue;
}
Operation operation = opMap.get(method.getName());
- if (operation == null) { // @WebMethod exclude=true
+ if (operation == null) { // @WebMethod exclude=true
continue;
}
DataBinding methodDataBinding = clazz.getAnnotation(DataBinding.class);
@@ -137,7 +138,8 @@
}
// JIRA: TUSCANY-842
- if (operation.getDataBinding() == null) {
+ String db = operation.getDataBinding();
+ if (db == null || JAXB_DATABINDING.equals(db)) {
assignOperationDataBinding(operation);
}
@@ -162,7 +164,9 @@
opDataTypes.addAll(operation.getInputType().getLogical());
opDataTypes.add(operation.getOutputType());
- opDataTypes.addAll(operation.getFaultTypes());
+ for (DataType<DataType> ft : operation.getFaultTypes()) {
+ opDataTypes.add(ft.getLogical());
+ }
for (DataType<?> d : opDataTypes) {
if (d != null) {
Modified: incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/Input2InputTransformer.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/Input2InputTransformer.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/Input2InputTransformer.java (original)
+++ incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/Input2InputTransformer.java Thu May 29 10:24:18 2008
@@ -49,7 +49,7 @@
protected Mediator mediator;
public Input2InputTransformer() {
- super();
+ super();
}
@Override
@@ -116,8 +116,10 @@
WrapperInfo wrapper = targetOp.getWrapper();
ElementInfo wrapperElement = wrapper.getInputWrapperElement();
+ Class<?> targetWrapperClass = wrapper != null ? wrapper.getInputWrapperClass() : null;
+
if (source == null) {
- Object targetWrapper = targetWrapperHandler.create(wrapperElement, null, context);
+ Object targetWrapper = targetWrapperHandler.create(wrapperElement, targetWrapperClass, context);
return new Object[] {targetWrapper};
}
@@ -128,12 +130,12 @@
DataType sourceWrapperType =
sourceWrapperHandler.getWrapperType(wrapperElement, sourceWrapperClass, context);
if (sourceWrapperType != null) {
- Object sourceWrapper = sourceWrapperHandler.create(wrapperElement, null, context);
+ Object sourceWrapper = sourceWrapperHandler.create(wrapperElement, sourceWrapperClass, context);
if (sourceWrapper != null) {
- for (int i = 0; i < source.length; i++) {
- ElementInfo argElement = wrapper.getInputChildElements().get(i);
- sourceWrapperHandler.setChild(sourceWrapper, i, argElement, source[i]);
- }
+ sourceWrapperHandler.setChildren(sourceWrapper,
+ wrapper.getInputChildElements(),
+ source,
+ context);
Object targetWrapper =
mediator.mediate(sourceWrapper, sourceWrapperType, targetType.getLogical().get(0), context
.getMetadata());
@@ -142,16 +144,17 @@
}
}
// Fall back to child by child transformation
- Object targetWrapper = targetWrapperHandler.create(wrapperElement, null, context);
+ Object targetWrapper = targetWrapperHandler.create(wrapperElement, targetWrapperClass, context);
List<DataType> argTypes = wrapper.getUnwrappedInputType().getLogical();
+ Object[] targetChildren = new Object[source.length];
for (int i = 0; i < source.length; i++) {
ElementInfo argElement = wrapper.getInputChildElements().get(i);
DataType<XMLType> argType = argTypes.get(i);
- Object child = source[i];
- child = mediator.mediate(source[i], sourceType.getLogical().get(i), argType, context.getMetadata());
- targetWrapperHandler.setChild(targetWrapper, i, argElement, child);
+ targetChildren[i] =
+ mediator.mediate(source[i], sourceType.getLogical().get(i), argType, context.getMetadata());
}
+ targetWrapperHandler.setChildren(targetWrapper, wrapper.getInputChildElements(), targetChildren, context);
return new Object[] {targetWrapper};
} else if (sourceWrapped && (!targetWrapped)) {
@@ -214,7 +217,12 @@
}
private String getDataBinding(Operation operation) {
- return operation.getDataBinding();
+ WrapperInfo wrapper = operation.getWrapper();
+ if (wrapper != null) {
+ return wrapper.getDataBinding();
+ } else {
+ return null;
+ }
}
}
Modified: incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/Output2OutputTransformer.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/Output2OutputTransformer.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/Output2OutputTransformer.java (original)
+++ incubator/tuscany/java/sca/modules/core-databinding/src/main/java/org/apache/tuscany/sca/core/databinding/transformers/Output2OutputTransformer.java Thu May 29 10:24:18 2008
@@ -50,7 +50,7 @@
*/
public Output2OutputTransformer() {
super();
- }
+ }
/**
* @param mediator the mediator to set
@@ -95,7 +95,12 @@
}
private String getDataBinding(Operation operation) {
- return operation.getDataBinding();
+ WrapperInfo wrapper = operation.getWrapper();
+ if (wrapper != null) {
+ return wrapper.getDataBinding();
+ } else {
+ return null;
+ }
}
private WrapperHandler getWrapperHandler(String dataBindingId, boolean required) {
@@ -133,19 +138,23 @@
WrapperInfo wrapper = targetOp.getWrapper();
ElementInfo wrapperElement = wrapper.getOutputWrapperElement();
List<ElementInfo> childElements = wrapper.getOutputChildElements();
+ Class<?> targetWrapperClass = wrapper != null ? wrapper.getOutputWrapperClass() : null;
// If the source can be wrapped, wrapped it first
if (sourceWrapperHandler != null) {
Class<?> sourceWrapperClass =
sourceOp.getWrapper() != null ? sourceOp.getWrapper().getOutputWrapperClass() : null;
- DataType sourceWrapperType = sourceWrapperHandler.getWrapperType(wrapperElement, null, context);
+ DataType sourceWrapperType =
+ sourceWrapperHandler.getWrapperType(wrapperElement, sourceWrapperClass, context);
if (sourceWrapperType != null) {
- Object sourceWrapper = sourceWrapperHandler.create(wrapperElement, null, context);
+ Object sourceWrapper = sourceWrapperHandler.create(wrapperElement, sourceWrapperClass, context);
if (sourceWrapper != null) {
if (!childElements.isEmpty()) {
// Set the return value
- ElementInfo returnElement = wrapper.getOutputChildElements().get(0);
- sourceWrapperHandler.setChild(sourceWrapper, 0, returnElement, response);
+ sourceWrapperHandler.setChildren(sourceWrapper,
+ wrapper.getOutputChildElements(),
+ new Object[] {response},
+ context);
}
Object targetWrapper =
mediator.mediate(sourceWrapper, sourceWrapperType, targetType.getLogical(), context
@@ -154,17 +163,18 @@
}
}
}
- Object targetWrapper = targetWrapperHandler.create(wrapper.getOutputWrapperElement(), null, context);
+ Object targetWrapper =
+ targetWrapperHandler.create(wrapper.getOutputWrapperElement(), targetWrapperClass, context);
if (childElements.isEmpty()) {
// void output
return targetWrapper;
}
- ElementInfo argElement = childElements.get(0);
+
DataType<XMLType> argType = wrapper.getUnwrappedOutputType();
Object child = response;
child = mediator.mediate(response, sourceType.getLogical(), argType, context.getMetadata());
- targetWrapperHandler.setChild(targetWrapper, 0, argElement, child);
+ targetWrapperHandler.setChildren(targetWrapper, childElements, new Object[] {child}, context);
return targetWrapper;
} else if (sourceWrapped && (!targetWrapped)) {
// Wrapped to Unwrapped
Modified: incubator/tuscany/java/sca/modules/databinding-axiom/src/main/java/org/apache/tuscany/sca/databinding/axiom/OMElementWrapperHandler.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-axiom/src/main/java/org/apache/tuscany/sca/databinding/axiom/OMElementWrapperHandler.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-axiom/src/main/java/org/apache/tuscany/sca/databinding/axiom/OMElementWrapperHandler.java (original)
+++ incubator/tuscany/java/sca/modules/databinding-axiom/src/main/java/org/apache/tuscany/sca/databinding/axiom/OMElementWrapperHandler.java Thu May 29 10:24:18 2008
@@ -20,10 +20,8 @@
package org.apache.tuscany.sca.databinding.axiom;
import java.util.ArrayList;
-import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
-import java.util.Set;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
@@ -60,6 +58,16 @@
return wrapper;
}
+ public void setChildren(OMElement wrapper,
+ List<ElementInfo> childElements,
+ Object[] childObjects,
+ TransformationContext context) {
+ for (int i = 0; i < childElements.size(); i++) {
+ setChild(wrapper, i, childElements.get(i), childObjects[i]);
+ }
+
+ }
+
public void setChild(OMElement wrapper, int i, ElementInfo childElement, Object value) {
if (childElement.isMany()) {
Object[] elements = (Object[])value;
@@ -103,7 +111,9 @@
/**
* @see org.apache.tuscany.sca.databinding.WrapperHandler#getWrapperType(org.apache.tuscany.sca.interfacedef.util.ElementInfo, Class, org.apache.tuscany.sca.databinding.TransformationContext)
*/
- public DataType getWrapperType(ElementInfo element, Class<? extends OMElement> wrapperClass, TransformationContext context) {
+ public DataType getWrapperType(ElementInfo element,
+ Class<? extends OMElement> wrapperClass,
+ TransformationContext context) {
DataType<XMLType> wrapperType =
new DataTypeImpl<XMLType>(AxiomDataBinding.NAME, OMElement.class, new XMLType(element));
return wrapperType;
@@ -117,6 +127,8 @@
if (!element.getQName().equals(wrapper.getQName())) {
return false;
}
+ return true;
+ /*
Set<QName> names = new HashSet<QName>();
for (ElementInfo e : childElements) {
names.add(e.getQName());
@@ -128,6 +140,7 @@
}
}
return true;
+ */
}
private static final QName XSI_TYPE_QNAME = new QName("http://www.w3.org/2001/XMLSchema-instance", "type", "xsi");
Added: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DataConverter.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DataConverter.java?rev=661410&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DataConverter.java (added)
+++ incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DataConverter.java Thu May 29 10:24:18 2008
@@ -0,0 +1,379 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.databinding.jaxb;
+
+import java.awt.Image;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import javax.activation.DataHandler;
+import javax.imageio.ImageIO;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.ws.WebServiceException;
+
+import org.osoa.sca.ServiceRuntimeException;
+
+/**
+ * Provides utilities to convert an object into a different kind of Object. For example, convert a
+ * String[] into a List<String>
+ */
+public class DataConverter {
+
+ /**
+ * This method should return true if the convert method will succeed.
+ * <p/>
+ * Note that any changes to isConvertable() must also be accompanied by similar changes to
+ * convert()
+ *
+ * @param obj source object or class
+ * @param dest destination class
+ * @return boolean true if convert(..) can convert obj to the destination class
+ */
+ public static boolean isConvertable(Object obj, Class dest) {
+ Class src = null;
+
+ if (obj != null) {
+ if (obj instanceof Class) {
+ src = (Class)obj;
+ } else {
+ src = obj.getClass();
+ }
+ }
+
+ if (dest == null) {
+ return false;
+ }
+
+ if (src == null) {
+ return true;
+ }
+
+ // If we're directly assignable, we're good.
+ if (dest.isAssignableFrom(src)) {
+ return true;
+ }
+
+ // If it's a wrapping conversion, we're good.
+ if (getWrapperClass(src) == dest) {
+ return true;
+ }
+ if (getWrapperClass(dest) == src) {
+ return true;
+ }
+
+ // If it's List -> Array or vice versa, we're good.
+ if ((Collection.class.isAssignableFrom(src) || src.isArray()) && (Collection.class.isAssignableFrom(dest) || dest
+ .isArray())) {
+
+ // TODO this should consider the component types instead of returning true.
+ return true;
+ }
+
+ // Allow mapping of HashMaps to Hashtables
+ if (src == HashMap.class && dest == Hashtable.class)
+ return true;
+
+ // Allow mapping of Calendar to Date
+ if (Calendar.class.isAssignableFrom(src) && dest == Date.class) {
+ return true;
+ }
+
+ if (src.isPrimitive()) {
+ return isConvertable(getWrapperClass(src), dest);
+ }
+
+ if (InputStream.class.isAssignableFrom(src) && dest == byte[].class) {
+ return true;
+ }
+
+ if (Source.class.isAssignableFrom(src) && dest == byte[].class) {
+ return true;
+ }
+
+ if (DataHandler.class.isAssignableFrom(src) && isConvertable(byte[].class, dest)) {
+ return true;
+ }
+
+ if (DataHandler.class.isAssignableFrom(src) && dest == Image.class) {
+ return true;
+ }
+
+ if (DataHandler.class.isAssignableFrom(src) && dest == Source.class) {
+ return true;
+ }
+
+ if (byte[].class.isAssignableFrom(src) && dest == String.class) {
+ return true;
+ }
+
+ // If it's a MIME type mapping and we want a DataHandler,
+ // then we're good.
+ // REVIEW Do we want to support this
+ /*
+ if (dest.getName().equals("javax.activation.DataHandler")) {
+ String name = src.getName();
+ if (src == String.class
+ || src == java.awt.Image.class
+ || name.equals("javax.mail.internet.MimeMultipart")
+ || name.equals("javax.xml.transform.Source"))
+ return true;
+ }
+ */
+
+ return false;
+ }
+
+ /**
+ * Utility function to convert an Object to some desired Class.
+ * <p/>
+ * Normally this is used for T[] to List<T> processing. Other conversions are also done (i.e.
+ * HashMap <->Hashtable, etc.)
+ * <p/>
+ * Use the isConvertable() method to determine if conversion is possible. Note that any changes
+ * to convert() must also be accompanied by similar changes to isConvertable()
+ *
+ * @param arg the array to convert
+ * @param destClass the actual class we want
+ * @return object of destClass if conversion possible, otherwise returns arg
+ */
+ public static Object convert(Object arg, Class destClass) throws WebServiceException {
+ if (destClass == null) {
+ return arg;
+ }
+
+ if (arg != null && destClass.isAssignableFrom(arg.getClass())) {
+ return arg;
+ }
+
+ // Convert between Calendar and Date
+ if (arg instanceof Calendar && destClass == Date.class) {
+ return ((Calendar)arg).getTime();
+ }
+
+ // Convert between HashMap and Hashtable
+ if (arg instanceof HashMap && destClass == Hashtable.class) {
+ return new Hashtable((HashMap)arg);
+ }
+
+ if (arg instanceof InputStream && destClass == byte[].class) {
+
+ try {
+ InputStream is = (InputStream)arg;
+ return getBytesFromStream(is);
+ } catch (IOException e) {
+ throw new ServiceRuntimeException(e);
+ }
+ }
+
+ if (arg instanceof Source && destClass == byte[].class) {
+ try {
+ if (arg instanceof StreamSource) {
+ InputStream is = ((StreamSource)arg).getInputStream();
+ if (is != null) {
+ return getBytesFromStream(is);
+ }
+ }
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ Result result = new StreamResult(out);
+ Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ transformer.transform((Source)arg, result);
+ byte[] bytes = out.toByteArray();
+ return bytes;
+
+ } catch (Exception e) {
+ throw new ServiceRuntimeException(e);
+ }
+ }
+
+ if (arg instanceof DataHandler) {
+ try {
+ InputStream is = ((DataHandler)arg).getInputStream();
+ if (destClass == Image.class) {
+ return ImageIO.read(is);
+ } else if (destClass == Source.class) {
+ return new StreamSource(is);
+ }
+ byte[] bytes = getBytesFromStream(is);
+ return convert(bytes, destClass);
+ } catch (Exception e) {
+ throw new ServiceRuntimeException(e);
+ }
+ }
+
+ if (arg instanceof byte[] && destClass == String.class) {
+ return new String((byte[])arg);
+ }
+
+ // If the destination is an array and the source
+ // is a suitable component, return an array with
+ // the single item.
+ /* REVIEW do we need to support atomic to array conversion ?
+ if (arg != null &&
+ destClass.isArray() &&
+ !destClass.getComponentType().equals(Object.class) &&
+ destClass.getComponentType().isAssignableFrom(arg.getClass())) {
+ Object array =
+ Array.newInstance(destClass.getComponentType(), 1);
+ Array.set(array, 0, arg);
+ return array;
+ }
+ */
+
+ // Return if no conversion is available
+ if (!(arg instanceof Collection || (arg != null && arg.getClass().isArray()))) {
+ return arg;
+ }
+
+ if (arg == null) {
+ return null;
+ }
+
+ // The arg may be an array or List
+ Object destValue = null;
+ int length = 0;
+ if (arg.getClass().isArray()) {
+ length = Array.getLength(arg);
+ } else {
+ length = ((Collection)arg).size();
+ }
+
+ try {
+ if (destClass.isArray()) {
+ if (destClass.getComponentType().isPrimitive()) {
+
+ Object array = Array.newInstance(destClass.getComponentType(), length);
+ // Assign array elements
+ if (arg.getClass().isArray()) {
+ for (int i = 0; i < length; i++) {
+ Array.set(array, i, Array.get(arg, i));
+ }
+ } else {
+ int idx = 0;
+ for (Iterator i = ((Collection)arg).iterator(); i.hasNext();) {
+ Array.set(array, idx++, i.next());
+ }
+ }
+ destValue = array;
+
+ } else {
+ Object[] array;
+ try {
+ array = (Object[])Array.newInstance(destClass.getComponentType(), length);
+ } catch (Exception e) {
+ return arg;
+ }
+
+ // Use convert to assign array elements.
+ if (arg.getClass().isArray()) {
+ for (int i = 0; i < length; i++) {
+ array[i] = convert(Array.get(arg, i), destClass.getComponentType());
+ }
+ } else {
+ int idx = 0;
+ for (Iterator i = ((Collection)arg).iterator(); i.hasNext();) {
+ array[idx++] = convert(i.next(), destClass.getComponentType());
+ }
+ }
+ destValue = array;
+ }
+ } else if (Collection.class.isAssignableFrom(destClass)) {
+ Collection newList = null;
+ try {
+ // if we are trying to create an interface, build something
+ // that implements the interface
+ if (destClass == Collection.class || destClass == List.class) {
+ newList = new ArrayList();
+ } else if (destClass == Set.class) {
+ newList = new HashSet();
+ } else {
+ newList = (Collection)destClass.newInstance();
+ }
+ } catch (Exception e) {
+ // No FFDC code needed
+ // Couldn't build one for some reason... so forget it.
+ return arg;
+ }
+
+ if (arg.getClass().isArray()) {
+ for (int j = 0; j < length; j++) {
+ newList.add(Array.get(arg, j));
+ }
+ } else {
+ for (Iterator j = ((Collection)arg).iterator(); j.hasNext();) {
+ newList.add(j.next());
+ }
+ }
+ destValue = newList;
+ } else {
+ destValue = arg;
+ }
+ } catch (Throwable t) {
+ throw new ServiceRuntimeException(t);
+ }
+
+ return destValue;
+ }
+
+ private static byte[] getBytesFromStream(InputStream is) throws IOException {
+ // TODO This code assumes that available is the length of the stream.
+ byte[] bytes = new byte[is.available()];
+ is.read(bytes);
+ return bytes;
+ }
+
+ public static Class getWrapperClass(Class primitive) {
+ if (primitive == int.class) {
+ return java.lang.Integer.class;
+ } else if (primitive == short.class) {
+ return java.lang.Short.class;
+ } else if (primitive == boolean.class) {
+ return java.lang.Boolean.class;
+ } else if (primitive == byte.class) {
+ return java.lang.Byte.class;
+ } else if (primitive == long.class) {
+ return java.lang.Long.class;
+ } else if (primitive == double.class) {
+ return java.lang.Double.class;
+ } else if (primitive == float.class) {
+ return java.lang.Float.class;
+ } else if (primitive == char.class) {
+ return java.lang.Character.class;
+ }
+
+ return null;
+ }
+}
Propchange: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DataConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DataConverter.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java (original)
+++ incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java Thu May 29 10:24:18 2008
@@ -27,6 +27,8 @@
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
+import org.apache.tuscany.sca.databinding.WrapperHandler;
+import org.apache.tuscany.sca.databinding.XMLTypeHelper;
import org.apache.tuscany.sca.databinding.impl.BaseDataBinding;
import org.apache.tuscany.sca.databinding.impl.DOMHelper;
import org.apache.tuscany.sca.interfacedef.DataType;
@@ -44,9 +46,14 @@
public static final String ROOT_NAMESPACE = "http://tuscany.apache.org/xmlns/sca/databinding/jaxb/1.0";
public static final QName ROOT_ELEMENT = new QName(ROOT_NAMESPACE, "root");
-
+
+ private JAXBWrapperHandler wrapperHandler;
+ private JAXBTypeHelper xmlTypeHelper;
+
public JAXBDataBinding() {
super(NAME, ALIASES, JAXBElement.class);
+ this.wrapperHandler = new JAXBWrapperHandler();
+ this.xmlTypeHelper = new JAXBTypeHelper();
}
@Override
@@ -105,4 +112,14 @@
}
}
+ @Override
+ public WrapperHandler getWrapperHandler() {
+ return wrapperHandler;
+ }
+
+ @Override
+ public XMLTypeHelper getXMLTypeHelper() {
+ return xmlTypeHelper;
+ }
+
}
Added: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBPropertyDescriptor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBPropertyDescriptor.java?rev=661410&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBPropertyDescriptor.java (added)
+++ incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBPropertyDescriptor.java Thu May 29 10:24:18 2008
@@ -0,0 +1,294 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.databinding.jaxb;
+
+import java.beans.IndexedPropertyDescriptor;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collection;
+
+import javax.xml.bind.JAXBElement;
+import javax.xml.namespace.QName;
+
+import org.osoa.sca.ServiceRuntimeException;
+
+/**
+ * A PropertyDescriptor provides acesss to a bean property. Values can be queried/changed using the
+ * read and writer methods of the PropertyDescriptor.
+ * <p/>
+ * A PropertyDescriptorPlus object wraps a PropertyDescriptor and supplies enhanced set/get methods
+ * that match JAXB semantis.
+ * <p/>
+ * For example, the set(..) method is smart enough to add lists, arrays and atomic values on JAXB
+ * beans.
+ * <p/>
+ * The PropertyDescriptorPlus object also stores the xmlName of the property.
+ *
+ * @See XMLRootElementUtil.createPropertyDescriptorMap , which creates the PropertyDescriptorPlus
+ * objects
+ */
+public class JAXBPropertyDescriptor {
+ PropertyDescriptor descriptor;
+ QName xmlName = null;
+
+ /**
+ * Package protected constructor. Only created by XMLRootElementUtil.createPropertyDescriptorMap
+ *
+ * @param propertyName
+ * @param descriptor
+ * @see XMLRootElementUtil.createPropertyDescriptorMap
+ */
+ JAXBPropertyDescriptor(PropertyDescriptor descriptor, QName xmlName) {
+ super();
+ this.descriptor = descriptor;
+ this.xmlName = xmlName;
+ }
+
+ /**
+ * Package protected constructor. Only created by XMLRootElementUtil.createPropertyDescriptorMap
+ *
+ * @param propertyName
+ * @param descriptor
+ * @see XMLRootElementUtil.createPropertyDescriptorMap
+ */
+ JAXBPropertyDescriptor(PropertyDescriptor descriptor, String xmlName) {
+ super();
+ this.descriptor = descriptor;
+ this.xmlName = new QName("", xmlName);
+ }
+
+ /** @return xmlname */
+ public String getXmlName() {
+ return xmlName.getLocalPart();
+ }
+
+ public QName getXmlQName() {
+ return xmlName;
+ }
+
+ /** @return property type */
+ public Class<?> getPropertyType() {
+ return descriptor.getPropertyType();
+ }
+
+ /** @return property name */
+ public String getPropertyName() {
+ return descriptor.getName();
+ }
+
+ /**
+ * Get the object
+ *
+ * @param targetBean
+ * @return Object for this property or null
+ * @throws InvocationTargetException
+ * @throws IllegalAccessException
+ */
+ public Object get(Object targetBean) throws InvocationTargetException, IllegalAccessException {
+ Method method = descriptor.getReadMethod();
+ if (method == null && descriptor.getPropertyType() == Boolean.class) {
+ String propertyName = descriptor.getName();
+ if (propertyName != null) {
+ String methodName = "is";
+ methodName =
+ methodName + ((propertyName.length() > 0) ? propertyName.substring(0, 1).toUpperCase() : "");
+ methodName = methodName + ((propertyName.length() > 1) ? propertyName.substring(1) : "");
+ try {
+ method = targetBean.getClass().getMethod(methodName);
+ } catch (NoSuchMethodException e) {
+ }
+ }
+ }
+ if (method == null) {
+ throw new ServiceRuntimeException("No getter is found");
+ }
+ Object ret = method.invoke(targetBean);
+ if (method.getReturnType() == JAXBElement.class) {
+ ret = ((JAXBElement<?>)ret).getValue();
+ }
+ return ret;
+ }
+
+ /**
+ * Set the object
+ *
+ * @param targetBean
+ * @param propValue
+ * @throws InvocationTargetException
+ * @throws IllegalAccessException
+ * @throws JAXBWrapperException
+ */
+ public void set(Object targetBean, Object propValue) throws InvocationTargetException, IllegalAccessException,
+ JAXBWrapperException {
+
+ Method writeMethod = null;
+ try {
+ // No set occurs if the value is null
+ if (propValue == null) {
+ return;
+ }
+
+ // There are 3 different types of setters that can occur.
+ // 1) Normal Attomic Setter : setFoo(type)
+ // 2) Indexed Array Setter : setFoo(type[])
+ // 3) No Setter case if the property is a List<T>.
+
+ writeMethod = descriptor.getWriteMethod();
+ if (descriptor instanceof IndexedPropertyDescriptor) {
+ // Set for indexed T[]
+ setIndexedArray(targetBean, propValue, writeMethod);
+ } else if (writeMethod == null) {
+ // Set for List<T>
+ setList(targetBean, propValue);
+ } else if (descriptor.getPropertyType() == JAXBElement.class) {
+ if (propValue != null) {
+ Class<?> clazz = propValue.getClass();
+ JAXBElement<?> element = new JAXBElement(xmlName, clazz, propValue);
+ setAtomic(targetBean, element, writeMethod);
+ }
+ } else {
+ // Normal case
+ setAtomic(targetBean, propValue, writeMethod);
+ }
+ } catch (RuntimeException e) {
+ throw e;
+ }
+ }
+
+ /**
+ * Set the property value onto targetBean using the writeMethod
+ *
+ * @param targetBean
+ * @param propValue
+ * @param writeMethod (set(T))
+ * @throws InvocationTargetException
+ * @throws IllegalAccessException
+ * @throws JAXBWrapperException
+ */
+ private void setAtomic(Object targetBean, Object propValue, Method writeMethod) throws InvocationTargetException,
+ IllegalAccessException, JAXBWrapperException {
+ // JAXB provides setters for atomic value.
+
+ if (propValue != null) {
+ // Normal case
+ Object[] SINGLE_PARAM = new Object[1];
+ SINGLE_PARAM[0] = propValue;
+ writeMethod.invoke(targetBean, SINGLE_PARAM);
+ } else {
+ Class<?>[] paramTypes = writeMethod.getParameterTypes();
+
+ if (paramTypes != null && paramTypes.length == 1) {
+ Class<?> paramType = paramTypes[0];
+ if (paramType.isPrimitive() && propValue == null) {
+ //Ignoring null value for primitive type, this could potentially be the way of a customer indicating to set
+ //default values defined in JAXBObject/xmlSchema.
+ return;
+ }
+ }
+ }
+
+ }
+
+ /**
+ * Set the property value using the indexed array setter
+ *
+ * @param targetBean
+ * @param propValue
+ * @param writeMethod set(T[])
+ * @throws InvocationTargetException
+ * @throws IllegalAccessException
+ * @throws JAXBWrapperException
+ */
+ private void setIndexedArray(Object targetBean, Object propValue, Method writeMethod)
+ throws InvocationTargetException, IllegalAccessException, JAXBWrapperException {
+
+ Class<?> paramType = writeMethod.getParameterTypes()[0];
+ Object value = asArray(propValue, paramType);
+ // JAXB provides setters for atomic value.
+ Object[] SINGLE_PARAM = new Object[1];
+ SINGLE_PARAM[0] = value;
+
+ writeMethod.invoke(targetBean, SINGLE_PARAM);
+ }
+
+ /**
+ * Set the property value for the collection case.
+ *
+ * @param targetBean
+ * @param propValue
+ * @throws InvocationTargetException
+ * @throws IllegalAccessException
+ * @throws JAXBWrapperException
+ */
+ private void setList(Object targetBean, Object propValue) throws InvocationTargetException, IllegalAccessException,
+ JAXBWrapperException {
+ // For the List<T> case, there is no setter.
+ // You are supposed to use the getter to obtain access to the collection and then add the collection
+
+ Collection value = asCollection(propValue, descriptor.getPropertyType());
+ Collection collection = (Collection)get(targetBean);
+
+ // Now add our our object to the collection
+ collection.clear();
+ if (propValue != null) {
+ collection.addAll(value);
+ }
+ }
+
+ /**
+ * @param propValue
+ * @param destType
+ * @return propValue as a Collection
+ */
+ private static Collection asCollection(Object propValue, Class<?> destType) {
+ // TODO Missing function
+ // Convert the object into an equivalent object that is a collection
+ if (DataConverter.isConvertable(propValue, destType)) {
+ return (Collection)DataConverter.convert(propValue, destType);
+ } else {
+ String objectClass = (propValue == null) ? "null" : propValue.getClass().getName();
+ throw new ServiceRuntimeException("Cannot convert " + objectClass);
+ }
+ }
+
+ /**
+ * @param propValue
+ * @param destType T[]
+ * @return array of component type
+ */
+ private static Object asArray(Object propValue, Class<?> destType) {
+ if (DataConverter.isConvertable(propValue, destType)) {
+ return DataConverter.convert(propValue, destType);
+ } else {
+ String objectClass = (propValue == null) ? "null" : propValue.getClass().getName();
+ throw new ServiceRuntimeException("Cannot convert " + objectClass);
+
+ }
+ }
+
+ public String toString() {
+ String value = "PropertyDescriptorPlus[";
+ value += " name=" + this.getPropertyName();
+ value += " type=" + this.getPropertyType().getName();
+ value += " propertyDecriptor=" + this.descriptor;
+ return value + "]";
+ }
+}
Propchange: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBPropertyDescriptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBPropertyDescriptor.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Copied: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTypeHelper.java (from r660979, incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/JAXBTypeHelper.java)
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTypeHelper.java?p2=incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTypeHelper.java&p1=incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/JAXBTypeHelper.java&r1=660979&r2=661410&rev=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/impl/JAXBTypeHelper.java (original)
+++ incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBTypeHelper.java Thu May 29 10:24:18 2008
@@ -17,7 +17,7 @@
* under the License.
*/
-package org.apache.tuscany.sca.databinding.impl;
+package org.apache.tuscany.sca.databinding.jaxb;
import java.beans.Introspector;
import java.io.IOException;
Added: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperException.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperException.java?rev=661410&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperException.java (added)
+++ incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperException.java Thu May 29 10:24:18 2008
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.databinding.jaxb;
+
+import org.osoa.sca.ServiceRuntimeException;
+
+public class JAXBWrapperException extends ServiceRuntimeException {
+ private static final long serialVersionUID = 1L;
+
+ /**
+ *
+ */
+ public JAXBWrapperException() {
+ super();
+
+ }
+
+ /**
+ * @param message
+ * @param cause
+ */
+ public JAXBWrapperException(String message, Throwable cause) {
+ super(message, cause);
+
+ }
+
+ /** @param message */
+ public JAXBWrapperException(String message) {
+ super(message);
+
+ }
+
+ /** @param cause */
+ public JAXBWrapperException(Throwable cause) {
+ super(cause);
+
+ }
+
+}
Propchange: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperException.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHandler.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHandler.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHandler.java (original)
+++ incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHandler.java Thu May 29 10:24:18 2008
@@ -24,9 +24,11 @@
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
import java.util.List;
-
-import javax.xml.bind.annotation.XmlType;
+import java.util.Map;
import org.apache.tuscany.sca.databinding.TransformationContext;
import org.apache.tuscany.sca.databinding.TransformationException;
@@ -42,6 +44,7 @@
* @version $Rev$ $Date$
*/
public class JAXBWrapperHandler implements WrapperHandler<Object> {
+ private JAXBWrapperHelper helper = new JAXBWrapperHelper();
public Object create(ElementInfo element, final Class<? extends Object> wrapperClass, TransformationContext context) {
try {
@@ -58,21 +61,51 @@
}
}
+ public void setChildren(Object wrapper,
+ List<ElementInfo> childElements,
+ Object[] childObjects,
+ TransformationContext context) {
+ List<String> childNames = new ArrayList<String>();
+ Map<String, Object> values = new HashMap<String, Object>();
+ for (int i = 0; i < childElements.size(); i++) {
+ ElementInfo e = childElements.get(i);
+ String name = e.getQName().getLocalPart();
+ childNames.add(name);
+ values.put(name, childObjects[i]);
+ }
+ // Get the property descriptor map
+ Map<String, JAXBPropertyDescriptor> pdMap = null;
+ try {
+ pdMap = XMLRootElementUtil.createPropertyDescriptorMap(wrapper.getClass());
+ } catch (Throwable t) {
+ throw new JAXBWrapperException(t);
+ }
+ helper.wrap(wrapper, childNames, values, pdMap);
+ }
+
public void setChild(Object wrapper, int i, ElementInfo childElement, Object value) {
Object wrapperValue = wrapper;
Class<?> wrapperClass = wrapperValue.getClass();
- XmlType xmlType = wrapperClass.getAnnotation(XmlType.class);
- String[] properties = xmlType.propOrder();
- String property = properties[i];
-
+ // FIXME: We probably should use the jaxb-reflection to handle the properties
try {
+ String prop = childElement.getQName().getLocalPart();
+ boolean collection = (value instanceof Collection);
+ Method getter = null;
for (Method m : wrapperClass.getMethods()) {
- if (m.getName().equals("set" + capitalize(property))) {
+ Class<?>[] paramTypes = m.getParameterTypes();
+ if (paramTypes.length == 1 && m.getName().equals("set" + capitalize(prop))) {
m.invoke(wrapperValue, new Object[] {value});
return;
}
+ if (collection && paramTypes.length == 0 && m.getName().equals("get" + capitalize(prop))) {
+ getter = m;
+ }
+ }
+ if (getter != null && Collection.class.isAssignableFrom(getter.getReturnType())) {
+ ((Collection)getter.invoke(wrapperValue)).addAll((Collection)value);
}
+
} catch (Throwable e) {
throw new TransformationException(e);
}
@@ -87,22 +120,11 @@
* @see org.apache.tuscany.sca.databinding.WrapperHandler#getChildren(java.lang.Object, List, TransformationContext)
*/
public List getChildren(Object wrapper, List<ElementInfo> childElements, TransformationContext context) {
- Object wrapperValue = wrapper;
- Class<?> wrapperClass = wrapperValue.getClass();
-
- XmlType xmlType = wrapperClass.getAnnotation(XmlType.class);
- String[] properties = xmlType.propOrder();
- List<Object> elements = new ArrayList<Object>();
- for (String p : properties) {
- try {
- Method method = wrapperClass.getMethod("get" + capitalize(p), (Class[])null);
- Object value = method.invoke(wrapperValue, (Object[])null);
- elements.add(value);
- } catch (Throwable e) {
- throw new TransformationException(e);
- }
+ List<String> childNames = new ArrayList<String>();
+ for (ElementInfo e : childElements) {
+ childNames.add(e.getQName().getLocalPart());
}
- return elements;
+ return Arrays.asList(helper.unwrap(wrapper, childNames));
}
/**
Added: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHelper.java?rev=661410&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHelper.java (added)
+++ incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHelper.java Thu May 29 10:24:18 2008
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.databinding.jaxb;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * The JAXBWrapper tool is used to create a JAXB Object from a series of child objects (wrap) or get
+ * the child objects from a JAXB Object (unwrap)
+ */
+public class JAXBWrapperHelper {
+
+
+ /**
+ * unwrap Returns the list of child objects of the jaxb object
+ *
+ * @param jaxbObject that represents the type
+ * @param childNames list of xml child names as String
+ * @param pdMap PropertyDescriptor map for this jaxbObject
+ * @return list of Objects in the same order as the element names.
+ */
+ public Object[] unwrap(Object jaxbObject,
+ List<String> childNames,
+ Map<String, JAXBPropertyDescriptor> pdMap) throws JAXBWrapperException {
+
+
+ // Get the object that will have the property descriptors (i.e. the object representing the complexType)
+ Object jaxbComplexTypeObj = jaxbObject;
+
+
+ // Get the PropertyDescriptorPlus map.
+ // The method makes sure that each child name has a matching jaxb property
+ // checkPropertyDescriptorMap(jaxbComplexTypeObj.getClass(), childNames, pdMap);
+
+ // Get the corresponsing objects from the jaxb bean
+ ArrayList<Object> objList = new ArrayList<Object>();
+ for (String childName : childNames) {
+ JAXBPropertyDescriptor propInfo = pdMap.get(childName);
+ Object object = null;
+ try {
+ object = propInfo.get(jaxbComplexTypeObj);
+ } catch (Throwable e) {
+ throw new JAXBWrapperException(e);
+ }
+ objList.add(object);
+ }
+ Object[] jaxbObjects = objList.toArray();
+ objList = null;
+ return jaxbObjects;
+
+ }
+
+ /**
+ * wrap Creates a jaxb object that is initialized with the child objects.
+ * <p/>
+ * Note that the jaxbClass must be the class the represents the complexType. (It should never be
+ * JAXBElement)
+ *
+ * @param jaxbClass
+ * @param childNames list of xml child names as String
+ * @param childObjects, component type objects
+ * @param pdMap PropertyDescriptor map for this jaxbObject
+ */
+ public Object wrap(Class<?> jaxbClass,
+ List<String> childNames,
+ Map<String, Object> childObjects,
+ Map<String, JAXBPropertyDescriptor> pdMap) throws JAXBWrapperException {
+
+
+ // Just like unWrap, get the property info map
+ // checkPropertyDescriptorMap(jaxbClass, childNames, pdMap);
+
+ // The jaxb object always has a default constructor. Create the object
+ Object jaxbObject = null;
+ try {
+ jaxbObject = jaxbClass.newInstance();
+ } catch (Throwable t) {
+ throw new JAXBWrapperException(t);
+ }
+
+ wrap(jaxbObject, childNames, childObjects, pdMap);
+
+ // Return the jaxb object
+ return jaxbObject;
+ }
+
+ public void wrap(Object jaxbObject,
+ List<String> childNames,
+ Map<String, Object> childObjects,
+ Map<String, JAXBPropertyDescriptor> pdMap) {
+ // Now set each object onto the jaxb object
+ for (String childName : childNames) {
+ JAXBPropertyDescriptor propInfo = pdMap.get(childName);
+ Object value = childObjects.get(childName);
+ try {
+ propInfo.set(jaxbObject, value);
+ } catch (Throwable t) {
+ throw new JAXBWrapperException(t);
+ }
+ }
+ }
+
+
+ public Object[] unwrap(Object jaxbObject, List<String> childNames) throws JAXBWrapperException {
+ // Get the property descriptor map for this JAXBClass
+ Class<?> jaxbClass = jaxbObject.getClass();
+ Map<String, JAXBPropertyDescriptor> pdMap = null;
+ try {
+ pdMap = XMLRootElementUtil.createPropertyDescriptorMap(jaxbClass);
+ } catch (Throwable t) {
+ throw new JAXBWrapperException(t);
+ }
+
+ // Delegate
+ return unwrap(jaxbObject, childNames, pdMap);
+ }
+
+ public Object wrap(Class<?> jaxbClass, List<String> childNames, Map<String, Object> childObjects)
+ throws JAXBWrapperException {
+ // Get the property descriptor map
+ Map<String, JAXBPropertyDescriptor> pdMap = null;
+ try {
+ pdMap = XMLRootElementUtil.createPropertyDescriptorMap(jaxbClass);
+ } catch (Throwable t) {
+ throw new JAXBWrapperException(t);
+ }
+
+ // Delegate
+ return wrap(jaxbClass, childNames, childObjects, pdMap);
+ }
+
+
+}
Propchange: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBWrapperHelper.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/XMLRootElementUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/XMLRootElementUtil.java?rev=661410&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/XMLRootElementUtil.java (added)
+++ incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/XMLRootElementUtil.java Thu May 29 10:24:18 2008
@@ -0,0 +1,282 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.databinding.jaxb;
+
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlElementRef;
+import javax.xml.bind.annotation.XmlEnumValue;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlSchema;
+import javax.xml.namespace.QName;
+
+/**
+ *
+ */
+public class XMLRootElementUtil {
+
+ /** Constructor is intentionally private. This class only provides static utility methods */
+ private XMLRootElementUtil() {
+
+ }
+
+ /**
+ * @param clazz
+ * @return namespace of root element qname or null if this is not object does not represent a
+ * root element
+ */
+ public static QName getXmlRootElementQNameFromObject(Object obj) {
+
+ // A JAXBElement stores its name
+ if (obj instanceof JAXBElement) {
+ return ((JAXBElement<?>)obj).getName();
+ }
+
+ Class<?> clazz = (obj instanceof java.lang.Class) ? (Class<?>)obj : obj.getClass();
+ return getXmlRootElementQName(clazz);
+ }
+
+ /**
+ * @param clazz
+ * @return namespace of root element qname or null if this is not object does not represent a
+ * root element
+ */
+ public static QName getXmlRootElementQName(Class<?> clazz) {
+
+ // See if the object represents a root element
+ XmlRootElement root = (XmlRootElement)getAnnotation(clazz, XmlRootElement.class);
+ if (root == null) {
+ return null;
+ }
+
+ String name = root.name();
+ String namespace = root.namespace();
+
+ // The name may need to be defaulted
+ if (name == null || name.length() == 0 || name.equals("##default")) {
+ name = getSimpleName(clazz.getCanonicalName());
+ }
+
+ // The namespace may need to be defaulted
+ if (namespace == null || namespace.length() == 0 || namespace.equals("##default")) {
+ Package pkg = clazz.getPackage();
+ XmlSchema schema = (XmlSchema)getAnnotation(pkg, XmlSchema.class);
+ if (schema != null) {
+ namespace = schema.namespace();
+ } else {
+ namespace = "";
+ }
+ }
+
+ return new QName(namespace, name);
+ }
+
+ /**
+ * @param clazz
+ * @return namespace of root element qname or null if this is not object does not represent a root element
+ */
+ public static String getEnumValue(Enum<?> myEnum) {
+ Field f;
+ String value;
+ try {
+ f = myEnum.getClass().getField(myEnum.name());
+
+ f.setAccessible(true);
+
+ XmlEnumValue xev = (XmlEnumValue)getAnnotation(f, XmlEnumValue.class);
+ if (xev == null) {
+ value = f.getName();
+ } else {
+ value = xev.value();
+ }
+ } catch (SecurityException e) {
+ value = null;
+ } catch (NoSuchFieldException e) {
+ value = null;
+ }
+
+ return value;
+ }
+
+ /**
+ * utility method to get the last token in a "."-delimited package+classname string
+ *
+ * @return
+ */
+ private static String getSimpleName(String in) {
+ if (in == null || in.length() == 0) {
+ return in;
+ }
+ String out = null;
+ StringTokenizer tokenizer = new StringTokenizer(in, ".");
+ if (tokenizer.countTokens() == 0)
+ out = in;
+ else {
+ while (tokenizer.hasMoreTokens()) {
+ out = tokenizer.nextToken();
+ }
+ }
+ return out;
+ }
+
+ /**
+ * The JAXBClass has a set of bean properties each represented by a PropertyDescriptor Each of
+ * the fields of the class has an associated xml name. The method returns a map where the key is
+ * the xml name and value is the PropertyDescriptor
+ *
+ * @param jaxbClass
+ * @return map
+ */
+ public static Map<String, JAXBPropertyDescriptor> createPropertyDescriptorMap(Class<?> jaxbClass)
+ throws NoSuchFieldException, IntrospectionException {
+
+ PropertyDescriptor[] pds = Introspector.getBeanInfo(jaxbClass).getPropertyDescriptors();
+ Map<String, JAXBPropertyDescriptor> map = new HashMap<String, JAXBPropertyDescriptor>();
+
+ // Unfortunately the element names are stored on the fields.
+ // Get all of the fields in the class and super classes
+
+ List<Field> fields = getFields(jaxbClass);
+
+ // Now match up the fields with the property descriptors...Sigh why didn't JAXB put the @XMLElement annotations on the
+ // property methods!
+ for (PropertyDescriptor pd : pds) {
+
+ // Skip over the class property..it is never represented as an xml element
+ if (pd.getName().equals("class")) {
+ continue;
+ }
+
+ // For the current property, find a matching field...so that we can get the xml name
+ boolean found = false;
+
+ for (Field field : fields) {
+ String fieldName = field.getName();
+
+ // Use the name of the field and property to find the match
+ if (fieldName.equalsIgnoreCase(pd.getDisplayName()) || fieldName.equalsIgnoreCase(pd.getName())) {
+ // Get the xmlElement name for this field
+ QName xmlName = getXmlElementRefOrElementQName(field.getDeclaringClass(), field);
+ found = true;
+ map.put(xmlName.getLocalPart(), new JAXBPropertyDescriptor(pd, xmlName));
+ break;
+ }
+
+ // Unfortunately, sometimes the field name is preceeded by an underscore
+ if (fieldName.startsWith("_")) {
+ fieldName = fieldName.substring(1);
+ if (fieldName.equalsIgnoreCase(pd.getDisplayName()) || fieldName.equalsIgnoreCase(pd.getName())) {
+ // Get the xmlElement name for this field
+ QName xmlName = getXmlElementRefOrElementQName(field.getDeclaringClass(), field);
+ found = true;
+
+ map.put(xmlName.getLocalPart(), new JAXBPropertyDescriptor(pd, xmlName));
+ break;
+ }
+ }
+ }
+
+ // We didn't find a field. Default the xmlname to the property name
+ if (!found) {
+ String xmlName = pd.getName();
+
+ map.put(xmlName, new JAXBPropertyDescriptor(pd, xmlName));
+ }
+
+ }
+ return map;
+ }
+
+ /**
+ * Gets all of the fields in this class and the super classes
+ *
+ * @param beanClass
+ * @return
+ */
+ static private List<Field> getFields(final Class<?> beanClass) {
+ // This class must remain private due to Java 2 Security concerns
+ List<Field> fields = AccessController.doPrivileged(new PrivilegedAction<List<Field>>() {
+ public List<Field> run() {
+ List<Field> fields = new ArrayList<Field>();
+ Class<?> cls = beanClass;
+ while (cls != null) {
+ Field[] fieldArray = cls.getDeclaredFields();
+ for (Field field : fieldArray) {
+ fields.add(field);
+ }
+ cls = cls.getSuperclass();
+ }
+ return fields;
+ }
+ });
+
+ return fields;
+ }
+
+ /**
+ * Get the name of the field by looking at the XmlElement annotation.
+ *
+ * @param jaxbClass
+ * @param fieldName
+ * @return
+ * @throws NoSuchFieldException
+ */
+ private static QName getXmlElementRefOrElementQName(Class<?> jaxbClass, Field field) throws NoSuchFieldException {
+ XmlElementRef xmlElementRef = (XmlElementRef)getAnnotation(field, XmlElementRef.class);
+ if (xmlElementRef != null) {
+ return new QName(xmlElementRef.namespace(), xmlElementRef.name());
+ }
+ XmlElement xmlElement = (XmlElement)getAnnotation(field, XmlElement.class);
+
+ // If XmlElement does not exist, default to using the field name
+ if (xmlElement == null || xmlElement.name().equals("##default")) {
+ return new QName("", field.getName());
+ }
+ return new QName(xmlElement.namespace(), xmlElement.name());
+ }
+
+ /**
+ * Get an annotation. This is wrappered to avoid a Java2Security violation.
+ * @param cls Class that contains annotation
+ * @param annotation Class of requrested Annotation
+ * @return annotation or null
+ */
+ private static <T extends Annotation> T getAnnotation(final AnnotatedElement element, final Class<T> annotation) {
+ return AccessController.doPrivileged(new PrivilegedAction<T>() {
+ public T run() {
+ return element.getAnnotation(annotation);
+ }
+ });
+ }
+}
Propchange: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/XMLRootElementUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/XMLRootElementUtil.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/tuscany/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/JAXBReflectionTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/JAXBReflectionTestCase.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/JAXBReflectionTestCase.java (original)
+++ incubator/tuscany/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/JAXBReflectionTestCase.java Thu May 29 10:24:18 2008
@@ -34,14 +34,16 @@
import org.jvnet.jaxb.reflection.model.core.Ref;
import org.jvnet.jaxb.reflection.model.impl.RuntimeModelBuilder;
import org.jvnet.jaxb.reflection.model.runtime.RuntimeClassInfo;
+import org.jvnet.jaxb.reflection.model.runtime.RuntimePropertyInfo;
import org.jvnet.jaxb.reflection.model.runtime.RuntimeTypeInfoSet;
import org.jvnet.jaxb.reflection.runtime.IllegalAnnotationsException;
import org.jvnet.jaxb.reflection.runtime.JAXBContextImpl;
import org.w3c.dom.Node;
+import com.example.ipo.jaxb.ObjectFactory;
import com.example.ipo.jaxb.PurchaseOrderType;
import com.example.ipo.jaxb.USAddress;
-
+import com.example.ipo.jaxb.USState;
/**
* @version $Rev$ $Date$
@@ -53,7 +55,7 @@
Node schema = JAXBContextHelper.generateSchema(context);
System.out.println(new Node2String().transform(schema, null));
}
-
+
/**
* This is a workaround for the NPE bug in jaxb-reflection
* @param classes
@@ -79,9 +81,14 @@
public void testReflection() throws Exception {
org.jvnet.jaxb.reflection.model.runtime.RuntimeTypeInfoSet model = create(PurchaseOrderType.class);
- RuntimeClassInfo type = (RuntimeClassInfo) model.getTypeInfo(PurchaseOrderType.class);
+ RuntimeClassInfo type = (RuntimeClassInfo)model.getTypeInfo(PurchaseOrderType.class);
Assert.assertEquals(new QName("http://www.example.com/IPO", "PurchaseOrderType"), type.getTypeName());
- type = (RuntimeClassInfo) model.getTypeInfo(USAddress.class);
+ type = (RuntimeClassInfo)model.getTypeInfo(USAddress.class);
Assert.assertEquals(new QName("http://www.example.com/IPO", "USAddress"), type.getTypeName());
+ RuntimePropertyInfo prop = type.getProperty("state");
+ Assert.assertNotNull(prop);
+ USAddress address = new ObjectFactory().createUSAddress();
+ prop.getAccessor().set(address, USState.CA);
+ Assert.assertEquals(USState.CA, address.getState());
}
}
Modified: incubator/tuscany/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/JAXBWrapperHandlerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/JAXBWrapperHandlerTestCase.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/JAXBWrapperHandlerTestCase.java (original)
+++ incubator/tuscany/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/databinding/jaxb/JAXBWrapperHandlerTestCase.java Thu May 29 10:24:18 2008
@@ -19,6 +19,7 @@
package org.apache.tuscany.databinding.jaxb;
+import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
@@ -38,6 +39,7 @@
*/
public class JAXBWrapperHandlerTestCase extends TestCase {
private static final QName ELEMENT = new QName("http://www.example.com/stock", "stockQuoteOffer");
+ private static final QName INPUT = new QName("", "input");
private JAXBWrapperHandler handler;
/**
@@ -57,14 +59,16 @@
public void testSetChild() {
StockQuoteOffer wrapper = new StockQuoteOffer();
- handler.setChild(wrapper, 0, null, "IBM");
+ handler.setChild(wrapper, 0, new ElementInfo(INPUT, null), "IBM");
Assert.assertEquals("IBM", wrapper.getInput());
}
public void testGetChildren() {
StockQuoteOffer wrapper = new StockQuoteOffer();
wrapper.setInput("IBM");
- List children = handler.getChildren(wrapper, null, null);
+ List<ElementInfo> elements = new ArrayList<ElementInfo>();
+ elements.add(new ElementInfo(INPUT, null));
+ List children = handler.getChildren(wrapper, elements, null);
assertNotNull(children);
assertEquals(1, children.size());
assertEquals("IBM", children.get(0));
Modified: incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOContextHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOContextHelper.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOContextHelper.java (original)
+++ incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOContextHelper.java Thu May 29 10:24:18 2008
@@ -30,6 +30,7 @@
import org.apache.tuscany.sca.databinding.TransformationException;
import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.interfacedef.util.WrapperInfo;
import org.apache.tuscany.sca.interfacedef.util.XMLType;
import org.apache.tuscany.sdo.api.SDOUtil;
@@ -63,6 +64,11 @@
if (op != null) {
found = register(helperContext, op.getInputType()) || found;
found = register(helperContext, op.getOutputType()) || found;
+ WrapperInfo wrapper = op.getWrapper();
+ if (wrapper != null) {
+ found = register(helperContext, wrapper.getInputWrapperClass()) || found;
+ found = register(helperContext, wrapper.getOutputWrapperClass()) || found;
+ }
} else {
found = register(helperContext, context.getSourceDataType()) || found;
}
@@ -71,6 +77,11 @@
if (op != null) {
found = register(helperContext, op.getInputType()) || found;
found = register(helperContext, op.getOutputType()) || found;
+ WrapperInfo wrapper = op.getWrapper();
+ if (wrapper != null) {
+ found = register(helperContext, wrapper.getInputWrapperClass()) || found;
+ found = register(helperContext, wrapper.getOutputWrapperClass()) || found;
+ }
} else {
found = register(helperContext, context.getTargetDataType()) || found;
}
@@ -123,6 +134,9 @@
*/
private static boolean register(HelperContext helperContext, Class javaType) {
+ if (javaType == null) {
+ return false;
+ }
try {
Type type = helperContext.getTypeHelper().getType(javaType);
if (type != null && (!type.isDataType())) {
Modified: incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDODataBinding.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDODataBinding.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDODataBinding.java (original)
+++ incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDODataBinding.java Thu May 29 10:24:18 2008
@@ -27,6 +27,7 @@
import org.apache.tuscany.sca.databinding.SimpleTypeMapper;
import org.apache.tuscany.sca.databinding.WrapperHandler;
+import org.apache.tuscany.sca.databinding.XMLTypeHelper;
import org.apache.tuscany.sca.databinding.impl.BaseDataBinding;
import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.util.XMLType;
@@ -51,10 +52,12 @@
public static final QName ROOT_ELEMENT = new QName(ROOT_NAMESPACE, "dataObject");
private WrapperHandler<Object> wrapperHandler;
+ private XMLTypeHelper xmlTypeHelper;
public SDODataBinding() {
super(NAME, ALIASES, DataObject.class);
wrapperHandler = new SDOWrapperHandler();
+ xmlTypeHelper = new SDOTypeHelper();
}
@Override
@@ -109,8 +112,8 @@
}
@Override
- public Class getXMLTypeHelperClass() {
- return SDOTypeHelper.class;
+ public XMLTypeHelper getXMLTypeHelper() {
+ return xmlTypeHelper;
}
@Override
Modified: incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOWrapperHandler.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOWrapperHandler.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOWrapperHandler.java (original)
+++ incubator/tuscany/java/sca/modules/databinding-sdo/src/main/java/org/apache/tuscany/sca/databinding/sdo/SDOWrapperHandler.java Thu May 29 10:24:18 2008
@@ -63,6 +63,16 @@
return null;
}
+ public void setChildren(Object wrapper,
+ List<ElementInfo> childElements,
+ Object[] childObjects,
+ TransformationContext context) {
+ for (int i = 0; i < childElements.size(); i++) {
+ setChild(wrapper, i, childElements.get(i), childObjects[i]);
+ }
+
+ }
+
/**
* @see org.apache.tuscany.sca.databinding.WrapperHandler#setChild(java.lang.Object, int, ElementInfo,
* java.lang.Object)
@@ -70,7 +80,13 @@
public void setChild(Object wrapper, int i, ElementInfo childElement, Object value) {
DataObject wrapperDO =
(wrapper instanceof XMLDocument) ? ((XMLDocument)wrapper).getRootObject() : (DataObject)wrapper;
- wrapperDO.set(i, value);
+ String name = childElement.getQName().getLocalPart();
+ if (childElement.isMany()) {
+ // FIXME: If we look up by name, we need to make sure the WrapperInfo has the correct element names
+ wrapperDO.getList(i).addAll((Collection)value);
+ } else {
+ wrapperDO.set(i, value);
+ }
}
@SuppressWarnings("unchecked")
@@ -107,7 +123,9 @@
/**
* @see org.apache.tuscany.sca.databinding.WrapperHandler#getWrapperType(org.apache.tuscany.sca.interfacedef.util.ElementInfo, Class, org.apache.tuscany.sca.databinding.TransformationContext)
*/
- public DataType getWrapperType(ElementInfo element, Class<? extends Object> wrapperClass, TransformationContext context) {
+ public DataType getWrapperType(ElementInfo element,
+ Class<? extends Object> wrapperClass,
+ TransformationContext context) {
HelperContext helperContext = SDOContextHelper.getHelperContext(context);
Type sdoType = getSDOType(helperContext, element);
if (sdoType != null) {
Modified: incubator/tuscany/java/sca/modules/databinding/pom.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding/pom.xml?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding/pom.xml (original)
+++ incubator/tuscany/java/sca/modules/databinding/pom.xml Thu May 29 10:24:18 2008
@@ -65,12 +65,6 @@
<version>1.0.1</version>
</dependency>
- <dependency>
- <groupId>javax.xml.bind</groupId>
- <artifactId>jaxb-api</artifactId>
- <version>2.1</version>
- </dependency>
-
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>wstx-asl</artifactId>
Modified: incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DataBinding.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DataBinding.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DataBinding.java (original)
+++ incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DataBinding.java Thu May 29 10:24:18 2008
@@ -96,6 +96,6 @@
* Get the XML type helper for Java types
* @return The databinding-specific XML type helper class
*/
- Class getXMLTypeHelperClass();
+ XMLTypeHelper getXMLTypeHelper();
}
Modified: incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DefaultDataBindingExtensionPoint.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DefaultDataBindingExtensionPoint.java?rev=661410&r1=661409&r2=661410&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DefaultDataBindingExtensionPoint.java (original)
+++ incubator/tuscany/java/sca/modules/databinding/src/main/java/org/apache/tuscany/sca/databinding/DefaultDataBindingExtensionPoint.java Thu May 29 10:24:18 2008
@@ -183,8 +183,8 @@
return getDataBinding().getSimpleTypeMapper();
}
- public Class getXMLTypeHelperClass() {
- return getDataBinding().getXMLTypeHelperClass();
+ public XMLTypeHelper getXMLTypeHelper() {
+ return getDataBinding().getXMLTypeHelper();
}
public WrapperHandler getWrapperHandler() {