jakarta-bsf-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From antel...@apache.org
Subject svn commit: r589348 - in /jakarta/bsf/trunk/bsf3/bsf-utils: ./ src/main/java/org/apache/bsf/xml/
Date Sun, 28 Oct 2007 13:12:58 GMT
Author: antelder
Date: Sun Oct 28 06:12:57 2007
New Revision: 589348

URL: http://svn.apache.org/viewvc?rev=589348&view=rev
Log:
Update XMLHelpers to use Axiom E4X impl if its available

Added:
    jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XAxiomHelper.java
  (with props)
    jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XXmlBeansHelper.java
  (with props)
Modified:
    jakarta/bsf/trunk/bsf3/bsf-utils/pom.xml
    jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XHelper.java
    jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/XMLHelper.java

Modified: jakarta/bsf/trunk/bsf3/bsf-utils/pom.xml
URL: http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/pom.xml?rev=589348&r1=589347&r2=589348&view=diff
==============================================================================
--- jakarta/bsf/trunk/bsf3/bsf-utils/pom.xml (original)
+++ jakarta/bsf/trunk/bsf3/bsf-utils/pom.xml Sun Oct 28 06:12:57 2007
@@ -72,7 +72,7 @@
         <dependency>
             <groupId>rhino</groupId>
             <artifactId>js</artifactId>
-            <version>1.6R5</version>
+            <version>1.6R6</version>
             <scope>compile</scope>
         </dependency>
         <dependency>

Added: jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XAxiomHelper.java
URL: http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XAxiomHelper.java?rev=589348&view=auto
==============================================================================
--- jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XAxiomHelper.java
(added)
+++ jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XAxiomHelper.java
Sun Oct 28 06:12:57 2007
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.bsf.xml;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+
+import org.apache.axiom.om.OMElement;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ContextFactory;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.xml.XMLLib;
+import org.mozilla.javascript.xml.XMLObject;
+
+/**
+ * XMLHelper for JavaScript E4X using Axiom
+ */
+public class JavaScriptE4XAxiomHelper extends DefaultXMLHelper {
+
+	private Scriptable scope;
+
+    class AxiomE4XContextFactory extends ContextFactory {
+
+        protected XMLLib.Factory getE4xImplementationFactory() {
+            return org.mozilla.javascript.xml.XMLLib.Factory.create(
+                    "org.wso2.javascript.xmlimpl.XMLLibImpl"
+            );
+        }
+    }
+
+    JavaScriptE4XAxiomHelper(ScriptEngine engine) {
+
+    	// tell Rhino to use Axiom E4X impl
+        if (!ContextFactory.hasExplicitGlobal()) {
+	     	ContextFactory.initGlobal(new AxiomE4XContextFactory());
+        }
+
+    	Context cx = Context.enter();
+	    try {
+
+	    	this.scope = cx.initStandardObjects();
+
+	    } finally {
+	        Context.exit();
+	    }
+	}
+
+	public OMElement toOMElement(Object scriptXML) throws ScriptException {
+        if (scriptXML == null) {
+        	return null;
+        }
+
+        if (!(scriptXML instanceof XMLObject)) {
+            return null;
+        }
+
+        return (OMElement) ScriptableObject.callMethod( (Scriptable) scriptXML, "getXmlObject",
new Object[0]);
+	}
+
+	public Object toScriptXML(OMElement om) throws ScriptException {
+        if (om == null) {
+        	return null;
+        }
+        Context cx = Context.enter();
+        try {
+
+           return cx.newObject(scope, "XML", new Object[]{om});
+
+        } finally {
+            Context.exit();
+        }
+	}
+
+}

Propchange: jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XAxiomHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XAxiomHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XHelper.java
URL: http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XHelper.java?rev=589348&r1=589347&r2=589348&view=diff
==============================================================================
--- jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XHelper.java
(original)
+++ jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XHelper.java
Sun Oct 28 06:12:57 2007
@@ -19,99 +19,21 @@
 package org.apache.bsf.xml;
 
 import javax.script.ScriptEngine;
