Author: antelder
Date: Mon Mar 19 16:52:38 2007
New Revision: 520184

URL: http://svn.apache.org/viewvc?view=rev&rev=520184
Log:
Add XMLHelper function to support converting btw Java XML representations like 
StAX or Axiom into script language representatiions such as E4X or ReXML

Added:
    
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/utils/http/ScriptEnginePool.java
      - copied, changed from r516699, 
jakarta/bsf/trunk/bsf3/old/src/org/apache/bsf/util/ScriptEnginePool.java
    jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/
    
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/DefaultXMLHelper.java
   (with props)
    
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JRubyReXMLHelper.java
   (with props)
    
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XHelper.java
   (with props)
    
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/XMLHelper.java
   (with props)
    jakarta/bsf/trunk/bsf3/bsf-utils/src/test/java/org/
    jakarta/bsf/trunk/bsf3/bsf-utils/src/test/java/org/apache/
    jakarta/bsf/trunk/bsf3/bsf-utils/src/test/java/org/apache/bsf/
    jakarta/bsf/trunk/bsf3/bsf-utils/src/test/java/org/apache/bsf/xml/
    
jakarta/bsf/trunk/bsf3/bsf-utils/src/test/java/org/apache/bsf/xml/JavaScriptXMLHelperTestCase.java
   (with props)
Removed:
    jakarta/bsf/trunk/bsf3/testing/e4x/src/test/java/org/apache/bsf/e4x/
Modified:
    jakarta/bsf/trunk/bsf3/bsf-utils/pom.xml
    
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/utils/http/HttpScriptServlet.java
    jakarta/bsf/trunk/bsf3/pom.xml
    jakarta/bsf/trunk/bsf3/testing/e4x/pom.xml
    
jakarta/bsf/trunk/bsf3/testing/e4x/src/test/java/org/apache/bsf/testing/e4x/HelloTestCase.java

Modified: jakarta/bsf/trunk/bsf3/bsf-utils/pom.xml
URL: 
http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/pom.xml?view=diff&rev=520184&r1=520183&r2=520184
==============================================================================
--- jakarta/bsf/trunk/bsf3/bsf-utils/pom.xml (original)
+++ jakarta/bsf/trunk/bsf3/bsf-utils/pom.xml Mon Mar 19 16:52:38 2007
@@ -38,6 +38,49 @@
           <version>3.0-beta1-SNAPSHOT</version>
           <scope>compile</scope>
        </dependency>
+      <dependency>
+          <groupId>org.apache.bsf</groupId>
+          <artifactId>bsf-engines</artifactId>
+          <version>3.0-beta1-SNAPSHOT</version>
+          <scope>compile</scope>
+       </dependency>
+
+      <dependency>
+          <groupId>org.apache.ws.commons.axiom</groupId>
+          <artifactId>axiom-api</artifactId>
+          <version>1.2.2</version>
+          <scope>compile</scope>
+      </dependency>
+      <dependency>
+          <groupId>org.apache.ws.commons.axiom</groupId>
+          <artifactId>axiom-impl</artifactId>
+          <version>1.2.2</version>
+          <scope>compile</scope>
+      </dependency>
+        <dependency>
+            <groupId>stax</groupId>
+            <artifactId>stax-api</artifactId>
+            <version>1.0.1</version>
+          <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.codehaus.woodstox</groupId>
+            <artifactId>wstx</artifactId>
+            <version>asl-3.2.0</version>
+          <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>rhino</groupId>
+            <artifactId>js</artifactId>
+            <version>1.6R5</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>xmlbeans</groupId>
+            <artifactId>xbean</artifactId>
+            <version>2.2.0</version>
+            <scope>compile</scope>
+        </dependency>
 
       <dependency>
           <groupId>javax.servlet</groupId>

Modified: 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/utils/http/HttpScriptServlet.java
URL: 
http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/utils/http/HttpScriptServlet.java?view=diff&rev=520184&r1=520183&r2=520184
==============================================================================
--- 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/utils/http/HttpScriptServlet.java
 (original)
+++ 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/utils/http/HttpScriptServlet.java
 Mon Mar 19 16:52:38 2007
