You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2010/09/09 08:53:35 UTC

svn commit: r995334 - in /sling/trunk/bundles/scripting/javascript/src: main/java/org/apache/sling/scripting/javascript/helper/ test/java/org/apache/sling/scripting/javascript/internal/

Author: cziegeler
Date: Thu Sep  9 06:53:35 2010
New Revision: 995334

URL: http://svn.apache.org/viewvc?rev=995334&view=rev
Log:
SLING-1594 : Support CommonJS Modules i.e. the require function. Apply patch from Lars Trieloff

Added:
    sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ExportsObject.java   (with props)
    sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleObject.java   (with props)
    sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleScope.java   (with props)
    sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java   (with props)
Modified:
    sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingGlobal.java

Added: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ExportsObject.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ExportsObject.java?rev=995334&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ExportsObject.java (added)
+++ sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ExportsObject.java Thu Sep  9 06:53:35 2010
@@ -0,0 +1,32 @@
+/*
+ * 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.sling.scripting.javascript.helper;
+
+import org.mozilla.javascript.NativeObject;
+import org.mozilla.javascript.Scriptable;
+
+public class ExportsObject extends NativeObject {
+
+    private static final long serialVersionUID = 7608182741100799506L;
+
+    public ExportsObject(Scriptable parent) {
+		this.setParentScope(parent);
+		this.setPrototype(getObjectPrototype(parent));
+	}
+}

Propchange: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ExportsObject.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ExportsObject.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ExportsObject.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleObject.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleObject.java?rev=995334&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleObject.java (added)
+++ sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleObject.java Thu Sep  9 06:53:35 2010
@@ -0,0 +1,43 @@
+/*
+ * 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.sling.scripting.javascript.helper;
+
+import org.mozilla.javascript.NativeObject;
+import org.mozilla.javascript.Scriptable;
+
+public class ModuleObject extends NativeObject implements Scriptable {
+
+    private static final long serialVersionUID = -7538465610554258220L;
+
+    private final ModuleScope module;
+
+        ModuleObject(ModuleScope parent) {
+            setParentScope(parent);
+            setPrototype(getObjectPrototype(parent));
+            this.module = parent;
+        }
+
+        @Override
+        protected Object equivalentValues(Object value) {
+            if (value instanceof String) {
+                return this.module.getModuleName().equals(value) ? Boolean.TRUE : Boolean.FALSE;
+            }
+            return NOT_FOUND;
+        }
+}

Propchange: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleObject.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleObject.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleObject.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleScope.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleScope.java?rev=995334&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleScope.java (added)
+++ sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleScope.java Thu Sep  9 06:53:35 2010
@@ -0,0 +1,59 @@
+/*
+ * 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.sling.scripting.javascript.helper;
+
+import org.mozilla.javascript.ImporterTopLevel;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+
+public class ModuleScope extends ImporterTopLevel {
+
+    private static final long serialVersionUID = -6063613037620927512L;
+
+    private ExportsObject exports;
+	private Scriptable module;
+	private String name;
+
+	public ModuleScope(Scriptable prototype, String moduleName) {
+		this.setParentScope(null);
+		this.setPrototype(prototype);
+		this.activatePrototypeMap(3);
+
+		this.name = moduleName;
+
+		this.reset();
+	}
+
+	public Scriptable getExports() {
+		return this.exports = (ExportsObject) get("exports", this);
+	}
+
+	public void reset() {
+		this.module = new ModuleObject(this);
+		int attr = READONLY | PERMANENT;
+        ScriptableObject.defineProperty(this.module, "id", this.getModuleName(), attr);
+
+		this.exports = new ExportsObject(this);
+		this.defineProperty("exports", this.exports, DONTENUM);
+	}
+
+	public String getModuleName() {
+		return this.name;
+	}
+}

Propchange: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleScope.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleScope.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/ModuleScope.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingGlobal.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingGlobal.java?rev=995334&r1=995333&r2=995334&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingGlobal.java (original)
+++ sling/trunk/bundles/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/helper/SlingGlobal.java Thu Sep  9 06:53:35 2010
@@ -25,6 +25,7 @@ import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.Serializable;
 
+import org.apache.sling.api.resource.NonExistingResource;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceUtil;
@@ -50,8 +51,8 @@ import org.slf4j.LoggerFactory;
  * <p>
  * <dl>
  * <dt><code>print(args, ...)</code></dt>
- * <dd>Prints the arguments <code>args</code> in a single message to the
- * scripts logger available as the global <em>log</em> variable.</dd>
+ * <dd>Prints the arguments <code>args</code> in a single message to the scripts
+ * logger available as the global <em>log</em> variable.</dd>
  * <dt><code>load(args, ...)</code></dt>
  * <dd>Loads the scripts named as parameters into the current scope one, after
  * the other. Usually the script files are read as plain JavaScript files. If
@@ -62,182 +63,300 @@ import org.slf4j.LoggerFactory;
  * </dl>
  */
 public class SlingGlobal implements Serializable, IdFunctionCall {
-    static final long serialVersionUID = 6080442165748707530L;
+	static final long serialVersionUID = 6080442165748707530L;
 
-    private static final Object FTAG = new Object();
+	private static final Object FTAG = new Object();
 
-    private static final int Id_load = 1;
+	private static final int Id_load = 1;
 
-    private static final int Id_print = 2;
+	private static final int Id_print = 2;
 
-    private static final int LAST_SCOPE_FUNCTION_ID = 2;
-
-    /** default log */
-    private final Logger defaultLog = LoggerFactory.getLogger(getClass());
-
-    public static void init(Scriptable scope, boolean sealed) {
-        SlingGlobal obj = new SlingGlobal();
-
-        for (int id = 1; id <= LAST_SCOPE_FUNCTION_ID; ++id) {
-            String name;
-            int arity = 1;
-            switch (id) {
-                case Id_load:
-                    name = "load";
-                    break;
-                case Id_print:
-                    name = "print";
-                    break;
-                default:
-                    throw Kit.codeBug();
-            }
-            IdFunctionObject f = new IdFunctionObject(obj, FTAG, id, name,
-                arity, scope);
-            if (sealed) {
-                f.sealObject();
-            }
-            f.exportAsScopeProperty();
-        }
-
-    }
-
-    public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
-            Scriptable thisObj, Object[] args) {
-        if (f.hasTag(FTAG)) {
-            int methodId = f.methodId();
-            switch (methodId) {
-                case Id_load: {
-                    load(cx, thisObj, args);
-                    return Context.getUndefinedValue();
-                }
-
-                case Id_print: {
-                    print(cx, thisObj, args);
-                    return Context.getUndefinedValue();
-                }
-            }
-        }
-        throw f.unknown();
-    }
-
-    private void print(Context cx, Scriptable thisObj, Object[] args) {
-        StringBuffer message = new StringBuffer();
-        for (int i = 0; i < args.length; i++) {
-            if (i > 0) {
-                message.append(" ");
-            }
-            // Convert the arbitrary JavaScript value into a string form.
-            String s = ScriptRuntime.toString(args[i]);
-
-            message.append(s);
-        }
-
-        getLogger(cx, thisObj).info(message.toString());
-    }
-
-    private void load(Context cx, Scriptable thisObj, Object[] args) {
-
-        SlingScriptHelper sling = getProperty(cx, thisObj, SlingBindings.SLING,
-            SlingScriptHelper.class);
-        if (sling == null) {
-            throw new NullPointerException(SlingBindings.SLING);
-        }
-
-        Scriptable globalScope = ScriptableObject.getTopLevelScope(thisObj);
-
-        Resource scriptResource = sling.getScript().getScriptResource();
-        ResourceResolver resolver = scriptResource.getResourceResolver();
-
-        // the path of the current script to resolve realtive paths
-        String currentScript = sling.getScript().getScriptResource().getPath();
-        String scriptParent = ResourceUtil.getParent(currentScript);
-
-        for (Object arg : args) {
-            String scriptName = ScriptRuntime.toString(arg);
-
-            Resource loadScript = null;
-            if (!scriptName.startsWith("/")) {
-                String absScriptName = scriptParent + "/" + scriptName;
-                loadScript = resolver.resolve(absScriptName);
-            }
-
-            // not resolved relative to the current script
-            if (loadScript == null) {
-                loadScript = resolver.resolve(scriptName);
-            }
-
-            if (loadScript == null) {
-                throw Context.reportRuntimeError("Script file " + scriptName
-                    + " not found");
-            }
-
-            InputStream scriptStream = loadScript.adaptTo(InputStream.class);
-            if (scriptStream == null) {
-                throw Context.reportRuntimeError("Script file " + scriptName
-                    + " cannot be read from");
-            }
-
-            try {
-                // reader for the stream
-                Reader scriptReader = new InputStreamReader(scriptStream);
-
-                // check whether we have to wrap the basic reader
-                if (scriptName.endsWith(RhinoJavaScriptEngineFactory.ESP_SCRIPT_EXTENSION)) {
-                    scriptReader = new EspReader(scriptReader);
-                }
-
-                // read the suff buffered for better performance
-                scriptReader = new BufferedReader(scriptReader);
-
-                // now, let's go
-                cx.evaluateReader(globalScope, scriptReader, scriptName, 1,
-                    null);
-
-            } catch (IOException ioe) {
-
-                throw Context.reportRuntimeError("Failure reading file "
-                    + scriptName + ": " + ioe);
-
-            } finally {
-                // ensure the script input stream is closed
-                try {
-                    scriptStream.close();
-                } catch (IOException ignore) {
-                }
-            }
-        }
-    }
-
-    /**
-     * Returns the script logger or the logger of this class as a fallback
-     * default if the global log variable is not accessible.
-     */
-    private Logger getLogger(Context cx, Scriptable scope) {
-        Logger log = getProperty(cx, scope, SlingBindings.LOG, Logger.class);
-        if (log == null) {
-            log = this.defaultLog;
-        }
-        return log;
-    }
-
-    /**
-     * Returns the named toplevel property converted to the requested
-     * <code>type</code> or <code>null</code> if no such property exists or
-     * the property is of the wrong type.
-     */
-    @SuppressWarnings("unchecked")
-    private <Type> Type getProperty(Context cx, Scriptable scope, String name,
-            Class<Type> type) {
-        Object prop = ScriptRuntime.name(cx, scope, name);
-
-        if (prop instanceof Wrapper) {
-            prop = ((Wrapper) prop).unwrap();
-        }
-
-        if (type.isInstance(prop)) {
-            return (Type) prop; // unchecked case
-        }
+	private static final int Id_require = 3;
+
+	private static final int LAST_SCOPE_FUNCTION_ID = 3;
+
+	/** default log */
+	private final Logger defaultLog = LoggerFactory.getLogger(getClass());
+
+	public static void init(Scriptable scope, boolean sealed) {
+		SlingGlobal obj = new SlingGlobal();
+
+		for (int id = 1; id <= LAST_SCOPE_FUNCTION_ID; ++id) {
+			String name;
+			int arity = 1;
+			switch (id) {
+			case Id_load:
+				name = "load";
+				break;
+			case Id_print:
+				name = "print";
+				break;
+			case Id_require:
+				name = "require";
+				break;
+			default:
+				throw Kit.codeBug();
+			}
+			IdFunctionObject f = new IdFunctionObject(obj, FTAG, id, name,
+					arity, scope);
+			if (sealed) {
+				f.sealObject();
+			}
+			f.exportAsScopeProperty();
+		}
+
+	}
+
+	public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
+			Scriptable thisObj, Object[] args) {
+		if (f.hasTag(FTAG)) {
+			int methodId = f.methodId();
+			switch (methodId) {
+			case Id_load: {
+				load(cx, thisObj, args);
+				return Context.getUndefinedValue();
+			}
+
+			case Id_print: {
+				print(cx, thisObj, args);
+				return Context.getUndefinedValue();
+			}
+
+			case Id_require: {
+				return require(cx, thisObj, args);
+			}
+			}
+		}
+		throw f.unknown();
+	}
+
+	private void print(Context cx, Scriptable thisObj, Object[] args) {
+		StringBuffer message = new StringBuffer();
+		for (int i = 0; i < args.length; i++) {
+			if (i > 0) {
+				message.append(" ");
+			}
+			// Convert the arbitrary JavaScript value into a string form.
+			String s = ScriptRuntime.toString(args[i]);
+
+			message.append(s);
+		}
+
+		getLogger(cx, thisObj).info(message.toString());
+	}
+
+	private void load(Context cx, Scriptable thisObj, Object[] args) {
+
+		SlingScriptHelper sling = getProperty(cx, thisObj, SlingBindings.SLING,
+				SlingScriptHelper.class);
+		if (sling == null) {
+			throw new NullPointerException(SlingBindings.SLING);
+		}
+
+		Scriptable globalScope = ScriptableObject.getTopLevelScope(thisObj);
+
+		Resource scriptResource = sling.getScript().getScriptResource();
+		ResourceResolver resolver = scriptResource.getResourceResolver();
+
+		// the path of the current script to resolve realtive paths
+		String currentScript = sling.getScript().getScriptResource().getPath();
+		String scriptParent = ResourceUtil.getParent(currentScript);
+
+		for (Object arg : args) {
+			String scriptName = ScriptRuntime.toString(arg);
+
+			Resource loadScript = null;
+			if (!scriptName.startsWith("/")) {
+				String absScriptName = scriptParent + "/" + scriptName;
+				loadScript = resolver.resolve(absScriptName);
+			}
+
+			// not resolved relative to the current script
+			if (loadScript == null) {
+				loadScript = resolver.resolve(scriptName);
+			}
+
+			if (loadScript == null) {
+				throw Context.reportRuntimeError("Script file " + scriptName
+						+ " not found");
+			}
+
+			InputStream scriptStream = loadScript.adaptTo(InputStream.class);
+			if (scriptStream == null) {
+				throw Context.reportRuntimeError("Script file " + scriptName
+						+ " cannot be read from");
+			}
+
+			try {
+				// reader for the stream
+				Reader scriptReader = new InputStreamReader(scriptStream);
+
+				// check whether we have to wrap the basic reader
+				if (scriptName
+						.endsWith(RhinoJavaScriptEngineFactory.ESP_SCRIPT_EXTENSION)) {
+					scriptReader = new EspReader(scriptReader);
+				}
+
+				// read the suff buffered for better performance
+				scriptReader = new BufferedReader(scriptReader);
+
+				// now, let's go
+				cx.evaluateReader(globalScope, scriptReader, scriptName, 1,
+						null);
+
+			} catch (IOException ioe) {
+
+				throw Context.reportRuntimeError("Failure reading file "
+						+ scriptName + ": " + ioe);
+
+			} finally {
+				// ensure the script input stream is closed
+				try {
+					scriptStream.close();
+				} catch (IOException ignore) {
+				}
+			}
+		}
+	}
+
+	public Object require(Context cx, Scriptable thisObj, Object[] args) {
+		if (args.length != 1 || !(args[0] instanceof String)) {
+			throw Context
+					.reportRuntimeError("require() requires a String argument");
+		}
+		String modulePath = (String) args[0];
+
+		ModuleScope moduleScope = null;
+		if (thisObj instanceof ModuleScope) {
+			moduleScope = (ModuleScope) thisObj;
+		}
+
+		ModuleScope module = loadModule(cx, modulePath.trim(), moduleScope,
+				thisObj);
+		return module.getExports();
+	}
+
+	private ModuleScope loadModule(Context cx, String modulePath,
+			ModuleScope moduleScope, Scriptable thisObj) {
+		String absolutePath = modulePath;
+		if (modulePath.startsWith(".")) {
+			// relative
+			if (moduleScope == null) {
+				throw Context
+						.reportRuntimeError("Cannot resolve relative module name outside of a module scope.");
+			}
+			absolutePath = (moduleScope.getModuleName() + "/" + modulePath)
+					.replaceAll("[^/]*/\\./", "");
+			while (absolutePath.matches("([^/]*/)?[^/]*/\\.\\./")) {
+				absolutePath = absolutePath
+						.replaceAll("([^/]*/)?[^/]*/\\.\\./", "");
+			}
+		}
+		absolutePath = absolutePath + ".js";
+
+		SlingScriptHelper sling = getProperty(cx, thisObj, SlingBindings.SLING,
+				SlingScriptHelper.class);
+		if (sling == null) {
+			throw new NullPointerException(SlingBindings.SLING);
+		}
+		ResourceResolver resrev = sling.getScript().getScriptResource().getResourceResolver();
+
+		Resource script = null;
+		String scriptName = null;
+		for (String basepath : resrev.getSearchPath()) {
+			script = resrev.resolve(basepath + absolutePath);
+			if (script!=null&&!(script instanceof NonExistingResource)) {
+				scriptName = basepath + absolutePath;
+				break;
+			}
+		}
+		if (script==null) {
+			throw Context.reportRuntimeError("Unable to resolve module " + absolutePath + " in search path");
+		}
+
+		InputStream scriptStream = script.adaptTo(InputStream.class);
+		if (scriptStream == null) {
+			//try once again
+			 scriptStream = resrev.resolve(scriptName).adaptTo(InputStream.class);
+			if (scriptStream==null) {
+				throw Context.reportRuntimeError("Script file " + script.getPath()
+						+ " cannot be read");
+			}
+		}
+
+
+		try {
+			// reader for the stream
+			Reader scriptReader = new InputStreamReader(scriptStream);
+
+			// check whether we have to wrap the basic reader
+			if (scriptName
+					.endsWith(RhinoJavaScriptEngineFactory.ESP_SCRIPT_EXTENSION)) {
+				scriptReader = new EspReader(scriptReader);
+			}
+
+			// read the suff buffered for better performance
+			scriptReader = new BufferedReader(scriptReader);
+
+			//TODO: execute script with ModuleScope
+			// now, let's go
+
+			ModuleScope scope = moduleScope;
+			if (scope==null) {
+				scope = new ModuleScope(thisObj, absolutePath.substring(0, absolutePath.length() - 3));
+			} else {
+				scope.reset();
+			}
+
+			cx.evaluateReader(scope, scriptReader, scriptName, 1,
+					null);
+
+			return scope;
+
+		} catch (IOException ioe) {
+
+			throw Context.reportRuntimeError("Failure reading file "
+					+ scriptName + ": " + ioe);
+
+		} finally {
+			// ensure the script input stream is closed
+			try {
+				scriptStream.close();
+			} catch (IOException ignore) {
+			}
+		}
+	}
+
+	/**
+	 * Returns the script logger or the logger of this class as a fallback
+	 * default if the global log variable is not accessible.
+	 */
+	private Logger getLogger(Context cx, Scriptable scope) {
+		Logger log = getProperty(cx, scope, SlingBindings.LOG, Logger.class);
+		if (log == null) {
+			log = this.defaultLog;
+		}
+		return log;
+	}
+
+	/**
+	 * Returns the named toplevel property converted to the requested
+	 * <code>type</code> or <code>null</code> if no such property exists or the
+	 * property is of the wrong type.
+	 */
+	@SuppressWarnings("unchecked")
+	private <Type> Type getProperty(Context cx, Scriptable scope, String name,
+			Class<Type> type) {
+		Object prop = ScriptRuntime.name(cx, scope, name);
+
+		if (prop instanceof Wrapper) {
+			prop = ((Wrapper) prop).unwrap();
+		}
+
+		if (type.isInstance(prop)) {
+			return (Type) prop; // unchecked case
+		}
 
-        return null;
-    }
+		return null;
+	}
 }