-import javax.script.ScriptException;
-import javax.xml.stream.XMLStreamException;
-
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.impl.builder.StAXOMBuilder;
-import org.apache.xmlbeans.XmlObject;
-import org.mozilla.javascript.Context;
-import org.mozilla.javascript.Scriptable;
-import org.mozilla.javascript.ScriptableObject;
-import org.mozilla.javascript.Wrapper;
-import org.mozilla.javascript.xml.XMLObject;
 
 /**
  * XMLHelper for JavaScript E4X
  */
 public class JavaScriptE4XHelper extends DefaultXMLHelper {
 
-	private Scriptable scope;
-
-    private static boolean axiomE4XImpl = false;
-    static {
-        try {
-            Class.forName("org.mozilla.javascript.xmlimpl.AxiomNode");
-            axiomE4XImpl = true;
-        } catch (ClassNotFoundException ignore) {}
-    }
-
-    JavaScriptE4XHelper(ScriptEngine engine) {
-	    Context cx = Context.enter();
-	    try {
-	        this.scope = cx.initStandardObjects();
-	    } finally {
-	        Context.exit();
-	    }
-	}
-
-	public OMElement toOMElement(Object scriptXML) throws ScriptException {
-        if (scriptXML == null) {
-        	return null;
-        }
-
-        if (!(scriptXML instanceof XMLObject)) {
-            return null;
-        }
-
-        if (axiomE4XImpl) {
-            return (OMElement) ScriptableObject.callMethod(
-                (Scriptable) scriptXML, "getXmlObject", new Object[0]);
-
-        } else {
-            // TODO: E4X Bug? Shouldn't need this copy, but without it the outer element
gets lost???
-            Scriptable jsXML =
-                (Scriptable) ScriptableObject.callMethod((Scriptable) scriptXML, "copy",
new Object[0]);
-            Wrapper wrapper =
-                (Wrapper) ScriptableObject.callMethod((XMLObject)jsXML, "getXmlObject", new
Object[0]);
-
-            XmlObject xmlObject = (XmlObject) wrapper.unwrap();
-
-            try {
-                StAXOMBuilder builder = new StAXOMBuilder(xmlObject.newInputStream());
-                return builder.getDocumentElement();
-
-            } catch (XMLStreamException e) {
-            	throw new ScriptException(e);
-            }
-        }
-	}
-
-	public Object toScriptXML(OMElement om) throws ScriptException {
-        if (om == null) {
-        	return null;
-        }
-        Context cx = Context.enter();
-        try {
-        	if (axiomE4XImpl) {
-
-        		return cx.newObject(scope, "XML", new Object[]{om});
-
-        	} else {
-
-        		XmlObject xml = null;
-                try {
-                    xml = XmlObject.Factory.parse(om.getXMLStreamReader());
-                } catch (Exception e) {
-                	throw new ScriptException(e);
-                }
-                Object wrappedXML = cx.getWrapFactory().wrap(cx, scope, xml, XmlObject.class);
-                return cx.newObject(scope, "XML", new Object[]{wrappedXML});
+	public static XMLHelper getXMLHelper(ScriptEngine engine) {
 
-        	}
-        } finally {
-            Context.exit();
-        }
+		try {
+			Class.forName("org.wso2.javascript.xmlimpl.XMLLibImpl", true, JavaScriptE4XHelper.class.getClassLoader());
+			return new JavaScriptE4XAxiomHelper(engine);
+		} catch (ClassNotFoundException e) {
+			// TODO: support Rhino 1.6R7 DOM based E4X impl 
+			return new JavaScriptE4XXmlBeansHelper(engine);
+		}
 	}
 
 }

Added: jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XXmlBeansHelper.java
URL: http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XXmlBeansHelper.java?rev=589348&view=auto
==============================================================================
--- jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XXmlBeansHelper.java
(added)
+++ jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XXmlBeansHelper.java
Sun Oct 28 06:12:57 2007
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.bsf.xml;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.xmlbeans.XmlObject;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.Wrapper;
+import org.mozilla.javascript.xml.XMLObject;
+
+/**
+ * XMLHelper for JavaScript E4X using XmlBeans
+ */
+public class JavaScriptE4XXmlBeansHelper extends DefaultXMLHelper {
+
+	private Scriptable scope;
+
+    JavaScriptE4XXmlBeansHelper(ScriptEngine engine) {
+	    Context cx = Context.enter();
+	    try {
+	        this.scope = cx.initStandardObjects();
+	    } finally {
+	        Context.exit();
+	    }
+	}
+
+	public OMElement toOMElement(Object scriptXML) throws ScriptException {
+        if (scriptXML == null) {
+        	return null;
+        }
+
+        if (!(scriptXML instanceof XMLObject)) {
+            return null;
+        }
+
+        // TODO: E4X Bug? Shouldn't need this copy, but without it the outer element gets
lost???
+        Scriptable jsXML =
+            (Scriptable) ScriptableObject.callMethod((Scriptable) scriptXML, "copy", new
Object[0]);
+        Wrapper wrapper =
+            (Wrapper) ScriptableObject.callMethod((XMLObject)jsXML, "getXmlObject", new Object[0]);
+
+        XmlObject xmlObject = (XmlObject) wrapper.unwrap();
+
+        try {
+            StAXOMBuilder builder = new StAXOMBuilder(xmlObject.newInputStream());
+            return builder.getDocumentElement();
+
+        } catch (XMLStreamException e) {
+        	throw new ScriptException(e);
+        }
+	}
+
+	public Object toScriptXML(OMElement om) throws ScriptException {
+        if (om == null) {
+        	return null;
+        }
+        Context cx = Context.enter();
+        try {
+
+    		XmlObject xml = null;
+            try {
+                xml = XmlObject.Factory.parse(om.getXMLStreamReader());
+            } catch (Exception e) {
+            	throw new ScriptException(e);
+            }
+            Object wrappedXML = cx.getWrapFactory().wrap(cx, scope, xml, XmlObject.class);
+            return cx.newObject(scope, "XML", new Object[]{wrappedXML});
+
+        } finally {
+            Context.exit();
+        }
+	}
+
+}

Propchange: jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XXmlBeansHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XXmlBeansHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/XMLHelper.java
URL: http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/XMLHelper.java?rev=589348&r1=589347&r2=589348&view=diff
==============================================================================
--- jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/XMLHelper.java (original)
+++ jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/XMLHelper.java Sun Oct
28 06:12:57 2007
@@ -39,14 +39,14 @@
 		}
 		String language = engine.getFactory().getLanguageName();
 		if ("ECMAScript".endsWith(language)) {
-			return new JavaScriptE4XHelper(engine);
+			return JavaScriptE4XHelper.getXMLHelper(engine);
 		} else if ("ruby".endsWith(language)) {
 			return new JRubyReXMLHelper(engine);
 		} else {
 			return new DefaultXMLHelper();
 		}
 	}
-
+	
 	public abstract Object toScriptXML(OMElement om) throws ScriptException;
 	public abstract OMElement toOMElement(Object scriptXML) throws ScriptException;
 



---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org


Mime
View raw message