diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2008-11-19 05:27:58 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2008-11-19 05:27:58 +0000 |
commit | 5f3869c451e46aadc943d00087d6847877dd1c50 (patch) | |
tree | e22baaff1fb9ea42606b7d04af52e032e3bc03bc /java/sca/modules/databinding-jaxb | |
parent | 60744a36aae604ac3c4499ed54f1082ab8f5947d (diff) |
Merging the 1.x delta on top of the equinox based modules
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@718858 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/databinding-jaxb')
8 files changed, 185 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/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java index eebb4b2e37..38efd288cb 100644 --- a/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java +++ b/java/sca/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXBDataBinding.java @@ -43,7 +43,6 @@ import org.w3c.dom.Document; */ public class JAXBDataBinding extends BaseDataBinding { public static final String NAME = JAXBElement.class.getName(); - public static final String[] ALIASES = new String[] {"jaxb"}; 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"); @@ -52,7 +51,7 @@ public class JAXBDataBinding extends BaseDataBinding { private JAXBTypeHelper xmlTypeHelper; public JAXBDataBinding() { - super(NAME, ALIASES, JAXBElement.class); + super(NAME, JAXBElement.class); this.wrapperHandler = new JAXBWrapperHandler(); this.xmlTypeHelper = new JAXBTypeHelper(); } diff --git a/java/sca/modules/databinding-jaxb/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.DataBinding b/java/sca/modules/databinding-jaxb/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.DataBinding index 58d6123373..3f2978ad25 100644 --- a/java/sca/modules/databinding-jaxb/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.DataBinding +++ b/java/sca/modules/databinding-jaxb/src/main/resources/META-INF/services/org.apache.tuscany.sca.databinding.DataBinding @@ -16,4 +16,4 @@ # under the License. # implementation classes for the databindings -org.apache.tuscany.sca.databinding.jaxb.JAXBDataBinding;type=javax.xml.bind.JAXBElement,name=jaxb +org.apache.tuscany.sca.databinding.jaxb.JAXBDataBinding;name=javax.xml.bind.JAXBElement 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 74f124f892..08522864b5 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 @@ -24,7 +24,6 @@ import javax.xml.bind.JAXBContext; import junit.framework.TestCase; import org.apache.tuscany.sca.databinding.xml.Node2String; -import org.w3c.dom.Node; /** * @version $Rev$ $Date$ @@ -33,7 +32,7 @@ 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 2603412a9d..e70987de51 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 @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.databinding.jaxb; import java.io.StringReader; import java.io.StringWriter; +import java.util.Map; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; @@ -34,6 +35,24 @@ import junit.framework.TestCase; * @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(); @@ -60,7 +79,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"); @@ -82,7 +101,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); } /* |