Added: sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java?rev=995334&view=auto
==============================================================================
--- sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java (added)
+++ sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java Thu Sep  9 06:53:35 2010
@@ -0,0 +1,40 @@
+/*
+ * 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.sling.scripting.javascript.internal;
+
+import junit.framework.TestCase;
+
+
+public class TestPathRegexp extends TestCase {
+	public void testParentPath() {
+		String regexp = "([^/]*/)?[^/]*/\\.\\./";
+		assertEquals("math", "/../math".replaceAll(regexp, ""));
+		assertEquals("math", "increment/../math".replaceAll(regexp, ""));
+		assertEquals("math", "foo/increment/../math".replaceAll(regexp, ""));
+		assertEquals("foo/math", "foo/bar/increment/../math".replaceAll(regexp, ""));
+	}
+
+	public void testCurrentPath() {
+		String regexp = "[^/]*/\\./";
+		assertEquals("math", "/./math".replaceAll(regexp, ""));
+		assertEquals("math", "increment/./math".replaceAll(regexp, ""));
+		assertEquals("foo/math", "foo/increment/./math".replaceAll(regexp, ""));
+		assertEquals("foo/bar/math", "foo/bar/increment/./math".replaceAll(regexp, ""));
+	}
+}

Propchange: sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/scripting/javascript/src/test/java/org/apache/sling/scripting/javascript/internal/TestPathRegexp.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain