cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dani...@apache.org
Subject svn commit: r125680 - in cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script: . event
Date Wed, 19 Jan 2005 22:59:38 GMT
Author: danielf
Date: Wed Jan 19 14:59:37 2005
New Revision: 125680

URL: http://svn.apache.org/viewcvs?view=rev&rev=125680
Log:
Moved instruction execution code from the invoker to execute methods in the instruction classes.
Modified:
   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/event/StartChoose.java
   cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartComment.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/StartEval.java
   cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEvalBody.java
   cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartForEach.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/StartImport.java
   cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartInstruction.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/StartSet.java
   cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartTemplate.java

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=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/Invoker.java&r2=125680
==============================================================================
--- 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	Wed Jan 19 14:59:37 2005
@@ -18,11 +18,8 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
-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;
@@ -33,11 +30,9 @@
 import org.apache.cocoon.template.jxtg.script.event.*;
 import org.apache.cocoon.xml.IncludeXMLConsumer;
 import org.apache.cocoon.xml.XMLConsumer;
-import org.apache.cocoon.xml.XMLUtils;
 import org.apache.cocoon.xml.dom.DOMBuilder;
 import org.apache.cocoon.xml.dom.DOMStreamer;
 import org.apache.commons.lang.ArrayUtils;
-import org.apache.commons.lang.StringUtils;
 import org.apache.excalibur.xml.sax.XMLizable;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -62,6 +57,8 @@
         consumer.setDocumentLocator(loc);
         while (ev != endEvent) {
             loc.setDocumentLocator(ev.getLocation());
+
+            // ContentHandler methods
             if (ev instanceof Characters) {
                 TextEvent text = (TextEvent) ev;
                 Iterator iter = text.getSubstitutions().iterator();
@@ -111,6 +108,8 @@
                     }
                     consumer.characters(chars, 0, chars.length);
                 }
+            } else if (ev instanceof EndDocument) {
+                consumer.endDocument();
             } else if (ev instanceof EndElement) {
                 EndElement endElement = (EndElement) ev;
                 StartElement startElement = endElement.getStartElement();
@@ -128,165 +127,17 @@
                                        consumer.ignorableWhitespace(ch, offset, len);
                                    }
                                });
