diff options
Diffstat (limited to 'sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java')
18 files changed, 1397 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/DataObject2String.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/DataObject2String.java new file mode 100755 index 0000000000..c188e94f3b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/DataObject2String.java @@ -0,0 +1,61 @@ +/* + * 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.sdo; + +import javax.xml.namespace.QName; + +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 commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLHelper; + +@Service(Transformer.class) +public class DataObject2String extends TransformerExtension<DataObject, String> implements + PullTransformer<DataObject, String> { + + public String transform(DataObject source, TransformationContext context) { + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context); + XMLHelper xmlHelper = helperContext.getXMLHelper(); + QName elementName = SDOContextHelper.getElement(context.getSourceDataType()); + return xmlHelper.save(source, elementName.getNamespaceURI(), elementName.getLocalPart()); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + public Class getSourceType() { + return DataObject.class; + } + + public Class getTargetType() { + return String.class; + } + + public int getWeight() { + return 40; + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/DataObject2XMLStreamReader.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/DataObject2XMLStreamReader.java new file mode 100755 index 0000000000..9c93d75adf --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/DataObject2XMLStreamReader.java @@ -0,0 +1,70 @@ +/* + * 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.sdo; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sdo.helper.XMLStreamHelper; +import org.apache.tuscany.sdo.util.SDOUtil; +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 commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; + +@Service(Transformer.class) +public class DataObject2XMLStreamReader extends TransformerExtension<DataObject, XMLStreamReader> implements + PullTransformer<DataObject, XMLStreamReader> { + + public XMLStreamReader transform(DataObject source, TransformationContext context) { + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context); + XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(helperContext.getTypeHelper()); + QName elementName = SDOContextHelper.getElement(context.getSourceDataType()); + XMLHelper xmlHelper = helperContext.getXMLHelper(); + XMLDocument document = + xmlHelper.createDocument(source, elementName.getNamespaceURI(), elementName.getLocalPart()); + return streamHelper.createXMLStreamReader(document); + } catch (XMLStreamException e) { + // TODO: Add context to the exception + throw new TransformationException(e); + } + } + + public Class getSourceType() { + return DataObject.class; + } + + public Class getTargetType() { + return XMLStreamReader.class; + } + + public int getWeight() { + return 10; + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/DataObjectLoader.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/DataObjectLoader.java new file mode 100755 index 0000000000..25793d1777 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/DataObjectLoader.java @@ -0,0 +1,73 @@ +/* + * 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.sdo; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sdo.helper.XMLStreamHelper; +import org.apache.tuscany.sdo.util.SDOUtil; +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.model.ModelObject; +import org.osoa.sca.annotations.EagerInit; +import org.osoa.sca.annotations.Property; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; + +/** + * A SDO model-based Loader to load DataObject from the XML stream + */ +@EagerInit +public class DataObjectLoader extends LoaderExtension<ModelDataObject> { + private QName elementName; + + public DataObjectLoader(@Autowire LoaderRegistry registry, + @Property(name = "namespace", required = true) String namespace, + @Property(name = "name", required = true) String name) { + super(registry); + this.elementName = new QName(namespace, name); + } + + public ModelDataObject load(CompositeComponent parent, + ModelObject object, + XMLStreamReader reader, + DeploymentContext deploymentContext) throws XMLStreamException, LoaderException { + assert elementName.equals(reader.getName()); + HelperContext helperContext = SDOContextHelper.getHelperContext(object); + TypeHelper typeHelper = helperContext.getTypeHelper(); + XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(typeHelper); + DataObject dataObject = streamHelper.loadObject(reader); + // TODO: Is it required that the object always extends from ModelObject? + return new ModelDataObject(dataObject); + } + + @Override + public QName getXMLType() { + return elementName; + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ImportSDO.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ImportSDO.java new file mode 100644 index 0000000000..8d14773b88 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ImportSDO.java @@ -0,0 +1,47 @@ +/* + * 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.sdo; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.spi.model.ModelObject; + +import commonj.sdo.helper.HelperContext; + +/** + * The model object for import.sdo + * + * @version $Rev$ $Date$ + */ +public class ImportSDO extends ModelObject { + public static final QName IMPORT_SDO = + new QName("http://tuscany.apache.org/xmlns/sca/databinding/sdo/1.0", "import.sdo"); + + private HelperContext helperContext; + + public ImportSDO(HelperContext helperContext) { + super(); + this.helperContext = helperContext; + } + + public HelperContext getHelperContext() { + return helperContext; + } +}
\ No newline at end of file diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ImportSDOBuilder.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ImportSDOBuilder.java new file mode 100644 index 0000000000..31f17860b4 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ImportSDOBuilder.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.databinding.sdo; + +import org.apache.tuscany.spi.builder.BuilderException; +import org.apache.tuscany.spi.component.ComponentRegistrationException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.GenericBuilderExtension; + +import commonj.sdo.helper.HelperContext; + +/** + * @version $Rev$ $Date$ + */ +public class ImportSDOBuilder extends GenericBuilderExtension<SCAObject, ImportSDO> { + + @Override + protected Class<ImportSDO> getModelType() { + return ImportSDO.class; + } + + public SCAObject build(SCAObject parent, ImportSDO modelObject, DeploymentContext deploymentContext) + throws BuilderException { + if (parent instanceof CompositeComponent) { + CompositeComponent component = (CompositeComponent)parent; + SDOHelperContext obj = new SDOHelperContext(component, modelObject.getHelperContext()); + try { + component.registerJavaObject(obj.getName(), HelperContext.class, obj.getHelperContext()); + } catch (ComponentRegistrationException e) { + throw new IllegalArgumentException(e); + } + return obj; + } else { + return null; + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ImportSDOLoader.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ImportSDOLoader.java new file mode 100755 index 0000000000..f115fc78ec --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ImportSDOLoader.java @@ -0,0 +1,163 @@ +/* + * 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.sdo; + +import static org.apache.tuscany.databinding.sdo.ImportSDO.IMPORT_SDO; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.net.URI; +import java.net.URL; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.loader.LoaderUtil; +import org.apache.tuscany.spi.model.ModelObject; +import org.osoa.sca.annotations.Constructor; + +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XSDHelper; +import commonj.sdo.impl.HelperProvider; + +/** + * Loader that handles <import.sdo> elements. + * + * @version $Rev$ $Date$ + */ +public class ImportSDOLoader extends LoaderExtension { + + @Constructor( {"registry"}) + public ImportSDOLoader(@Autowire + LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return IMPORT_SDO; + } + + public ModelObject load(CompositeComponent parent, + ModelObject object, + XMLStreamReader reader, + DeploymentContext deploymentContext) throws XMLStreamException, LoaderException { + assert IMPORT_SDO.equals(reader.getName()); + + // FIXME: [rfeng] How to associate the TypeHelper with deployment + // context? + HelperContext helperContext = SDOContextHelper.getHelperContext(object); + + importFactory(reader, deploymentContext, helperContext); + importWSDL(reader, deploymentContext, helperContext); + LoaderUtil.skipToEndElement(reader); + return new ImportSDO(helperContext); + } + + private void importFactory(XMLStreamReader reader, DeploymentContext deploymentContext, HelperContext helperContext) + throws LoaderException { + String factoryName = reader.getAttributeValue(null, "factory"); + if (factoryName != null) { + ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); + try { + // set TCCL as SDO needs it + ClassLoader cl = deploymentContext.getClassLoader(); + Thread.currentThread().setContextClassLoader(cl); + Class<?> factoryClass = cl.loadClass(factoryName); + register(factoryClass, helperContext); + } catch (Exception e) { + throw new LoaderException(e.getMessage(), e); + } finally { + Thread.currentThread().setContextClassLoader(oldCL); + } + } + } + + private static void register(Class factoryClass, HelperContext helperContext) throws Exception { + Field field = factoryClass.getField("INSTANCE"); + Object factory = field.get(null); + Method method = factory.getClass().getMethod("register", new Class[] {HelperContext.class}); + method.invoke(factory, new Object[] {helperContext}); + + // FIXME: How do we associate the application HelperContext with the one + // imported by the composite + HelperContext defaultContext = HelperProvider.getDefaultContext(); + method.invoke(factory, new Object[] {defaultContext}); + } + + private void importWSDL(XMLStreamReader reader, DeploymentContext deploymentContext, HelperContext helperContext) + throws LoaderException { + String location = reader.getAttributeValue(null, "location"); + if (location == null) { + location = reader.getAttributeValue(null, "wsdlLocation"); + } + if (location != null) { + try { + URL wsdlURL = null; + URI uri = URI.create(location); + if (uri.isAbsolute()) { + wsdlURL = uri.toURL(); + } + wsdlURL = deploymentContext.getClassLoader().getResource(location); + if (null == wsdlURL) { + LoaderException loaderException = new LoaderException("WSDL location error"); + loaderException.setResourceURI(location); + throw loaderException; + } + InputStream xsdInputStream = wsdlURL.openStream(); + try { + XSDHelper xsdHelper = helperContext.getXSDHelper(); + xsdHelper.define(xsdInputStream, wsdlURL.toExternalForm()); + } finally { + xsdInputStream.close(); + } + // FIXME: How do we associate the application HelperContext with the one + // imported by the composite + HelperContext defaultContext = HelperProvider.getDefaultContext(); + xsdInputStream = wsdlURL.openStream(); + try { + XSDHelper xsdHelper = defaultContext.getXSDHelper(); + ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); + try { + // set TCCL as SDO needs it + ClassLoader cl = deploymentContext.getClassLoader(); + Thread.currentThread().setContextClassLoader(cl); + xsdHelper.define(xsdInputStream, wsdlURL.toExternalForm()); + } finally { + Thread.currentThread().setContextClassLoader(oldCL); + } + } finally { + xsdInputStream.close(); + } + } catch (IOException e) { + LoaderException sfe = new LoaderException(e.getMessage()); + sfe.setResourceURI(location); + throw sfe; + } + } + } +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ModelDataObject.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ModelDataObject.java new file mode 100755 index 0000000000..ba171af498 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/ModelDataObject.java @@ -0,0 +1,43 @@ +/* + * 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.sdo; + +import org.apache.tuscany.spi.model.ModelObject; + +import commonj.sdo.DataObject; + +/** + * Wrapper of DataObject as a ModelObject + * + * @version $Rev$ $Date$ + */ +public class ModelDataObject extends ModelObject { + private DataObject dataObject; + + public ModelDataObject(DataObject dataObject) { + super(); + this.dataObject = dataObject; + } + + public DataObject getDataObject() { + return dataObject; + } + + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOContextHelper.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOContextHelper.java new file mode 100644 index 0000000000..9d37c79228 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOContextHelper.java @@ -0,0 +1,111 @@ +/* + * 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.sdo; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sdo.util.SDOUtil; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.databinding.TransformationContext; +import org.apache.tuscany.spi.idl.XMLType; +import org.apache.tuscany.spi.model.CompositeComponentType; +import org.apache.tuscany.spi.model.DataType; +import org.apache.tuscany.spi.model.ModelObject; + +import commonj.sdo.helper.HelperContext; +import commonj.sdo.impl.HelperProvider; + +/** + * Helper class to get TypeHelper from the context + */ +public final class SDOContextHelper { + private SDOContextHelper() { + } + + public static HelperContext getHelperContext(TransformationContext context) { + if (context == null || context.getMetadata() == null) { + return getDefaultHelperContext(); + } + HelperContext helperContext = null; + CompositeComponent composite = (CompositeComponent)context.getMetadata().get(CompositeComponent.class); + if (composite != null) { +// SDOHelperContext sdoContext = +// (SDOHelperContext)composite.getExtensions().get(HelperContext.class.getName()); +// if (sdoContext != null) { +// helperContext = sdoContext.getHelperContext(); +// } + AtomicComponent child = (AtomicComponent)composite.getSystemChild(HelperContext.class.getName()); + try { + // The child can be null if no import.sdo is declared + if (child != null) { + helperContext = (HelperContext)child.getTargetInstance(); + } + } catch (TargetResolutionException e) { + helperContext = null; + } + } + if (helperContext == null) { + return getDefaultHelperContext(); + } else { + return helperContext; + } + } + + public static HelperContext getHelperContext(ModelObject model) { + HelperContext helperContext = null; + if (model instanceof CompositeComponentType) { + // HACK: Retrieve the SDO HelperContext from the + // CompositeComponentType + // extensions + helperContext = (HelperContext)model.getExtensions().get(ImportSDO.IMPORT_SDO); + if (helperContext == null) { + helperContext = SDOUtil.createHelperContext(); + ((CompositeComponentType<?, ?, ?>)model).getDeclaredExtensions().put(ImportSDO.IMPORT_SDO, + helperContext); + } + } + + if (helperContext == null) { + helperContext = getDefaultHelperContext(); + } + + return helperContext; + } + + protected static HelperContext getDefaultHelperContext() { + // SDOUtil.createHelperContext(); + return HelperProvider.getDefaultContext(); + } + + public static QName getElement(DataType<?> dataType) { + Object logical = dataType.getLogical(); + QName elementName = SDODataBinding.ROOT_ELEMENT; + if (logical instanceof XMLType) { + XMLType xmlType = (XMLType)logical; + QName element = xmlType.getElementName(); + if (element != null) { + elementName = element; + } + } + return elementName; + } +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDODataBinding.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDODataBinding.java new file mode 100644 index 0000000000..4178ee9f63 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDODataBinding.java @@ -0,0 +1,122 @@ +/* + * 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.sdo; + +import java.lang.annotation.Annotation; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.spi.databinding.ExceptionHandler; +import org.apache.tuscany.spi.databinding.SimpleTypeMapper; +import org.apache.tuscany.spi.databinding.WrapperHandler; +import org.apache.tuscany.spi.databinding.extension.DataBindingExtension; +import org.apache.tuscany.spi.idl.XMLType; +import org.apache.tuscany.spi.model.DataType; + +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.CopyHelper; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.impl.HelperProvider; + +/** + * SDO Databinding + * + * @version $Reve$ $Date$ + */ +public class SDODataBinding extends DataBindingExtension { + public static final String NAME = DataObject.class.getName(); + public static final String[] ALIASES = new String[] {"sdo"}; + + public static final String ROOT_NAMESPACE = "commonj.sdo"; + public static final QName ROOT_ELEMENT = new QName(ROOT_NAMESPACE, "dataObject"); + + private WrapperHandler<Object> wrapperHandler; + + public SDODataBinding() { + super(NAME, ALIASES, DataObject.class); + wrapperHandler = new SDOWrapperHandler(); + } + + @Override + public boolean introspect(DataType dataType, Annotation[] annotations) { + Object physical = dataType.getPhysical(); + if (!(physical instanceof Class)) { + return false; + } + Class javaType = (Class)physical; + HelperContext context = HelperProvider.getDefaultContext(); + // FIXME: Need a better to test dynamic SDO + if (DataObject.class.isAssignableFrom(javaType)) { + // Dynamic SDO + dataType.setDataBinding(getName()); + dataType.setLogical(XMLType.UNKNOWN); + return true; + } + // FIXME: We need to access HelperContext + Type type = context.getTypeHelper().getType(javaType); + if (type == null) { + return false; + } + if (type.isDataType()) { + // FIXME: Ignore simple types? + return false; + } + String namespace = type.getURI(); + String name = context.getXSDHelper().getLocalName(type); + QName xmlType = new QName(namespace, name); + dataType.setDataBinding(getName()); + dataType.setLogical(new XMLType(null, xmlType)); + return true; + } + + @Override + public WrapperHandler getWrapperHandler() { + return wrapperHandler; + } + + public SimpleTypeMapper getSimpleTypeMapper() { + return new SDOSimpleTypeMapper(); + } + + @Override + public Object copy(Object arg) { + HelperContext context = HelperProvider.getDefaultContext(); + CopyHelper copyHelper = context.getCopyHelper(); + if (arg instanceof XMLDocument) { + XMLDocument document = (XMLDocument)arg; + DataObject dataObject = copyHelper.copy(document.getRootObject()); + return context.getXMLHelper().createDocument(dataObject, + document.getRootElementURI(), + document.getRootElementName()); + } else if (arg instanceof DataObject) { + return context.getCopyHelper().copy((DataObject)arg); + } else { + return super.copy(arg); + } + } + + @Override + public ExceptionHandler getExceptionHandler() { + return new SDOExceptionHandler(); + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOExceptionHandler.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOExceptionHandler.java new file mode 100644 index 0000000000..28a36ea2c8 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOExceptionHandler.java @@ -0,0 +1,123 @@ +/* + * 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.sdo; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.spi.databinding.ExceptionHandler; +import org.apache.tuscany.spi.idl.XMLType; +import org.apache.tuscany.spi.model.DataType; + +import commonj.sdo.Type; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.impl.HelperProvider; + +/** + * SDO implementation of ExceptionHandler + * + * @version $Rev$ $Date$ + */ +public class SDOExceptionHandler implements ExceptionHandler { + private static final Class[] EMPTY_CLASS_ARRAY = new Class[0]; + + // FIXME: Need a way to pass in the HelperContext + private HelperContext helperContext = HelperProvider.getDefaultContext(); + + /** + * <ul> + * <li>WrapperException(String message, FaultBean faultInfo) <br> + * A constructor where WrapperException is replaced with the name of the + * generated wrapper exception and FaultBean is replaced by the name of the + * generated fault bean. + * <li> WrapperException(String message, FaultBean faultInfo, Throwable + * cause) <br> + * A constructor whereWrapperException is replaced with the name of the + * generated wrapper exception and FaultBean is replaced by the name of the + * generated fault bean. The last argument, cause, may be used to convey + * protocol specific fault information + * </ul> + */ + public Exception createException(DataType<DataType> exceptionType, String message, Object faultInfo, Throwable cause) { + Class exceptionClass = (Class)exceptionType.getPhysical(); + DataType<?> faultBeanType = exceptionType.getLogical(); + Class faultBeanClass = (Class)faultBeanType.getPhysical(); + try { + Constructor constructor = + exceptionClass.getConstructor(new Class[] {String.class, faultBeanClass, Throwable.class}); + return (Exception)constructor.newInstance(new Object[] {message, faultInfo, cause}); + } catch (Throwable e) { + throw new IllegalArgumentException(e); + } + } + + public Object getFaultInfo(Exception exception) { + if (exception == null) { + return null; + } + try { + Method method = exception.getClass().getMethod("getFaultInfo", EMPTY_CLASS_ARRAY); + return method.invoke(exception, (Object[])null); + } catch (Throwable e) { + throw new IllegalArgumentException(e); + } + } + + public DataType<?> getFaultType(DataType exceptionDataType) { + Class exceptionType = (Class) exceptionDataType.getPhysical(); + Class faultBeanClass = null; + try { + Method method = exceptionType.getMethod("getFaultInfo", EMPTY_CLASS_ARRAY); + faultBeanClass = method.getReturnType(); + } catch (NoSuchMethodException e) { + faultBeanClass = null; + } + if (faultBeanClass == null) { + return null; + } + + QName faultElement = null; + try { + Field field = exceptionType.getField("FAULT_ELEMENT"); + faultElement = (QName)field.get(null); + } catch (NoSuchFieldException e) { + // Fall back to type inspection + Type type = helperContext.getTypeHelper().getType(faultBeanClass); + if (type != null) { + String ns = type.getURI(); + String name = helperContext.getXSDHelper().getLocalName(type); + faultElement = new QName(ns, name); + } + } catch (Throwable e) { + // Ignore + } + if (faultElement == null) { + return null; + } + DataType<XMLType> faultType = + new DataType<XMLType>(SDODataBinding.NAME, faultBeanClass, new XMLType(faultElement, null)); + return faultType; + + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOHelperContext.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOHelperContext.java new file mode 100644 index 0000000000..3091d7ec8d --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOHelperContext.java @@ -0,0 +1,48 @@ +/* + * 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.sdo; + +import org.apache.tuscany.spi.component.AbstractSCAObject; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.model.Scope; + +import commonj.sdo.helper.HelperContext; + +public class SDOHelperContext extends AbstractSCAObject { + private HelperContext helperContext; + + /** + * @param name + * @param parent + */ + public SDOHelperContext(CompositeComponent parent, HelperContext helperContext) { + super(HelperContext.class.getName(), parent); + this.helperContext = helperContext; + } + + public Scope getScope() { + return Scope.COMPOSITE; + } + + public HelperContext getHelperContext() { + return helperContext; + } + +}
\ No newline at end of file diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOSimpleTypeMapper.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOSimpleTypeMapper.java new file mode 100644 index 0000000000..3b57323c73 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOSimpleTypeMapper.java @@ -0,0 +1,66 @@ +/* + * 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.sdo; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sdo.util.SDOUtil; +import org.apache.tuscany.spi.databinding.SimpleTypeMapper; +import org.apache.tuscany.spi.databinding.TransformationContext; + +import commonj.sdo.Type; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; + +/** + * SDO Java/XML mapping for simple XSD types + */ +public class SDOSimpleTypeMapper implements SimpleTypeMapper { + public static final String URI_2001_SCHEMA_XSD = "http://www.w3.org/2001/XMLSchema"; + + public SDOSimpleTypeMapper() { + super(); + } + + public Object toJavaObject(QName typeName, String value, TransformationContext context) { + Type type = null; + if (URI_2001_SCHEMA_XSD.equals(typeName.getNamespaceURI())) { + type = SDOUtil.getXSDSDOType(typeName.getLocalPart()); + } else { + HelperContext helperContext = SDOContextHelper.getHelperContext(context); + TypeHelper typeHelper = helperContext.getTypeHelper(); + type = typeHelper.getType(typeName.getNamespaceURI(), typeName.getLocalPart()); + } + return SDOUtil.createFromString(type, value); + } + + public String toXMLLiteral(QName typeName, Object obj, TransformationContext context) { + Type type = null; + if (URI_2001_SCHEMA_XSD.equals(typeName.getNamespaceURI())) { + type = SDOUtil.getXSDSDOType(typeName.getLocalPart()); + } else { + HelperContext helperContext = SDOContextHelper.getHelperContext(context); + TypeHelper typeHelper = helperContext.getTypeHelper(); + type = typeHelper.getType(typeName.getNamespaceURI(), typeName.getLocalPart()); + } + return SDOUtil.convertToString(type, obj); + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOWrapperHandler.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOWrapperHandler.java new file mode 100644 index 0000000000..dfbf7dabce --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOWrapperHandler.java @@ -0,0 +1,99 @@ +/* + * 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.sdo; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.spi.databinding.TransformationContext; +import org.apache.tuscany.spi.databinding.WrapperHandler; +import org.apache.tuscany.spi.idl.ElementInfo; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Sequence; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; + +/** + * SDO Wrapper Handler + */ +public class SDOWrapperHandler implements WrapperHandler<Object> { + + /** + * @see org.apache.tuscany.spi.databinding.WrapperHandler#create(ElementInfo, TransformationContext) + */ + public Object create(ElementInfo element, TransformationContext context) { + HelperContext helperContext = SDOContextHelper.getHelperContext(context); + QName typeName = element.getType().getQName(); + DataFactory dataFactory = helperContext.getDataFactory(); + DataObject root = dataFactory.create(typeName.getNamespaceURI(), typeName.getLocalPart()); + XMLHelper xmlHelper = helperContext.getXMLHelper(); + return xmlHelper.createDocument(root, element.getQName().getNamespaceURI(), element.getQName().getLocalPart()); + } + + /** + * @see org.apache.tuscany.spi.databinding.WrapperHandler#setChild(java.lang.Object, int, ElementInfo, + * java.lang.Object) + */ + public void setChild(Object wrapper, int i, ElementInfo childElement, Object value) { + DataObject wrapperDO = + (wrapper instanceof XMLDocument) ? ((XMLDocument)wrapper).getRootObject() : (DataObject)wrapper; + wrapperDO.set(i, value); + } + + @SuppressWarnings("unchecked") + public List getChildren(Object wrapper) { + DataObject wrapperDO = + (wrapper instanceof XMLDocument) ? ((XMLDocument)wrapper).getRootObject() : (DataObject)wrapper; + List<Property> properties = wrapperDO.getInstanceProperties(); + List<Object> elements = new ArrayList<Object>(); + Type type = wrapperDO.getType(); + if (type.isSequenced()) { + // Add values in the sequence + Sequence sequence = wrapperDO.getSequence(); + for (int i = 0; i < sequence.size(); i++) { + // Skip mixed text + if (sequence.getProperty(i) != null) { + elements.add(sequence.getValue(i)); + } + } + } else { + for (Property p : properties) { + Object child = wrapperDO.get(p); + if (p.isMany()) { + for (Object c : (Collection<?>)child) { + elements.add(c); + } + } else { + elements.add(child); + } + } + } + return elements; + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/String2DataObject.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/String2DataObject.java new file mode 100755 index 0000000000..bd288c74e7 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/String2DataObject.java @@ -0,0 +1,58 @@ +/* + * 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.sdo; + +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 commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLHelper; + +@Service(Transformer.class) +public class String2DataObject extends TransformerExtension<String, DataObject> implements + PullTransformer<String, DataObject> { + + public DataObject transform(String source, TransformationContext context) { + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context); + XMLHelper xmlHelper = helperContext.getXMLHelper(); + return xmlHelper.load(source).getRootObject(); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + public Class getSourceType() { + return String.class; + } + + public Class getTargetType() { + return DataObject.class; + } + + public int getWeight() { + return 50; + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLDocument2String.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLDocument2String.java new file mode 100644 index 0000000000..38f48cebee --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLDocument2String.java @@ -0,0 +1,62 @@ +/* + * 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.sdo; + +import java.io.StringWriter; + +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 commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; + +@Service(Transformer.class) +public class XMLDocument2String extends TransformerExtension<XMLDocument, String> implements + PullTransformer<XMLDocument, String> { + + public String transform(XMLDocument source, TransformationContext context) { + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context); + XMLHelper xmlHelper = helperContext.getXMLHelper(); + StringWriter writer = new StringWriter(); + xmlHelper.save(source, writer, null); + return writer.toString(); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + public Class getSourceType() { + return XMLDocument.class; + } + + public Class getTargetType() { + return String.class; + } + + public int getWeight() { + return 40; + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLDocument2XMLStreamReader.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLDocument2XMLStreamReader.java new file mode 100755 index 0000000000..bd20089001 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLDocument2XMLStreamReader.java @@ -0,0 +1,66 @@ +/* + * 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.sdo; + +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sdo.helper.XMLStreamHelper; +import org.apache.tuscany.sdo.util.SDOUtil; +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 commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; + +@Service(Transformer.class) +public class XMLDocument2XMLStreamReader extends TransformerExtension<XMLDocument, XMLStreamReader> implements + PullTransformer<XMLDocument, XMLStreamReader> { + /** + * @param source + * @param context + * @return + */ + public XMLStreamReader transform(XMLDocument source, TransformationContext context) { + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context); + XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(helperContext.getTypeHelper()); + return streamHelper.createXMLStreamReader(source); + } catch (XMLStreamException e) { + throw new TransformationException(e); + } + } + + public Class getSourceType() { + return XMLDocument.class; + } + + public Class getTargetType() { + return XMLStreamReader.class; + } + + public int getWeight() { + return 10; + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLStreamReader2DataObject.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLStreamReader2DataObject.java new file mode 100755 index 0000000000..c29413101d --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLStreamReader2DataObject.java @@ -0,0 +1,67 @@ +/* + * 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.sdo; + +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sdo.helper.XMLStreamHelper; +import org.apache.tuscany.sdo.util.SDOUtil; +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 commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; + +@Service(Transformer.class) +public class XMLStreamReader2DataObject extends TransformerExtension<XMLStreamReader, DataObject> implements + PullTransformer<XMLStreamReader, DataObject> { + + public DataObject transform(XMLStreamReader source, TransformationContext context) { + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context); + XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(helperContext.getTypeHelper()); + // The XMLStreamHelper requires that the reader is posistioned at + // START_ELEMENT + while (source.getEventType() != XMLStreamConstants.START_ELEMENT && source.hasNext()) { + source.next(); + } + return streamHelper.loadObject(source); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + public Class getTargetType() { + return DataObject.class; + } + + public Class getSourceType() { + return XMLStreamReader.class; + } + + public int getWeight() { + return 15; + } + +} diff --git a/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLStreamReader2XMLDocument.java b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLStreamReader2XMLDocument.java new file mode 100755 index 0000000000..ff9e0aa2e1 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-integration/sca/services/databinding/databinding-sdo/src/main/java/org/apache/tuscany/databinding/sdo/XMLStreamReader2XMLDocument.java @@ -0,0 +1,61 @@ +/* + * 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.sdo; + +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sdo.helper.XMLStreamHelper; +import org.apache.tuscany.sdo.util.SDOUtil; +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 commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; + +@Service(Transformer.class) +public class XMLStreamReader2XMLDocument extends TransformerExtension<XMLStreamReader, XMLDocument> implements + PullTransformer<XMLStreamReader, XMLDocument> { + + public XMLDocument transform(XMLStreamReader source, TransformationContext context) { + try { + HelperContext helperContext = SDOContextHelper.getHelperContext(context); + XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(helperContext.getTypeHelper()); + return streamHelper.load(source); + } catch (Exception e) { + throw new TransformationException(e); + } + } + + public Class getTargetType() { + return XMLDocument.class; + } + + public Class getSourceType() { + return XMLStreamReader.class; + } + + public int getWeight() { + return 15; + } + +} |