diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-09-30 22:07:31 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2011-09-30 22:07:31 +0000 |
commit | 9d3cbbfe0b8a62ed9e61bf32e72f6da27c73bba8 (patch) | |
tree | 8a85efd8e08b8b7ece9094c7d633b74c7b22c6af /sca-java-2.x/trunk/modules/databinding-jaxb/src/main/java | |
parent | 00be9b54b4d44b7dbd0e62a0f760067fbbf58f50 (diff) |
Enhance Tuscany JAX-RS reader/writer to better handle JAXB
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1177856 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/databinding-jaxb/src/main/java')
2 files changed, 157 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/InputStream2JAXB.java b/sca-java-2.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/InputStream2JAXB.java new file mode 100644 index 0000000000..90ce48d086 --- /dev/null +++ b/sca-java-2.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/InputStream2JAXB.java @@ -0,0 +1,79 @@ +/* + * 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.io.InputStream; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Unmarshaller; +import javax.xml.transform.stream.StreamSource; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PullTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; + +/** + * + * @version $Rev$ $Date$ + */ +public class InputStream2JAXB extends BaseTransformer<InputStream, Object> implements + PullTransformer<InputStream, Object> { + private JAXBContextHelper contextHelper; + + public InputStream2JAXB(ExtensionPointRegistry registry) { + contextHelper = JAXBContextHelper.getInstance(registry); + } + public Object transform(final InputStream source, final TransformationContext context) { + if (source == null) { + return null; + } + try { + JAXBContext jaxbContext = contextHelper.createJAXBContext(context, false); + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + StreamSource streamSource = new StreamSource(source); + Object result = unmarshaller.unmarshal(streamSource, JAXBContextHelper.getJavaType(context.getTargetDataType())); + return JAXBContextHelper.createReturnValue(jaxbContext, context.getTargetDataType(), result); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + protected Class<InputStream> getSourceType() { + return InputStream.class; + } + + @Override + protected Class<Object> getTargetType() { + return Object.class; + } + + @Override + public int getWeight() { + return 30; + } + + @Override + public String getTargetDataBinding() { + return JAXBDataBinding.NAME; + } + +} diff --git a/sca-java-2.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2OutputStream.java b/sca-java-2.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2OutputStream.java new file mode 100644 index 0000000000..2e7ab628c1 --- /dev/null +++ b/sca-java-2.x/trunk/modules/databinding-jaxb/src/main/java/org/apache/tuscany/sca/databinding/jaxb/JAXB2OutputStream.java @@ -0,0 +1,78 @@ +/* + * 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.io.OutputStream; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.BaseTransformer; +import org.apache.tuscany.sca.databinding.PushTransformer; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformationException; + +/** + * @version $Rev$ $Date$ + */ +public class JAXB2OutputStream extends BaseTransformer<Object, OutputStream> implements + PushTransformer<Object, OutputStream> { + + private JAXBContextHelper contextHelper; + + public JAXB2OutputStream(ExtensionPointRegistry registry) { + contextHelper = JAXBContextHelper.getInstance(registry); + } + + @Override + protected Class<Object> getSourceType() { + return Object.class; + } + + @Override + protected Class<OutputStream> getTargetType() { + return OutputStream.class; + } + + /** + * @see org.apache.tuscany.sca.databinding.PushTransformer#transform(java.lang.Object, java.lang.Object, org.apache.tuscany.sca.databinding.TransformationContext) + */ + public void transform(Object source, OutputStream target, TransformationContext tContext) { + try { + JAXBContext context = contextHelper.createJAXBContext(tContext, true); + Marshaller marshaller = context.createMarshaller(); + Object jaxbElement = JAXBContextHelper.createJAXBElement(context, tContext.getSourceDataType(), source); + marshaller.marshal(jaxbElement, target); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + @Override + public int getWeight() { + return 20; + } + + @Override + public String getSourceDataBinding() { + return JAXBDataBinding.NAME; + } +} |