+            } else if (ev instanceof ProcessingInstruction) {
+                ProcessingInstruction pi = (ProcessingInstruction) ev;
+                consumer.processingInstruction(pi.getTarget(), pi.getData());
             } else if (ev instanceof SkippedEntity) {
                 SkippedEntity skippedEntity = (SkippedEntity) ev;
                 consumer.skippedEntity(skippedEntity.getName());
-            } else if (ev instanceof StartIf) {
-                StartIf startIf = (StartIf) ev;
-                Object val;
-                try {
-                    val = startIf.getTest().getValue(expressionContext);
-                } catch (Exception e) {
-                    throw new SAXParseException(e.getMessage(), ev.getLocation(), e);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(), ev.getLocation(),
-                                                new ErrorHolder(err));
-                }
-                boolean result = false;
-                if (val instanceof Boolean) {
-                    result = ((Boolean) val).booleanValue();
-                } else {
-                    result = (val != null);
-                }
-                if (!result) {
-                    ev = startIf.getEndInstruction().getNext();
-                    continue;
-                }
-            } else if (ev instanceof StartForEach) {
-                StartForEach startForEach = (StartForEach) ev;
-                final JXTExpression items = startForEach.getItems();
-                Iterator iter = null;
-                int begin, end, step;
-                String var, varStatus;
-                try {
-                    iter = items.getIterator(expressionContext);
-                    begin = startForEach.getBegin() == null
-                        ? 0
-                        : startForEach.getBegin().getIntValue(expressionContext);
-                    end = startForEach.getEnd() == null
-                        ? Integer.MAX_VALUE
-                        : startForEach.getEnd().getIntValue(expressionContext);
-                    step = startForEach.getStep() == null
-                        ? 1
-                        : startForEach.getStep().getIntValue(expressionContext);
-                    var = startForEach.getVar().getStringValue(expressionContext);
-                    varStatus =
-                        startForEach.getVarStatus().getStringValue(expressionContext);
-                } catch (Exception exc) {
-                    throw new SAXParseException(exc.getMessage(),
-                                                ev.getLocation(), exc);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(),
-                                                ev.getLocation(), new ErrorHolder(err));
-                }
-                ExpressionContext localExpressionContext =
-                    new ExpressionContext(expressionContext);
-                int i = 0;
-                // Move to the begin row
-                while (i < begin && iter.hasNext()) {
-                    iter.next();
-                    i++;
-                }
-                LoopTagStatus status = null;
-                if (varStatus != null) {
-                    status = new LoopTagStatus();
-                    status.setBegin(begin);
-                    status.setEnd(end);
-                    status.setStep(step);
-                    status.setFirst(true);
-                    localExpressionContext.put(varStatus, status);
-                }
-                int skipCounter, count = 1;
-                while (i <= end && iter.hasNext()) {
-                    Object value = iter.next();
-                    localExpressionContext.setContextBean(value);
-                    if (var != null) {
-                        localExpressionContext.put(var, value);
-                    }
-                    if (status != null) {
-                        status.setIndex(i);
-                        status.setCount(count);
-                        status.setFirst(i == begin);
-                        status.setCurrent(value);
-                        status.setLast((i == end || !iter.hasNext()));
-                    }
-                    execute(consumer, localExpressionContext, executionContext,
-                            macroCall, startForEach.getNext(),
-                            startForEach.getEndInstruction());
-                    // Skip rows
-                    skipCounter = step;
-                    while (--skipCounter > 0 && iter.hasNext()) {
-                        iter.next();
-                    }
-                    // Increase index
-                    i += step;
-                    count++;
-                }
-                ev = startForEach.getEndInstruction().getNext();
-                continue;
-            } else if (ev instanceof StartChoose) {
-                StartChoose startChoose = (StartChoose) ev;
-                StartWhen startWhen = startChoose.getFirstChoice();
-                while (startWhen != null) {
-                    Object val;
-                    try {
-                        val = startWhen.getTest().getValue(expressionContext);
-                    } catch (Exception e) {
-                        throw new SAXParseException(e.getMessage(), ev.getLocation(), e);
-                    }
-                    boolean result;
-                    if (val instanceof Boolean) {
-                        result = ((Boolean) val).booleanValue();
-                    } else {
-                        result = (val != null);
-                    }
-                    if (result) {
-                        execute(consumer, expressionContext, executionContext,
-                                macroCall, startWhen.getNext(),
-                                startWhen.getEndInstruction());
-                        break;
-                    }
-                    startWhen = startWhen.getNextChoice();
-                }
-                if (startWhen == null && startChoose.getOtherwise() != null) {
-                    execute(consumer, expressionContext, executionContext,
-                            macroCall, startChoose.getOtherwise().getNext(),
-                            startChoose.getOtherwise().getEndInstruction());
-                }
-                ev = startChoose.getEndInstruction().getNext();
-                continue;
-            } else if (ev instanceof StartSet) {
-                StartSet startSet = (StartSet) ev;
-                Object value = null;
-                String var = null;
-                try {
-                    if (startSet.getVar() != null) {
-                        var = startSet.getVar().getStringValue(expressionContext);
-                    }
-                    if (startSet.getValue() != null) {
-                        value = startSet.getValue().getNode(expressionContext);
-                    }
-                } catch (Exception exc) {
-                    throw new SAXParseException(exc.getMessage(), ev.getLocation(), exc);
-                }
-                if (value == null) {
-                    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];
-                    for (int i = 0; i < len; i++) {
-                        nodeArr[i] = nodeList.item(i);
-                    }
-                    value = nodeArr;
-                }
-                if (var != null) {
-                    expressionContext.put(var, value);
+            } else if (ev instanceof StartDocument) {
+                if (((StartDocument) ev).getEndDocument() != null) {
+                    // if this isn't a document fragment
+                    consumer.startDocument();
                 }
-                ev = startSet.getEndInstruction().getNext();
-                continue;
             } else if (ev instanceof StartElement) {
                 StartElement startElement = (StartElement) ev;
                 StartDefine def = (StartDefine) executionContext
@@ -419,68 +270,12 @@
                 consumer.startElement(startElement.getNamespaceURI(),
                                       startElement.getLocalName(), startElement.getRaw(),
                                       attrs);
-            } else if (ev instanceof StartFormatNumber) {
-                StartFormatNumber startFormatNumber = (StartFormatNumber) ev;
-                try {
-                    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);
-                } catch (Error 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(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);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(), ev
-                            .getLocation(), new ErrorHolder(err));
-                }
             } else if (ev instanceof StartPrefixMapping) {
                 StartPrefixMapping startPrefixMapping = (StartPrefixMapping) ev;
                 consumer.startPrefixMapping(startPrefixMapping.getPrefix(),
-                        startPrefixMapping.getUri());
-            } else if (ev instanceof StartComment) {
-                StartComment startJXComment = (StartComment) ev;
-                // Parse the body of the comment
-                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();
-                Properties omit = XMLUtils.createPropertiesForXML(true);
-                for (int i = 0; i < len; i++) {
-                    try {
-                        String str = XMLUtils.serializeNode(nodeList.item(i),
-                                omit);
-                        buf.append(StringUtils.substringAfter(str, ">")); // cut
-                        // the
-                        // XML
-                        // header
-                    } catch (ProcessingException e) {
-                        throw new SAXParseException(e.getMessage(),
-                                                    startJXComment.getLocation(), e);
-                    }
-                }
-                char[] chars = new char[buf.length()];
-                buf.getChars(0, chars.length, chars, 0);
-                consumer.comment(chars, 0, chars.length);
-                ev = startJXComment.getEndInstruction().getNext();
-                continue;
+                                            startPrefixMapping.getUri());
+
+                // LexicalHandler methods
             } else if (ev instanceof EndCDATA) {
                 consumer.endCDATA();
             } else if (ev instanceof EndDTD) {
@@ -492,165 +287,16 @@
             } else if (ev instanceof StartDTD) {
                 StartDTD startDTD = (StartDTD) ev;
                 consumer.startDTD(startDTD.getName(), startDTD.getPublicId(),
-                        startDTD.getSystemId());
+                                  startDTD.getSystemId());
             } else if (ev instanceof StartEntity) {
                 consumer.startEntity(((StartEntity) ev).getName());
-            } else if (ev instanceof StartOut) {
-                StartOut startOut = (StartOut) ev;
-                Object val;
-                try {
-                    val = startOut.getCompiledExpression().getNode(expressionContext);
-                    if (val instanceof Node) {
-                        executeDOM(consumer, (Node) val);
-                    } else if (val instanceof NodeList) {
-                        NodeList nodeList = (NodeList) val;
-                        int len = nodeList.getLength();
-                        for (int i = 0; i < len; i++) {
-                            Node n = nodeList.item(i);
-                            executeDOM(consumer, n);
-                        }
-                    } else if (val instanceof Node[]) {
-                        Node[] nodeList = (Node[]) val;
-                        int len = nodeList.length;
-                        for (int i = 0; i < len; i++) {
-                            Node n = nodeList[i];
-                            executeDOM(consumer, n);
-                        }
-                    } else if (val instanceof XMLizable) {
-                        ((XMLizable) val)
-                                .toSAX(new IncludeXMLConsumer(consumer));
-                    } else {
-                        char[] ch = val == null ? ArrayUtils.EMPTY_CHAR_ARRAY
-                                : val.toString().toCharArray();
-                        consumer.characters(ch, 0, ch.length);
-                    }
-                } catch (Exception e) {
-                    throw new SAXParseException(e.getMessage(), ev
-                            .getLocation(), e);
-                }
-            } else if (ev instanceof StartTemplate) {
-                // EMPTY
-            } else if (ev instanceof StartEval) {
-                StartEval startEval = (StartEval) ev;
-                JXTExpression expr = startEval.getValue();
-                try {
-                    Object val = expr.getNode(expressionContext);
-                    if (!(val instanceof StartElement)) {
-                        throw new Exception(
-                                "macro invocation required instead of: " + val);
-                    }
-                    StartElement call = (StartElement) val;
-                    execute(consumer, expressionContext, executionContext,
-                            call, call.getNext(), call.getEndElement());
-                } catch (Exception exc) {
-                    throw new SAXParseException(exc.getMessage(), ev
-                            .getLocation(), exc);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(), ev
-                            .getLocation(), new ErrorHolder(err));
-                }
-                ev = startEval.getEndInstruction().getNext();
-                continue;
-            } else if (ev instanceof StartEvalBody) {
-                StartEvalBody startEval = (StartEvalBody) ev;
-                try {
-                    execute(consumer, expressionContext, executionContext,
-                            null, macroCall.getNext(), macroCall.getEndElement());
-                } catch (Exception exc) {
-                    throw new SAXParseException(exc.getMessage(),
-                                                ev.getLocation(), exc);
-                } catch (Error 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);
-                ev = startDefine.getEndInstruction().getNext();
-                continue;
-            } else if (ev instanceof StartImport) {
-                StartImport startImport = (StartImport) ev;
-                String uri;
-                AttributeEvent e = startImport.getUri();
-                if (e instanceof CopyAttribute) {
-                    CopyAttribute copy = (CopyAttribute) e;
-                    uri = copy.getValue();
-                } else {
-                    StringBuffer buf = new StringBuffer();
-                    SubstituteAttribute substAttr = (SubstituteAttribute) e;
-                    Iterator i = substAttr.getSubstitutions().iterator();
-                    while (i.hasNext()) {
-                        Subst subst = (Subst) i.next();
-                        if (subst instanceof Literal) {
-                            Literal lit = (Literal) subst;
-                            buf.append(lit.getValue());
-                        } else if (subst instanceof JXTExpression) {
-                            JXTExpression expr = (JXTExpression) subst;
-                            Object val;
-                            try {
-                                val = expr.getValue(expressionContext);
-                            } catch (Exception exc) {
-                                throw new SAXParseException(exc.getMessage(),
-                                        ev.getLocation(), exc);
-                            } catch (Error err) {
-                                throw new SAXParseException(err.getMessage(),
-                                        ev.getLocation(), new ErrorHolder(err));
-                            }
-                            buf.append(val != null ? val.toString() : "");
-                        }
-                    }
-                    uri = buf.toString();
-                }
-                StartDocument doc;
-                try {
-                    doc = executionContext.getScriptManager().resolveTemplate(uri);
-                } catch (ProcessingException exc) {
-                    throw new SAXParseException(exc.getMessage(), ev
-                            .getLocation(), exc);
-                }
-                ExpressionContext selectExpressionContext = expressionContext;
-                if (startImport.getSelect() != null) {
-                    try {
-                        Object obj =
-                            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);
-                    } catch (Error err) {
-                        throw new SAXParseException(err.getMessage(),
-                                                    ev.getLocation(),
-                                                    new ErrorHolder(err));
-                    }
-                }
-                try {
-                    execute(consumer, expressionContext, executionContext,
-                            macroCall, doc.getNext(), doc.getEndDocument());
-                } catch (Exception exc) {
-                    throw new SAXParseException(
-                            "Exception occurred in imported template " + uri
-                                    + ": " + exc.getMessage(),
-                            ev.getLocation(), exc);
-                }
-                ev = startImport.getEndInstruction().getNext();
+
+                // Instructions
+            } else if (ev instanceof StartInstruction) {
+                ev = ((StartInstruction)ev).execute(consumer,
+                                                    expressionContext, executionContext,
+                                                    macroCall, startEvent, endEvent);
                 continue;
-            } else if (ev instanceof StartDocument) {
-                if (((StartDocument) ev).getEndDocument() != null) {
-                    // if this isn't a document fragment
-                    consumer.startDocument();
-                }
-            } else if (ev instanceof EndDocument) {
-                consumer.endDocument();
-            } else if (ev instanceof ProcessingInstruction) {
-                ProcessingInstruction pi = (ProcessingInstruction) ev;
-                consumer.processingInstruction(pi.getTarget(), pi.getData());
             }
             ev = ev.getNext();
         }
