Author: lgawron Date: Tue Dec 14 15:24:43 2004 New Revision: 111901 URL: http://svn.apache.org/viewcvs?view=rev&rev=111901 Log: ScriptManager - manages template compilation and caches the compiled form. ScriptManager is NOT static (previous cache was). TODO: caching should not be performed in a HashMap, rather cocoon caching mechanisms should be used. Added: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/ScriptManager.java (contents, props changed) Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.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/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/StartDefine.java cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDocument.java
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=111901&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java&r1=111900&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/JXTemplateGenerator.java&r2=111901 ============================================================================== --- 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 Tue Dec 14 15:24:43 2004 @@ -21,16 +21,16 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import org.apache.avalon.framework.parameters.Parameters; +import org.apache.avalon.framework.service.ServiceException; +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.components.source.SourceUtil; import org.apache.cocoon.environment.ObjectModelHelper; import org.apache.cocoon.environment.Request; import org.apache.cocoon.environment.SourceResolver; @@ -44,10 +44,11 @@ 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.Parser; -import org.apache.cocoon.template.jxtg.script.event.*; +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.JexlContext; import org.apache.commons.jexl.util.Introspector; import org.apache.commons.jxpath.DynamicPropertyHandler; import org.apache.commons.jxpath.JXPathBeanInfo; @@ -55,8 +56,6 @@ import org.apache.commons.jxpath.JXPathContextFactory; import org.apache.commons.jxpath.JXPathIntrospector; import org.apache.commons.jxpath.Variables; -import org.apache.excalibur.source.Source; -import org.apache.excalibur.source.SourceException; import org.apache.excalibur.source.SourceValidity; import org.xml.sax.Attributes; import org.xml.sax.Locator; @@ -79,7 +78,8 @@ * @cocoon.sitemap.component.pooling.grow 2 * * - * @version CVS $Id$ + * @version CVS $Id: JXTemplateGenerator.java 111658 2004-12-12 17:28:46Z + * danielf $ */ public class JXTemplateGenerator extends ServiceableGenerator implements CacheableProcessingComponent { @@ -90,34 +90,6 @@ .newInstance(); private static final Attributes EMPTY_ATTRS = new AttributesImpl(); - private static final Iterator EMPTY_ITER = new Iterator() { - public boolean hasNext() { - return false; - } - - public Object next() { - return null; - } - - public void remove() { - // EMPTY - } - }; - - private static final Iterator NULL_ITER = new Iterator() { - public boolean hasNext() { - return true; - } - - public Object next() { - return null; - } - - public void remove() { - // EMPTY - } - }; - public static final Locator NULL_LOCATOR = new LocatorImpl(); public final static String CACHE_KEY = "cache-key"; @@ -126,8 +98,8 @@ private JXPathContext jxpathContext; private MyJexlContext globalJexlContext; private Variables variables; - private static Map cache = new HashMap(); - private Source inputSource; + private ScriptManager scriptManager = new ScriptManager(); + private StartDocument startDocument; private Map definitions; private Map cocoon; @@ -154,11 +126,13 @@ return globalJexlContext; } + public void service(ServiceManager manager) throws ServiceException { + super.service(manager); + scriptManager.setServiceManager(manager); + } + public void recycle() { - if (this.resolver != null) { - this.resolver.release(this.inputSource); - } - this.inputSource = null; + this.startDocument = null; this.jxpathContext = null; this.globalJexlContext = null; this.variables = null; @@ -179,46 +153,9 @@ IOException { super.setup(resolver, objectModel, src, parameters); - if (src != null) { - try { - this.inputSource = resolver.resolveURI(src); - } catch (SourceException se) { - throw SourceUtil.handle("Error during resolving of '" + src - + "'.", se); - } - final String uri = inputSource.getURI(); - boolean regenerate = false; - StartDocument startEvent = null; - synchronized (cache) { - startEvent = (StartDocument) cache.get(uri); - if (startEvent != null) { - int valid = SourceValidity.UNKNOWN; - if (startEvent.getCompileTime() != null) { - valid = startEvent.getCompileTime().isValid(); - } - if (valid == SourceValidity.UNKNOWN - && startEvent.getCompileTime() != null) { - SourceValidity validity = inputSource.getValidity(); - valid = startEvent.getCompileTime().isValid(validity); - } - if (valid != SourceValidity.VALID) { - cache.remove(uri); - regenerate = true; - } - } else { - regenerate = true; - } - } - if (regenerate) { - Parser parser = new Parser(); - SourceUtil.parse(this.manager, this.inputSource, parser); - startEvent = parser.getStartEvent(); - startEvent.setCompileTime(this.inputSource.getValidity()); - synchronized (cache) { - cache.put(uri, startEvent); - } - } - } + if (src != null) + startDocument = scriptManager.resolveTemplate(src); + Object bean = FlowHelper.getContextObject(objectModel); WebContinuation kont = FlowHelper.getWebContinuation(objectModel); setContexts(bean, kont, parameters, objectModel); @@ -330,14 +267,8 @@ */ public void generate() throws IOException, SAXException, ProcessingException { - final String cacheKey = this.inputSource.getURI(); - - StartDocument startEvent; - synchronized (cache) { - startEvent = (StartDocument) cache.get(cacheKey); - } performGeneration(this.xmlConsumer, globalJexlContext, jxpathContext, - null, startEvent, null); + null, startDocument, null); } public void performGeneration(final XMLConsumer consumer, @@ -345,11 +276,9 @@ StartElement macroCall, Event startEvent, Event endEvent) throws SAXException { cocoon.put("consumer", consumer); - Invoker.execute(this.xmlConsumer, - new ExecutionContext(jexlContext, jxpathContext, - this.variables, this.definitions, this.cache, - this.manager), - null, startEvent, null); + Invoker.execute(this.xmlConsumer, new ExecutionContext(jexlContext, + jxpathContext, this.variables, this.definitions), null, + startEvent, null, scriptManager); } /* @@ -358,12 +287,13 @@ * @see org.apache.cocoon.caching.CacheableProcessingComponent#getKey() */ public Serializable getKey() { - JXTExpression cacheKeyExpr = (JXTExpression) getCurrentTemplateProperty(JXTemplateGenerator.CACHE_KEY); + JXTExpression cacheKeyExpr = (JXTExpression) this.startDocument + .getTemplateProperty(JXTemplateGenerator.CACHE_KEY); try { final Serializable templateKey = (Serializable) ValueHelper .getValue(cacheKeyExpr, globalJexlContext, jxpathContext); if (templateKey != null) { - return new JXCacheKey(this.inputSource.getURI(), templateKey); + return new JXCacheKey(startDocument.getUri(), templateKey); } } catch (Exception e) { getLogger().error("error evaluating cache key", e); @@ -377,10 +307,11 @@ * @see org.apache.cocoon.caching.CacheableProcessingComponent#getValidity() */ public SourceValidity getValidity() { - JXTExpression validityExpr = (JXTExpression) getCurrentTemplateProperty(JXTemplateGenerator.VALIDITY); + JXTExpression validityExpr = (JXTExpression) this.startDocument + .getTemplateProperty(JXTemplateGenerator.VALIDITY); try { - final SourceValidity sourceValidity = this.inputSource - .getValidity(); + final SourceValidity sourceValidity = this.startDocument + .getSourceValidity(); final SourceValidity templateValidity = (SourceValidity) ValueHelper .getValue(validityExpr, globalJexlContext, jxpathContext); if (sourceValidity != null && templateValidity != null) { @@ -391,14 +322,4 @@ } return null; } - - private Object getCurrentTemplateProperty(String propertyName) { - final String uri = this.inputSource.getURI(); - StartDocument startEvent; - synchronized (cache) { - startEvent = (StartDocument) cache.get(uri); - } - return (startEvent != null) ? startEvent.getTemplateProperties().get( - propertyName) : null; - } -} +} \ 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=111901&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java&r1=111900&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/environment/ExecutionContext.java&r2=111901 ============================================================================== --- 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 Tue Dec 14 15:24:43 2004 @@ -17,7 +17,6 @@ import java.util.Map; -import org.apache.avalon.framework.service.ServiceManager; import org.apache.cocoon.template.jxtg.expression.MyJexlContext; import org.apache.commons.jxpath.JXPathContext; import org.apache.commons.jxpath.Variables; @@ -27,18 +26,13 @@ private JXPathContext jxpathContext; private Variables variables; private Map definitions; - private Map cache; - private ServiceManager manager; - public ExecutionContext(MyJexlContext jexlContext, JXPathContext jxpathContext, - Variables variables, Map definitions, Map cache, - ServiceManager manager) { + public ExecutionContext(MyJexlContext jexlContext, + JXPathContext jxpathContext, Variables variables, Map definitions) { this.jexlContext = jexlContext; this.jxpathContext = jxpathContext; this.variables = variables; this.definitions = definitions; - this.cache = cache; - this.manager = manager; } public MyJexlContext getJexlContext() { @@ -57,18 +51,9 @@ return this.definitions; } - public Map getCache() { - return this.cache; - } - - public ServiceManager getServiceManager() { - return this.manager; - } - public ExecutionContext getChildContext(MyJexlContext jexlContext, - JXPathContext jxpathContext) { - return new ExecutionContext(jexlContext, jxpathContext, - this.variables, this.definitions, this.cache, - this.manager); + JXPathContext jxpathContext) { + return new ExecutionContext(jexlContext, jxpathContext, this.variables, + this.definitions); } -} +} \ No newline at end of file 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=111901&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java&r1=111900&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java&r2=111901 ============================================================================== --- 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 Tue Dec 14 15:24:43 2004 @@ -20,10 +20,7 @@ import java.util.Map; import java.util.Properties; -import org.apache.avalon.framework.service.ServiceManager; import org.apache.cocoon.ProcessingException; -import org.apache.cocoon.components.source.SourceUtil; -import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.template.jxtg.JXTemplateGenerator; import org.apache.cocoon.template.jxtg.environment.ErrorHolder; import org.apache.cocoon.template.jxtg.environment.ExecutionContext; @@ -35,7 +32,6 @@ 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.Parser; import org.apache.cocoon.template.jxtg.script.event.*; import org.apache.cocoon.xml.IncludeXMLConsumer; import org.apache.cocoon.xml.XMLConsumer; @@ -43,7 +39,6 @@ import org.apache.cocoon.xml.dom.DOMBuilder; import org.apache.cocoon.xml.dom.DOMStreamer; import org.apache.commons.jexl.Expression; -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; @@ -52,8 +47,6 @@ import org.apache.commons.jxpath.Pointer; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; -import org.apache.excalibur.source.Source; -import org.apache.excalibur.source.SourceValidity; import org.apache.excalibur.xml.sax.XMLizable; import org.mozilla.javascript.NativeArray; import org.w3c.dom.Node; @@ -65,8 +58,8 @@ import org.xml.sax.helpers.AttributesImpl; public class Invoker { - private static final JXPathContextFactory jxpathContextFactory = - JXPathContextFactory.newInstance(); + private static final JXPathContextFactory jxpathContextFactory = JXPathContextFactory + .newInstance(); private static final Attributes EMPTY_ATTRS = new AttributesImpl(); private static final Iterator EMPTY_ITER = new Iterator() { @@ -97,9 +90,10 @@ } }; - public static void execute(final XMLConsumer consumer, ExecutionContext executionContext, - StartElement macroCall, Event startEvent, Event endEvent) - throws SAXException { + public static void execute(final XMLConsumer consumer, + ExecutionContext executionContext, StartElement macroCall, + Event startEvent, Event endEvent, ScriptManager scriptManager) + throws SAXException { MyJexlContext jexlContext = executionContext.getJexlContext(); JXPathContext jxpathContext = executionContext.getJXPathContext(); @@ -327,9 +321,10 @@ status.setCurrent(value); status.setLast((i == end || !iter.hasNext())); } - execute(consumer, - executionContext.getChildContext(localJexlContext, localJXPathContext), - macroCall, startForEach.getNext(), startForEach.getEndInstruction()); + execute(consumer, executionContext.getChildContext( + localJexlContext, localJXPathContext), macroCall, + startForEach.getNext(), startForEach + .getEndInstruction(), scriptManager); // Skip rows skipCounter = step; while (--skipCounter > 0 && iter.hasNext()) { @@ -347,8 +342,8 @@ while (startWhen != null) { Object val; try { - val = ValueHelper.getValue(startWhen.getTest(), jexlContext, - jxpathContext, Boolean.TRUE); + val = ValueHelper.getValue(startWhen.getTest(), + jexlContext, jxpathContext, Boolean.TRUE); } catch (Exception e) { throw new SAXParseException(e.getMessage(), ev .getLocation(), e); @@ -360,16 +355,17 @@ result = (val != null); } if (result) { - execute(consumer, executionContext, - macroCall, startWhen.getNext(), startWhen.getEndInstruction()); + execute(consumer, executionContext, macroCall, + startWhen.getNext(), startWhen + .getEndInstruction(), scriptManager); break; } startWhen = startWhen.getNextChoice(); } if (startWhen == null && startChoose.getOtherwise() != null) { - execute(consumer, executionContext, macroCall, - startChoose.getOtherwise().getNext(), - startChoose.getOtherwise().getEndInstruction()); + execute(consumer, executionContext, macroCall, startChoose + .getOtherwise().getNext(), startChoose + .getOtherwise().getEndInstruction(), scriptManager); } ev = startChoose.getEndInstruction().getNext(); continue; @@ -392,7 +388,7 @@ } if (value == null) { NodeList nodeList = toDOMNodeList("set", startSet, - executionContext, macroCall); + executionContext, macroCall, scriptManager); // JXPath doesn't handle NodeList, so convert it to an array int len = nodeList.getLength(); Node[] nodeArr = new Node[len]; @@ -409,9 +405,8 @@ continue; } else if (ev instanceof StartElement) { StartElement startElement = (StartElement) ev; - StartDefine def = - (StartDefine) executionContext.getDefinitions().get(startElement - .getQname()); + StartDefine def = (StartDefine) executionContext + .getDefinitions().get(startElement.getQname()); if (def != null) { Map attributeMap = new HashMap(); Iterator i = startElement.getAttributeEvents().iterator(); @@ -506,8 +501,9 @@ .newContext(null, jxpathContext.getContextBean()); localJXPathContext.setVariables(vars); call(ev.getLocation(), startElement, consumer, - executionContext.getChildContext(localJexlContext, localJXPathContext), - def.getBody(), def.getEndInstruction()); + executionContext.getChildContext(localJexlContext, + localJXPathContext), def.getBody(), def + .getEndInstruction(), scriptManager); ev = startElement.getEndElement().getNext(); continue; } @@ -595,7 +591,7 @@ StartComment startJXComment = (StartComment) ev; // Parse the body of the comment NodeList nodeList = toDOMNodeList("comment", startJXComment, - executionContext, macroCall); + executionContext, macroCall, scriptManager); // JXPath doesn't handle NodeList, so convert it to an array int len = nodeList.getLength(); final StringBuffer buf = new StringBuffer(); @@ -679,8 +675,8 @@ "macro invocation required instead of: " + val); } StartElement call = (StartElement) val; - execute(consumer, executionContext, call, call - .getNext(), call.getEndElement()); + execute(consumer, executionContext, call, call.getNext(), + call.getEndElement(), scriptManager); } catch (Exception exc) { throw new SAXParseException(exc.getMessage(), ev .getLocation(), exc); @@ -693,8 +689,9 @@ } else if (ev instanceof StartEvalBody) { StartEvalBody startEval = (StartEvalBody) ev; try { - execute(consumer, executionContext, null, - macroCall.getNext(), macroCall.getEndElement()); + execute(consumer, executionContext, null, macroCall + .getNext(), macroCall.getEndElement(), + scriptManager); } catch (Exception exc) { throw new SAXParseException(exc.getMessage(), ev .getLocation(), exc); @@ -706,7 +703,8 @@ continue; } else if (ev instanceof StartDefine) { StartDefine startDefine = (StartDefine) ev; - executionContext.getDefinitions().put(startDefine.getQname(), startDefine); + executionContext.getDefinitions().put(startDefine.getQname(), + startDefine); ev = startDefine.getEndInstruction().getNext(); continue; } else if (ev instanceof StartImport) { @@ -743,57 +741,12 @@ } uri = buf.toString(); } - Source input = null; StartDocument doc; - ServiceManager manager = executionContext.getServiceManager(); - SourceResolver resolver = null; try { - resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE); - input = resolver.resolveURI(uri); - SourceValidity validity = null; - Map cache = executionContext.getCache(); - synchronized (cache) { - doc = (StartDocument) cache.get(input.getURI()); - if (doc != null) { - boolean recompile = false; - if (doc.getCompileTime() == null) { - recompile = true; - } else { - int valid = doc.getCompileTime().isValid(); - if (valid == SourceValidity.UNKNOWN) { - validity = input.getValidity(); - valid = doc.getCompileTime().isValid( - validity); - } - if (valid != SourceValidity.VALID) { - recompile = true; - } - } - if (recompile) { - doc = null; // recompile - } - } - } - if (doc == null) { - Parser parser = new Parser(); - // call getValidity before using the stream is faster if - // the source is a SitemapSource - if (validity == null) { - validity = input.getValidity(); - } - SourceUtil.parse(manager, input, parser); - doc = parser.getStartEvent(); - doc.setCompileTime(validity); - synchronized (cache) { - cache.put(input.getURI(), doc); - } - } - } catch (Exception exc) { + doc = scriptManager.resolveTemplate(uri); + } catch (ProcessingException exc) { throw new SAXParseException(exc.getMessage(), ev .getLocation(), exc); - } finally { - resolver.release(input); - manager.release(resolver); } JXPathContext selectJXPath = jxpathContext; MyJexlContext selectJexl = jexlContext; @@ -803,7 +756,8 @@ .getSelect(), jexlContext, jxpathContext); selectJXPath = jxpathContextFactory.newContext(null, obj); - selectJXPath.setVariables(executionContext.getVariables()); + selectJXPath.setVariables(executionContext + .getVariables()); selectJexl = new MyJexlContext(jexlContext); JXTemplateGenerator.fillContext(obj, selectJexl); } catch (Exception exc) { @@ -815,9 +769,9 @@ } } try { - execute(consumer, - executionContext.getChildContext(selectJexl, selectJXPath), - macroCall, doc.getNext(), doc.getEndDocument()); + execute(consumer, executionContext.getChildContext( + selectJexl, selectJXPath), macroCall, + doc.getNext(), doc.getEndDocument(), scriptManager); } catch (Exception exc) { throw new SAXParseException( "Exception occurred in imported template " + uri @@ -847,8 +801,7 @@ } private static void characters(ExecutionContext executionContext, - TextEvent event, CharHandler handler) - throws SAXException { + TextEvent event, CharHandler handler) throws SAXException { Iterator iter = event.getSubstitutions().iterator(); while (iter.hasNext()) { Object subst = iter.next(); @@ -858,9 +811,9 @@ } else { JXTExpression expr = (JXTExpression) subst; try { - Object val = ValueHelper.getValue(expr, - executionContext.getJexlContext(), - executionContext.getJXPathContext()); + Object val = ValueHelper.getValue(expr, executionContext + .getJexlContext(), executionContext + .getJXPathContext()); chars = val != null ? val.toString().toCharArray() : ArrayUtils.EMPTY_CHAR_ARRAY; } catch (Exception e) { @@ -887,29 +840,32 @@ } private static void call(Locator location, StartElement macroCall, - final XMLConsumer consumer, ExecutionContext executionContext, - Event startEvent, Event endEvent) - throws SAXException { + final XMLConsumer consumer, ExecutionContext executionContext, + Event startEvent, Event endEvent, ScriptManager scriptManager) + throws SAXException { try { - execute(consumer, executionContext, macroCall, startEvent, endEvent); + execute(consumer, executionContext, macroCall, startEvent, + endEvent, scriptManager); } catch (SAXParseException exc) { throw new SAXParseException(macroCall.getLocalName() + ": " + exc.getMessage(), location, exc); } } - private static NodeList toDOMNodeList(String elementName, StartInstruction si, - ExecutionContext executionContext, StartElement macroCall) + private static NodeList toDOMNodeList(String elementName, + StartInstruction si, ExecutionContext executionContext, + StartElement macroCall, ScriptManager scriptManager) throws SAXException { DOMBuilder builder = new DOMBuilder(); builder.startDocument(); - builder.startElement(JXTemplateGenerator.NS, elementName, elementName, EMPTY_ATTRS); - execute(builder, executionContext, macroCall, si.getNext(), - si.getEndInstruction()); + builder.startElement(JXTemplateGenerator.NS, elementName, elementName, + EMPTY_ATTRS); + execute(builder, executionContext, macroCall, si.getNext(), si + .getEndInstruction(), scriptManager); builder.endElement(JXTemplateGenerator.NS, elementName, elementName); builder.endDocument(); Node node = builder.getDocument().getDocumentElement(); return node.getChildNodes(); } -} +} \ No newline at end of file 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=111901&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Parser.java&r1=111900&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Parser.java&r2=111901 ============================================================================== --- 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 Tue Dec 14 15:24:43 2004 @@ -17,7 +17,6 @@ import java.io.IOException; import java.io.StringReader; -import java.util.Iterator; import java.util.Stack; import org.apache.cocoon.template.jxtg.JXTemplateGenerator; Added: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/ScriptManager.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/ScriptManager.java?view=auto&rev=111901 ============================================================================== --- (empty file) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/ScriptManager.java Tue Dec 14 15:24:43 2004 @@ -0,0 +1,117 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed 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.cocoon.template.jxtg.script; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.avalon.framework.service.ServiceManager; +import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.components.source.SourceUtil; +import org.apache.cocoon.template.jxtg.script.event.StartDocument; +import org.apache.excalibur.source.Source; +import org.apache.excalibur.source.SourceException; +import org.apache.excalibur.source.SourceResolver; +import org.apache.excalibur.source.SourceValidity; +import org.xml.sax.Locator; +import org.xml.sax.SAXParseException; + +public class ScriptManager { + private ServiceManager serviceManager; + private final Map cache = new HashMap(); + + public ScriptManager() { + } + + public ServiceManager getServiceManager() { + return serviceManager; + } + + public void setServiceManager(ServiceManager manager) { + this.serviceManager = manager; + } + + private Map getCache() { + return cache; + } + + public StartDocument resolveTemplate(String uri) throws SAXParseException, + ProcessingException { + return resolveTemplate(uri, null); + } + + public StartDocument resolveTemplate(String uri, Locator location) + throws SAXParseException, ProcessingException { + Source input = null; + StartDocument doc = null; + ServiceManager manager = getServiceManager(); + SourceResolver resolver = null; + try { + resolver = (SourceResolver) getServiceManager().lookup( + SourceResolver.ROLE); + input = resolver.resolveURI(uri); + SourceValidity validity = null; + synchronized (getCache()) { + doc = (StartDocument) getCache().get(input.getURI()); + if (doc != null) { + boolean recompile = false; + if (doc.getSourceValidity() == null) { + recompile = true; + } else { + int valid = doc.getSourceValidity().isValid(); + if (valid == SourceValidity.UNKNOWN) { + validity = input.getValidity(); + valid = doc.getSourceValidity().isValid(validity); + } + if (valid != SourceValidity.VALID) { + recompile = true; + } + } + if (recompile) { + doc = null; // recompile + } + } + } + + if (doc == null) { + Parser parser = new Parser(); + // call getValidity before using the stream is faster if + // the source is a SitemapSource + if (validity == null) { + validity = input.getValidity(); + } + SourceUtil.parse(manager, input, parser); + doc = parser.getStartEvent(); + doc.setUri(input.getURI()); + doc.setSourceValidity(validity); + synchronized (getCache()) { + getCache().put(input.getURI(), doc); + } + } + } catch (SourceException se) { + throw SourceUtil.handle("Error during resolving of '" + uri + "'.", + se); + } catch (Exception exc) { + throw new SAXParseException(exc.getMessage(), location, exc); + } finally { + if (input != null) + resolver.release(input); + if (resolver != null) + manager.release(resolver); + } + return doc; + } +} \ No newline at end of file Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDefine.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDefine.java?view=diff&rev=111901&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDefine.java&r1=111900&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDefine.java&r2=111901 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDefine.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDefine.java Tue Dec 14 15:24:43 2004 @@ -19,8 +19,6 @@ import java.util.Map; import java.util.Stack; -import org.apache.cocoon.template.jxtg.expression.JXTExpression; -import org.apache.cocoon.template.jxtg.script.Parser; import org.apache.commons.lang.StringUtils; import org.xml.sax.Attributes; import org.xml.sax.SAXException; Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDocument.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDocument.java?view=diff&rev=111901&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDocument.java&r1=111900&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDocument.java&r2=111901 ============================================================================== --- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDocument.java (original) +++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDocument.java Tue Dec 14 15:24:43 2004 @@ -27,9 +27,10 @@ templateProperties = new HashMap(); } - private SourceValidity compileTime; - EndDocument endDocument; // null if document fragment - Map templateProperties; + private SourceValidity sourceValidity; + private String uri; + private EndDocument endDocument; // null if document fragment + private Map templateProperties; public EndDocument getEndDocument() { return endDocument; @@ -43,19 +44,34 @@ return templateProperties; } + public String getUri() { + return this.uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + /** - * @param compileTime + * @param sourceValidity * The compileTime to set. */ - public void setCompileTime(SourceValidity compileTime) { - this.compileTime = compileTime; + public void setSourceValidity(SourceValidity sourceValidity) { + this.sourceValidity = sourceValidity; } /** * @return Returns the compileTime. */ - public SourceValidity getCompileTime() { - return compileTime; + public SourceValidity getSourceValidity() { + return sourceValidity; } + /** + * @param validity + * @return + */ + public Object getTemplateProperty(String name) { + return getTemplateProperties().get(name); + } }