summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060522/java/sca/bindings/binding.axis2/src/main/java/org/apache/tuscany/binding/axis2/util/SDODataBinding.java
blob: 0caf2293d6da7d7e99a4244b7aa49d8d7325c0e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed 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.binding.axis2.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.namespace.QName;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMXMLParserWrapper;
import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
import org.apache.tuscany.core.wire.InvocationRuntimeException;
import org.apache.tuscany.databinding.sdo.SDOXMLHelper;
import org.apache.tuscany.sdo.util.SDOUtil;
import org.osoa.sca.ServiceRuntimeException;

import commonj.sdo.DataObject;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.XMLHelper;

/**
 * DataBinding for converting between AXIOM OMElement and Java Objects
 */
public class SDODataBinding {

    private ClassLoader classLoader;

    private TypeHelper typeHelper;

    private QName elementQName;

    private boolean isWrapped;

    public SDODataBinding(ClassLoader classLoader, TypeHelper typeHelper, QName elementQName, boolean isWrapped) {
        this.classLoader = classLoader;
        this.typeHelper = typeHelper;
        this.elementQName = elementQName;
        this.isWrapped = isWrapped;
    }

    public Object[] fromOMElement(OMElement omElement) {
        try {

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            omElement.serialize(baos);

            baos.flush();
            baos.close();

            return SDOXMLHelper.toObjects(classLoader,typeHelper, baos.toByteArray(), isWrapped);

        } catch (IOException e) {
            throw new InvocationRuntimeException(e);
        } catch (XMLStreamException e) {
            throw new InvocationRuntimeException(e);
        }
    }

    public OMElement toOMElement(Object[] os) {
        try {

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            DataObject dataObject = SDOXMLHelper.toDataObject(classLoader, typeHelper, os, elementQName, isWrapped);
            XMLHelper xmlHelper = SDOUtil.createXMLHelper(typeHelper);
            xmlHelper.save(dataObject, elementQName.getNamespaceURI(), elementQName.getLocalPart(), baos);
            baos.close();

            XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));
            OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), xsr);
            OMElement omElement = builder.getDocumentElement();

            return omElement;

        } catch (IOException e) {
            throw new ServiceRuntimeException(e);
        } catch (XMLStreamException e) {
            throw new ServiceRuntimeException(e);
        } catch (FactoryConfigurationError e) {
            throw new ServiceRuntimeException(e);
        }
    }
}