Author: sebb Date: Sat Aug 1 21:37:09 2009 New Revision: 799932 URL: http://svn.apache.org/viewvc?rev=799932&view=rev Log: Initial implementation of "JEXL" engine-scoped object
Added: commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptObject.java (with props) Modified: commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptEngine.java commons/proper/jexl/branches/2.0/src/test/org/apache/commons/jexl/scripting/JexlScriptEngineTest.java Modified: commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptEngine.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptEngine.java?rev=799932&r1=799931&r2=799932&view=diff ============================================================================== --- commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptEngine.java (original) +++ commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptEngine.java Sat Aug 1 21:37:09 2009 @@ -49,12 +49,18 @@ * If no variable is found, and the JEXL script is writing to a variable, * it will be stored in the ENGINE scope. * </p> + * <p> + * The implementation also creates the "JEXL" script object as an instance of the + * class {...@link JexlScriptObject} for access to utility methods and variables. + * </p> * See * <a href="http://java.sun.com/javase/6/docs/api/javax/script/package-summary.html">Java Scripting API</a> * Javadoc. */ public class JexlScriptEngine extends AbstractScriptEngine { + public static final String JEXL_OBJECT_KEY = "JEXL"; + public static final String CONTEXT_KEY = "context"; private final ScriptEngineFactory factory; @@ -68,6 +74,8 @@ public JexlScriptEngine(final ScriptEngineFactory _factory) { factory = _factory; engine = new JexlEngine(); + // Add utility object + this.put(JEXL_OBJECT_KEY, new JexlScriptObject()); } /** {...@inheritdoc} */ @@ -110,6 +118,7 @@ } // This is mandated by JSR-223 (end of section SCR.4.3.4.1.2 - Script Execution) context.setAttribute(CONTEXT_KEY, context, ScriptContext.ENGINE_SCOPE); + try { Script script = engine.createScript(scriptText); JexlContext ctxt = new JexlContext(){ Added: commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptObject.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptObject.java?rev=799932&view=auto ============================================================================== --- commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptObject.java (added) +++ commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptObject.java Sat Aug 1 21:37:09 2009 @@ -0,0 +1,48 @@ +/* + * 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.commons.jexl.scripting; + +import java.io.PrintStream; + +/** + * Implements variables and methods for use by JEXL scripts. + * <p> + * The following items are defined: + * <ul> + * <li>out - System.out</li> + * <li>err - System.err</li> + * <li></li> + * </ul> + * </p> + */ +public class JexlScriptObject { + + public JexlScriptObject(){ + + } + + public static PrintStream getOut() { + return System.out; + } + + public static PrintStream getErr() { + return System.err; + } + +} Propchange: commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptObject.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/scripting/JexlScriptObject.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: commons/proper/jexl/branches/2.0/src/test/org/apache/commons/jexl/scripting/JexlScriptEngineTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/org/apache/commons/jexl/scripting/JexlScriptEngineTest.java?rev=799932&r1=799931&r2=799932&view=diff ============================================================================== --- commons/proper/jexl/branches/2.0/src/test/org/apache/commons/jexl/scripting/JexlScriptEngineTest.java (original) +++ commons/proper/jexl/branches/2.0/src/test/org/apache/commons/jexl/scripting/JexlScriptEngineTest.java Sat Aug 1 21:37:09 2009 @@ -50,6 +50,10 @@ assertEquals(initialValue,engine.get("old")); assertEquals(newValue,engine.get("value")); assertEquals(engine.getContext(),engine.get(JexlScriptEngine.CONTEXT_KEY)); + // Check behaviour of JEXL object + assertNotNull(engine.get("JEXL")); + assertEquals(System.out,engine.eval("JEXL.out")); + assertEquals(System.err,engine.eval("JEXL.err")); } public void testNulls() throws Exception {