cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From coli...@apache.org
Subject cvs commit: xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript ScriptablePointer.java ScriptablePointerFactory.java ScriptablePropertyHandler.java ScriptablePropertyPointer.java JSCocoon.java JavaScriptInterpreter.java
Date Thu, 20 Feb 2003 18:22:43 GMT
coliver     2003/02/20 10:22:43

  Modified:    src/java/org/apache/cocoon/components/flow/javascript
                        JSCocoon.java JavaScriptInterpreter.java
  Added:       src/java/org/apache/cocoon/components/flow/javascript
                        ScriptablePointer.java
                        ScriptablePointerFactory.java
                        ScriptablePropertyHandler.java
                        ScriptablePropertyPointer.java
  Log:
  Added support for using Rhino objects with JXPath
  
  Revision  Changes    Path
  1.15      +11 -8     xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/JSCocoon.java
  
  Index: JSCocoon.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/JSCocoon.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- JSCocoon.java	31 Jan 2003 22:51:24 -0000	1.14
  +++ JSCocoon.java	20 Feb 2003 18:22:43 -0000	1.15
  @@ -203,7 +203,7 @@
     public void jsFunction_forwardTo(String uri, Object bizData, Object cont)
       throws Exception
     {
  -    bizData = jsobjectToObject((Scriptable)bizData);
  +    bizData = jsobjectToObject(bizData);
       
       WebContinuation kont = null;
   
  @@ -274,13 +274,16 @@
       return hash;
     }
   
  -    public static Object jsobjectToObject(Object obj) {
  -        if (obj instanceof Wrapper)
  -            obj = ((Wrapper) obj).unwrap();
  -        else if (obj instanceof Scriptable)
  -            obj = jsobjectToMap((Scriptable)obj);
  -        return obj;
  -    }
  +  public static Object jsobjectToObject(Object obj) 
  +  {
  +      // unwrap Scriptable wrappers of real Java objects
  +      if (obj instanceof Wrapper) {
  +	  obj = ((Wrapper) obj).unwrap();
  +      } else if (obj == Undefined.instance) {
  +	  obj = null;
  +      }
  +      return obj;
  +  }
   
       public Scriptable jsFunction_callAction(String type,
                                               String source,
  
  
  
  1.17      +13 -4     xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/JavaScriptInterpreter.java
  
  Index: JavaScriptInterpreter.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/JavaScriptInterpreter.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- JavaScriptInterpreter.java	31 Jan 2003 22:51:24 -0000	1.16
  +++ JavaScriptInterpreter.java	20 Feb 2003 18:22:43 -0000	1.17
  @@ -75,8 +75,9 @@
   import org.mozilla.javascript.ScriptRuntime;
   import org.mozilla.javascript.Scriptable;
   import org.mozilla.javascript.ScriptableObject;
  +import org.mozilla.javascript.JavaScriptException;
   import org.mozilla.javascript.tools.debugger.ScopeProvider;
  -
  +import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
   /**
    * Interface with the JavaScript interpreter.
    *
  @@ -145,13 +146,15 @@
           new Runnable() { public void run() { db.setVisible(false); } }
         );
         db.setVisible(true);
  -
         debugger = db;
         Context.addContextListener(debugger);
  +      debugger.doBreak();
       }
   
       Context context = Context.enter();
       context.setOptimizationLevel(OPTIMIZATION_LEVEL);
  +    // add support for Rhino objects to JXPath
  +    JXPathContextReferenceImpl.addNodePointerFactory(new ScriptablePointerFactory());
   
       try {
         scope = new JSGlobal(context);
  @@ -476,7 +479,6 @@
               new ScopeProvider()
               { public Scriptable getScope() {return s;} }
           );
  -        debugger.doBreak();
           if (!debugger.isVisible())
               debugger.setVisible(true);
         }
  @@ -509,6 +511,14 @@
         Object callFunArgs[] = { fun, funArgsArray };
         ((Function) callFunction).call(context, thrScope, thrScope, callFunArgs);
       }
  +    catch (JavaScriptException ex) {
  +      ex.printStackTrace();
  +      Object value = ex.getValue();
  +      if (value instanceof Exception) {
  +	  throw (Exception)value;
  +      }
  +      throw ex;
  +    }
       catch (Exception ex) {
         ex.printStackTrace();
         throw ex;
  @@ -549,7 +559,6 @@
           new ScopeProvider()
             { public Scriptable getScope() {return kScope;} }
         );
  -      debugger.doBreak();
         if (!debugger.isVisible())
             debugger.setVisible(true);
       }
  
  
  
  1.1                  xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ScriptablePointer.java
  
  Index: ScriptablePointer.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
  */
  package org.apache.cocoon.components.flow.javascript;
  import org.mozilla.javascript.*;
  import org.apache.commons.jxpath.ri.QName;
  import org.apache.commons.jxpath.*;
  import org.apache.commons.jxpath.ri.model.beans.*;
  import org.apache.commons.jxpath.ri.model.*;
  import java.util.Locale;
  
  public class ScriptablePointer extends DynamicPointer {
  
      Scriptable node;
  
      final static ScriptablePropertyHandler handler = 
          new ScriptablePropertyHandler();
  
      public ScriptablePointer(NodePointer parent,
                               QName name,
                               Scriptable object) {
          super(parent, name, object, handler);
          node = object;
      }
  
      public ScriptablePointer(QName name,
                               Scriptable object,
                               Locale locale) {
          super(name, object, handler, locale);
          node = object;
      }
  
      public int getLength() {
          if (ScriptableObject.hasProperty(node, "length")) {
              Object val = ScriptableObject.getProperty(node, "length");
              if (val instanceof Number) {
                  return ((Number)val).intValue();
              }
          }
          return super.getLength();
      }
  
      public PropertyPointer getPropertyPointer(){
          return new ScriptablePropertyPointer(this, handler);
      }
  
  }
  
  
  
  1.1                  xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ScriptablePointerFactory.java
  
  Index: ScriptablePointerFactory.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
  */
  package org.apache.cocoon.components.flow.javascript;
  import org.apache.commons.jxpath.ri.model.NodePointerFactory;
  import org.apache.commons.jxpath.ri.model.NodePointer;
  import org.apache.commons.jxpath.ri.QName;
  import java.util.Locale;
  import org.mozilla.javascript.Scriptable;
  
  public class ScriptablePointerFactory implements NodePointerFactory {
  
      public int getOrder() {
          return 1;
      }
  
      public NodePointer createNodePointer(QName name, Object object,
                                           Locale locale) {
          if (object instanceof Scriptable) {
              return new ScriptablePointer(name, (Scriptable)object, locale);
          }
          return null;
      }
  
      public NodePointer createNodePointer(NodePointer parent,
                                           QName name, Object object) {
          if (object instanceof Scriptable) {
              return new ScriptablePointer(parent, name, 
                                           (Scriptable)object);
          }
          return null;
      }
  }
  
  
  
  1.1                  xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ScriptablePropertyHandler.java
  
  Index: ScriptablePropertyHandler.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
  */
  package org.apache.cocoon.components.flow.javascript;
  import org.apache.commons.jxpath.DynamicPropertyHandler;
  import org.mozilla.javascript.*;
  
  public class ScriptablePropertyHandler implements DynamicPropertyHandler {
  
      public Object getProperty(Object obj, String propertyName) {
          Context cx = null;
          try {
              cx = Context.enter();
              Scriptable s = (Scriptable)obj;
              Object result = ScriptableObject.getProperty(s, propertyName);
              if (result == ScriptableObject.NOT_FOUND) {
                  result = null;
              } else if (result instanceof Wrapper) {
                  result = ((Wrapper)result).unwrap();
              } else if (result == Undefined.instance) {
  		result = null;
  	    }
              return result;
          } finally {
              cx.exit();
          }
      }
      
      public String[] getPropertyNames(Object obj) {
          Context cx = null;
          try {
              cx = Context.enter();
              Object[] ids;
              if (obj instanceof ScriptableObject) {
                  ids = ((ScriptableObject)obj).getAllIds();
              } else {
                  ids = ((Scriptable)obj).getIds();
              }
              String[] result = new String[ids.length];
              for (int i = 0; i < result.length; i++) {
                  result[i] = (String)ids[i];
              }
              return result;
          } finally {
              cx.exit();
          }
      }
      
      public void setProperty(Object obj, String propertyName,
                              Object value) {
          Context cx = null;
          try {
              cx = Context.enter();
              ScriptableObject.putProperty((Scriptable)obj,
                                           propertyName,
                                           Context.toObject(value, 
                                                            (Scriptable)obj));
          } finally {
              cx.exit();
          }
      }
  }
  
  
  
  1.1                  xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ScriptablePropertyPointer.java
  
  Index: ScriptablePropertyPointer.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
  */
  package org.apache.cocoon.components.flow.javascript;
  import org.mozilla.javascript.*;
  import org.apache.commons.jxpath.ri.QName;
  import org.apache.commons.jxpath.*;
  import org.apache.commons.jxpath.ri.model.beans.*;
  import org.apache.commons.jxpath.ri.model.*;
  import org.apache.commons.jxpath.util.*;
  import java.util.Locale;
  
  public class ScriptablePropertyPointer extends DynamicPropertyPointer {
  
      DynamicPropertyHandler handler;
  
      public ScriptablePropertyPointer(NodePointer parent,
                                       DynamicPropertyHandler handler) {
          super(parent, handler);
          this.handler = handler;
      }
  
      public int getLength() {
          Object obj = getBaseValue();
          if (obj instanceof Scriptable) {
              Scriptable node = (Scriptable)obj;
              if (ScriptableObject.hasProperty(node, "length")) {
                  Object val = ScriptableObject.getProperty(node, "length");
                  if (val instanceof Number) {
                      return ((Number)val).intValue();
                  }
              }
          }
          return super.getLength();
      }
  
      public Object getNodeValue() {
          Object value;
          if (index == WHOLE_COLLECTION){
              value = handler.getProperty(getBean(), getPropertyName());
          } else {
              value = handler.getProperty(getBean(), getPropertyName());
              if (value instanceof Scriptable &&
                  ScriptableObject.hasProperty((Scriptable)value, index)) {
                  value = ScriptableObject.getProperty((Scriptable)value, index);
              } else {
                  return super.getNodeValue();
              }
          }
          return value;
      }
  
      public void setValue(Object value){
          if (index == WHOLE_COLLECTION){
              handler.setProperty(getBean(), getPropertyName(), value);
          } else {
              Object val = handler.getProperty(getBean(), getPropertyName());
              if (val instanceof Scriptable) {
                  ScriptableObject.putProperty((Scriptable)val, index, value);
              } else {
  		super.setValue(value);
              }
          }
      }
  }
  
  
  

Mime
View raw message