You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2012/04/25 18:25:46 UTC

svn commit: r1330394 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Scriptlet.java

Author: adrianc
Date: Wed Apr 25 16:25:45 2012
New Revision: 1330394

URL: http://svn.apache.org/viewvc?rev=1330394&view=rev
Log:
Added a Scriptlet class to the base component - this will be used to simplify scriptlet implementation.

Added:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Scriptlet.java   (with props)

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Scriptlet.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Scriptlet.java?rev=1330394&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Scriptlet.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Scriptlet.java Wed Apr 25 16:25:45 2012
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * 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.ofbiz.base.util;
+
+import java.util.Map;
+
+/**
+ * Contains a simple script (scriptlet).
+ * <p>A scriptlet is a small script that is commonly found in a scripting XML file.
+ * The scriptlet is composed of two parts: the prefix - which is the script language
+ * followed by a colon (":"), and the script. Example: <code>groovy:return foo.bar();</code>.
+ * </p>
+ */
+public final class Scriptlet {
+
+    private final String language;
+    private final String script;
+
+    public Scriptlet(String script) {
+        Assert.notNull("script", script);
+        int colonPos = script.indexOf(":");
+        if (colonPos == -1) {
+            throw new IllegalArgumentException("Invalid script: " + script);
+        }
+        this.language = script.substring(0, colonPos);
+        this.script = script.substring(colonPos + 1);
+    }
+
+    /**
+     * Executes the scriptlet and returns the result.
+     * 
+     * @param context The script bindings
+     * @return The scriptlet result
+     * @throws Exception
+     */
+    public Object executeScript(Map<String, Object> context) throws Exception {
+        return ScriptUtil.evaluate(language, script, null, context);
+    }
+
+    @Override
+    public String toString() {
+        return this.language.concat(":").concat(this.script);
+    }
+}

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Scriptlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Scriptlet.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL