2121
2222import org .apache .axiom .om .OMElement ;
2323import org .apache .axiom .om .OMXMLBuilderFactory ;
24- import org .apache .axiom .om .OMXMLParserWrapper ;
2524import org .apache .xmlbeans .XmlObject ;
2625import org .mozilla .javascript .Context ;
2726import org .mozilla .javascript .Scriptable ;
2827import org .mozilla .javascript .ScriptableObject ;
2928import org .mozilla .javascript .Wrapper ;
3029import org .mozilla .javascript .xml .XMLObject ;
3130
32- import java .io .StringReader ;
33-
3431/**
3532 * JSObjectConvertor converts between OMElements and JavaScript E4X XML objects
3633 */
3734public 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