diff options
Diffstat (limited to '')
6 files changed, 184 insertions, 34 deletions
diff --git a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/AnyTypeXmlAdapter.java b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/AnyTypeXmlAdapter.java new file mode 100644 index 0000000000..57922e1c89 --- /dev/null +++ b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/AnyTypeXmlAdapter.java @@ -0,0 +1,39 @@ +/* + * 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.annotation.adapters.XmlAdapter; + +/** + * This special XmlAdapter can be used by JAXB classes to annotate the references to java interfaces + */ +public class AnyTypeXmlAdapter extends XmlAdapter<Object, Object> { + + @Override + public Object marshal(Object v) throws Exception { + return v; + } + + @Override + public Object unmarshal(Object v) throws Exception { + return v; + } + +} diff --git a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DOMElementXmlAdapter.java b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DOMElementXmlAdapter.java new file mode 100644 index 0000000000..91cb39b0f2 --- /dev/null +++ b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/DOMElementXmlAdapter.java @@ -0,0 +1,57 @@ +/* + * 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.annotation.adapters.XmlAdapter; + +import org.apache.tuscany.sca.databinding.Mediator; +import org.apache.tuscany.sca.databinding.xml.DOMDataBinding; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.w3c.dom.Element; + +/** + * A generic XmlAdapter for JAXB to marshal/unmarshal between the java objects and DOM elements + */ +public class DOMElementXmlAdapter extends XmlAdapter<Element, Object> { + private Mediator mediator; + private DataType dataType; + private DataType domType; + + public DOMElementXmlAdapter(Mediator mediator, DataType dataType) { + this.mediator = mediator; + this.dataType = dataType; + this.domType = new DataTypeImpl(DOMDataBinding.NAME, Element.class, dataType.getLogical()); + } + + @Override + public Element marshal(Object value) throws Exception { + return (Element) mediator.mediate(value, dataType, domType, null); + } + + @Override + public Object unmarshal(Element element) throws Exception { + return mediator.mediate(element, domType, dataType, null); + } + + public void setMediator(Mediator mediator) { + this.mediator = mediator; + } +} diff --git a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java index 7297399f5e..5265184ba6 100644 --- a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java +++ b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBContextHelper.java @@ -18,7 +18,6 @@ */ package org.apache.tuscany.sca.databinding.jaxb; -import java.io.IOException; import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -34,15 +33,12 @@ import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBIntrospector; import javax.xml.bind.Marshaller; -import javax.xml.bind.SchemaOutputResolver; import javax.xml.bind.Unmarshaller; 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 javax.xml.transform.Result; -import javax.xml.transform.dom.DOMResult; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.TransformationException; @@ -53,7 +49,6 @@ import org.apache.tuscany.sca.interfacedef.Interface; 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.w3c.dom.Node; /** * @@ -444,25 +439,4 @@ public class JAXBContextHelper { return name.substring(0, decap).toLowerCase() + name.substring(decap); } - public static Node generateSchema(JAXBContext context) throws Exception { - SchemaOutputResolverImpl resolver = new SchemaOutputResolverImpl(); - context.generateSchema(resolver); - return resolver.getSchema(); - } - - public static class SchemaOutputResolverImpl extends SchemaOutputResolver { - private DOMResult result = new DOMResult(); - - @Override - public Result createOutput(String ns, String file) throws IOException { - result.setSystemId("sca:dom"); - return result; - } - - public Node getSchema() { - return result != null ? result.getNode() : null; - } - - } - } diff --git a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBReflectionTestCase.java b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBReflectionTestCase.java index cbc32d4353..500422c9c7 100644 --- a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBReflectionTestCase.java +++ b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/JAXBReflectionTestCase.java @@ -21,6 +21,7 @@ package org.apache.tuscany.sca.databinding.jaxb; import java.lang.reflect.Type; import java.util.Collections; +import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.namespace.QName; @@ -28,8 +29,6 @@ import javax.xml.namespace.QName; import junit.framework.Assert; import junit.framework.TestCase; -import org.apache.tuscany.sca.databinding.jaxb.JAXBContextHelper; -import org.apache.tuscany.sca.databinding.xml.Node2String; import org.jvnet.jaxb.reflection.model.annotation.RuntimeInlineAnnotationReader; import org.jvnet.jaxb.reflection.model.core.Ref; import org.jvnet.jaxb.reflection.model.impl.RuntimeModelBuilder; @@ -38,7 +37,6 @@ 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; @@ -52,8 +50,8 @@ public class JAXBReflectionTestCase extends TestCase { public void testGenerateSchema() throws Exception { JAXBContext context = JAXBContext.newInstance("com.example.ipo.jaxb"); - Node schema = JAXBContextHelper.generateSchema(context); - System.out.println(new Node2String().transform(schema, null)); + Map<String, String> schemas = JAXBTypeHelper.generateSchema(context); + System.out.println(schemas); } /** diff --git a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyJaxbBean.java b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyJaxbBean.java new file mode 100644 index 0000000000..3c3992524a --- /dev/null +++ b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/MyJaxbBean.java @@ -0,0 +1,64 @@ +/* + * 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.annotation.XmlAnyElement; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlAdapter; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import org.w3c.dom.Element; + +/** + * + */ +@XmlRootElement(name = "myBean", namespace = "http://ns1") +public class MyJaxbBean { + public MyBean myBean; + + @XmlJavaTypeAdapter(AnyTypeXmlAdapter.class) + public MyInterface myInterface; + + @XmlElement(type = MyInterfaceImpl.class) + public MyInterface myInterface1; + + @XmlJavaTypeAdapter(MyInterfaceAdapter.class) + public MyInterface myInterface2; + + public Object myObject; + + @XmlAnyElement + public Element anyElement; + + public static class MyInterfaceAdapter extends XmlAdapter<MyInterfaceImpl, MyInterface> { + + @Override + public MyInterfaceImpl marshal(MyInterface v) throws Exception { + return (MyInterfaceImpl) v; + } + + @Override + public MyInterface unmarshal(MyInterfaceImpl v) throws Exception { + return (MyInterface) v; + } + + } +} diff --git a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/POJOTestCase.java b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/POJOTestCase.java index 0a3d64a705..fdfa87ff43 100644 --- a/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/POJOTestCase.java +++ b/java/sca/modules/databinding-jaxb/src/test/java/org/apache/tuscany/sca/databinding/jaxb/POJOTestCase.java @@ -23,6 +23,7 @@ import java.io.StringWriter; import java.lang.reflect.Type; import java.util.Collection; import java.util.Collections; +import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; @@ -34,7 +35,6 @@ import junit.framework.TestCase; import org.apache.tuscany.sca.databinding.TransformationContext; import org.apache.tuscany.sca.databinding.impl.TransformationContextImpl; -import org.apache.tuscany.sca.databinding.jaxb.JAXB2XMLStreamReader; import com.sun.xml.bind.v2.model.annotation.RuntimeInlineAnnotationReader; import com.sun.xml.bind.v2.model.core.Ref; @@ -50,6 +50,24 @@ import com.sun.xml.bind.v2.runtime.JAXBContextImpl; * @version $Rev$ $Date$ */ public class POJOTestCase extends TestCase { + public void testAdapter() throws Exception { + JAXBContext context = JAXBContext.newInstance(MyJaxbBean.class, MyInterfaceImpl.class); + StringWriter writer = new StringWriter(); + MyJaxbBean bean = new MyJaxbBean(); + bean.myBean = new MySubBean(); + bean.myBean.setName("Ray"); + bean.myInterface = new MyInterfaceImpl(); + bean.myInterface.setId("001"); + bean.myObject = new MyBean(); + ((MyBean) bean.myObject).setName("Y"); + context.createMarshaller().marshal(bean, writer); + System.out.println(writer.toString()); + Object result = context.createUnmarshaller().unmarshal(new StringReader(writer.toString())); + assertTrue(result instanceof MyJaxbBean); + Map<String, String> schemas = JAXBTypeHelper.generateSchema(context); + System.out.println(schemas); + } + public void testPOJO() throws Exception { JAXBContext context = JAXBContext.newInstance(MyBean.class, MyInterfaceImpl.class); StringWriter writer = new StringWriter(); @@ -76,7 +94,7 @@ public class POJOTestCase extends TestCase { } public void testPOJOArray() throws Exception { - JAXBContext context = JAXBContext.newInstance(MyBean[].class); + JAXBContext context = JAXBContext.newInstance(MyBean[].class, MySubBean.class); StringWriter writer = new StringWriter(); MySubBean bean = new MySubBean(); bean.setAddtional("SUB"); @@ -98,7 +116,7 @@ public class POJOTestCase extends TestCase { JAXBElement e2 = (JAXBElement)result; assertTrue(e2.getValue() instanceof MyBean[]); MyBean newBean = ((MyBean[])e2.getValue())[0]; - assertFalse(newBean instanceof MySubBean); + assertTrue(newBean instanceof MySubBean); } public void testXMLStreamReader() throws Exception { |