@@ -693,7 +339,7 @@
      * dump a DOM document, using an IncludeXMLConsumer to filter out start/end
      * document events
      */
-    private static void executeDOM(final XMLConsumer consumer, Node node)
+    public static void executeDOM(final XMLConsumer consumer, Node node)
             throws SAXException {
         IncludeXMLConsumer includer = new IncludeXMLConsumer(consumer);
         DOMStreamer streamer = new DOMStreamer(includer);
@@ -715,11 +361,11 @@
         }
     }
 
-    private static NodeList toDOMNodeList(String elementName,
-                                          StartInstruction si,
-                                          ExpressionContext expressionContext,
-                                          ExecutionContext executionContext,
-                                          StartElement macroCall)
+    public static NodeList toDOMNodeList(String elementName,
+                                         StartInstruction si,
+                                         ExpressionContext expressionContext,
+                                         ExecutionContext executionContext,
+                                         StartElement macroCall)
             throws SAXException {
         DOMBuilder builder = new DOMBuilder();
         builder.startDocument();

Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartChoose.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartChoose.java?view=diff&rev=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartChoose.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartChoose.java&r2=125680
==============================================================================
--- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartChoose.java	(original)
+++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartChoose.java	Wed Jan 19 14:59:37 2005
@@ -17,7 +17,13 @@
 
 import java.util.Stack;
 
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
+import org.apache.cocoon.template.jxtg.script.Invoker;
+import org.apache.cocoon.xml.XMLConsumer;
 import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 public class StartChoose extends StartInstruction {
 
@@ -26,6 +32,40 @@
 
     public StartChoose(StartElement raw, Attributes attrs, Stack stack) {
         super(raw);
+    }
+
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        StartWhen startWhen = this.firstChoice;
+        while (startWhen != null) {
+            Object val;
+            try {
+                val = startWhen.getTest().getValue(expressionContext);
+            } catch (Exception e) {
+                throw new SAXParseException(e.getMessage(), getLocation(), e);
+            }
+            boolean result;
+            if (val instanceof Boolean) {
+                result = ((Boolean) val).booleanValue();
+            } else {
+                result = (val != null);
+            }
+            if (result) {
+                Invoker.execute(consumer, expressionContext, executionContext,
+                                macroCall, startWhen.getNext(),
+                                startWhen.getEndInstruction());
+                break;
+            }
+            startWhen = startWhen.getNextChoice();
+        }
+        if (startWhen == null && this.otherwise != null) {
+            Invoker.execute(consumer, expressionContext, executionContext,
+                            macroCall, this.otherwise.getNext(),
+                            this.otherwise.getEndInstruction());
+        }
+        return getEndInstruction().getNext();
     }
 
     public void setFirstChoice(StartWhen firstChoice) {

Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartComment.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartComment.java?view=diff&rev=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartComment.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartComment.java&r2=125680
==============================================================================
--- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartComment.java	(original)
+++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartComment.java	Wed Jan 19 14:59:37 2005
@@ -15,13 +15,52 @@
  */
 package org.apache.cocoon.template.jxtg.script.event;
 
+import java.util.Properties;
 import java.util.Stack;
 
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
+import org.apache.cocoon.template.jxtg.script.Invoker;
+import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.cocoon.xml.XMLUtils;
+import org.apache.commons.lang.StringUtils;
+import org.w3c.dom.NodeList;
 import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 public class StartComment extends StartInstruction {
     public StartComment(StartElement raw, Attributes attrs, Stack stack) {
         // <jx:comment>This will be parsed</jx:comment>
         super(raw);
+    }
+
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        // Parse the body of the comment
+        NodeList nodeList =
+            Invoker.toDOMNodeList("comment", this,
+                                  expressionContext, executionContext,
+                                  macroCall);
+        // JXPath doesn't handle NodeList, so convert it to an array
+        int len = nodeList.getLength();
+        final StringBuffer buf = new StringBuffer();
+        Properties omit = XMLUtils.createPropertiesForXML(true);
+        for (int i = 0; i < len; i++) {
+            try {
+                String str = XMLUtils.serializeNode(nodeList.item(i), omit);
+                buf.append(StringUtils.substringAfter(str, ">")); // cut
+                // the XML header
+            } catch (ProcessingException e) {
+                throw new SAXParseException(e.getMessage(), getLocation(), e);
+            }
+        }
+        char[] chars = new char[buf.length()];
+        buf.getChars(0, chars.length, chars, 0);
+        consumer.comment(chars, 0, chars.length);
+        return getEndInstruction().getNext();
     }
 }

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=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDefine.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartDefine.java&r2=125680
==============================================================================
--- 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	Wed Jan 19 14:59:37 2005
@@ -19,6 +19,9 @@
 import java.util.Map;
 import java.util.Stack;
 
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
+import org.apache.cocoon.xml.XMLConsumer;
 import org.apache.commons.lang.StringUtils;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
@@ -26,8 +29,8 @@
 
 public class StartDefine extends StartInstruction {
 
-    final String name;
-    final String namespace;
+    private final String name;
+    private final String namespace;
     private final String qname;
     private final Map parameters;
     private Event body;
@@ -52,6 +55,14 @@
         this.parameters = new HashMap();
     }
 
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        executionContext.getDefinitions().put(this.qname, this);
+        return getEndInstruction().getNext();
+    }
+
     public void finish() throws SAXException {
         Event e = next;
         boolean params = true;
@@ -64,7 +75,7 @@
                                     + startParam.name + "\"", startParam
                                     .getLocation(), null);
                 }
