01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.scripting.convertors;
21:
22: import javax.xml.stream.XMLStreamException;
23:
24: import org.apache.axiom.om.OMElement;
25: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
26: import org.apache.xmlbeans.XmlObject;
27: import org.mozilla.javascript.Context;
28: import org.mozilla.javascript.Scriptable;
29: import org.mozilla.javascript.ScriptableObject;
30: import org.mozilla.javascript.Wrapper;
31: import org.mozilla.javascript.xml.XMLObject;
32:
33: /**
34: * JSObjectConvertor converts between OMElements and JavaScript E4X XML objects
35: */
36: public class JSOMElementConvertor extends DefaultOMElementConvertor {
37:
38: protected Scriptable scope;
39:
40: public JSOMElementConvertor() {
41: Context cx = Context.enter();
42: try {
43: this .scope = cx.initStandardObjects();
44: } finally {
45: Context.exit();
46: }
47: }
48:
49: public Object toScript(OMElement o) {
50: XmlObject xml;
51: try {
52: xml = XmlObject.Factory.parse(o.getXMLStreamReader());
53: } catch (Exception e) {
54: throw new RuntimeException(
55: "exception getting message XML: " + e);
56: }
57:
58: Context cx = Context.enter();
59: try {
60:
61: Object wrappedXML = cx.getWrapFactory().wrap(cx, scope,
62: xml, XmlObject.class);
63: Scriptable jsXML = cx.newObject(scope, "XML",
64: new Object[] { wrappedXML });
65:
66: return jsXML;
67:
68: } finally {
69: Context.exit();
70: }
71: }
72:
73: public OMElement fromScript(Object o) {
74: if (!(o instanceof XMLObject)) {
75: return super .fromScript(o);
76: }
77:
78: // TODO: E4X Bug? Shouldn't need this copy, but without it the outer element gets lost. See Mozilla bugzilla 361722
79: Scriptable jsXML = (Scriptable) ScriptableObject.callMethod(
80: (Scriptable) o, "copy", new Object[0]);
81: Wrapper wrapper = (Wrapper) ScriptableObject.callMethod(
82: (XMLObject) jsXML, "getXmlObject", new Object[0]);
83: XmlObject xmlObject = (XmlObject) wrapper.unwrap();
84: try {
85:
86: StAXOMBuilder builder = new StAXOMBuilder(xmlObject
87: .newInputStream());
88: OMElement omElement = builder.getDocumentElement();
89:
90: return omElement;
91:
92: } catch (XMLStreamException e) {
93: throw new RuntimeException(e);
94: }
95: }
96:
97: }
|