@@ -41,9 +41,10 @@
  * @author Nandika Jayawardana <[EMAIL PROTECTED]>
  * @author Sanka Samaranayake  <[EMAIL PROTECTED]>
  */
-public abstract class HttpScriptServlet extends GenericServlet {
-    
-    public HttpScriptServlet(){
+public class HttpScriptServlet extends GenericServlet {
+       private static final long serialVersionUID = 1L;
+
+       public HttpScriptServlet(){
     }
    
        /**
@@ -56,8 +57,11 @@
         * @return an initialized HttpScriptContext
         * @throws ServletException if an error occurs
         */
-    public abstract HttpScriptContext getContext(HttpServletRequest req,
-                       HttpServletResponse res) throws ServletException;
+    public HttpScriptContext getContext(HttpServletRequest req,
+                       HttpServletResponse res) throws ServletException {
+       
+       return null; // TODO
+    }
        
     /**
      * Returns a ScriptEngine instance which is used by the 
@@ -67,7 +71,9 @@
      * @return an instance of a ScriptEngine which is used by the 
      *         HttpScriptServlet to executes a single request
      */
-       public abstract ScriptEngine getEngine(HttpServletRequest req);
+       public ScriptEngine getEngine(HttpServletRequest req) {
+       return null; // TODO            
+       }
        
     /**
      * Called to indicate that the ScriptEngine returned by call to
@@ -75,7 +81,9 @@
      * 
      * @param eng the ScriptEngine which is no longer in use
      */
-    public abstract void releaseEngine(ScriptEngine eng);
+    public void releaseEngine(ScriptEngine eng) {
+       
+    }
     
     /**
      * Executes a script using the HttpScriptContext obtained by a 

Copied: 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/utils/http/ScriptEnginePool.java
 (from r516699, 
jakarta/bsf/trunk/bsf3/old/src/org/apache/bsf/util/ScriptEnginePool.java)
URL: 
http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/utils/http/ScriptEnginePool.java?view=diff&rev=520184&p1=jakarta/bsf/trunk/bsf3/old/src/org/apache/bsf/util/ScriptEnginePool.java&r1=516699&p2=jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/utils/http/ScriptEnginePool.java&r2=520184
==============================================================================
--- jakarta/bsf/trunk/bsf3/old/src/org/apache/bsf/util/ScriptEnginePool.java 
(original)
+++ 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/utils/http/ScriptEnginePool.java
 Mon Mar 19 16:52:38 2007
@@ -1,5 +1,23 @@
+/*
+ * 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.util;
+package org.apache.bsf.utils.http;
 
 import java.util.LinkedList;
 

Added: 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/DefaultXMLHelper.java
URL: 
http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/DefaultXMLHelper.java?view=auto&rev=520184
==============================================================================
--- 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/DefaultXMLHelper.java
 (added)
+++ 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/DefaultXMLHelper.java
 Mon Mar 19 16:52:38 2007
@@ -0,0 +1,69 @@
+/*
+ * 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 java.io.ByteArrayInputStream;
+
+import javax.script.ScriptException;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.w3c.dom.Node;
+
+/**
+ * Default XMLHelper for when no specific XMLHelper is implemented
+ * for a script engine. Just converts XML to text Strings.
+ */
+public class DefaultXMLHelper extends XMLHelper {
+
+       public OMElement toOMElement(Object scriptXML) throws ScriptException {
+        try {
+
+               StAXOMBuilder builder = new StAXOMBuilder(
+                new ByteArrayInputStream(scriptXML.toString().getBytes()));
+            return builder.getDocumentElement();
+
+        } catch (XMLStreamException e) {
+               throw new ScriptException(e);
+        }
+       }
+
+       public Object toScriptXML(OMElement om) throws ScriptException {
+        return om.toString();
+       }
+
+       public Node toDOMNode(Object scriptXML) throws ScriptException {
+               throw new UnsupportedOperationException("not implemented yet");
+       }
+
+       public Object toScriptXML(XMLStreamReader reader) throws 
ScriptException {
+               throw new UnsupportedOperationException("not implemented yet");
+       }
+
+       public Object toScriptXML(Node node) throws ScriptException {
+               throw new UnsupportedOperationException("not implemented yet");
+       }
+
+       public XMLStreamReader toXMLStreamReader(Object scriptXML) throws 
ScriptException {
+               throw new UnsupportedOperationException("not implemented yet");
+       }
+
+}

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

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

Added: 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JRubyReXMLHelper.java
URL: 
http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JRubyReXMLHelper.java?view=auto&rev=520184
==============================================================================
--- 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JRubyReXMLHelper.java
 (added)
+++ 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JRubyReXMLHelper.java
 Mon Mar 19 16:52:38 2007
@@ -0,0 +1,68 @@
+/*
+ * 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 java.io.ByteArrayInputStream;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+
+/**
+ * XMLHelper for Ruby ReXML
+ * 
+ * TODO: Find a more efficent way to do this with JRuby
+ */
+public class JRubyReXMLHelper extends DefaultXMLHelper {
+
+       private ScriptEngine scriptEngine;
+
+       JRubyReXMLHelper(ScriptEngine scriptEngine) {
+               this.scriptEngine = scriptEngine;
+       }
+
+       public OMElement toOMElement(Object scriptXML) throws ScriptException {
+               if (scriptXML == null) {
+                       return null;
+               }
+               try {
+
+                       byte[] xmlBytes = scriptXML.toString().getBytes();
+                       StAXOMBuilder builder = new StAXOMBuilder(new 
ByteArrayInputStream(xmlBytes));
+                       return builder.getDocumentElement();
+
+               } catch (Exception e) {
+                       throw new ScriptException(e);
+               }
+       }
+
+       public Object toScriptXML(OMElement om) throws ScriptException {
+               if (om == null) {
+                       return null;
+               }
+               StringBuffer srcFragment = new 
StringBuffer("Document.new(<<EOF\n");
+               srcFragment.append(om.toString());
+               srcFragment.append("\nEOF\n");
+               srcFragment.append(")");
+               return scriptEngine.eval(srcFragment.toString());
+       }
+
+}

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

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

Added: 
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?view=auto&rev=520184
==============================================================================
--- 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XHelper.java
 (added)
+++ 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/JavaScriptE4XHelper.java
 Mon Mar 19 16:52:38 2007
@@ -0,0 +1,117 @@
+/*
+ * 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
+ */
+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});
+
+               }
+        } finally {
+            Context.exit();
+        }
+       }
+
+}

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

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