-                Object prev = getParameters().put(startParam.name, startParam);
+                Object prev = this.parameters.put(startParam.name, startParam);
                 if (prev != null) {
                     throw new SAXParseException("duplicate parameter: \""
                             + startParam.name + "\"", location, null);
@@ -80,7 +91,7 @@
                     if (!Character.isWhitespace(ch[i])) {
                         if (params) {
                             params = false;
-                            setBody(e);
+                            this.body = e;
                         }
                         break;
                     }
@@ -88,13 +99,13 @@
             } else {
                 if (params) {
                     params = false;
-                    setBody(e);
+                    this.body = e;
                 }
             }
             e = e.getNext();
         }
-        if (this.getBody() == null) {
-            this.setBody(this.getEndInstruction());
+        if (this.body == null) {
+            this.body = this.getEndInstruction();
         }
     }
 
@@ -102,7 +113,7 @@
         return parameters;
     }
 
-    void setBody(Event body) {
+    private void setBody(Event body) {
         this.body = body;
     }
 
@@ -110,7 +121,7 @@
         return body;
     }
 
-    public String getQname() {
+    private String getQname() {
         return qname;
     }
 }

Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEval.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEval.java?view=diff&rev=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEval.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEval.java&r2=125680
==============================================================================
--- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEval.java	(original)
+++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEval.java	Wed Jan 19 14:59:37 2005
@@ -17,9 +17,15 @@
 
 import java.util.Stack;
 
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ErrorHolder;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
 import org.apache.cocoon.template.jxtg.expression.JXTExpression;
