Skip to content

Commit 52303e7

Browse files
committed
scripting: assume XMLBeans 5.x, replace call to getXmlObject with toXMLString
Removes field scope from class JSOMElementConvertor. This field was created within an enter/exit block in the constructor, but after the exit block, its state was invalid. Reduces try/catch scope when parsing xml object.
1 parent aa14469 commit 52303e7

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

modules/scripting/src/org/apache/axis2/scripting/convertors/JSOMElementConvertor.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,38 @@
2121

2222
import org.apache.axiom.om.OMElement;
2323
import org.apache.axiom.om.OMXMLBuilderFactory;
24-
import org.apache.axiom.om.OMXMLParserWrapper;
2524
import org.apache.xmlbeans.XmlObject;
2625
import org.mozilla.javascript.Context;
2726
import org.mozilla.javascript.Scriptable;
2827
import org.mozilla.javascript.ScriptableObject;
2928
import org.mozilla.javascript.Wrapper;
3029
import org.mozilla.javascript.xml.XMLObject;
3130

32-
import java.io.StringReader;
33-
3431
/**
3532
* JSObjectConvertor converts between OMElements and JavaScript E4X XML objects
3633
*/
3734
public class JSOMElementConvertor extends DefaultOMElementConvertor {
35+
public Object toScript(OMElement o) {
36+
XmlObject xml;
37+
try {
38+
xml = XmlObject.Factory.parse(o.getXMLStreamReader());
39+
} catch (Exception e) {
40+
throw new RuntimeException("exception getting message XML: " + e);
41+
}
3842

39-
protected Scriptable scope;
40-
41-
public JSOMElementConvertor() {
4243
Context cx = Context.enter();
4344
try {
44-
this.scope = cx.initStandardObjects();
45+
// Enable E4X support
46+
cx.setLanguageVersion(Context.VERSION_1_6);
47+
Scriptable tempScope = cx.initStandardObjects();
48+
49+
// Wrap the XmlObject directly
50+
return cx.getWrapFactory().wrap(cx, tempScope, xml, XmlObject.class);
4551
} finally {
4652
Context.exit();
4753
}
4854
}
4955

50-
public Object toScript(OMElement o) {
51-
try {
52-
XmlObject xml = XmlObject.Factory.parse(o.getXMLStreamReader());
53-
54-
Context cx = Context.enter();
55-
try {
56-
// Enable E4X support
57-
cx.setLanguageVersion(Context.VERSION_1_6);
58-
Scriptable tempScope = cx.initStandardObjects();
59-
60-
// Wrap the XmlObject directly
61-
return cx.getWrapFactory().wrap(cx, tempScope, xml, XmlObject.class);
62-
63-
} finally {
64-
Context.exit();
65-
}
66-
} catch (Exception e) {
67-
throw new RuntimeException("exception getting message XML: " + e);
68-
}
69-
}
70-
7156
public OMElement fromScript(Object o) {
7257
if (!(o instanceof XMLObject) && !(o instanceof Wrapper)) {
7358
return super.fromScript(o);
@@ -83,7 +68,7 @@ public OMElement fromScript(Object o) {
8368
xmlObject = (XmlObject) unwrapped;
8469
}
8570
}
86-
71+
8772
// If we have an XMLObject but not a wrapped XmlObject, try the old approach
8873
if (xmlObject == null && o instanceof XMLObject) {
8974
// TODO: E4X Bug? Shouldn't need this copy, but without it the outer element gets lost. See Mozilla bugzilla 361722
@@ -105,7 +90,7 @@ public OMElement fromScript(Object o) {
10590
.createOMBuilder(new java.io.StringReader(normalizedXML))
10691
.getDocumentElement();
10792
}
108-
93+
10994
if (xmlObject != null) {
11095
return OMXMLBuilderFactory
11196
.createOMBuilder(xmlObject.newInputStream())

0 commit comments

Comments
 (0)