diff options
Diffstat (limited to 'branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache')
7 files changed, 0 insertions, 601 deletions
diff --git a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXB2Node.java b/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXB2Node.java deleted file mode 100755 index 607b515809..0000000000 --- a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXB2Node.java +++ /dev/null @@ -1,71 +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.databinding.jaxb; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.Marshaller; - -import org.apache.tuscany.spi.databinding.PullTransformer; -import org.apache.tuscany.spi.databinding.TransformationContext; -import org.apache.tuscany.spi.databinding.TransformationException; -import org.apache.tuscany.spi.databinding.Transformer; -import org.apache.tuscany.spi.databinding.extension.DOMHelper; -import org.apache.tuscany.spi.databinding.extension.TransformerExtension; -import org.osoa.sca.annotations.Service; -import org.w3c.dom.Document; -import org.w3c.dom.Node; - -@Service(Transformer.class) -public class JAXB2Node extends TransformerExtension<Object, Node> implements PullTransformer<Object, Node> { - - public Node transform(Object source, TransformationContext tContext) { - if (source == null) { - return null; - } - try { - JAXBContext context = JAXBContextHelper.createJAXBContext(tContext, true); - Marshaller marshaller = context.createMarshaller(); - // FIXME: The default Marshaller doesn't support - // marshaller.getNode() - Document document = DOMHelper.newDocument(); - marshaller.marshal(JAXBContextHelper.createJAXBElement(tContext.getSourceDataType(), source), - document); - return document; - } catch (Exception e) { - throw new TransformationException(e); - } - } - - public Class getSourceType() { - return Object.class; - } - - public Class getTargetType() { - return Node.class; - } - - public int getWeight() { - return 30; - } - - @Override - public String getSourceDataBinding() { - return JAXBDataBinding.NAME; - } -} diff --git a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXBContextHelper.java b/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXBContextHelper.java deleted file mode 100644 index 457232635f..0000000000 --- a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXBContextHelper.java +++ /dev/null @@ -1,110 +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.databinding.jaxb; - -import java.lang.reflect.Type; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBElement; -import javax.xml.bind.JAXBException; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.namespace.QName; - -import org.apache.tuscany.spi.databinding.TransformationContext; -import org.apache.tuscany.spi.databinding.TransformationException; -import org.apache.tuscany.spi.model.DataType; -import org.apache.tuscany.spi.model.Operation; - -public class JAXBContextHelper { - private static final QName JAXB_ELEMENT = new QName("http://jaxb", "element"); - // TODO: Do we need to set them for source and target? - public static final String JAXB_CLASSES = "jaxb.classes"; - - public static final String JAXB_CONTEXT_PATH = "jaxb.contextPath"; - - private JAXBContextHelper() { - } - - public static JAXBContext createJAXBContext(TransformationContext tContext, boolean source) - throws JAXBException { - if (tContext == null) - throw new TransformationException("JAXB context is not set for the transformation."); - - // FIXME: We should check the context path or classes - // FIXME: What should we do if JAXB is an intermediate node? - DataType<?> bindingContext = source ? tContext.getSourceDataType() : tContext.getTargetDataType(); - String contextPath = (String)bindingContext.getMetadata(JAXB_CONTEXT_PATH); - if (contextPath == null) { - Operation op = (Operation)bindingContext.getOperation(); - contextPath = op != null ? (String)op.getMetaData().get(JAXB_CONTEXT_PATH) : null; - } - JAXBContext context = null; - if (contextPath != null) { - context = JAXBContext.newInstance(contextPath); - } else { - Class[] classes = (Class[])bindingContext.getMetadata(JAXB_CLASSES); - if (classes != null) { - context = JAXBContext.newInstance(classes); - } else { - Type type = bindingContext.getPhysical(); - if (type instanceof Class) { - Class cls = (Class)type; - if (cls.getPackage() != null) { - contextPath = cls.getPackage().getName(); - context = JAXBContext.newInstance(contextPath); - } - } - } - } - if (context == null) { - throw new TransformationException("JAXB context is not set for the transformation."); - } - return context; - } - - @SuppressWarnings("unchecked") - public static JAXBElement createJAXBElement(DataType dataType, Object value) { - if (value instanceof JAXBElement) { - return (JAXBElement)value; - } else { - Object logical = dataType.getLogical(); - if (!(logical instanceof QName)) { - logical = JAXB_ELEMENT; - } - return new JAXBElement((QName)logical, (Class)dataType.getPhysical(), value); - } - } - - @SuppressWarnings("unchecked") - public static Object createReturnValue(DataType dataType, Object value) { - Class<?> cls = (Class)dataType.getPhysical(); - XmlRootElement element = cls.getAnnotation(XmlRootElement.class); - if (element == null) { - if (value instanceof JAXBElement) { - return ((JAXBElement)value).getValue(); - } else { - return value; - } - } else { - QName root = new QName(element.namespace(), element.name()); - return new JAXBElement(root, (Class)dataType.getPhysical(), value); - } - } - -} diff --git a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXBDataBinding.java b/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXBDataBinding.java deleted file mode 100644 index c43e9bcd42..0000000000 --- a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXBDataBinding.java +++ /dev/null @@ -1,109 +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.databinding.jaxb; - -import java.beans.Introspector; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -import javax.xml.bind.JAXBElement; -import javax.xml.bind.annotation.XmlEnum; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlSchema; -import javax.xml.bind.annotation.XmlType; -import javax.xml.namespace.QName; - -import org.apache.tuscany.spi.databinding.extension.DataBindingExtension; -import org.apache.tuscany.spi.model.DataType; - -/** - * JAXB DataBinding - */ -public class JAXBDataBinding extends DataBindingExtension { - - public static final String NAME = JAXBElement.class.getName(); - - @Override - public DataType introspect(Class<?> javaType) { - if (JAXBElement.class.isAssignableFrom(javaType)) { - Type type = javaType.getGenericSuperclass(); - if (type instanceof ParameterizedType) { - ParameterizedType parameterizedType = ((ParameterizedType) type); - Type rawType = parameterizedType.getRawType(); - if (rawType == JAXBElement.class) { - Type actualType = parameterizedType.getActualTypeArguments()[0]; - if (actualType instanceof Class) { - return introspect((Class<?>) actualType); - } - } - } - return new DataType<QName>(getName(), javaType, null); - } - - String namespace = null; - String name = null; - Package pkg = javaType.getPackage(); - if (pkg != null) { - XmlSchema schema = pkg.getAnnotation(XmlSchema.class); - namespace = schema.namespace(); - } - XmlType type = javaType.getAnnotation(XmlType.class); - if (type != null) { - String typeNamespace = type.namespace(); - String typeName = type.name(); - - if (typeNamespace.equals("##default") && typeName.equals("")) { - XmlRootElement rootElement = javaType.getAnnotation(XmlRootElement.class); - if (rootElement != null) { - namespace = rootElement.namespace(); - } else { - // FIXME: The namespace should be from the referencing property - namespace = null; - } - } else if (typeNamespace.equals("##default")) { - // namespace is from the package - } else { - namespace = typeNamespace; - } - - if (typeName.equals("##default")) { - name = Introspector.decapitalize(javaType.getSimpleName()); - } else { - name = typeName; - } - } else { - XmlEnum xmlEnum = javaType.getAnnotation(XmlEnum.class); - if (xmlEnum != null) { - name = Introspector.decapitalize(javaType.getSimpleName()); - } - } - if (namespace == null && name == null) { - return null; - } - QName xmlType = new QName(namespace, name); - DataType<QName> dataType = new DataType<QName>(getName(), javaType, xmlType); - return dataType; - } - - public JAXBDataBinding() { - super(NAME, JAXBElement.class); - } - -} diff --git a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXBWrapperHandler.java b/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXBWrapperHandler.java deleted file mode 100644 index 507f36eb19..0000000000 --- a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/JAXBWrapperHandler.java +++ /dev/null @@ -1,102 +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.databinding.jaxb; - -import java.beans.Introspector; -import java.beans.PropertyDescriptor; -import java.lang.reflect.Method; - -import javax.xml.bind.JAXBElement; -import javax.xml.bind.annotation.XmlElementDecl; -import javax.xml.bind.annotation.XmlRegistry; -import javax.xml.namespace.QName; - -import org.apache.tuscany.spi.databinding.TransformationContext; -import org.apache.tuscany.spi.databinding.TransformationException; -import org.apache.tuscany.spi.databinding.WrapperHandler; -import org.apache.tuscany.spi.idl.ElementInfo; - -/** - * JAXB WrapperHandler implementation - */ -public class JAXBWrapperHandler implements WrapperHandler<JAXBElement<?>> { - - public JAXBElement<?> create(ElementInfo element, TransformationContext context) { - try { - String packageName = null; - String factoryClassName = packageName + ".ObjectFactory"; - Class<?> factoryClass = Class.forName(factoryClassName, true, context.getClassLoader()); - assert factoryClass.isAnnotationPresent(XmlRegistry.class); - Object factory = factoryClass.newInstance(); - QName elementName = element.getQName(); - Method method = null; - for (Method m : factoryClass.getMethods()) { - XmlElementDecl xmlElement = m.getAnnotation(XmlElementDecl.class); - QName name = new QName(xmlElement.namespace(), xmlElement.name()); - if (xmlElement != null && name.equals(elementName)) { - method = m; - break; - } - } - if (method != null) { - Class typeClass = method.getParameterTypes()[0]; - Object value = typeClass.newInstance(); - return (JAXBElement<?>) method.invoke(factory, new Object[] { value }); - } else { - throw new TransformationException("ObjectFactory cannot be resolved."); - } - } catch (Exception e) { - throw new TransformationException(e); - } - } - - public Object getChild(JAXBElement<?> wrapper, int i, ElementInfo element) { - try { - Object value = wrapper.getValue(); - PropertyDescriptor descriptors[] = - Introspector.getBeanInfo(wrapper.getDeclaredType()).getPropertyDescriptors(); - for (PropertyDescriptor d : descriptors) { - if (d.getName().equals(element.getQName().getLocalPart())) { - return d.getReadMethod().invoke(value, new Object[] {}); - } - } - return null; - } catch (Exception e) { - throw new TransformationException(e); - } - } - - public void setChild(JAXBElement<?> wrapper, int i, ElementInfo childElement, Object value) { - try { - Object wrapperValue = wrapper.getValue(); - PropertyDescriptor descriptors[] = - Introspector.getBeanInfo(wrapper.getDeclaredType()).getPropertyDescriptors(); - for (PropertyDescriptor d : descriptors) { - if (d.getName().equals(childElement.getQName().getLocalPart())) { - d.getWriteMethod().invoke(wrapperValue, new Object[] { value }); - break; - } - } - } catch (Exception e) { - throw new TransformationException(e); - } - } - -} diff --git a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/Node2JAXB.java b/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/Node2JAXB.java deleted file mode 100755 index 301e316889..0000000000 --- a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/Node2JAXB.java +++ /dev/null @@ -1,69 +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.databinding.jaxb; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.Unmarshaller; - -import org.apache.tuscany.spi.databinding.PullTransformer; -import org.apache.tuscany.spi.databinding.TransformationContext; -import org.apache.tuscany.spi.databinding.TransformationException; -import org.apache.tuscany.spi.databinding.Transformer; -import org.apache.tuscany.spi.databinding.extension.TransformerExtension; -import org.osoa.sca.annotations.Service; -import org.w3c.dom.Node; - -@Service(Transformer.class) -public class Node2JAXB extends TransformerExtension<Node, Object> implements PullTransformer<Node, Object> { - - public Node2JAXB() { - super(); - } - - public Object transform(Node source, TransformationContext context) { - if (source == null) - return null; - try { - JAXBContext jaxbContext = JAXBContextHelper.createJAXBContext(context, false); - Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); - Object result = unmarshaller.unmarshal(source); - return JAXBContextHelper.createReturnValue(context.getTargetDataType(), result); - } catch (Exception e) { - throw new TransformationException(e); - } - } - - public Class getSourceType() { - return Node.class; - } - - public Class getTargetType() { - return Object.class; - } - - public int getWeight() { - return 30; - } - - @Override - public String getTargetDataBinding() { - return JAXBDataBinding.NAME; - } - -} diff --git a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/Reader2JAXB.java b/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/Reader2JAXB.java deleted file mode 100644 index 273c15db1b..0000000000 --- a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/Reader2JAXB.java +++ /dev/null @@ -1,70 +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.databinding.jaxb; - -import java.io.Reader; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.Unmarshaller; -import javax.xml.transform.stream.StreamSource; - -import org.apache.tuscany.spi.databinding.PullTransformer; -import org.apache.tuscany.spi.databinding.TransformationContext; -import org.apache.tuscany.spi.databinding.TransformationException; -import org.apache.tuscany.spi.databinding.Transformer; -import org.apache.tuscany.spi.databinding.extension.TransformerExtension; -import org.osoa.sca.annotations.Service; - -@Service(Transformer.class) -public class Reader2JAXB extends TransformerExtension<Reader, Object> implements - PullTransformer<Reader, Object> { - - public Object transform(final Reader source, final TransformationContext context) { - if (source == null) { - return null; - } - try { - JAXBContext jaxbContext = JAXBContextHelper.createJAXBContext(context, false); - Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); - StreamSource streamSource = new StreamSource(source); - Object result = unmarshaller.unmarshal(streamSource); - return JAXBContextHelper.createReturnValue(context.getTargetDataType(), result); - } catch (Exception e) { - throw new TransformationException(e); - } - } - - public Class getSourceType() { - return Reader.class; - } - - public Class getTargetType() { - return Object.class; - } - - public int getWeight() { - return 30; - } - - @Override - public String getTargetDataBinding() { - return JAXBDataBinding.NAME; - } - -} diff --git a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/XMLStreamReader2JAXB.java b/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/XMLStreamReader2JAXB.java deleted file mode 100755 index 3f6a2f64d3..0000000000 --- a/branches/pre-spec-changes/services/databinding/databinding-jaxb/src/main/java/org/apache/tuscany/databinding/jaxb/XMLStreamReader2JAXB.java +++ /dev/null @@ -1,70 +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.databinding.jaxb; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.Unmarshaller; -import javax.xml.stream.XMLStreamReader; - -import org.apache.tuscany.spi.databinding.PullTransformer; -import org.apache.tuscany.spi.databinding.TransformationContext; -import org.apache.tuscany.spi.databinding.TransformationException; -import org.apache.tuscany.spi.databinding.Transformer; -import org.apache.tuscany.spi.databinding.extension.TransformerExtension; -import org.osoa.sca.annotations.Service; - -@Service(Transformer.class) -public class XMLStreamReader2JAXB extends TransformerExtension<XMLStreamReader, Object> implements - PullTransformer<XMLStreamReader, Object> { - - public XMLStreamReader2JAXB() { - super(); - } - - public Object transform(XMLStreamReader source, TransformationContext context) { - if (source == null) { - return null; - } - try { - JAXBContext jaxbContext = JAXBContextHelper.createJAXBContext(context, false); - Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); - Object result = unmarshaller.unmarshal(source); - return JAXBContextHelper.createReturnValue(context.getTargetDataType(), result); - } catch (Exception e) { - throw new TransformationException(e); - } - } - - public Class getSourceType() { - return XMLStreamReader.class; - } - - public Class getTargetType() { - return Object.class; - } - - public int getWeight() { - return 10; - } - - @Override - public String getTargetDataBinding() { - return JAXBDataBinding.NAME; - } -} |