+import org.apache.cocoon.template.jxtg.script.Invoker;
+import org.apache.cocoon.xml.XMLConsumer;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 public class StartEval extends StartInstruction {
     private final JXTExpression value;
@@ -33,7 +39,23 @@
         this.value = JXTExpression.compileExpr(select, "eval: \"select\":", getLocation());
     }
 
-    public JXTExpression getValue() {
-        return value;
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        try {
+            Object val = this.value.getNode(expressionContext);
+            if (!(val instanceof StartElement)) {
+                throw new Exception("macro invocation required instead of: " + val);
+            }
+            StartElement call = (StartElement) val;
+            Invoker.execute(consumer, expressionContext, executionContext,
+                            call, call.getNext(), call.getEndElement());
+        } catch (Exception exc) {
+            throw new SAXParseException(exc.getMessage(), getLocation(), exc);
+        } catch (Error err) {
+            throw new SAXParseException(err.getMessage(), getLocation(), new ErrorHolder(err));
+        }
+        return getEndInstruction().getNext();
     }
 }

Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEvalBody.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEvalBody.java?view=diff&rev=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEvalBody.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEvalBody.java&r2=125680
==============================================================================
--- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEvalBody.java	(original)
+++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartEvalBody.java	Wed Jan 19 14:59:37 2005
@@ -17,10 +17,33 @@
 
 import java.util.Stack;
 
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ErrorHolder;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
+import org.apache.cocoon.template.jxtg.script.Invoker;
+import org.apache.cocoon.xml.XMLConsumer;
 import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 public class StartEvalBody extends StartInstruction {
     public StartEvalBody(StartElement raw, Attributes attrs, Stack stack) {
         super(raw);
+    }
+
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        try {
+            Invoker.execute(consumer, expressionContext, executionContext,
+                            null, macroCall.getNext(), macroCall.getEndElement());
+        } catch (Exception exc) {
+            throw new SAXParseException(exc.getMessage(), getLocation(), exc);
+        } catch (Error err) {
+            throw new SAXParseException(err.getMessage(), getLocation(),
+                                        new ErrorHolder(err));
+        }
+        return getEndInstruction().getNext();
     }
 }

Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartForEach.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartForEach.java?view=diff&rev=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartForEach.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartForEach.java&r2=125680
==============================================================================
--- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartForEach.java	(original)
+++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartForEach.java	Wed Jan 19 14:59:37 2005
@@ -15,9 +15,15 @@
  */
 package org.apache.cocoon.template.jxtg.script.event;
 
+import java.util.Iterator;
 import java.util.Stack;
 
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ErrorHolder;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
 import org.apache.cocoon.template.jxtg.expression.JXTExpression;
