summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-M1-final/java/sca/databinding/sdo/src/main/java/org/apache/tuscany/databinding/sdo/SDOXMLHelper.java
blob: d732676c76172aec3cb198a9bec1d50d84e4de34 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 *
 *  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.databinding.sdo;

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

import javax.xml.namespace.QName;

import org.apache.tuscany.core.wire.InvocationRuntimeException;
import org.apache.tuscany.sdo.util.SDOUtil;
import org.osoa.sca.ServiceRuntimeException;

import commonj.sdo.DataObject;
import commonj.sdo.Property;
import commonj.sdo.Type;
import commonj.sdo.helper.DataFactory;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.XMLDocument;
import commonj.sdo.helper.XMLHelper;
import commonj.sdo.helper.XSDHelper;

/**
 * Utility methods to convert between XML byte arrays, SDO DataObjects, and Java objects.
 * 
 * Most of these methods rely on the schemas having been registered with XSDHelper.define
 */
public final class SDOXMLHelper {

    private SDOXMLHelper() {
        // utility class, never contructed
    }

    /**
     * Deserialize an XML byte array into Java Objects
     * 
     * @param xmlBytes
     *            the byte array containing the XML
     * @param isWrapped
     * 
     * @return the array of deserialized Java objects
     * @deprecated TUSCANY-333 use the method that takes a ClassLoader
     */
    public static Object[] toObjects(TypeHelper typeHelper, byte[] xmlBytes, boolean isWrapped) {
        DataObject dataObject = toDataObject(typeHelper, xmlBytes);
        return toObjects(dataObject, isWrapped);
    }

    /**
     * Convert a typed DataObject to Java objects
     * 
     * @param dataObject
     * @param isWrapped
     * @return the array of Objects from the DataObject
     */
    public static Object[] toObjects(DataObject dataObject, boolean isWrapped) {
        if (isWrapped) {
            List ips = dataObject.getInstanceProperties();
            Object[] os = new Object[ips.size()];
            for (int i = 0; i < ips.size(); i++) {
                os[i] = dataObject.get((Property) ips.get(i));
            }
            return os;
        } else {
            Object object = dataObject;
            Type type = dataObject.getType();
            if (type.isSequenced()) {
                object = dataObject.getSequence().getValue(0);
            }
            return new Object[] { object };
        }
    }

    /**
     * Serializes objects to an XML byte array
     * 
     * @param os
     * @param typeNS
     * @param typeName
     * @return a byte array containing the XML
     * @deprecated TUSCANY-333 use the method that takes a ClassLoader
     */
    public static byte[] toXMLBytes(TypeHelper typeHelper, Object[] os, QName elementQName, boolean isWrapped) {
        DataObject dataObject = toDataObject(typeHelper, os, elementQName, isWrapped);
        return toXMLbytes(typeHelper, dataObject, elementQName);
    }

    /**
     * Convert a DataObject to an XML byte array
     * 
     * @param dataObject
     * @param typeNS
     * @param typeName
     * @return a byte array containing the XML bytes
     * @deprecated TUSCANY-333 use the method that takes a ClassLoader
     */
    public static byte[] toXMLbytes(TypeHelper typeHelper, DataObject dataObject, QName elementQName) {
        try {

            ByteArrayOutputStream pos = new ByteArrayOutputStream();
            XMLHelper xmlHelper = SDOUtil.createXMLHelper(typeHelper);
            xmlHelper.save(dataObject, elementQName.getNamespaceURI(), elementQName.getLocalPart(), pos);
            pos.close();

            return pos.toByteArray();

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

    /**
     * Deserialize an XML byte array into a DataObject
     * 
     * @param xmlBytes
     * @return a DataObject
     * @deprecated TUSCANY-333 use the method that takes a ClassLoader
     */
    public static DataObject toDataObject(TypeHelper typeHelper, byte[] xmlBytes) {
        try {

            XMLHelper xmlHelper = SDOUtil.createXMLHelper(typeHelper);
            XMLDocument document = xmlHelper.load(new ByteArrayInputStream(xmlBytes));

            return document.getRootObject();

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

    /**
     * Convert objects to typed DataObject
     * 
     * @param typeNS
     * @param typeName
     * @param os
     * @return the DataObject
     * @deprecated TUSCANY-333 use the method that takes a ClassLoader
     */
    public static DataObject toDataObject(TypeHelper typeHelper, Object[] os, QName elementQName, boolean isWrapped) {
        XSDHelper xsdHelper = SDOUtil.createXSDHelper(typeHelper);

        Property property = xsdHelper.getGlobalProperty(elementQName.getNamespaceURI(), elementQName.getLocalPart(), true);
        if (null == property) {
            throw new InvocationRuntimeException("Type '" + elementQName.toString() + "' not found in registered SDO types.");
        }
        if (isWrapped) {
            DataFactory dataFactory = SDOUtil.createDataFactory(typeHelper);
            DataObject dataObject = dataFactory.create(property.getType());
            List ips = dataObject.getInstanceProperties();
            for (int i = 0; i < ips.size(); i++) {
                dataObject.set(i, os[i]);
            }
            return dataObject;
        } else {
            Object value = os[0];
            Type type = property.getType();
            if (!type.isDataType()) {
                return (DataObject) value;
            } else {
                return SDOUtil.createDataTypeWrapper(type, value);
            }
        }
    }
    
// ---    

    public static DataObject toDataObject(ClassLoader classLoader, TypeHelper typeHelper, Object[] os, QName elementQName, boolean isWrapped) {
        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        try {
            if (tccl != classLoader) {
                Thread.currentThread().setContextClassLoader(classLoader);
            }

            return toDataObject(typeHelper, os, elementQName, isWrapped);

        } finally {
            if (tccl != classLoader) {
                Thread.currentThread().setContextClassLoader(tccl);
            }
        }
    }

    public static DataObject toDataObject(ClassLoader classLoader, TypeHelper typeHelper, byte[] xmlBytes) {
        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        try {
            if (tccl != classLoader) {
                Thread.currentThread().setContextClassLoader(classLoader);
            }

            return toDataObject(typeHelper, xmlBytes);

        } finally {
            if (tccl != classLoader) {
                Thread.currentThread().setContextClassLoader(tccl);
            }
        }
    }
    
    public static byte[] toXMLbytes(ClassLoader classLoader, TypeHelper typeHelper, DataObject dataObject, QName elementQName) {
        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        try {
            if (tccl != classLoader) {
                Thread.currentThread().setContextClassLoader(classLoader);
            }

            return toXMLbytes(typeHelper, dataObject, elementQName);

        } finally {
            if (tccl != classLoader) {
                Thread.currentThread().setContextClassLoader(tccl);
            }
        }
    }
    
    public static byte[] toXMLBytes(ClassLoader classLoader, TypeHelper typeHelper, Object[] os, QName elementQName, boolean isWrapped) {
        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        try {
            if (tccl != classLoader) {
                Thread.currentThread().setContextClassLoader(classLoader);
            }

            return toXMLBytes(typeHelper, os, elementQName, isWrapped);

        } finally {
            if (tccl != classLoader) {
                Thread.currentThread().setContextClassLoader(tccl);
            }
        }
    }

    public static Object[] toObjects(ClassLoader classLoader, TypeHelper typeHelper, byte[] xmlBytes, boolean isWrapped) {
        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        try {
            if (tccl != classLoader) {
                Thread.currentThread().setContextClassLoader(classLoader);
            }

            return toObjects(typeHelper, xmlBytes, isWrapped);

        } finally {
            if (tccl != classLoader) {
                Thread.currentThread().setContextClassLoader(tccl);
            }
        }
    }
}