Author: danielf Date: Sun Jan 16 13:35:37 2005 New Revision: 125367 URL: http://svn.apache.org/viewcvs?view=rev&rev=125367 Log: Refactored to use pluggable expressions and TemplateObjectModelHelper. Removed: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/JSIntrospector.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/MyVariables.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/expression/MyJexlContext.java Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/Expression.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/environment/FlowObjectModelHelper.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/TransformerAdapter.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/expression/JXTExpression.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Parser.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/Event.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatDate.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatNumber.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartIf.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartOut.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartWhen.java
Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/Expression.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/Expression.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/Expression.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/Expression.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/Expression.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/Expression.java Sun Jan 16 13:35:37 2005 @@ -27,4 +27,13 @@ public String getExpression(); public String getLanguage(); + + /* This method is added to handle that JXPath have two access + methods getValue and getNode, where getNode gives direct access + to the object while getValue might do some conversion of the + object. I would prefer to get rid of the getNode method, but + have not yet figured out how to get work in JXTG */ + public Object getNode(ExpressionContext context) throws ExpressionException; + + public void setProperty(String property, Object value); } Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jexl/JexlExpression.java Sun Jan 16 13:35:37 2005 @@ -91,6 +91,14 @@ return this.language; } + public void setProperty(String property, Object value) { + // Has no properties + } + + public Object getNode(ExpressionContext context) throws ExpressionException { + return evaluate(context); + } + private static class ContextAdapter implements JexlContext { private final ExpressionContext context; public ContextAdapter(ExpressionContext context) { Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/components/expression/jxpath/JXPathExpression.java Sun Jan 16 13:35:37 2005 @@ -16,21 +16,30 @@ package org.apache.cocoon.components.expression.jxpath; import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import org.apache.cocoon.components.expression.Expression; import org.apache.cocoon.components.expression.ExpressionCompiler; import org.apache.cocoon.components.expression.ExpressionContext; import org.apache.cocoon.components.expression.ExpressionException; +import org.apache.cocoon.components.expression.jexl.JSIntrospector; import org.apache.commons.jxpath.CompiledExpression; import org.apache.commons.jxpath.JXPathContext; +import org.apache.commons.jxpath.Pointer; import org.apache.commons.jxpath.Variables; +import org.mozilla.javascript.NativeArray; +import org.w3c.dom.Node; public class JXPathExpression implements Expression { private final String language; private final String expression; private final CompiledExpression compiledExpression; + private boolean lenient = false; + + public static final String LENIENT = "lenient"; public JXPathExpression(String language, String expression) throws ExpressionException { @@ -46,7 +55,29 @@ public Iterator iterate(ExpressionContext context) throws ExpressionException { - return this.compiledExpression.iterate(getContext(context)); + final JXPathContext jxpathContext = getContext(context); + Object val = + this.compiledExpression.getPointer(jxpathContext, this.expression).getNode(); + // FIXME: workaround for JXPath bug + if (val instanceof NativeArray) + return new JSIntrospector.NativeArrayIterator((NativeArray) val); + else + return new Iterator() { + Iterator iter = + compiledExpression.iteratePointers(jxpathContext); + + public boolean hasNext() { + return iter.hasNext(); + } + + public Object next() { + return ((Pointer)iter.next()).getNode(); + } + + public void remove() { + iter.remove(); + } + }; } public void assign(ExpressionContext context, Object value) @@ -62,11 +93,46 @@ return this.language; } + public void setProperty(String property, Object value) { + if (LENIENT.equals(property)) + this.lenient = ((Boolean)value).booleanValue(); + } + + // Hack: try to prevent JXPath from converting result to a String + public Object getNode(ExpressionContext context) throws ExpressionException { + Iterator iter = + this.compiledExpression.iteratePointers(getContext(context)); + if (iter.hasNext()) { + Pointer first = (Pointer)iter.next(); + if (iter.hasNext()) { + List result = new LinkedList(); + result.add(first.getNode()); + boolean dom = (first.getNode() instanceof Node); + while (iter.hasNext()) { + Object obj = ((Pointer)iter.next()).getNode(); + dom = dom && (obj instanceof Node); + result.add(obj); + } + Object[] arr; + if (dom) { + arr = new Node[result.size()]; + } else { + arr = new Object[result.size()]; + } + result.toArray(arr); + return arr; + } + return first.getNode(); + } + return null; + } + private JXPathContext getContext(ExpressionContext context) { // This could be made more efficient by caching the // JXPathContext within the Context object. - JXPathContext jxcontext = JXPathContext.newContext(null, context.getContextBean()); + JXPathContext jxcontext = JXPathContext.newContext(context.getContextBean()); jxcontext.setVariables(new VariableAdapter(context)); + jxcontext.setLenient(this.lenient); return jxcontext; } Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/environment/FlowObjectModelHelper.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/environment/FlowObjectModelHelper.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/environment/FlowObjectModelHelper.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/environment/FlowObjectModelHelper.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/environment/FlowObjectModelHelper.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/environment/FlowObjectModelHelper.java Sun Jan 16 13:35:37 2005 @@ -47,7 +47,7 @@ /** Avoid instantiation */ private FlowObjectModelHelper() {} - private static void fillContext(Object contextObject, Map map) { + public static void fillContext(Object contextObject, Map map) { // Hack: I use jxpath to populate the context object's properties // in the jexl context final JXPathBeanInfo bi = Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java Sun Jan 16 13:35:37 2005 @@ -15,11 +15,8 @@ */ package org.apache.cocoon.template.jxtg; -import java.beans.PropertyDescriptor; import java.io.IOException; import java.io.Serializable; -import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; @@ -28,39 +25,22 @@ import org.apache.avalon.framework.service.ServiceManager; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.caching.CacheableProcessingComponent; -import org.apache.cocoon.components.flow.FlowHelper; -import org.apache.cocoon.components.flow.WebContinuation; -import org.apache.cocoon.components.flow.javascript.fom.FOM_JavaScriptFlowHelper; -import org.apache.cocoon.environment.ObjectModelHelper; -import org.apache.cocoon.environment.Request; +import org.apache.cocoon.components.expression.ExpressionContext; +import org.apache.cocoon.environment.FlowObjectModelHelper; import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.generation.ServiceableGenerator; import org.apache.cocoon.template.jxtg.environment.ExecutionContext; -import org.apache.cocoon.template.jxtg.environment.JSIntrospector; import org.apache.cocoon.template.jxtg.environment.JXCacheKey; import org.apache.cocoon.template.jxtg.environment.JXSourceValidity; -import org.apache.cocoon.template.jxtg.environment.MyVariables; import org.apache.cocoon.template.jxtg.expression.JXTExpression; -import org.apache.cocoon.template.jxtg.expression.MyJexlContext; import org.apache.cocoon.template.jxtg.script.Invoker; import org.apache.cocoon.template.jxtg.script.ScriptManager; import org.apache.cocoon.template.jxtg.script.event.Event; import org.apache.cocoon.template.jxtg.script.event.StartDocument; import org.apache.cocoon.template.jxtg.script.event.StartElement; import org.apache.cocoon.xml.XMLConsumer; -import org.apache.commons.jexl.util.Introspector; -import org.apache.commons.jxpath.DynamicPropertyHandler; -import org.apache.commons.jxpath.JXPathBeanInfo; -import org.apache.commons.jxpath.JXPathContext; -import org.apache.commons.jxpath.JXPathContextFactory; -import org.apache.commons.jxpath.JXPathIntrospector; -import org.apache.commons.jxpath.Variables; import org.apache.excalibur.source.SourceValidity; -import org.xml.sax.Attributes; -import org.xml.sax.Locator; import org.xml.sax.SAXException; -import org.xml.sax.helpers.AttributesImpl; -import org.xml.sax.helpers.LocatorImpl; /** * @cocoon.sitemap.component.documentation Provides a generic page template with @@ -85,46 +65,18 @@ /** The namespace used by this generator */ public final static String NS = "http://apache.org/cocoon/templates/jx/1.0"; - private static final JXPathContextFactory jxpathContextFactory = JXPathContextFactory - .newInstance(); - private static final Attributes EMPTY_ATTRS = new AttributesImpl(); - - public static final Locator NULL_LOCATOR = new LocatorImpl(); - public final static String CACHE_KEY = "cache-key"; public final static String VALIDITY = "cache-validity"; - private JXPathContext jxpathContext; - private MyJexlContext globalJexlContext; - private Variables variables; + private ExpressionContext expressionContext; private ScriptManager scriptManager = new ScriptManager(); private StartDocument startDocument; private Map definitions; - private Map cocoon; - - static { - // Hack: there's no _nice_ way to add my introspector to Jexl right now - try { - Field field = Introspector.class.getDeclaredField("uberSpect"); - field.setAccessible(true); - field.set(null, new JSIntrospector()); - } catch (Exception e) { - e.printStackTrace(); - } - } public XMLConsumer getConsumer() { return this.xmlConsumer; } - public JXPathContext getJXPathContext() { - return jxpathContext; - } - - public MyJexlContext getJexlContext() { - return globalJexlContext; - } - public void service(ServiceManager manager) throws ServiceException { super.service(manager); scriptManager.setServiceManager(manager); @@ -132,11 +84,8 @@ public void recycle() { this.startDocument = null; - this.jxpathContext = null; - this.globalJexlContext = null; - this.variables = null; + this.expressionContext = null; this.definitions = null; - this.cocoon = null; super.recycle(); } @@ -148,117 +97,18 @@ * org.apache.avalon.framework.parameters.Parameters) */ public void setup(SourceResolver resolver, Map objectModel, String src, - Parameters parameters) throws ProcessingException, SAXException, - IOException { + Parameters parameters) + throws ProcessingException, SAXException, IOException { super.setup(resolver, objectModel, src, parameters); if (src != null) startDocument = scriptManager.resolveTemplate(src); - Object bean = FlowHelper.getContextObject(objectModel); - WebContinuation kont = FlowHelper.getWebContinuation(objectModel); - setContexts(bean, kont, parameters, objectModel); + this.expressionContext = + FlowObjectModelHelper.getFOMExpressionContext(objectModel, parameters); this.definitions = new HashMap(); } - public static void fillContext(Object contextObject, Map map) { - if (contextObject != null) { - // Hack: I use jxpath to populate the context object's properties - // in the jexl context - final JXPathBeanInfo bi = JXPathIntrospector - .getBeanInfo(contextObject.getClass()); - if (bi.isDynamic()) { - Class cl = bi.getDynamicPropertyHandlerClass(); - try { - DynamicPropertyHandler h = (DynamicPropertyHandler) cl - .newInstance(); - String[] result = h.getPropertyNames(contextObject); - int len = result.length; - for (int i = 0; i < len; i++) { - try { - map.put(result[i], h.getProperty(contextObject, - result[i])); - } catch (Exception exc) { - exc.printStackTrace(); - } - } - } catch (Exception ignored) { - ignored.printStackTrace(); - } - } else { - PropertyDescriptor[] props = bi.getPropertyDescriptors(); - int len = props.length; - for (int i = 0; i < len; i++) { - try { - Method read = props[i].getReadMethod(); - if (read != null) { - map.put(props[i].getName(), read.invoke( - contextObject, null)); - } - } catch (Exception ignored) { - ignored.printStackTrace(); - } - } - } - } - } - - private void setContexts(Object contextObject, WebContinuation kont, - Parameters parameters, Map objectModel) { - final Request request = ObjectModelHelper.getRequest(objectModel); - final Object session = request.getSession(false); - final Object app = ObjectModelHelper.getContext(objectModel); - cocoon = new HashMap(); - cocoon.put("request", FOM_JavaScriptFlowHelper - .getFOM_Request(objectModel)); - if (session != null) { - cocoon.put("session", FOM_JavaScriptFlowHelper - .getFOM_Session(objectModel)); - } - cocoon.put("context", FOM_JavaScriptFlowHelper - .getFOM_Context(objectModel)); - cocoon.put("continuation", FOM_JavaScriptFlowHelper - .getFOM_WebContinuation(objectModel)); - cocoon.put("parameters", Parameters.toProperties(parameters)); - this.variables = new MyVariables(cocoon, contextObject, kont, request, - session, app, parameters); - Map map; - if (contextObject instanceof Map) { - map = (Map) contextObject; - } else { - map = new HashMap(); - fillContext(contextObject, map); - } - jxpathContext = jxpathContextFactory.newContext(null, contextObject); - jxpathContext.setVariables(variables); - jxpathContext.setLenient(parameters.getParameterAsBoolean( - "lenient-xpath", false)); - globalJexlContext = new MyJexlContext(); - globalJexlContext.setVars(map); - map = globalJexlContext.getVars(); - map.put("cocoon", cocoon); - if (contextObject != null) { - map.put("flowContext", contextObject); - // FIXME (VG): Is this required (what it's used for - examples)? - // Here I use Rhino's live-connect objects to allow Jexl to call - // java constructors - Object javaPkg = FOM_JavaScriptFlowHelper - .getJavaPackage(objectModel); - Object pkgs = FOM_JavaScriptFlowHelper.getPackages(objectModel); - map.put("java", javaPkg); - map.put("Packages", pkgs); - } - if (kont != null) { - map.put("continuation", kont); - } - map.put("request", request); - map.put("context", app); - map.put("parameters", parameters); - if (session != null) { - map.put("session", session); - } - } - /* * (non-Javadoc) * @@ -266,18 +116,16 @@ */ public void generate() throws IOException, SAXException, ProcessingException { - performGeneration(this.xmlConsumer, globalJexlContext, jxpathContext, - null, startDocument, null); + performGeneration(this.xmlConsumer, null, this.startDocument, null); } public void performGeneration(final XMLConsumer consumer, - MyJexlContext jexlContext, JXPathContext jxpathContext, - StartElement macroCall, Event startEvent, Event endEvent) + StartElement macroCall, Event startEvent, Event endEvent) throws SAXException { - cocoon.put("consumer", consumer); - Invoker.execute(this.xmlConsumer, new ExecutionContext(jexlContext, - jxpathContext, this.definitions), null, - startEvent, null, scriptManager); + ((Map)expressionContext.get("cocoon")).put("consumer", consumer); + Invoker.execute(this.xmlConsumer, this.expressionContext, + new ExecutionContext(this.definitions, this.scriptManager), + null, startEvent, null); } /* @@ -290,9 +138,9 @@ .getTemplateProperty(JXTemplateGenerator.CACHE_KEY); try { final Serializable templateKey = - (Serializable) cacheKeyExpr.getValue(globalJexlContext, jxpathContext); + (Serializable) cacheKeyExpr.getValue(this.expressionContext); if (templateKey != null) { - return new JXCacheKey(startDocument.getUri(), templateKey); + return new JXCacheKey(this.startDocument.getUri(), templateKey); } } catch (Exception e) { getLogger().error("error evaluating cache key", e); @@ -309,10 +157,10 @@ JXTExpression validityExpr = (JXTExpression) this.startDocument .getTemplateProperty(JXTemplateGenerator.VALIDITY); try { - final SourceValidity sourceValidity = this.startDocument - .getSourceValidity(); + final SourceValidity sourceValidity = + this.startDocument.getSourceValidity(); final SourceValidity templateValidity = - (SourceValidity) validityExpr.getValue(globalJexlContext, jxpathContext); + (SourceValidity) validityExpr.getValue(this.expressionContext); if (sourceValidity != null && templateValidity != null) { return new JXSourceValidity(sourceValidity, templateValidity); } Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/TransformerAdapter.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/TransformerAdapter.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/TransformerAdapter.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/TransformerAdapter.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/TransformerAdapter.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/TransformerAdapter.java Sun Jan 16 13:35:37 2005 @@ -53,7 +53,7 @@ public void endDocument() throws SAXException { super.endDocument(); - gen.performGeneration(gen.getConsumer(), gen.getJexlContext(), gen.getJXPathContext(), null, getStartEvent(), null); + gen.performGeneration(gen.getConsumer(), null, getStartEvent(), null); } void setConsumer(XMLConsumer consumer) { @@ -91,4 +91,5 @@ super.setConsumer(templateConsumer); templateConsumer.setConsumer(xmlConsumer); } -} \ No newline at end of file +} + Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java Sun Jan 16 13:35:37 2005 @@ -17,35 +17,22 @@ import java.util.Map; -import org.apache.cocoon.template.jxtg.expression.MyJexlContext; -import org.apache.commons.jxpath.JXPathContext; +import org.apache.cocoon.template.jxtg.script.ScriptManager; public class ExecutionContext { - private MyJexlContext jexlContext; - private JXPathContext jxpathContext; private Map definitions; + private ScriptManager scriptManager; - public ExecutionContext(MyJexlContext jexlContext, - JXPathContext jxpathContext, Map definitions) { - this.jexlContext = jexlContext; - this.jxpathContext = jxpathContext; + public ExecutionContext(Map definitions, ScriptManager scriptManager) { this.definitions = definitions; - } - - public MyJexlContext getJexlContext() { - return this.jexlContext; - } - - public JXPathContext getJXPathContext() { - return this.jxpathContext; + this.scriptManager = scriptManager; } public Map getDefinitions() { return this.definitions; } - public ExecutionContext getChildContext(MyJexlContext jexlContext, - JXPathContext jxpathContext) { - return new ExecutionContext(jexlContext, jxpathContext, this.definitions); + public ScriptManager getScriptManager() { + return this.scriptManager; } } Deleted: /cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/JSIntrospector.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/JSIntrospector.java?view=auto&rev=125366 ============================================================================== Deleted: /cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/MyVariables.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/MyVariables.java?view=auto&rev=125366 ============================================================================== Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/expression/JXTExpression.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/expression/JXTExpression.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/expression/JXTExpression.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/expression/JXTExpression.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/expression/JXTExpression.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/expression/JXTExpression.java Sun Jan 16 13:35:37 2005 @@ -17,40 +17,39 @@ import java.io.IOException; import java.io.StringReader; -import java.lang.reflect.InvocationTargetException; import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import org.apache.cocoon.components.expression.Expression; +import org.apache.cocoon.components.expression.ExpressionContext; +import org.apache.cocoon.components.expression.jexl.JexlCompiler; +import org.apache.cocoon.components.expression.jxpath.JXPathCompiler; +import org.apache.cocoon.components.expression.jxpath.JXPathExpression; import org.apache.cocoon.template.jxtg.environment.ErrorHolder; -import org.apache.cocoon.template.jxtg.environment.JSIntrospector; -import org.apache.commons.jexl.Expression; -import org.apache.commons.jexl.ExpressionFactory; -import org.apache.commons.jexl.JexlContext; -import org.apache.commons.jexl.util.Introspector; -import org.apache.commons.jexl.util.introspection.Info; -import org.apache.commons.jxpath.CompiledExpression; -import org.apache.commons.jxpath.JXPathContext; -import org.apache.commons.jxpath.Pointer; -import org.mozilla.javascript.NativeArray; -import org.w3c.dom.Node; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class JXTExpression extends Subst { - // Factory classes + private String raw; + private Object compiledExpression; - public static JXTExpression compile(final String variable, boolean xpath) - throws Exception { - Object compiled; + private static JXPathCompiler jxpathCompiler = new JXPathCompiler(); + private static JexlCompiler jexlCompiler = new JexlCompiler(); + private static String JXPATH = "jxpath"; + private static String JEXL = "jexl"; + + // Factory methods + + public static JXTExpression compile(final String expression, boolean xpath) + throws Exception { + Expression compiled; if (xpath) { - compiled = JXPathContext.compile(variable); + compiled = jxpathCompiler.compile(JXPATH, expression); } else { - compiled = ExpressionFactory.createExpression(variable); + compiled = jexlCompiler.compile(JEXL, expression); } - return new JXTExpression(variable, compiled); + return new JXTExpression(expression, compiled); } public static JXTExpression compileBoolean(String val, String msg, @@ -126,7 +125,7 @@ */ public static JXTExpression compileExpr(String expr, String errorPrefix, - Locator location) throws SAXParseException { + Locator location) throws SAXParseException { try { return compileExpr(expr); } catch (Exception exc) { @@ -141,14 +140,11 @@ // Members - public JXTExpression(String raw, Object expr) { + private JXTExpression(String raw, Object expr) { this.raw = raw; this.compiledExpression = expr; } - String raw; - Object compiledExpression; - private Object getCompiledExpression() { return compiledExpression; } @@ -164,88 +160,21 @@ // Geting the value of the expression in various forms // Hack: try to prevent JXPath from converting result to a String - public Object getNode(JexlContext jexlContext, JXPathContext jxpathContext, Boolean lenient) + public Object getNode(ExpressionContext expressionContext) throws Exception { - try { - Object compiled = this.getCompiledExpression(); - if (compiled instanceof CompiledExpression) { - CompiledExpression e = (CompiledExpression)compiled; - boolean oldLenient = jxpathContext.isLenient(); - if (lenient != null) jxpathContext.setLenient(lenient.booleanValue()); - try { - Iterator iter = e.iteratePointers(jxpathContext); - if (iter.hasNext()) { - Pointer first = (Pointer)iter.next(); - if (iter.hasNext()) { - List result = new LinkedList(); - result.add(first.getNode()); - boolean dom = (first.getNode() instanceof Node); - while (iter.hasNext()) { - Object obj = ((Pointer)iter.next()).getNode(); - dom = dom && (obj instanceof Node); - result.add(obj); - } - Object[] arr; - if (dom) { - arr = new Node[result.size()]; - } else { - arr = new Object[result.size()]; - } - result.toArray(arr); - return arr; - } - return first.getNode(); - } - return null; - } finally { - jxpathContext.setLenient(oldLenient); - } - } else if (compiled instanceof Expression) { - Expression e = (Expression)compiled; - return e.evaluate(jexlContext); - } - return this.getRaw(); - } catch (InvocationTargetException e) { - Throwable t = e.getTargetException(); - if (t instanceof Exception) { - throw (Exception)t; - } - throw (Error)t; - } + Object compiled = this.getCompiledExpression(); + if (compiled instanceof Expression) + return ((Expression)compiled).getNode(expressionContext); + return this.getRaw(); } - public Object getNode(JexlContext jexlContext, JXPathContext jxpathContext) + public Iterator getIterator(ExpressionContext expressionContext) throws Exception { - return getNode(jexlContext, jxpathContext, null); - } - - public Iterator getIterator(JexlContext jexlContext, JXPathContext jxpathContext, Locator loc) throws Exception { Iterator iter = null; if (this.getCompiledExpression() != null || this.getRaw() != null) { - if (this.getCompiledExpression() instanceof CompiledExpression) { - CompiledExpression compiledExpression = - (CompiledExpression) this.getCompiledExpression(); - Object val = - compiledExpression.getPointer(jxpathContext, this.getRaw()).getNode(); - // FIXME: workaround for JXPath bug + if (this.getCompiledExpression() instanceof Expression) { iter = - val instanceof NativeArray ? - new JSIntrospector.NativeArrayIterator((NativeArray) val) - : compiledExpression.iteratePointers(jxpathContext); - } else if (this.getCompiledExpression() instanceof Expression) { - Expression e = (Expression) this.getCompiledExpression(); - Object result = e.evaluate(jexlContext); - if (result != null) { - iter = Introspector.getUberspect().getIterator( - result, - new Info( - loc.getSystemId(), - loc.getLineNumber(), - loc.getColumnNumber())); - } - if (iter == null) { - iter = EMPTY_ITER; - } + ((Expression)this.getCompiledExpression()).iterate(expressionContext); } else { // literal value iter = new Iterator() { @@ -272,15 +201,15 @@ return iter; } - public Boolean getBooleanValue(JexlContext jexlContext, JXPathContext jxpathContext) + public Boolean getBooleanValue(ExpressionContext expressionContext) throws Exception { - Object res = getValue(jexlContext, jxpathContext); + Object res = getValue(expressionContext); return res instanceof Boolean ? (Boolean)res : null; } - public String getStringValue(JexlContext jexlContext, JXPathContext jxpathContext) + public String getStringValue(ExpressionContext expressionContext) throws Exception { - Object res = getValue(jexlContext, jxpathContext); + Object res = getValue(expressionContext); if (res != null) { return res.toString(); } @@ -290,9 +219,9 @@ return null; } - public Number getNumberValue(JexlContext jexlContext, JXPathContext jxpathContext) + public Number getNumberValue(ExpressionContext expressionContext) throws Exception { - Object res = getValue(jexlContext, jxpathContext); + Object res = getValue(expressionContext); if (res instanceof Number) { return (Number)res; } @@ -302,48 +231,27 @@ return null; } - public int getIntValue(JexlContext jexlContext, JXPathContext jxpathContext) + public int getIntValue(ExpressionContext expressionContext) throws Exception { - Object res = getValue(jexlContext, jxpathContext); + Object res = getValue(expressionContext); return res instanceof Number ? ((Number)res).intValue() : 0; } - public Object getValue(JexlContext jexlContext, JXPathContext jxpathContext) + public Object getValue(ExpressionContext expressionContext) throws Exception { - return getValue(jexlContext, jxpathContext, null); - } - - public Object getValue(JexlContext jexlContext, JXPathContext jxpathContext, - Boolean lenient) throws Exception { if (this.getCompiledExpression() != null) { Object compiled = this.getCompiledExpression(); - try { - if (compiled instanceof CompiledExpression) { - CompiledExpression e = (CompiledExpression) compiled; - boolean oldLenient = jxpathContext.isLenient(); - if (lenient != null) { - jxpathContext.setLenient(lenient.booleanValue()); - } - try { - return e.getValue(jxpathContext); - } finally { - jxpathContext.setLenient(oldLenient); - } - } else if (compiled instanceof Expression) { - Expression e = (Expression) compiled; - return e.evaluate(jexlContext); - } + if (compiled instanceof Expression) + return ((Expression)compiled).evaluate(expressionContext); + else return compiled; - } catch (InvocationTargetException e) { - Throwable t = e.getTargetException(); - if (t instanceof Exception) { - throw (Exception) t; - } - throw (Error) t; - } - } else { + } else return null; - } + } + + public void setLenient(Boolean lenient) { + if (this.compiledExpression instanceof Expression) + ((Expression)this.compiledExpression).setProperty(JXPathExpression.LENIENT, lenient); } private static final Iterator EMPTY_ITER = new Iterator() { Deleted: /cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/expression/MyJexlContext.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/expression/MyJexlContext.java?view=auto&rev=125366 ============================================================================== Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java Sun Jan 16 13:35:37 2005 @@ -21,14 +21,14 @@ import java.util.Properties; import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.components.expression.ExpressionContext; +import org.apache.cocoon.environment.FlowObjectModelHelper; import org.apache.cocoon.template.jxtg.JXTemplateGenerator; import org.apache.cocoon.template.jxtg.environment.ErrorHolder; import org.apache.cocoon.template.jxtg.environment.ExecutionContext; import org.apache.cocoon.template.jxtg.environment.LocatorFacade; -import org.apache.cocoon.template.jxtg.environment.MyVariables; import org.apache.cocoon.template.jxtg.expression.JXTExpression; import org.apache.cocoon.template.jxtg.expression.Literal; -import org.apache.cocoon.template.jxtg.expression.MyJexlContext; import org.apache.cocoon.template.jxtg.expression.Subst; import org.apache.cocoon.template.jxtg.script.event.*; import org.apache.cocoon.xml.IncludeXMLConsumer; @@ -36,9 +36,6 @@ import org.apache.cocoon.xml.XMLUtils; import org.apache.cocoon.xml.dom.DOMBuilder; import org.apache.cocoon.xml.dom.DOMStreamer; -import org.apache.commons.jxpath.JXPathContext; -import org.apache.commons.jxpath.JXPathContextFactory; -import org.apache.commons.jxpath.Pointer; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.apache.excalibur.xml.sax.XMLizable; @@ -51,18 +48,15 @@ import org.xml.sax.helpers.AttributesImpl; public class Invoker { - private static final JXPathContextFactory jxpathContextFactory = JXPathContextFactory - .newInstance(); private static final Attributes EMPTY_ATTRS = new AttributesImpl(); public static void execute(final XMLConsumer consumer, - ExecutionContext executionContext, StartElement macroCall, - Event startEvent, Event endEvent, ScriptManager scriptManager) + ExpressionContext expressionContext, + ExecutionContext executionContext, + StartElement macroCall, + Event startEvent, Event endEvent) throws SAXException { - MyJexlContext jexlContext = executionContext.getJexlContext(); - JXPathContext jxpathContext = executionContext.getJXPathContext(); - Event ev = startEvent; LocatorFacade loc = new LocatorFacade(ev.getLocation()); consumer.setDocumentLocator(loc); @@ -79,7 +73,7 @@ } else { JXTExpression expr = (JXTExpression) subst; try { - Object val = expr.getNode(jexlContext, jxpathContext); + Object val = expr.getNode(expressionContext); if (val instanceof Node) { executeDOM(consumer, (Node) val); continue; @@ -107,11 +101,12 @@ chars = val != null ? val.toString().toCharArray() : ArrayUtils.EMPTY_CHAR_ARRAY; } catch (Exception e) { - throw new SAXParseException(e.getMessage(), ev - .getLocation(), e); + throw new SAXParseException(e.getMessage(), + ev.getLocation(), e); } catch (Error err) { - throw new SAXParseException(err.getMessage(), ev - .getLocation(), new ErrorHolder(err)); + throw new SAXParseException(err.getMessage(), + ev.getLocation(), + new ErrorHolder(err)); } } consumer.characters(chars, 0, chars.length); @@ -126,12 +121,13 @@ consumer.endPrefixMapping(endPrefixMapping.getPrefix()); } else if (ev instanceof IgnorableWhitespace) { TextEvent text = (TextEvent) ev; - characters(executionContext, text, new CharHandler() { - public void characters(char[] ch, int offset, int len) - throws SAXException { - consumer.ignorableWhitespace(ch, offset, len); - } - }); + characters(expressionContext, executionContext, text, + new CharHandler() { + public void characters(char[] ch, int offset, int len) + throws SAXException { + consumer.ignorableWhitespace(ch, offset, len); + } + }); } else if (ev instanceof SkippedEntity) { SkippedEntity skippedEntity = (SkippedEntity) ev; consumer.skippedEntity(skippedEntity.getName()); @@ -139,8 +135,7 @@ StartIf startIf = (StartIf) ev; Object val; try { - val = startIf.getTest().getValue(jexlContext, jxpathContext, - Boolean.TRUE); + val = startIf.getTest().getValue(expressionContext); } catch (Exception e) { throw new SAXParseException(e.getMessage(), ev.getLocation(), e); } catch (Error err) { @@ -164,30 +159,28 @@ int begin, end, step; String var, varStatus; try { - iter = items.getIterator(jexlContext, jxpathContext, - ev.getLocation()); + iter = items.getIterator(expressionContext); begin = startForEach.getBegin() == null ? 0 - : startForEach.getBegin().getIntValue(jexlContext, jxpathContext); + : startForEach.getBegin().getIntValue(expressionContext); end = startForEach.getEnd() == null ? Integer.MAX_VALUE - : startForEach.getEnd().getIntValue(jexlContext, jxpathContext); + : startForEach.getEnd().getIntValue(expressionContext); step = startForEach.getStep() == null ? 1 - : startForEach.getStep().getIntValue(jexlContext, jxpathContext); - var = startForEach.getVar().getStringValue(jexlContext, jxpathContext); + : startForEach.getStep().getIntValue(expressionContext); + var = startForEach.getVar().getStringValue(expressionContext); varStatus = - startForEach.getVarStatus().getStringValue(jexlContext, jxpathContext); + startForEach.getVarStatus().getStringValue(expressionContext); } catch (Exception exc) { - throw new SAXParseException(exc.getMessage(), ev - .getLocation(), exc); + throw new SAXParseException(exc.getMessage(), + ev.getLocation(), exc); } catch (Error err) { - throw new SAXParseException(err.getMessage(), ev - .getLocation(), new ErrorHolder(err)); + throw new SAXParseException(err.getMessage(), + ev.getLocation(), new ErrorHolder(err)); } - MyJexlContext localJexlContext = new MyJexlContext(jexlContext); - MyVariables localJXPathVariables = new MyVariables( - (MyVariables) jxpathContext.getVariables()); + ExpressionContext localExpressionContext = + new ExpressionContext(expressionContext); int i = 0; // Move to the begin row while (i < begin && iter.hasNext()) { @@ -201,30 +194,14 @@ status.setEnd(end); status.setStep(step); status.setFirst(true); - localJexlContext.put(varStatus, status); - localJXPathVariables.declareVariable(varStatus, status); + localExpressionContext.put(varStatus, status); } int skipCounter, count = 1; - JXPathContext localJXPathContext = null; while (i <= end && iter.hasNext()) { Object value = iter.next(); - if (value instanceof Pointer) { - Pointer ptr = (Pointer) value; - localJXPathContext = jxpathContext - .getRelativeContext(ptr); - try { - value = ptr.getNode(); - } catch (Exception exc) { - throw new SAXParseException(exc.getMessage(), ev - .getLocation(), null); - } - } else { - localJXPathContext = jxpathContextFactory.newContext( - jxpathContext, value); - } - localJXPathContext.setVariables(localJXPathVariables); + localExpressionContext.setContextBean(value); if (var != null) { - localJexlContext.put(var, value); + localExpressionContext.put(var, value); } if (status != null) { status.setIndex(i); @@ -233,10 +210,9 @@ status.setCurrent(value); status.setLast((i == end || !iter.hasNext())); } - execute(consumer, executionContext.getChildContext( - localJexlContext, localJXPathContext), macroCall, - startForEach.getNext(), startForEach - .getEndInstruction(), scriptManager); + execute(consumer, localExpressionContext, executionContext, + macroCall, startForEach.getNext(), + startForEach.getEndInstruction()); // Skip rows skipCounter = step; while (--skipCounter > 0 && iter.hasNext()) { @@ -254,8 +230,7 @@ while (startWhen != null) { Object val; try { - val = startWhen.getTest().getValue(jexlContext, jxpathContext, - Boolean.TRUE); + val = startWhen.getTest().getValue(expressionContext); } catch (Exception e) { throw new SAXParseException(e.getMessage(), ev.getLocation(), e); } @@ -266,17 +241,17 @@ result = (val != null); } if (result) { - execute(consumer, executionContext, macroCall, - startWhen.getNext(), startWhen - .getEndInstruction(), scriptManager); + execute(consumer, expressionContext, executionContext, + macroCall, startWhen.getNext(), + startWhen.getEndInstruction()); break; } startWhen = startWhen.getNextChoice(); } if (startWhen == null && startChoose.getOtherwise() != null) { - execute(consumer, executionContext, macroCall, startChoose - .getOtherwise().getNext(), startChoose - .getOtherwise().getEndInstruction(), scriptManager); + execute(consumer, expressionContext, executionContext, + macroCall, startChoose.getOtherwise().getNext(), + startChoose.getOtherwise().getEndInstruction()); } ev = startChoose.getEndInstruction().getNext(); continue; @@ -286,17 +261,19 @@ String var = null; try { if (startSet.getVar() != null) { - var = startSet.getVar().getStringValue(jexlContext, jxpathContext); + var = startSet.getVar().getStringValue(expressionContext); } if (startSet.getValue() != null) { - value = startSet.getValue().getNode(jexlContext, jxpathContext); + value = startSet.getValue().getNode(expressionContext); } } catch (Exception exc) { throw new SAXParseException(exc.getMessage(), ev.getLocation(), exc); } if (value == null) { - NodeList nodeList = toDOMNodeList("set", startSet, - executionContext, macroCall, scriptManager); + NodeList nodeList = + toDOMNodeList("set", startSet, + expressionContext, executionContext, + macroCall); // JXPath doesn't handle NodeList, so convert it to an array int len = nodeList.getLength(); Node[] nodeArr = new Node[len]; @@ -306,8 +283,7 @@ value = nodeArr; } if (var != null) { - jxpathContext.getVariables().declareVariable(var, value); - jexlContext.put(var, value); + expressionContext.put(var, value); } ev = startSet.getEndInstruction().getNext(); continue; @@ -329,25 +305,25 @@ } else if (attrEvent instanceof SubstituteAttribute) { SubstituteAttribute substEvent = (SubstituteAttribute) attrEvent; if (substEvent.getSubstitutions().size() == 1 - && substEvent.getSubstitutions().get(0) instanceof JXTExpression) { + && substEvent.getSubstitutions().get(0) instanceof JXTExpression) { JXTExpression expr = (JXTExpression) substEvent .getSubstitutions().get(0); Object val; try { - val = expr.getNode(jexlContext, jxpathContext); + val = expr.getNode(expressionContext); } catch (Exception e) { throw new SAXParseException(e.getMessage(), - ev.getLocation(), e); + ev.getLocation(), e); } catch (Error err) { - throw new SAXParseException(err - .getMessage(), ev.getLocation(), - new ErrorHolder(err)); + throw new SAXParseException(err.getMessage(), + ev.getLocation(), + new ErrorHolder(err)); } attributeValue = val != null ? val : ""; } else { StringBuffer buf = new StringBuffer(); - Iterator iterSubst = substEvent - .getSubstitutions().iterator(); + Iterator iterSubst = + substEvent.getSubstitutions().iterator(); while (iterSubst.hasNext()) { Subst subst = (Subst) iterSubst.next(); if (subst instanceof Literal) { @@ -357,19 +333,16 @@ JXTExpression expr = (JXTExpression) subst; Object val; try { - val = expr.getValue(jexlContext, jxpathContext); + val = expr.getValue(expressionContext); } catch (Exception e) { - throw new SAXParseException(e - .getMessage(), ev - .getLocation(), e); + throw new SAXParseException(e.getMessage(), + ev.getLocation(), e); } catch (Error err) { - throw new SAXParseException(err - .getMessage(), ev - .getLocation(), - new ErrorHolder(err)); + throw new SAXParseException(err.getMessage(), + ev.getLocation(), + new ErrorHolder(err)); } - buf.append(val != null ? val.toString() - : ""); + buf.append(val != null ? val.toString() : ""); } } attributeValue = buf.toString(); @@ -379,37 +352,28 @@ } attributeMap.put(attributeName, attributeValue); } - MyVariables parent = (MyVariables) jxpathContext - .getVariables(); - MyVariables vars = new MyVariables(parent); - MyJexlContext localJexlContext = new MyJexlContext( - jexlContext); + ExpressionContext localExpressionContext = + new ExpressionContext(expressionContext); HashMap macro = new HashMap(); macro.put("body", startElement); macro.put("arguments", attributeMap); - localJexlContext.put("macro", macro); - vars.declareVariable("macro", macro); + localExpressionContext.put("macro", macro); Iterator iter = def.getParameters().entrySet().iterator(); while (iter.hasNext()) { Map.Entry e = (Map.Entry) iter.next(); String key = (String) e.getKey(); - StartParameter startParam = (StartParameter) e - .getValue(); + StartParameter startParam = + (StartParameter) e.getValue(); Object default_ = startParam.getDefaultValue(); Object val = attributeMap.get(key); if (val == null) { val = default_; } - localJexlContext.put(key, val); - vars.declareVariable(key, val); + localExpressionContext.put(key, val); } - JXPathContext localJXPathContext = jxpathContextFactory - .newContext(null, jxpathContext.getContextBean()); - localJXPathContext.setVariables(vars); call(ev.getLocation(), startElement, consumer, - executionContext.getChildContext(localJexlContext, - localJXPathContext), def.getBody(), def - .getEndInstruction(), scriptManager); + localExpressionContext, executionContext, + def.getBody(), def.getEndInstruction()); ev = startElement.getEndElement().getNext(); continue; } @@ -419,14 +383,13 @@ AttributeEvent attrEvent = (AttributeEvent) i.next(); if (attrEvent instanceof CopyAttribute) { CopyAttribute copy = (CopyAttribute) attrEvent; - attrs.addAttribute(copy.getNamespaceURI(), copy - .getLocalName(), copy.getRaw(), copy.getType(), - copy.getValue()); + attrs.addAttribute(copy.getNamespaceURI(), + copy.getLocalName(), copy.getRaw(), + copy.getType(), copy.getValue()); } else if (attrEvent instanceof SubstituteAttribute) { StringBuffer buf = new StringBuffer(); SubstituteAttribute substEvent = (SubstituteAttribute) attrEvent; - Iterator iterSubst = substEvent.getSubstitutions() - .iterator(); + Iterator iterSubst = substEvent.getSubstitutions().iterator(); while (iterSubst.hasNext()) { Subst subst = (Subst) iterSubst.next(); if (subst instanceof Literal) { @@ -436,47 +399,44 @@ JXTExpression expr = (JXTExpression) subst; Object val; try { - val = expr.getValue(jexlContext, jxpathContext); + val = expr.getValue(expressionContext); } catch (Exception e) { throw new SAXParseException(e.getMessage(), - ev.getLocation(), e); + ev.getLocation(), e); } catch (Error err) { - throw new SAXParseException(err - .getMessage(), ev.getLocation(), - new ErrorHolder(err)); + throw new SAXParseException(err.getMessage(), + ev.getLocation(), + new ErrorHolder(err)); } buf.append(val != null ? val.toString() : ""); } } attrs.addAttribute(attrEvent.getNamespaceURI(), - attrEvent.getLocalName(), attrEvent.getRaw(), - attrEvent.getType(), buf.toString()); + attrEvent.getLocalName(), attrEvent.getRaw(), + attrEvent.getType(), buf.toString()); } } consumer.startElement(startElement.getNamespaceURI(), - startElement.getLocalName(), startElement.getRaw(), - attrs); + startElement.getLocalName(), startElement.getRaw(), + attrs); } else if (ev instanceof StartFormatNumber) { StartFormatNumber startFormatNumber = (StartFormatNumber) ev; try { - String result = startFormatNumber.format(jexlContext, - jxpathContext); + String result = startFormatNumber.format(expressionContext); if (result != null) { char[] chars = result.toCharArray(); consumer.characters(chars, 0, chars.length); } } catch (Exception e) { - throw new SAXParseException(e.getMessage(), ev - .getLocation(), e); + throw new SAXParseException(e.getMessage(), ev.getLocation(), e); } catch (Error err) { - throw new SAXParseException(err.getMessage(), ev - .getLocation(), new ErrorHolder(err)); + throw new SAXParseException(err.getMessage(), ev.getLocation(), + new ErrorHolder(err)); } } else if (ev instanceof StartFormatDate) { StartFormatDate startFormatDate = (StartFormatDate) ev; try { - String result = startFormatDate.format(jexlContext, - jxpathContext); + String result = startFormatDate.format(expressionContext); if (result != null) { char[] chars = result.toCharArray(); consumer.characters(chars, 0, chars.length); @@ -495,8 +455,10 @@ } else if (ev instanceof StartComment) { StartComment startJXComment = (StartComment) ev; // Parse the body of the comment - NodeList nodeList = toDOMNodeList("comment", startJXComment, - executionContext, macroCall, scriptManager); + NodeList nodeList = + toDOMNodeList("comment", startJXComment, + expressionContext, executionContext, + macroCall); // JXPath doesn't handle NodeList, so convert it to an array int len = nodeList.getLength(); final StringBuffer buf = new StringBuffer(); @@ -511,7 +473,7 @@ // header } catch (ProcessingException e) { throw new SAXParseException(e.getMessage(), - startJXComment.getLocation(), e); + startJXComment.getLocation(), e); } } char[] chars = new char[buf.length()]; @@ -537,8 +499,7 @@ StartOut startOut = (StartOut) ev; Object val; try { - val = startOut.getCompiledExpression().getNode(jexlContext, jxpathContext, - startOut.getLenient()); + val = startOut.getCompiledExpression().getNode(expressionContext); if (val instanceof Node) { executeDOM(consumer, (Node) val); } else if (val instanceof NodeList) { @@ -573,14 +534,14 @@ StartEval startEval = (StartEval) ev; JXTExpression expr = startEval.getValue(); try { - Object val = expr.getNode(jexlContext, jxpathContext); + Object val = expr.getNode(expressionContext); if (!(val instanceof StartElement)) { throw new Exception( "macro invocation required instead of: " + val); } StartElement call = (StartElement) val; - execute(consumer, executionContext, call, call.getNext(), - call.getEndElement(), scriptManager); + execute(consumer, expressionContext, executionContext, + call, call.getNext(), call.getEndElement()); } catch (Exception exc) { throw new SAXParseException(exc.getMessage(), ev .getLocation(), exc); @@ -593,22 +554,22 @@ } else if (ev instanceof StartEvalBody) { StartEvalBody startEval = (StartEvalBody) ev; try { - execute(consumer, executionContext, null, macroCall - .getNext(), macroCall.getEndElement(), - scriptManager); + execute(consumer, expressionContext, executionContext, + null, macroCall.getNext(), macroCall.getEndElement()); } catch (Exception exc) { - throw new SAXParseException(exc.getMessage(), ev - .getLocation(), exc); + throw new SAXParseException(exc.getMessage(), + ev.getLocation(), exc); } catch (Error err) { - throw new SAXParseException(err.getMessage(), ev - .getLocation(), new ErrorHolder(err)); + throw new SAXParseException(err.getMessage(), + ev.getLocation(), + new ErrorHolder(err)); } ev = startEval.getEndInstruction().getNext(); continue; } else if (ev instanceof StartDefine) { StartDefine startDefine = (StartDefine) ev; executionContext.getDefinitions().put(startDefine.getQname(), - startDefine); + startDefine); ev = startDefine.getEndInstruction().getNext(); continue; } else if (ev instanceof StartImport) { @@ -631,7 +592,7 @@ JXTExpression expr = (JXTExpression) subst; Object val; try { - val = expr.getValue(jexlContext, jxpathContext); + val = expr.getValue(expressionContext); } catch (Exception exc) { throw new SAXParseException(exc.getMessage(), ev.getLocation(), exc); @@ -646,33 +607,32 @@ } StartDocument doc; try { - doc = scriptManager.resolveTemplate(uri); + doc = executionContext.getScriptManager().resolveTemplate(uri); } catch (ProcessingException exc) { throw new SAXParseException(exc.getMessage(), ev .getLocation(), exc); } - JXPathContext selectJXPath = jxpathContext; - MyJexlContext selectJexl = jexlContext; + ExpressionContext selectExpressionContext = expressionContext; if (startImport.getSelect() != null) { try { Object obj = - startImport.getSelect().getValue(jexlContext, jxpathContext); - selectJXPath = jxpathContextFactory.newContext(null, obj); - selectJXPath.setVariables(jxpathContext.getVariables()); - selectJexl = new MyJexlContext(jexlContext); - JXTemplateGenerator.fillContext(obj, selectJexl); + startImport.getSelect().getValue(expressionContext); + selectExpressionContext = + new ExpressionContext(expressionContext); + selectExpressionContext.setContextBean(obj); + FlowObjectModelHelper.fillContext(obj, selectExpressionContext); } catch (Exception exc) { - throw new SAXParseException(exc.getMessage(), ev - .getLocation(), exc); + throw new SAXParseException(exc.getMessage(), + ev.getLocation(), exc); } catch (Error err) { - throw new SAXParseException(err.getMessage(), ev - .getLocation(), new ErrorHolder(err)); + throw new SAXParseException(err.getMessage(), + ev.getLocation(), + new ErrorHolder(err)); } } try { - execute(consumer, executionContext.getChildContext( - selectJexl, selectJXPath), macroCall, - doc.getNext(), doc.getEndDocument(), scriptManager); + execute(consumer, expressionContext, executionContext, + macroCall, doc.getNext(), doc.getEndDocument()); } catch (Exception exc) { throw new SAXParseException( "Exception occurred in imported template " + uri @@ -701,8 +661,10 @@ throws SAXException; } - private static void characters(ExecutionContext executionContext, - TextEvent event, CharHandler handler) throws SAXException { + private static void characters(ExpressionContext expressionContext, + ExecutionContext executionContext, + TextEvent event, CharHandler handler) + throws SAXException { Iterator iter = event.getSubstitutions().iterator(); while (iter.hasNext()) { Object subst = iter.next(); @@ -712,8 +674,7 @@ } else { JXTExpression expr = (JXTExpression) subst; try { - Object val = expr.getValue(executionContext.getJexlContext(), - executionContext.getJXPathContext()); + Object val = expr.getValue(expressionContext); chars = val != null ? val.toString().toCharArray() : ArrayUtils.EMPTY_CHAR_ARRAY; } catch (Exception e) { @@ -740,28 +701,32 @@ } private static void call(Locator location, StartElement macroCall, - final XMLConsumer consumer, ExecutionContext executionContext, - Event startEvent, Event endEvent, ScriptManager scriptManager) - throws SAXException { + final XMLConsumer consumer, + ExpressionContext expressionContext, + ExecutionContext executionContext, + Event startEvent, Event endEvent) + throws SAXException { try { - execute(consumer, executionContext, macroCall, startEvent, - endEvent, scriptManager); + execute(consumer, expressionContext, executionContext, + macroCall, startEvent, endEvent); } catch (SAXParseException exc) { throw new SAXParseException(macroCall.getLocalName() + ": " - + exc.getMessage(), location, exc); + + exc.getMessage(), location, exc); } } private static NodeList toDOMNodeList(String elementName, - StartInstruction si, ExecutionContext executionContext, - StartElement macroCall, ScriptManager scriptManager) + StartInstruction si, + ExpressionContext expressionContext, + ExecutionContext executionContext, + StartElement macroCall) throws SAXException { DOMBuilder builder = new DOMBuilder(); builder.startDocument(); builder.startElement(JXTemplateGenerator.NS, elementName, elementName, - EMPTY_ATTRS); - execute(builder, executionContext, macroCall, si.getNext(), si - .getEndInstruction(), scriptManager); + EMPTY_ATTRS); + execute(builder, expressionContext, executionContext, + macroCall, si.getNext(), si.getEndInstruction()); builder.endElement(JXTemplateGenerator.NS, elementName, elementName); builder.endDocument(); Node node = builder.getDocument().getDocumentElement(); Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Parser.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Parser.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Parser.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Parser.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Parser.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Parser.java Sun Jan 16 13:35:37 2005 @@ -56,6 +56,8 @@ final static String FORMAT_DATE = "formatDate"; final static String COMMENT = "comment"; + public static final Locator NULL_LOCATOR = new LocatorImpl(); + public Parser() { // EMPTY } @@ -103,8 +105,7 @@ throws SAXException { if (charBuf == null) { charBuf = new StringBuffer(length); - charLocation = locator != null ? new LocatorImpl(locator) - : JXTemplateGenerator.NULL_LOCATOR; + charLocation = locator != null ? new LocatorImpl(locator) : NULL_LOCATOR; } charBuf.append(ch, start, length); } Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/Event.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/Event.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/Event.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/Event.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/Event.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/Event.java Sun Jan 16 13:35:37 2005 @@ -23,9 +23,10 @@ protected final Locator location; protected Event next; // in document order + public static final Locator NULL_LOCATOR = new LocatorImpl(); + public Event(Locator locator) { - this.location = locator != null ? new LocatorImpl(locator) - : JXTemplateGenerator.NULL_LOCATOR; + this.location = locator != null ? new LocatorImpl(locator) : NULL_LOCATOR; } public final Locator getLocation() { @@ -53,4 +54,4 @@ } return buf.toString(); } -} \ No newline at end of file +} Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatDate.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatDate.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatDate.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatDate.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatDate.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatDate.java Sun Jan 16 13:35:37 2005 @@ -21,10 +21,9 @@ import java.util.Stack; import java.util.TimeZone; +import org.apache.cocoon.components.expression.ExpressionContext; import org.apache.cocoon.template.jxtg.environment.ValueHelper; import org.apache.cocoon.template.jxtg.expression.JXTExpression; -import org.apache.commons.jexl.JexlContext; -import org.apache.commons.jxpath.JXPathContext; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; @@ -61,17 +60,16 @@ this.locale = JXTExpression.compileExpr(attrs.getValue("locale"), null, locator); } - public String format(JexlContext jexl, JXPathContext jxp) throws Exception { - String var = this.var.getStringValue(jexl, jxp); - Object value = this.value.getValue(jexl, jxp); - Object locVal = this.locale.getValue(jexl, jxp); - String pattern = this.pattern.getStringValue(jexl, - jxp); - Object timeZone = this.timeZone.getValue(jexl, jxp); - - String type = this.type.getStringValue(jexl, jxp); - String timeStyle = this.timeStyle.getStringValue(jexl, jxp); - String dateStyle = this.dateStyle.getStringValue(jexl, jxp); + public String format(ExpressionContext expressionContext) throws Exception { + String var = this.var.getStringValue(expressionContext); + Object value = this.value.getValue(expressionContext); + Object locVal = this.locale.getValue(expressionContext); + String pattern = this.pattern.getStringValue(expressionContext); + Object timeZone = this.timeZone.getValue(expressionContext); + + String type = this.type.getStringValue(expressionContext); + String timeStyle = this.timeStyle.getStringValue(expressionContext); + String dateStyle = this.dateStyle.getStringValue(expressionContext); String formatted = null; @@ -113,8 +111,7 @@ } formatted = formatter.format(value); if (var != null) { - jexl.getVars().put(var, formatted); - jxp.getVariables().declareVariable(var, formatted); + expressionContext.put(var, formatted); return null; } return formatted; Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatNumber.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatNumber.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatNumber.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatNumber.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatNumber.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatNumber.java Sun Jan 16 13:35:37 2005 @@ -22,10 +22,9 @@ import java.util.Locale; import java.util.Stack; +import org.apache.cocoon.components.expression.ExpressionContext; import org.apache.cocoon.template.jxtg.environment.ValueHelper; import org.apache.cocoon.template.jxtg.expression.JXTExpression; -import org.apache.commons.jexl.JexlContext; -import org.apache.commons.jxpath.JXPathContext; import org.apache.commons.lang.StringUtils; import org.xml.sax.Attributes; import org.xml.sax.Locator; @@ -89,20 +88,20 @@ this.var = JXTExpression.compileExpr(attrs.getValue("var"), null, locator); } - public String format(JexlContext jexl, JXPathContext jxp) throws Exception { + public String format(ExpressionContext expressionContext) throws Exception { // Determine formatting locale - String var = this.var.getStringValue(jexl, jxp); - Number input = this.value.getNumberValue(jexl, jxp); - String type = this.type.getStringValue(jexl, jxp); - String pattern = this.pattern.getStringValue(jexl, jxp); - String currencyCode = this.currencyCode.getStringValue(jexl, jxp); - String currencySymbol = this.currencySymbol.getStringValue(jexl, jxp); - Boolean isGroupingUsed = this.isGroupingUsed.getBooleanValue(jexl, jxp); - Number maxIntegerDigits = this.maxIntegerDigits.getNumberValue(jexl, jxp); - Number minIntegerDigits = this.minIntegerDigits.getNumberValue(jexl, jxp); - Number maxFractionDigits = this.maxFractionDigits.getNumberValue(jexl, jxp); - Number minFractionDigits = this.minFractionDigits.getNumberValue(jexl, jxp); - String localeStr = this.locale.getStringValue(jexl, jxp); + String var = this.var.getStringValue(expressionContext); + Number input = this.value.getNumberValue(expressionContext); + String type = this.type.getStringValue(expressionContext); + String pattern = this.pattern.getStringValue(expressionContext); + String currencyCode = this.currencyCode.getStringValue(expressionContext); + String currencySymbol = this.currencySymbol.getStringValue(expressionContext); + Boolean isGroupingUsed = this.isGroupingUsed.getBooleanValue(expressionContext); + Number maxIntegerDigits = this.maxIntegerDigits.getNumberValue(expressionContext); + Number minIntegerDigits = this.minIntegerDigits.getNumberValue(expressionContext); + Number maxFractionDigits = this.maxFractionDigits.getNumberValue(expressionContext); + Number minFractionDigits = this.minFractionDigits.getNumberValue(expressionContext); + String localeStr = this.locale.getStringValue(expressionContext); Locale loc = localeStr != null ? ValueHelper.parseLocale(localeStr, null) : Locale.getDefault(); @@ -129,8 +128,7 @@ formatted = input.toString(); } if (var != null) { - jexl.getVars().put(var, formatted); - jxp.getVariables().declareVariable(var, formatted); + expressionContext.put(var, formatted); return null; } return formatted; Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartIf.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartIf.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartIf.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartIf.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartIf.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartIf.java Sun Jan 16 13:35:37 2005 @@ -35,6 +35,8 @@ String test = attrs.getValue("test"); if (test != null) { this.test = JXTExpression.compileExpr(test, "if: \"test\": ", locator); + // Why is test lenient? + this.test.setLenient(Boolean.TRUE); } else { throw new SAXParseException("if: \"test\" is required", locator, null); } Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartOut.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartOut.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartOut.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartOut.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartOut.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartOut.java Sun Jan 16 13:35:37 2005 @@ -25,7 +25,6 @@ public class StartOut extends StartInstruction { private final JXTExpression compiledExpression; - private final Boolean lenient; public StartOut(StartElement raw, Attributes attrs, Stack stack) throws SAXException { @@ -37,7 +36,9 @@ this.compiledExpression = JXTExpression.compileExpr(value, "out: \"value\": ", locator); String lenientValue = attrs.getValue("lenient"); - this.lenient = lenientValue == null ? null : Boolean.valueOf(lenientValue); + Boolean lenient = lenientValue == null ? null : Boolean.valueOf(lenientValue); + // Why can out be lenient? + this.compiledExpression.setLenient(lenient); } else { throw new SAXParseException("out: \"value\" is required", locator, null); } @@ -45,9 +46,5 @@ public JXTExpression getCompiledExpression() { return compiledExpression; - } - - public Boolean getLenient() { - return lenient; } } Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartWhen.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartWhen.java?view=diff&rev=125367&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartWhen.java&r1=125366&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartWhen.java&r2=125367 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartWhen.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartWhen.java Sun Jan 16 13:35:37 2005 @@ -40,6 +40,8 @@ String test = attrs.getValue("test"); if (test != null) { this.test = JXTExpression.compileExpr(test, "when: \"test\": ", locator); + // Why is test lenient? + this.test.setLenient(Boolean.TRUE); StartChoose startChoose = (StartChoose) stack.peek(); if (startChoose.getFirstChoice() != null) {