+import org.apache.cocoon.template.jxtg.script.Invoker;
+import org.apache.cocoon.xml.XMLConsumer;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
@@ -25,13 +31,13 @@
 
 public class StartForEach extends StartInstruction {
 
-    final JXTExpression items;
-    final JXTExpression var;
-    final JXTExpression varStatus;
-    final JXTExpression begin;
-    final JXTExpression end;
-    final JXTExpression step;
-    final Boolean lenient;
+    private final JXTExpression items;
+    private final JXTExpression var;
+    private final JXTExpression varStatus;
+    private final JXTExpression begin;
+    private final JXTExpression end;
+    private final JXTExpression step;
+    private final Boolean lenient;
 
     public StartForEach(StartElement raw, Attributes attrs, Stack stack)
         throws SAXException {
@@ -63,31 +69,75 @@
         this.items = JXTExpression.compileExpr(items == null ? select : items, null, locator);
     }
 
-    public JXTExpression getBegin() {
-        return begin;
-    }
-
-    public JXTExpression getEnd() {
-        return end;
-    }
-
-    public JXTExpression getItems() {
-        return items;
-    }
-
-    public Boolean getLenient() {
-        return lenient;
-    }
-
-    public JXTExpression getStep() {
-        return step;
-    }
-
-    public JXTExpression getVar() {
-        return var;
-    }
-
-    public JXTExpression getVarStatus() {
-        return varStatus;
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        Iterator iter = null;
+        int begin, end, step;
+        String var, varStatus;
+        try {
+            iter = this.items.getIterator(expressionContext);
+            begin = this.begin == null
+                ? 0
+                : this.begin.getIntValue(expressionContext);
+            end = this.end == null
+                ? Integer.MAX_VALUE
+                : this.end.getIntValue(expressionContext);
+            step = this.step == null
+                ? 1
+                : this.step.getIntValue(expressionContext);
+            var = this.var.getStringValue(expressionContext);
+            varStatus = this.varStatus.getStringValue(expressionContext);
+        } catch (Exception exc) {
+            throw new SAXParseException(exc.getMessage(),
+                                        getLocation(), exc);
+        } catch (Error err) {
+            throw new SAXParseException(err.getMessage(),
+                                        getLocation(), new ErrorHolder(err));
+        }
+        ExpressionContext localExpressionContext =
+            new ExpressionContext(expressionContext);
+        int i = 0;
+        // Move to the begin row
+        while (i < begin && iter.hasNext()) {
+            iter.next();
+            i++;
+        }
+        LoopTagStatus status = null;
+        if (varStatus != null) {
+            status = new LoopTagStatus();
+            status.setBegin(begin);
+            status.setEnd(end);
+            status.setStep(step);
+            status.setFirst(true);
+            localExpressionContext.put(varStatus, status);
+        }
+        int skipCounter, count = 1;
+        while (i <= end && iter.hasNext()) {
+            Object value = iter.next();
+            localExpressionContext.setContextBean(value);
+            if (var != null) {
+                localExpressionContext.put(var, value);
+            }
+            if (status != null) {
+                status.setIndex(i);
+                status.setCount(count);
+                status.setFirst(i == begin);
+                status.setCurrent(value);
+                status.setLast((i == end || !iter.hasNext()));
+            }
+            Invoker.execute(consumer, localExpressionContext, executionContext,
+                            macroCall, getNext(), getEndInstruction());
+            // Skip rows
+            skipCounter = step;
+            while (--skipCounter > 0 && iter.hasNext()) {
+                iter.next();
+            }
+            // Increase index
+            i += step;
+            count++;
+        }
+        return getEndInstruction().getNext();
     }
 }

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=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatDate.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatDate.java&r2=125680
==============================================================================
--- 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	Wed Jan 19 14:59:37 2005
@@ -22,11 +22,15 @@
 import java.util.TimeZone;
 
 import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ErrorHolder;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
 import org.apache.cocoon.template.jxtg.environment.ValueHelper;
 import org.apache.cocoon.template.jxtg.expression.JXTExpression;
