From 71961dd90a5eca97bf70f5ffcc0d9d713f616040 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Wed, 2 Jul 2008 23:30:42 +0000 Subject: Moved test classes to test/src and fixed scope of dependency on jaxb2-reflection.. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@673547 13f79535-47bb-0310-9956-ffa450edef68 --- java/sca/modules/databinding-jaxb/pom.xml | 2 +- .../databinding/jaxb/BeanXMLStreamReaderImpl.java | 320 --------------------- .../sca/databinding/jaxb/JAXB2XMLStreamReader.java | 84 ------ .../databinding/jaxb/BeanXMLStreamReaderImpl.java | 320 +++++++++++++++++++++ .../sca/databinding/jaxb/JAXB2XMLStreamReader.java | 84 ++++++ 5 files changed, 405 insertions(+), 405 deletions(-) delete mode 100644 java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java delete mode 100644 java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java create mode 100644 java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java create mode 100644 java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java (limited to 'java/sca/modules/databinding-jaxb') diff --git a/java/sca/modules/databinding-jaxb/pom.xml b/java/sca/modules/databinding-jaxb/pom.xml index 8392acbd24..8b819e06d3 100644 --- a/java/sca/modules/databinding-jaxb/pom.xml +++ b/java/sca/modules/databinding-jaxb/pom.xml @@ -63,7 +63,7 @@ org.jvnet.jaxb.reflection jaxb2-reflection 2.1.4 - compile + test diff --git a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java deleted file mode 100644 index 0fde2ee9a5..0000000000 --- a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java +++ /dev/null @@ -1,320 +0,0 @@ -/* - * 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.lang.reflect.Array; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import javax.xml.namespace.QName; - -import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; -import org.apache.tuscany.sca.databinding.xml.SimpleXmlNodeImpl; -import org.apache.tuscany.sca.databinding.xml.XmlNode; -import org.apache.tuscany.sca.databinding.xml.XmlTreeStreamReaderImpl; -import org.apache.tuscany.sca.interfacedef.util.TypeInfo; -import org.jvnet.jaxb.reflection.model.annotation.RuntimeInlineAnnotationReader; -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; - - - -/** - * @version $Rev$ $Date$ - */ -public class BeanXMLStreamReaderImpl extends XmlTreeStreamReaderImpl { - private static final Comparator COMPARATOR = new Comparator() { - public int compare(Accessor o1, Accessor o2) { - return o1.getName().compareTo(o2.getName()); - } - }; - - private static final String XSI_PREFIX = "xsi"; - private static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"; - - private static XmlNode getXSIType(QName realType) { - QName xsiType = new QName(XSI_NS, "type", XSI_PREFIX); - String prefix = realType.getPrefix(); - String typeName = realType.getLocalPart(); - if (prefix != null && !prefix.equals("")) { - typeName = prefix + ":" + realType.getLocalPart(); - } - return new SimpleXmlNodeImpl(xsiType, XmlNode.Type.ATTRIBUTE); - } - - /** - * Represent a Map.Entry XML node - * @version $Rev$ $Date$ - */ - public static class MapEntryXmlNodeImpl extends SimpleXmlNodeImpl implements XmlNode { - private Map.Entry entry; - - public MapEntryXmlNodeImpl(Entry entry) { - super(new QName("", "entry"), entry); - this.entry = entry; - } - - public Iterator children() { - List nodes = new ArrayList(); - XmlNode key = new BeanXmlNodeImpl(new QName("", "key"), entry.getKey()); - XmlNode value = new BeanXmlNodeImpl(new QName("", "value"), entry.getValue()); - nodes.add(key); - nodes.add(value); - return nodes.iterator(); - } - } - - public static class BeanXmlNodeImpl extends SimpleXmlNodeImpl implements XmlNode { - private static final Object[] NULL = null; - private static final SimpleTypeMapperImpl MAPPER = new SimpleTypeMapperImpl(); - - public BeanXmlNodeImpl(Object bean) { - super(getName(bean == null ? null : bean.getClass()), bean); - } - - public BeanXmlNodeImpl(QName name, Object bean) { - super(name, bean); - } - - private static boolean isSimpleType(Class javaType) { - return SimpleTypeMapperImpl.getXMLType(javaType) != null; - } - - private static String getStringValue(Object o) { - if (o == null) { - return null; - } - TypeInfo info = SimpleTypeMapperImpl.getXMLType(o.getClass()); - if (info != null) { - return MAPPER.toXMLLiteral(info.getQName(), o, null); - } else { - return String.valueOf(o); - } - } - - public Iterator children() { - if (name == null) { - return null; - } - if (value == null) { - return super.children(); - } - if (isSimpleType(value.getClass())) { - XmlNode textNode = new BeanXmlNodeImpl(null, value); - return Arrays.asList(textNode).iterator(); - } - if (Map.class.isAssignableFrom(value.getClass())) { - List entries = new ArrayList(); - Map map = (Map)value; - if (map != null) { - for (Object e : map.entrySet()) { - Map.Entry entry = (Map.Entry)e; - entries.add(new MapEntryXmlNodeImpl(entry)); - } - } - return entries.iterator(); - } - try { - Map accessorMap = getAccessors(value); - List accessorList = new ArrayList(accessorMap.values()); - Collections.sort(accessorList, COMPARATOR); - - List props = new ArrayList(); - for (Accessor accessor : accessorList) { - Class pType = accessor.getType(); - - QName pName = new QName(name.getNamespaceURI(), accessor.getName()); - Object pValue = accessor.getValue(); - if (pType.isArray()) { - if (pValue != null) { - int i1 = Array.getLength(pValue); - for (int j = 0; j < i1; j++) { - Object o = Array.get(pValue, j); - props.add(new BeanXmlNodeImpl(pName, o)); - } - } else { - // TODO: How to handle null? - } - } else if (Collection.class.isAssignableFrom(pType)) { - Collection objList = (Collection)pValue; - if (objList != null && objList.size() > 0) { - for (Iterator j = objList.iterator(); j.hasNext();) { - Object o = j.next(); - props.add(new BeanXmlNodeImpl(pName, o)); - } - - } else { - // How to handle null - } - } else { - props.add(new BeanXmlNodeImpl(pName, pValue)); - } - } - return props.iterator(); - } catch (Exception e) { - throw new IllegalArgumentException(e); - } - } - - public QName getName() { - return name; - } - - public String getValue() { - return getStringValue(value); - } - - private static String getPackageName(Class cls) { - String name = cls.getName(); - int index = name.lastIndexOf('.'); - return index == -1 ? "" : name.substring(0, index); - } - - public static QName getName(Class cls) { - if (cls == null) { - return null; - } - - String packageName = getPackageName(cls); - - if ("".equals(packageName)) { - return new QName("", cls.getSimpleName()); - } - StringBuffer ns = new StringBuffer("http://"); - String[] names = packageName.split("\\."); - for (int i = names.length - 1; i >= 0; i--) { - ns.append(names[i]); - if (i != 0) { - ns.append('.'); - } - } - ns.append('/'); - return new QName(ns.toString(), cls.getSimpleName()); - } - } - - public BeanXMLStreamReaderImpl(QName name, Object bean) { - super(getXmlNode(name, bean)); - } - - private static BeanXmlNodeImpl getXmlNode(QName name, Object bean) { - BeanXmlNodeImpl root = null; - if (name != null) { - root = new BeanXmlNodeImpl(name, bean); - } else { - root = new BeanXmlNodeImpl(bean); - } - return root; - } - - public static interface Accessor { - String getName(); - - Class getType(); - - Object getValue() throws Exception; - - void setValue(Object value) throws Exception; - } - - private static RuntimeTypeInfoSet create(Class... classes) throws Exception { - IllegalAnnotationsException.Builder errorListener = new IllegalAnnotationsException.Builder(); - RuntimeInlineAnnotationReader reader = new RuntimeInlineAnnotationReader(); - JAXBContextImpl context = - new JAXBContextImpl(classes, null, Collections. emptyMap(), null, false, reader, false, false); - RuntimeModelBuilder builder = - new RuntimeModelBuilder(context, reader, Collections. emptyMap(), null); - builder.setErrorHandler(errorListener); - for (Class c : classes) - builder.getTypeInfo(new Ref(c)); - - RuntimeTypeInfoSet r = builder.link(); - errorListener.check(); - return r; - } - - private static class JAXBAccessor implements Accessor { - private Object target; - private RuntimePropertyInfo prop; - - public JAXBAccessor(Object target, RuntimePropertyInfo field) { - super(); - this.target = target; - this.prop = field; - } - - public String getName() { - return prop.getName(); - } - - public Object getValue() throws Exception { - return prop.getAccessor().get(target); - } - - public void setValue(Object value) throws Exception { - prop.getAccessor().set(target, value); - } - - public Class getType() { - Type type = prop.getRawType(); - if (type instanceof Class) { - return (Class)type; - } else if (type instanceof ParameterizedType) { - ParameterizedType pType = (ParameterizedType)type; - type = pType.getRawType(); - if (type instanceof Class) { - return (Class)type; - } - } - return Object.class; - } - - } - - private static Map getAccessors(Object target) throws Exception { - if (target == null) { - return Collections.emptyMap(); - } - Map map = new HashMap(); - Class type = target.getClass(); - RuntimeTypeInfoSet set = create(type); - RuntimeClassInfo clsInfo = (RuntimeClassInfo)set.getTypeInfo(type); - for (RuntimePropertyInfo f : clsInfo.getProperties()) { - map.put(f.getName(), new JAXBAccessor(target, f)); - } - return map; - } - -} diff --git a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java deleted file mode 100644 index e2151e23a1..0000000000 --- a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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 javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBElement; -import javax.xml.namespace.QName; -import javax.xml.stream.XMLStreamReader; - -import org.apache.tuscany.sca.databinding.PullTransformer; -import org.apache.tuscany.sca.databinding.TransformationContext; -import org.apache.tuscany.sca.databinding.TransformationException; -import org.apache.tuscany.sca.databinding.impl.BaseTransformer; - -/** - * - * @version $Rev$ $Date$ - */ -public class JAXB2XMLStreamReader extends BaseTransformer implements - PullTransformer { - - public JAXB2XMLStreamReader() { - super(); - } - - public XMLStreamReader transform(Object source, TransformationContext context) { - if (source == null) { - return null; - } - try { - QName name = null; - Object bean = null; - if (source instanceof JAXBElement) { - bean = ((JAXBElement)source).getValue(); - name = ((JAXBElement)source).getName(); - } else { - JAXBContext jaxbContext = JAXBContextHelper.createJAXBContext(context, true); - Object element = JAXBContextHelper.createJAXBElement(jaxbContext, context.getSourceDataType(), source); - name = jaxbContext.createJAXBIntrospector().getElementName(element); - bean = source; - } - BeanXMLStreamReaderImpl reader = new BeanXMLStreamReaderImpl(name, bean); - return reader; - } catch (Exception e) { - throw new TransformationException(e); - } - } - - @Override - protected Class getSourceType() { - return Object.class; - } - - @Override - protected Class getTargetType() { - return XMLStreamReader.class; - } - - @Override - public int getWeight() { - return 10; - } - - @Override - public String getTargetDataBinding() { - return JAXBDataBinding.NAME; - } -} diff --git a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java new file mode 100644 index 0000000000..0fde2ee9a5 --- /dev/null +++ b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/BeanXMLStreamReaderImpl.java @@ -0,0 +1,320 @@ +/* + * 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.lang.reflect.Array; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl; +import org.apache.tuscany.sca.databinding.xml.SimpleXmlNodeImpl; +import org.apache.tuscany.sca.databinding.xml.XmlNode; +import org.apache.tuscany.sca.databinding.xml.XmlTreeStreamReaderImpl; +import org.apache.tuscany.sca.interfacedef.util.TypeInfo; +import org.jvnet.jaxb.reflection.model.annotation.RuntimeInlineAnnotationReader; +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; + + + +/** + * @version $Rev$ $Date$ + */ +public class BeanXMLStreamReaderImpl extends XmlTreeStreamReaderImpl { + private static final Comparator COMPARATOR = new Comparator() { + public int compare(Accessor o1, Accessor o2) { + return o1.getName().compareTo(o2.getName()); + } + }; + + private static final String XSI_PREFIX = "xsi"; + private static final String XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"; + + private static XmlNode getXSIType(QName realType) { + QName xsiType = new QName(XSI_NS, "type", XSI_PREFIX); + String prefix = realType.getPrefix(); + String typeName = realType.getLocalPart(); + if (prefix != null && !prefix.equals("")) { + typeName = prefix + ":" + realType.getLocalPart(); + } + return new SimpleXmlNodeImpl(xsiType, XmlNode.Type.ATTRIBUTE); + } + + /** + * Represent a Map.Entry XML node + * @version $Rev$ $Date$ + */ + public static class MapEntryXmlNodeImpl extends SimpleXmlNodeImpl implements XmlNode { + private Map.Entry entry; + + public MapEntryXmlNodeImpl(Entry entry) { + super(new QName("", "entry"), entry); + this.entry = entry; + } + + public Iterator children() { + List nodes = new ArrayList(); + XmlNode key = new BeanXmlNodeImpl(new QName("", "key"), entry.getKey()); + XmlNode value = new BeanXmlNodeImpl(new QName("", "value"), entry.getValue()); + nodes.add(key); + nodes.add(value); + return nodes.iterator(); + } + } + + public static class BeanXmlNodeImpl extends SimpleXmlNodeImpl implements XmlNode { + private static final Object[] NULL = null; + private static final SimpleTypeMapperImpl MAPPER = new SimpleTypeMapperImpl(); + + public BeanXmlNodeImpl(Object bean) { + super(getName(bean == null ? null : bean.getClass()), bean); + } + + public BeanXmlNodeImpl(QName name, Object bean) { + super(name, bean); + } + + private static boolean isSimpleType(Class javaType) { + return SimpleTypeMapperImpl.getXMLType(javaType) != null; + } + + private static String getStringValue(Object o) { + if (o == null) { + return null; + } + TypeInfo info = SimpleTypeMapperImpl.getXMLType(o.getClass()); + if (info != null) { + return MAPPER.toXMLLiteral(info.getQName(), o, null); + } else { + return String.valueOf(o); + } + } + + public Iterator children() { + if (name == null) { + return null; + } + if (value == null) { + return super.children(); + } + if (isSimpleType(value.getClass())) { + XmlNode textNode = new BeanXmlNodeImpl(null, value); + return Arrays.asList(textNode).iterator(); + } + if (Map.class.isAssignableFrom(value.getClass())) { + List entries = new ArrayList(); + Map map = (Map)value; + if (map != null) { + for (Object e : map.entrySet()) { + Map.Entry entry = (Map.Entry)e; + entries.add(new MapEntryXmlNodeImpl(entry)); + } + } + return entries.iterator(); + } + try { + Map accessorMap = getAccessors(value); + List accessorList = new ArrayList(accessorMap.values()); + Collections.sort(accessorList, COMPARATOR); + + List props = new ArrayList(); + for (Accessor accessor : accessorList) { + Class pType = accessor.getType(); + + QName pName = new QName(name.getNamespaceURI(), accessor.getName()); + Object pValue = accessor.getValue(); + if (pType.isArray()) { + if (pValue != null) { + int i1 = Array.getLength(pValue); + for (int j = 0; j < i1; j++) { + Object o = Array.get(pValue, j); + props.add(new BeanXmlNodeImpl(pName, o)); + } + } else { + // TODO: How to handle null? + } + } else if (Collection.class.isAssignableFrom(pType)) { + Collection objList = (Collection)pValue; + if (objList != null && objList.size() > 0) { + for (Iterator j = objList.iterator(); j.hasNext();) { + Object o = j.next(); + props.add(new BeanXmlNodeImpl(pName, o)); + } + + } else { + // How to handle null + } + } else { + props.add(new BeanXmlNodeImpl(pName, pValue)); + } + } + return props.iterator(); + } catch (Exception e) { + throw new IllegalArgumentException(e); + } + } + + public QName getName() { + return name; + } + + public String getValue() { + return getStringValue(value); + } + + private static String getPackageName(Class cls) { + String name = cls.getName(); + int index = name.lastIndexOf('.'); + return index == -1 ? "" : name.substring(0, index); + } + + public static QName getName(Class cls) { + if (cls == null) { + return null; + } + + String packageName = getPackageName(cls); + + if ("".equals(packageName)) { + return new QName("", cls.getSimpleName()); + } + StringBuffer ns = new StringBuffer("http://"); + String[] names = packageName.split("\\."); + for (int i = names.length - 1; i >= 0; i--) { + ns.append(names[i]); + if (i != 0) { + ns.append('.'); + } + } + ns.append('/'); + return new QName(ns.toString(), cls.getSimpleName()); + } + } + + public BeanXMLStreamReaderImpl(QName name, Object bean) { + super(getXmlNode(name, bean)); + } + + private static BeanXmlNodeImpl getXmlNode(QName name, Object bean) { + BeanXmlNodeImpl root = null; + if (name != null) { + root = new BeanXmlNodeImpl(name, bean); + } else { + root = new BeanXmlNodeImpl(bean); + } + return root; + } + + public static interface Accessor { + String getName(); + + Class getType(); + + Object getValue() throws Exception; + + void setValue(Object value) throws Exception; + } + + private static RuntimeTypeInfoSet create(Class... classes) throws Exception { + IllegalAnnotationsException.Builder errorListener = new IllegalAnnotationsException.Builder(); + RuntimeInlineAnnotationReader reader = new RuntimeInlineAnnotationReader(); + JAXBContextImpl context = + new JAXBContextImpl(classes, null, Collections. emptyMap(), null, false, reader, false, false); + RuntimeModelBuilder builder = + new RuntimeModelBuilder(context, reader, Collections. emptyMap(), null); + builder.setErrorHandler(errorListener); + for (Class c : classes) + builder.getTypeInfo(new Ref(c)); + + RuntimeTypeInfoSet r = builder.link(); + errorListener.check(); + return r; + } + + private static class JAXBAccessor implements Accessor { + private Object target; + private RuntimePropertyInfo prop; + + public JAXBAccessor(Object target, RuntimePropertyInfo field) { + super(); + this.target = target; + this.prop = field; + } + + public String getName() { + return prop.getName(); + } + + public Object getValue() throws Exception { + return prop.getAccessor().get(target); + } + + public void setValue(Object value) throws Exception { + prop.getAccessor().set(target, value); + } + + public Class getType() { + Type type = prop.getRawType(); + if (type instanceof Class) { + return (Class)type; + } else if (type instanceof ParameterizedType) { + ParameterizedType pType = (ParameterizedType)type; + type = pType.getRawType(); + if (type instanceof Class) { + return (Class)type; + } + } + return Object.class; + } + + } + + private static Map getAccessors(Object target) throws Exception { + if (target == null) { + return Collections.emptyMap(); + } + Map map = new HashMap(); + Class type = target.getClass(); + RuntimeTypeInfoSet set = create(type); + RuntimeClassInfo clsInfo = (RuntimeClassInfo)set.getTypeInfo(type); + for (RuntimePropertyInfo f : clsInfo.getProperties()) { + map.put(f.getName(), new JAXBAccessor(target, f)); + } + return map; + } + +} diff --git a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java new file mode 100644 index 0000000000..e2151e23a1 --- /dev/null +++ b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2XMLStreamReader.java @@ -0,0 +1,84 @@ +/* + * 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 javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; +import org.apache.tuscany.sca.databinding.impl.BaseTransformer; + +/** + * + * @version $Rev$ $Date$ + */ +public class JAXB2XMLStreamReader extends BaseTransformer implements + PullTransformer { + + public JAXB2XMLStreamReader() { + super(); + } + + public XMLStreamReader transform(Object source, TransformationContext context) { + if (source == null) { + return null; + } + try { + QName name = null; + Object bean = null; + if (source instanceof JAXBElement) { + bean = ((JAXBElement)source).getValue(); + name = ((JAXBElement)source).getName(); + } else { + JAXBContext jaxbContext = JAXBContextHelper.createJAXBContext(context, true); + Object element = JAXBContextHelper.createJAXBElement(jaxbContext, context.getSourceDataType(), source); + name = jaxbContext.createJAXBIntrospector().getElementName(element); + bean = source; + } + BeanXMLStreamReaderImpl reader = new BeanXMLStreamReaderImpl(name, bean); + return reader; + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + protected Class getSourceType() { + return Object.class; + } + + @Override + protected Class getTargetType() { + return XMLStreamReader.class; + } + + @Override + public int getWeight() { + return 10; + } + + @Override + public String getTargetDataBinding() { + return JAXBDataBinding.NAME; + } +} -- cgit v1.2.3