Added: 
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?view=auto&rev=520184
==============================================================================
--- 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/XMLHelper.java
 (added)
+++ 
jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/xml/XMLHelper.java
 Mon Mar 19 16:52:38 2007
@@ -0,0 +1,58 @@
+/*
+ * 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.XMLStreamReader;
+
+import org.apache.axiom.om.OMElement;
+import org.w3c.dom.Node;
+
+/**
+ * Utility for converting between Java representations of XML such DOM nodes,
+ * StAX streams, or Axiom OMElements, into script language representations such
+ * as JavaScript E4X, Ruby ReXML, or Python ElementTree.
+ */
+public abstract class XMLHelper {
+
+       public static XMLHelper getArgHelper(ScriptEngine engine) {
+               // TODO: better discovery mechanisim than hardcoded class names 
+               if (engine == null) {
+                       return null;
+               }
+               String language = engine.getFactory().getLanguageName();
+               if ("ECMAScript".endsWith(language)) {
+                       return new JavaScriptE4XHelper(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;
+
+       public abstract Object toScriptXML(XMLStreamReader reader) throws 
ScriptException;
+       public abstract XMLStreamReader toXMLStreamReader(Object scriptXML) 
throws ScriptException;
+
+       public abstract Object toScriptXML(Node node) throws ScriptException;
+       public abstract Node toDOMNode(Object scriptXML) throws ScriptException;
+}

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

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

Added: 
jakarta/bsf/trunk/bsf3/bsf-utils/src/test/java/org/apache/bsf/xml/JavaScriptXMLHelperTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/src/test/java/org/apache/bsf/xml/JavaScriptXMLHelperTestCase.java?view=auto&rev=520184
==============================================================================
--- 
jakarta/bsf/trunk/bsf3/bsf-utils/src/test/java/org/apache/bsf/xml/JavaScriptXMLHelperTestCase.java
 (added)
+++ 
jakarta/bsf/trunk/bsf3/bsf-utils/src/test/java/org/apache/bsf/xml/JavaScriptXMLHelperTestCase.java
 Mon Mar 19 16:52:38 2007
@@ -0,0 +1,65 @@
+package org.apache.bsf.xml;
+/*
+ * 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.    
+ */
+import javax.script.Bindings;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+import junit.framework.TestCase;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.bsf.xml.XMLHelper;
+import org.mozilla.javascript.xml.XMLObject;
+
+/**
+ * Tests for converting btw OMElement and E4X
+ */
+public class JavaScriptXMLHelperTestCase extends TestCase {
+
+       private ScriptEngine engine;
+
+       private XMLHelper xmlHelper;
+
+       public void testToOmElement() throws ScriptException {
+               Object scriptXML = engine.eval("<a><b>petra</b></a>");
+               assertTrue(scriptXML instanceof XMLObject);
+
+               OMElement om = xmlHelper.toOMElement(scriptXML);
+               assertNotNull(om);
+               assertEquals("<a><b>petra</b></a>", om.toString());
+       }
+
+       public void testToScriptXML() throws ScriptException {
+               OMElement om = 
xmlHelper.toOMElement(engine.eval("<a><b>petra</b></a>"));
+
+               Bindings bindings = engine.createBindings();
+               bindings.put("xml", xmlHelper.toScriptXML(om));
+
+               assertEquals("xml", engine.eval("typeof xml", bindings));
+               assertEquals("petra", engine.eval("xml.b.toString()", 
bindings));
+       }
+
+       public void setUp() {
+               ScriptEngineManager manager = new ScriptEngineManager();
+               engine = manager.getEngineByExtension("js");
+               xmlHelper = XMLHelper.getArgHelper(engine);
+       }
+
+}

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

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

Modified: jakarta/bsf/trunk/bsf3/pom.xml
URL: 
http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/pom.xml?view=diff&rev=520184&r1=520183&r2=520184
==============================================================================
--- jakarta/bsf/trunk/bsf3/pom.xml (original)
+++ jakarta/bsf/trunk/bsf3/pom.xml Mon Mar 19 16:52:38 2007
@@ -159,6 +159,8 @@
         <module>distribution</module>
         <module>testing/javascript</module>
         <module>testing/ruby</module>
+        <module>testing/python</module>
+        <module>testing/e4x</module>
     </modules>
 
 </project>

Modified: jakarta/bsf/trunk/bsf3/testing/e4x/pom.xml
URL: 
http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/testing/e4x/pom.xml?view=diff&rev=520184&r1=520183&r2=520184
==============================================================================
--- jakarta/bsf/trunk/bsf3/testing/e4x/pom.xml (original)
+++ jakarta/bsf/trunk/bsf3/testing/e4x/pom.xml Mon Mar 19 16:52:38 2007
@@ -62,6 +62,19 @@
         </dependency>
 
         <dependency>
+            <groupId>rhino</groupId>
+            <artifactId>js</artifactId>
+            <version>1.6R5</version>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>xmlbeans</groupId>
+            <artifactId>xbean</artifactId>
+            <version>2.2.0</version>
+            <scope>runtime</scope>
+        </dependency>
+<!-- 
+        <dependency>
             <groupId>org.mozilla.javascript</groupId>
             <artifactId>js-core</artifactId>
             <version>SNAPSHOT</version>
@@ -74,7 +87,7 @@
            <version>SNAPSHOT</version>
            <scope>runtime</scope>
         </dependency>
-
+-->
         <dependency>
             <groupId>org.apache.ws.commons.axiom</groupId>
             <artifactId>axiom-impl</artifactId>

Modified: 
jakarta/bsf/trunk/bsf3/testing/e4x/src/test/java/org/apache/bsf/testing/e4x/HelloTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/testing/e4x/src/test/java/org/apache/bsf/testing/e4x/HelloTestCase.java?view=diff&rev=520184&r1=520183&r2=520184
==============================================================================
--- 
jakarta/bsf/trunk/bsf3/testing/e4x/src/test/java/org/apache/bsf/testing/e4x/HelloTestCase.java
 (original)
+++ 
jakarta/bsf/trunk/bsf3/testing/e4x/src/test/java/org/apache/bsf/testing/e4x/HelloTestCase.java
 Mon Mar 19 16:52:38 2007
@@ -20,7 +20,7 @@
 
 import java.io.StringReader;
 
-import javax.script.Invocable;
+import javax.script.Bindings;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
@@ -32,42 +32,66 @@
 import junit.framework.TestCase;
 
 import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
-import org.apache.bsf.e4x.E4XHelper;
-import org.mozilla.javascript.xml.XMLObject;
+import org.apache.bsf.xml.XMLHelper;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ContextHelper;
 
 /**
  * Tests a basic JavaScrip/E4X invocation
  */
 public class HelloTestCase extends TestCase {
        
-       private E4XHelper e4xHelper;
+//     private ScriptEngine engine;
+//     private XMLHelper xmlHelper;
 
-       public void testInvokeFunction() throws ScriptException, 
XMLStreamException, FactoryConfigurationError {
-               ScriptEngineManager manager = new ScriptEngineManager();
-               ScriptEngine engine = manager.getEngineByExtension("js");
-               engine.eval("function hello(xml) { return <foo>{xml.b}</foo>; 
}" );
-               assertTrue(engine instanceof Invocable);
-               Invocable invocableScript = (Invocable) engine;
-
-               XMLObject xmlIn = 
e4xHelper.toE4X(createOm("<a><b>petra</b></a>"));
-
-               Object xmlOut = invocableScript.invokeFunction("hello", new 
Object[]{xmlIn});
-
-               OMNode omOut = e4xHelper.fromE4X((XMLObject) xmlOut);
-               assertEquals("<foo><b>petra</b></foo>", omOut.toString());
+//     public void testInvokeFunction() throws ScriptException, 
XMLStreamException, FactoryConfigurationError {
+//             engine.eval("function isXML(xml) { return typeof xml.b == xml; 
}" );
+//             engine.eval("function hello(xml) { return <foo>{xml.b}</foo>; 
}" );
+//             assertTrue(engine instanceof Invocable);
+//             Invocable invocableScript = (Invocable) engine;
+//
+//             Object xmlIn = 
xmlHelper.toScriptXML(createOm("<a><b>petra</b></a>"));
+//
+//             Object o = invocableScript.invokeFunction("isXML", new 
Object[]{xmlIn});
+//             assertTrue(o instanceof Boolean);
+//             assertTrue(((Boolean)o).booleanValue());
+//
+//             Object xmlOut = invocableScript.invokeFunction("hello", new 
Object[]{xmlIn});
+//
+//             OMElement omOut = xmlHelper.toOMElement(xmlOut);
+//             assertEquals("<foo><b>petra</b></foo>", omOut.toString());
+//     }
+       
+       public void testE4X() throws ScriptException, XMLStreamException, 
FactoryConfigurationError {
+        ScriptEngine engine = new 
ScriptEngineManager().getEngineByExtension("js");
+        XMLHelper convertor = XMLHelper.getArgHelper(engine);
+        Object o = 
convertor.toScriptXML(createOMElement("<a><b>petra</b></a>"));
+        OMElement om = convertor.toOMElement(o);
+        assertEquals("<a><b>petra</b></a>", om.toString());
+        
+        Bindings bindings = engine.createBindings();
+        bindings.put("o", o);
+        Object x = engine.eval("typeof o", bindings);
+        assertEquals("xml", x);
        }
        
-       protected OMElement createOm(String s) throws XMLStreamException, 
FactoryConfigurationError {
-               XMLStreamReader parser = 
-                       XMLInputFactory.newInstance().createXMLStreamReader(new 
StringReader(s));
+       protected OMElement createOMElement(String s) throws 
XMLStreamException, FactoryConfigurationError {
+               XMLStreamReader parser = 
XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(s));
                StAXOMBuilder builder = new StAXOMBuilder(parser);
                OMElement om = builder.getDocumentElement();
                return om;
        }
 
     protected void setUp() {
-       e4xHelper = new E4XHelper();
+//             ScriptEngineManager manager = new ScriptEngineManager();
+//             engine = manager.getEngineByExtension("js");
+//             xmlHelper = XMLHelper.getArgHelper(engine);
+        Context cx = Context.enter();
+        try {
+               ContextHelper.setTopCallScope(cx, cx.initStandardObjects());
+        } finally {
+            Context.exit();
+        }
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to