001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.template.environment;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.CascadingRuntimeException;
022: import org.apache.avalon.framework.parameters.Parameters;
023: import org.apache.cocoon.components.expression.ExpressionContext;
024: import org.apache.cocoon.components.flow.FlowHelper;
025: import org.apache.cocoon.components.flow.javascript.fom.FOM_JavaScriptFlowHelper;
026: import org.apache.cocoon.environment.TemplateObjectModelHelper;
027: import org.mozilla.javascript.Context;
028: import org.mozilla.javascript.NativeJavaPackage;
029: import org.mozilla.javascript.Scriptable;
030: import org.mozilla.javascript.ScriptableObject;
031:
032: /**
033: * Creation of an Expression context from the TemplateObjectModelHelper
034: *
035: * @version SVN $Id: FlowObjectModelHelper.java 449189 2006-09-23 06:52:29Z crossley $
036: */
037: public class FlowObjectModelHelper {
038:
039: private static Scriptable rootScope;
040:
041: /** Avoid instantiation. */
042: private FlowObjectModelHelper() {
043: }
044:
045: public static Scriptable getScope() {
046: Context ctx = Context.enter();
047: try {
048: // Create it if never used up to now
049: if (rootScope == null) {
050: rootScope = ctx.initStandardObjects(null);
051: }
052: try {
053: Scriptable scope = ctx.newObject(rootScope);
054: scope.setPrototype(rootScope);
055: scope.setParentScope(null);
056: return scope;
057: } catch (Exception e) {
058: throw new CascadingRuntimeException("Exception", e);
059: }
060: } finally {
061: Context.exit();
062: }
063: }
064:
065: /**
066: * Create an expression context that contains the object model
067: */
068: public static ExpressionContext getFOMExpressionContext(
069: final Map objectModel, final Parameters parameters) {
070: ExpressionContext context = new ExpressionContext();
071: Map expressionContext = TemplateObjectModelHelper
072: .getTemplateObjectModel(objectModel, parameters);
073: FlowObjectModelHelper.addJavaPackages(expressionContext);
074: context.setVars(expressionContext);
075: context
076: .setContextBean(FlowHelper
077: .getContextObject(objectModel));
078:
079: return context;
080: }
081:
082: /**
083: * Add java packages to object model. Allows to construct java objects.
084: * @param objectModel usually the result of invoking getTemplateObjectModel
085: */
086: public static void addJavaPackages(Map objectModel) {
087: Object javaPkg = FOM_JavaScriptFlowHelper
088: .getJavaPackage(objectModel);
089: Object pkgs = FOM_JavaScriptFlowHelper.getPackages(objectModel);
090:
091: // packages might have already been set up if flowscript is being used
092: if (javaPkg != null && pkgs != null) {
093: objectModel.put("Packages", javaPkg);
094: objectModel.put("java", pkgs);
095: } else {
096: Context.enter();
097: try {
098: final String JAVA_PACKAGE = "JavaPackage";
099: ClassLoader cl = Thread.currentThread()
100: .getContextClassLoader();
101: // FIXME - NativeJavaPackage is an internal class which we should not use
102: Scriptable newPackages = new NativeJavaPackage("", cl);
103: newPackages.setParentScope(getScope());
104: newPackages.setPrototype(ScriptableObject
105: .getClassPrototype(getScope(), JAVA_PACKAGE));
106: objectModel.put("Packages", newPackages);
107: objectModel.put("java", ScriptableObject.getProperty(
108: getScope(), "java"));
109: } finally {
110: Context.exit();
111: }
112: }
113: }
114:
115: }
|