+import org.apache.cocoon.xml.XMLConsumer;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 public class StartFormatDate extends StartInstruction {
 
@@ -34,14 +38,14 @@
     private static final String TIME = "time";
     private static final String DATETIME = "both";
 
-    JXTExpression var;
-    JXTExpression value;
-    JXTExpression type;
-    JXTExpression pattern;
-    JXTExpression timeZone;
-    JXTExpression dateStyle;
-    JXTExpression timeStyle;
-    JXTExpression locale;
+    private JXTExpression var;
+    private JXTExpression value;
+    private JXTExpression type;
+    private JXTExpression pattern;
+    private JXTExpression timeZone;
+    private JXTExpression dateStyle;
+    private JXTExpression timeStyle;
+    private JXTExpression locale;
 
     public StartFormatDate(StartElement raw, Attributes attrs, Stack stack) 
         throws SAXException{
@@ -60,7 +64,25 @@
         this.locale = JXTExpression.compileExpr(attrs.getValue("locale"), null, locator);
     }
 
-    public String format(ExpressionContext expressionContext) throws Exception {
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        try {
+            String result = format(expressionContext);
+            if (result != null) {
+                char[] chars = result.toCharArray();
+                consumer.characters(chars, 0, chars.length);
+            }
+        } catch (Exception e) {
+            throw new SAXParseException(e.getMessage(), getLocation(), e);
+        } catch (Error err) {
+            throw new SAXParseException(err.getMessage(), getLocation(), new ErrorHolder(err));
+        }
+        return getNext();
+    }
+
+    private String format(ExpressionContext expressionContext) throws Exception {
         String var = this.var.getStringValue(expressionContext);
         Object value = this.value.getValue(expressionContext);
         Object locVal = this.locale.getValue(expressionContext);
@@ -77,7 +99,7 @@
         Locale locale;
         if (locVal != null) {
             locale = locVal instanceof Locale ? (Locale) locVal
-                    : ValueHelper.parseLocale(locVal.toString(), null);
+                : ValueHelper.parseLocale(locVal.toString(), null);
         } else {
             locale = Locale.getDefault();
         }

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=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatNumber.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartFormatNumber.java&r2=125680
==============================================================================
--- 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	Wed Jan 19 14:59:37 2005
@@ -23,28 +23,32 @@
 import java.util.Stack;
 
 import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ErrorHolder;
 import org.apache.cocoon.template.jxtg.environment.ValueHelper;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
 import org.apache.cocoon.template.jxtg.expression.JXTExpression;
+import org.apache.cocoon.xml.XMLConsumer;
 import org.apache.commons.lang.StringUtils;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 public class StartFormatNumber extends StartInstruction {
 
-    JXTExpression value;
-    JXTExpression type;
-    JXTExpression pattern;
-    JXTExpression currencyCode;
-    JXTExpression currencySymbol;
-    JXTExpression isGroupingUsed;
-    JXTExpression maxIntegerDigits;
-    JXTExpression minIntegerDigits;
-    JXTExpression maxFractionDigits;
-    JXTExpression minFractionDigits;
-    JXTExpression locale;
+    private JXTExpression value;
+    private JXTExpression type;
+    private JXTExpression pattern;
+    private JXTExpression currencyCode;
+    private JXTExpression currencySymbol;
+    private JXTExpression isGroupingUsed;
+    private JXTExpression maxIntegerDigits;
+    private JXTExpression minIntegerDigits;
+    private JXTExpression maxFractionDigits;
+    private JXTExpression minFractionDigits;
+    private JXTExpression locale;
 
-    JXTExpression var;
+    private JXTExpression var;
 
     private static Class currencyClass;
     private static final String NUMBER = "number";
@@ -88,7 +92,26 @@
         this.var = JXTExpression.compileExpr(attrs.getValue("var"), null, locator);
     }
 
-    public String format(ExpressionContext expressionContext) throws Exception {
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        try {
+            String result = format(expressionContext);
+            if (result != null) {
+                char[] chars = result.toCharArray();
+                consumer.characters(chars, 0, chars.length);
+            }
+        } catch (Exception e) {
+            throw new SAXParseException(e.getMessage(), getLocation(), e);
+        } catch (Error err) {
+            throw new SAXParseException(err.getMessage(), getLocation(),
+                                        new ErrorHolder(err));
+        }
+        return getNext();
+    }
+
+    private String format(ExpressionContext expressionContext) throws Exception {
         // Determine formatting locale
         String var = this.var.getStringValue(expressionContext);
         Number input = this.value.getNumberValue(expressionContext);

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=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartIf.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartIf.java&r2=125680
==============================================================================
--- 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	Wed Jan 19 14:59:37 2005
@@ -17,14 +17,18 @@
 
 import java.util.Stack;
 
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ErrorHolder;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
 import org.apache.cocoon.template.jxtg.expression.JXTExpression;
+import org.apache.cocoon.xml.XMLConsumer;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 
 public class StartIf extends StartInstruction {
-    final JXTExpression test;
+    private final JXTExpression test;
 
     public StartIf(StartElement raw, Attributes attrs, Stack stack) 
         throws SAXException {
@@ -42,7 +46,29 @@
         }
     }
 
-    public JXTExpression getTest() {
-        return test;
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+
+        Object val;
+        try {
+            val = this.test.getValue(expressionContext);
+        } catch (Exception e) {
+            throw new SAXParseException(e.getMessage(), getLocation(), e);
+        } catch (Error err) {
+            throw new SAXParseException(err.getMessage(), getLocation(),
+                                        new ErrorHolder(err));
+        }
+        boolean result = false;
+        if (val instanceof Boolean) {
+            result = ((Boolean) val).booleanValue();
+        } else {
+            result = (val != null);
+        }
+        if (!result) {
+            return getEndInstruction().getNext();
+        }
+        return getNext();
     }
 }

Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartImport.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartImport.java?view=diff&rev=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartImport.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartImport.java&r2=125680
==============================================================================
--- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartImport.java	(original)
+++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartImport.java	Wed Jan 19 14:59:37 2005
@@ -18,7 +18,16 @@
 import java.util.Iterator;
 import java.util.Stack;
 
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.environment.FlowObjectModelHelper;
+import org.apache.cocoon.template.jxtg.environment.ErrorHolder;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
 import org.apache.cocoon.template.jxtg.expression.JXTExpression;
+import org.apache.cocoon.template.jxtg.expression.Literal;
+import org.apache.cocoon.template.jxtg.expression.Subst;
+import org.apache.cocoon.template.jxtg.script.Invoker;
+import org.apache.cocoon.xml.XMLConsumer;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
@@ -60,11 +69,68 @@
         this.select = select;
     }
 
-    public AttributeEvent getUri() {
-        return uri;
-    }
-
-    public JXTExpression getSelect() {
-        return select;
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        String uri;
+        AttributeEvent e = this.uri;
+        if (e instanceof CopyAttribute) {
+            CopyAttribute copy = (CopyAttribute) e;
+            uri = copy.getValue();
+        } else {
+            StringBuffer buf = new StringBuffer();
+            SubstituteAttribute substAttr = (SubstituteAttribute) e;
+            Iterator i = substAttr.getSubstitutions().iterator();
+            while (i.hasNext()) {
+                Subst subst = (Subst) i.next();
+                if (subst instanceof Literal) {
+                    Literal lit = (Literal) subst;
+                    buf.append(lit.getValue());
+                } else if (subst instanceof JXTExpression) {
+                    JXTExpression expr = (JXTExpression) subst;
+                    Object val;
+                    try {
+                        val = expr.getValue(expressionContext);
+                    } catch (Exception exc) {
+                        throw new SAXParseException(exc.getMessage(), getLocation(), exc);
+                    } catch (Error err) {
+                        throw new SAXParseException(err.getMessage(),
+                                                    getLocation(), new ErrorHolder(err));
+                    }
+                    buf.append(val != null ? val.toString() : "");
+                }
+            }
+            uri = buf.toString();
+        }
+        StartDocument doc;
+        try {
+            doc = executionContext.getScriptManager().resolveTemplate(uri);
+        } catch (ProcessingException exc) {
+            throw new SAXParseException(exc.getMessage(), getLocation(), exc);
+        }
+        ExpressionContext selectExpressionContext = expressionContext;
+        if (this.select != null) {
+            try {
+                Object obj = this.select.getValue(expressionContext);
+                selectExpressionContext = new ExpressionContext(expressionContext);
+                selectExpressionContext.setContextBean(obj);
+                FlowObjectModelHelper.fillContext(obj, selectExpressionContext);
+            } catch (Exception exc) {
+                throw new SAXParseException(exc.getMessage(), getLocation(), exc);
+            } catch (Error err) {
+                throw new SAXParseException(err.getMessage(), getLocation(),
+                                            new ErrorHolder(err));
+            }
+        }
+        try {
+            Invoker.execute(consumer, expressionContext, executionContext,
+                            macroCall, doc.getNext(), doc.getEndDocument());
+        } catch (Exception exc) {
+            throw new SAXParseException(
+                                        "Exception occurred in imported template " + uri
+                                        + ": " + exc.getMessage(), getLocation(), exc);
+        }
+        return getEndInstruction().getNext();
     }
 }

Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartInstruction.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartInstruction.java?view=diff&rev=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartInstruction.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartInstruction.java&r2=125680
==============================================================================
--- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartInstruction.java	(original)
+++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartInstruction.java	Wed Jan 19 14:59:37 2005
@@ -15,9 +15,14 @@
  */
 package org.apache.cocoon.template.jxtg.script.event;
 
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
+import org.apache.cocoon.xml.XMLConsumer;
+import org.xml.sax.SAXException;
 
