21
21
22
22
import org .apache .axiom .om .OMElement ;
23
23
import org .apache .axiom .om .OMXMLBuilderFactory ;
24
- import org .apache .axiom .om .OMXMLParserWrapper ;
25
24
import org .apache .xmlbeans .XmlObject ;
26
25
import org .mozilla .javascript .Context ;
27
26
import org .mozilla .javascript .Scriptable ;
28
27
import org .mozilla .javascript .ScriptableObject ;
29
28
import org .mozilla .javascript .Wrapper ;
30
29
import org .mozilla .javascript .xml .XMLObject ;
31
30
32
- import java .io .StringReader ;
33
-
34
31
/**
35
32
* JSObjectConvertor converts between OMElements and JavaScript E4X XML objects
36
33
*/
37
34
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
+ }
38
42
39
- protected Scriptable scope ;
40
-
41
- public JSOMElementConvertor () {
42
43
Context cx = Context .enter ();
43
44
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 );
45
51
} finally {
46
52
Context .exit ();
47
53
}
48
54
}
49
55
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
-
71
56
public OMElement fromScript (Object o ) {
72
57
if (!(o instanceof XMLObject ) && !(o instanceof Wrapper )) {
73
58
return super .fromScript (o );
@@ -83,7 +68,7 @@ public OMElement fromScript(Object o) {
83
68
xmlObject = (XmlObject ) unwrapped ;
84
69
}
85
70
}
86
-
71
+
87
72
// If we have an XMLObject but not a wrapped XmlObject, try the old approach
88
73
if (xmlObject == null && o instanceof XMLObject ) {
89
74
// 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) {
105
90
.createOMBuilder (new java .io .StringReader (normalizedXML ))
106
91
.getDocumentElement ();
107
92
}
108
-
93
+
109
94
if (xmlObject != null ) {
110
95
return OMXMLBuilderFactory
111
96
.createOMBuilder (xmlObject .newInputStream ())
0 commit comments