You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by di...@apache.org on 2006/04/03 13:22:31 UTC

svn commit: r391004 - in /jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl: Script.java ScriptFactory.java ScriptImpl.java

Author: dion
Date: Mon Apr  3 04:22:29 2006
New Revision: 391004

URL: http://svn.apache.org/viewcvs?rev=391004&view=rev
Log:
Start of script implementation

Added:
    jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/Script.java   (with props)
    jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java   (with props)
    jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptImpl.java   (with props)

Added: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/Script.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/Script.java?rev=391004&view=auto
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/Script.java (added)
+++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/Script.java Mon Apr  3 04:22:29 2006
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2002-2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.commons.jexl;
+
+public interface Script {
+    /**
+     * Executes the script with the variables contained in the
+     * supplied {@link JexlContext}. 
+     * 
+     * @param context A JexlContext containing variables.
+     * @return The result of this script, usually the result of the last statement.
+     */
+    Object execute(JexlContext context) throws Exception;
+
+    /**
+     * Returns the text of this Script.
+     * @return The script to be executed.
+     */
+    String getText();
+
+}

Propchange: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/Script.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java?rev=391004&view=auto
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java (added)
+++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java Mon Apr  3 04:22:29 2006
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2002-2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.commons.jexl;
+
+import java.io.StringReader;
+
+import org.apache.commons.jexl.parser.ASTJexlScript;
+import org.apache.commons.jexl.parser.Parser;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * <p> 
+ * Creates {@link Script}s.  To create a JEXL Script, pass
+ * valid JEXL syntax to the static createScript() method:
+ * </p>
+ * 
+ * <pre>
+ * String jexl = "y = x * 12 + 44; y = y * 4;";
+ * Script script = ScriptFactory.createScript( jexl );
+ * </pre>
+ * 
+ * <p>
+ * When an {@link Script} is created, the JEXL syntax is
+ * parsed and verified.
+ * </p>
+ * @since 1.1
+ * @version $Id$
+ */
+public class ScriptFactory {
+
+    /** The Log to which all ScriptFactory messages will be logged.*/
+    protected static Log log =
+        LogFactory.getLog("org.apache.commons.jexl.ScriptFactory");
+
+    /**
+     * The singleton ScriptFactory also holds a single instance of {@link Parser}.
+     * When parsing expressions, ScriptFactory synchronizes on Parser.
+     */
+    protected static Parser parser = new Parser(new StringReader(";")); //$NON-NLS-1$
+
+    /**
+     * ScriptFactory is a singleton and this is the private
+     * instance fufilling that pattern.
+     */
+    protected static ScriptFactory factory = new ScriptFactory();
+
+    /**
+     * Private constructor, the single instance is always obtained
+     * with a call to getInstance().
+     */
+    private ScriptFactory(){}
+
+    /**
+     * Returns the single instance of ScriptFactory.
+     * @return the instance of ScriptFactory.
+     */
+    protected static  ScriptFactory getInstance()
+    {
+        return factory;
+    }
+
+    /**
+     * Creates a Script from a String containing valid JEXL syntax. 
+     * This method parses the script which validates the syntax.
+     * 
+     * @param scriptText A String containing valid JEXL syntax
+     * @return A {@link Script} which can be executed with a {@link JexlContext}.
+     * @throws Exception An exception can be thrown if there is a problem parsing the script.
+     */
+    public static Script createScript(String scriptText) throws Exception
+    {
+        return getInstance().newScript(scriptText);
+    }
+
+
+    /**
+     *  Creates a new Script based on the string.
+     *
+     *  @param scriptText valid Jexl script
+     *  @return Script a new script
+     *  @throws Exception for a variety of reasons - mostly malformed scripts
+     */
+    protected Script newScript(String scriptText) throws Exception {
+        String cleanText = cleanScript(scriptText);
+        // Parse the Expression
+        ASTJexlScript tree;
+        synchronized(parser)
+        {
+            log.debug( "Parsing script: " + cleanText);
+            tree = (ASTJexlScript) parser.parse(new StringReader(cleanText));
+        }
+
+        return new ScriptImpl(cleanText, tree);
+
+    }
+
+    /**
+     * @todo move to ParseUtils?
+     * Trims the expression and adds a semi-colon if missing.
+     * @param expression to clean
+     * @return trimmed expression ending in a semi-colon
+     */
+    private String cleanScript(String script) {
+        String expr = script.trim();
+        if (!expr.endsWith(";"))
+        {
+            expr += ";";
+        }
+        return expr;
+    }
+
+}

Propchange: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptImpl.java?rev=391004&view=auto
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptImpl.java (added)
+++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptImpl.java Mon Apr  3 04:22:29 2006
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ * 
+ * Licensed 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.commons.jexl;
+
+import org.apache.commons.jexl.parser.ASTJexlScript;
+
+public class ScriptImpl implements Script {
+
+    /** text of the script */
+    private String text;
+    /** syntax tree */
+    private ASTJexlScript parsedScript;
+    
+    public ScriptImpl(String scriptText, ASTJexlScript scriptTree) {
+        text = scriptText;
+        parsedScript = scriptTree;
+    }
+
+    /**
+     * @see Script#execute(JexlContext)
+     */
+    public Object execute(JexlContext context) throws Exception {
+        // TODO Auto-generated method stub
+        return parsedScript.value(context);
+    }
+
+    /**
+     * @see Script#getText()
+     */
+    public String getText() {
+        return text;
+    }
+
+}

Propchange: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ScriptImpl.java
------------------------------------------------------------------------------
    svn:keywords = Id



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