-public class StartInstruction extends Event {
-    StartInstruction(StartElement startElement) {
+public abstract class StartInstruction extends Event {
+
+    public StartInstruction(StartElement startElement) {
         super(startElement.getLocation());
         this.startElement = startElement;
     }
@@ -32,4 +37,12 @@
     public void setEndInstruction(EndInstruction endInstruction) {
         this.endInstruction = endInstruction;
     }
-}
\ No newline at end of file
+
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext,
+                         ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        return getNext();
+    }
+}

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=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartOut.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartOut.java&r2=125680
==============================================================================
--- 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	Wed Jan 19 14:59:37 2005
@@ -17,7 +17,16 @@
 
 import java.util.Stack;
 
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
 import org.apache.cocoon.template.jxtg.expression.JXTExpression;
+import org.apache.cocoon.template.jxtg.script.Invoker;
+import org.apache.cocoon.xml.IncludeXMLConsumer;
+import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.excalibur.xml.sax.XMLizable;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
@@ -44,7 +53,40 @@
         }
     }
 
-    public JXTExpression getCompiledExpression() {
-        return compiledExpression;
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        Object val;
+        try {
+            val = this.compiledExpression.getNode(expressionContext);
+            if (val instanceof Node) {
+                Invoker.executeDOM(consumer, (Node) val);
+            } else if (val instanceof NodeList) {
+                NodeList nodeList = (NodeList) val;
+                int len = nodeList.getLength();
+                for (int i = 0; i < len; i++) {
+                    Node n = nodeList.item(i);
+                    Invoker.executeDOM(consumer, n);
+                }
+            } else if (val instanceof Node[]) {
+                Node[] nodeList = (Node[]) val;
+                int len = nodeList.length;
+                for (int i = 0; i < len; i++) {
+                    Node n = nodeList[i];
+                    Invoker.executeDOM(consumer, n);
+                }
+            } else if (val instanceof XMLizable) {
+                ((XMLizable) val).toSAX(new IncludeXMLConsumer(consumer));
+            } else {
+                char[] ch =
+                    val == null ? ArrayUtils.EMPTY_CHAR_ARRAY
+                    : val.toString().toCharArray();
+                consumer.characters(ch, 0, ch.length);
+            }
+        } catch (Exception e) {
+            throw new SAXParseException(e.getMessage(), getLocation(), e);
+        }
+        return getNext();
     }
 }

Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartSet.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartSet.java?view=diff&rev=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartSet.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartSet.java&r2=125680
==============================================================================
--- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartSet.java	(original)
+++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartSet.java	Wed Jan 19 14:59:37 2005
@@ -17,10 +17,17 @@
 
 import java.util.Stack;
 
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
 import org.apache.cocoon.template.jxtg.expression.JXTExpression;
+import org.apache.cocoon.template.jxtg.script.Invoker;
+import org.apache.cocoon.xml.XMLConsumer;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 public class StartSet extends StartInstruction {
 
@@ -47,11 +54,39 @@
         this.value = valueExpr;
     }
 
-    public JXTExpression getVar() {
-        return var;
-    }
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
 
-    public JXTExpression getValue() {
-        return value;
+        Object value = null;
+        String var = null;
+        try {
+            if (this.var != null) {
+                var = this.var.getStringValue(expressionContext);
+            }
+            if (this.value != null) {
+                value = this.value.getNode(expressionContext);
+            }
+        } catch (Exception exc) {
+            throw new SAXParseException(exc.getMessage(), getLocation(), exc);
+        }
+        if (value == null) {
+            NodeList nodeList =
+                Invoker.toDOMNodeList("set", this,
+                                      expressionContext, executionContext,
+                                      macroCall);
+            // JXPath doesn't handle NodeList, so convert it to an array
+            int len = nodeList.getLength();
+            Node[] nodeArr = new Node[len];
+            for (int i = 0; i < len; i++) {
+                nodeArr[i] = nodeList.item(i);
+            }
+            value = nodeArr;
+        }
+        if (var != null) {
+            expressionContext.put(var, value);
+        }
+        return getEndInstruction().getNext();
     }
 }

Modified: cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartTemplate.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartTemplate.java?view=diff&rev=125680&p1=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartTemplate.java&r1=125679&p2=cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartTemplate.java&r2=125680
==============================================================================
--- cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartTemplate.java	(original)
+++ cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/script/event/StartTemplate.java	Wed Jan 19 14:59:37 2005
@@ -17,10 +17,22 @@
 
 import java.util.Stack;
 
+import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
+import org.apache.cocoon.xml.XMLConsumer;
 import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
 
 public class StartTemplate extends StartInstruction {
     public StartTemplate(StartElement raw, Attributes attrs, Stack stack) {
         super(raw);
+    }
+
+    public Event execute(final XMLConsumer consumer,
+                         ExpressionContext expressionContext, ExecutionContext executionContext,
+                         StartElement macroCall, Event startEvent, Event endEvent) 
+        throws SAXException {
+        // EMPTY
+        return getNext();
     }
 }

Mime
View raw message