Author: lgawron
Date: Thu Feb 24 06:11:29 2005
New Revision: 155186

URL: http://svn.apache.org/viewcvs?view=rev&rev=155186
Log:
Factor out macro calling to StartCall class. It is a little bit messy not but 
will clear out as soon as it will also start to support jx:call macro="name" 
statement.

Added:
    
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartCall.java
   (with props)
    
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartParameterInstance.java
   (with props)
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/StartInstruction.java

Added: 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartCall.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartCall.java?view=auto&rev=155186
==============================================================================
--- 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartCall.java
 (added)
+++ 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartCall.java
 Thu Feb 24 06:11:29 2005
@@ -0,0 +1,126 @@
+/*
+ * 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.instruction;
+
+import java.util.HashMap;
+import java.util.Iterator;
+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.template.jxtg.script.Invoker;
+import org.apache.cocoon.template.jxtg.script.event.AttributeEvent;
+import org.apache.cocoon.template.jxtg.script.event.Event;
+import org.apache.cocoon.template.jxtg.script.event.StartElement;
+import org.apache.cocoon.template.jxtg.script.event.StartInstruction;
+import org.apache.cocoon.xml.XMLConsumer;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+
+public class StartCall extends StartInstruction {
+//    private JXTExpression macroName;
+    private Map parameters;
+    private StartElement body;
+    private StartDefine definition;
+
+    public StartCall(StartDefine definition, StartElement body)
+            throws SAXException {
+        super(body.getLocation());
+        this.parameters = new HashMap();
+        setBody(body);
+        setDefinition(definition);
+
+        Iterator i = this.body.getAttributeEvents().iterator();
+        while (i.hasNext()) {
+            AttributeEvent attrEvent = (AttributeEvent) i.next();
+            addParameterInstance(attrEvent);
+        }
+    }
+
+    public StartCall(StartElement raw, Attributes attrs, Stack stack)
+            throws SAXException {
+        super(raw);
+//        Locator locator = getLocation();
+//        String name = attrs.getValue("macro");
+//        if (name == null) {
+//            throw new SAXParseException("if: \"test\" is required", locator,
+//                    null);
+//        }
+//        this.macroName = JXTExpression.compileExpr(name, "call: \"macro\": ",
+//                locator);
+        this.parameters = new HashMap();
+    }
+
+    public void setDefinition(StartDefine definition) {
+        this.definition = definition;
+        setEndInstruction(definition.getEndInstruction());
+    }
+
+    public void addParameterInstance(AttributeEvent attributeEvent)
+            throws SAXException {
+        StartParameterInstance parameter = new StartParameterInstance(
+                attributeEvent);
+        this.parameters.put(parameter.getName(), parameter);
+    }
+
+    public Event execute(XMLConsumer consumer,
+            ExpressionContext expressionContext,
+            ExecutionContext executionContext, StartElement macroCall,
+            Event startEvent, Event endEvent) throws SAXException {
+        Map attributeMap = new HashMap();
+        Iterator i = parameters.keySet().iterator();
+        while (i.hasNext()) {
+            String parameterName = (String) i.next();
+            StartParameterInstance parameter = (StartParameterInstance) 
parameters
+                    .get(parameterName);
+            Object parameterValue = parameter.getValue(expressionContext);
+            attributeMap.put(parameterName, parameterValue);
+        }
+        ExpressionContext localExpressionContext = new ExpressionContext(
+                expressionContext);
+        HashMap macro = new HashMap();
+        macro.put("body", this.body);
+        macro.put("arguments", attributeMap);
+        localExpressionContext.put("macro", macro);
+
+        Iterator iter = this.definition.getParameters().entrySet().iterator();
+        while (iter.hasNext()) {
+            Map.Entry e = (Map.Entry) iter.next();
+            String key = (String) e.getKey();
+            StartParameter startParam = (StartParameter) e.getValue();
+            Object default_ = startParam.getDefaultValue();
+            Object val = attributeMap.get(key);
+            if (val == null) {
+                val = default_;
+            }
+            localExpressionContext.put(key, val);
+        }
+        Invoker.call(getLocation(), this.body, consumer,
+                localExpressionContext, executionContext, definition.getBody(),
+                definition.getEndInstruction());
+        // ev = startElement.getEndElement().getNext();
+        return getNext();
+    }
+
+    /**
+     * @param startElement
+     */
+    public void setBody(StartElement startElement) {
+        this.body = startElement;
+        setNext(startElement.getEndElement().getNext());
+    }
+}

Propchange: 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartCall.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartCall.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartParameterInstance.java
URL: 
http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartParameterInstance.java?view=auto&rev=155186
==============================================================================
--- 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartParameterInstance.java
 (added)
+++ 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartParameterInstance.java
 Thu Feb 24 06:11:29 2005
@@ -0,0 +1,102 @@
+/*
+ * 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.instruction;
+
+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.expression.JXTExpression;
+import org.apache.cocoon.template.jxtg.script.event.AttributeEvent;
+import org.apache.cocoon.template.jxtg.script.event.CopyAttribute;
+import org.apache.cocoon.template.jxtg.script.event.StartElement;
+import org.apache.cocoon.template.jxtg.script.event.StartInstruction;
+import org.apache.cocoon.template.jxtg.script.event.SubstituteAttribute;
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+public class StartParameterInstance extends StartInstruction {
+    final String name;
+    private final Object value;
+
+    public StartParameterInstance(StartElement raw, Attributes attrs,
+            Stack stack) throws SAXException {
+        super(raw);
+        Locator locator = getLocation();
+        if (stack.size() == 0 || !(stack.peek() instanceof StartCall)) {
+            throw new SAXParseException("<parameter> not allowed here",
+                    locator, null);
+        } else {
+            this.name = attrs.getValue("name");
+            if (this.name == null) {
+                throw new SAXParseException("parameter: \"name\" is required",
+                        locator, null);
+            }
+
+            String val = attrs.getValue("value");
+            if (val == null)
+                throw new SAXParseException("parameter: \"value\" is required",
+                        locator, null);
+
+            this.value = JXTExpression.compileExpr(val,
+                    "parameter: \"value\": ", locator);
+        }
+    }
+
+    public StartParameterInstance(AttributeEvent event) {
+        super( (Locator) null );
+        this.name = event.getLocalName();
+        this.value = event;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public Object getValue(ExpressionContext expressionContext)
+            throws SAXException {
+        if (this.value instanceof CopyAttribute) {
+            CopyAttribute copy = (CopyAttribute) this.value;
+            return copy.getValue();
+        } else if (this.value instanceof SubstituteAttribute) {
+            SubstituteAttribute substEvent = (SubstituteAttribute) this.value;
+            if (substEvent.getSubstitutions().size() == 1
+                    && substEvent.getSubstitutions().get(0) instanceof 
JXTExpression) {
+                JXTExpression expr = (JXTExpression) substEvent
+                        .getSubstitutions().get(0);
+                Object val;
+                try {
+                    val = expr.getNode(expressionContext);
+                } catch (Exception e) {
+                    throw new SAXParseException(e.getMessage(), getLocation(),
+                            e);
+                } catch (Error err) {
+                    throw new SAXParseException(err.getMessage(),
+                            getLocation(), new ErrorHolder(err));
+                }
+                return val != null ? val : "";
+            } else {
+                return substEvent.getSubstitutions().toString(getLocation(),
+                        expressionContext);
+            }
+        } else {
+            throw new Error("this shouldn't have happened");
+        }
+
+    }
+}

Propchange: 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartParameterInstance.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
cocoon/trunk/src/blocks/template/java/org/apache/cocoon/template/jxtg/instruction/StartParameterInstance.java
------------------------------------------------------------------------------
    svn:keywords = Id

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&r1=155185&r2=155186
==============================================================================
--- 
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
 Thu Feb 24 06:11:29 2005
@@ -15,24 +15,15 @@
  */
 package org.apache.cocoon.template.jxtg.script;
 
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-
 import org.apache.cocoon.components.expression.ExpressionContext;
 import org.apache.cocoon.template.jxtg.JXTemplateGenerator;
-import org.apache.cocoon.template.jxtg.environment.ErrorHolder;
 import org.apache.cocoon.template.jxtg.environment.ExecutionContext;
 import org.apache.cocoon.template.jxtg.environment.LocatorFacade;
-import org.apache.cocoon.template.jxtg.expression.JXTExpression;
+import org.apache.cocoon.template.jxtg.instruction.StartCall;
 import org.apache.cocoon.template.jxtg.instruction.StartDefine;
-import org.apache.cocoon.template.jxtg.instruction.StartParameter;
-import org.apache.cocoon.template.jxtg.script.event.AttributeEvent;
-import org.apache.cocoon.template.jxtg.script.event.CopyAttribute;
 import org.apache.cocoon.template.jxtg.script.event.Event;
 import org.apache.cocoon.template.jxtg.script.event.StartElement;
 import org.apache.cocoon.template.jxtg.script.event.StartInstruction;
-import org.apache.cocoon.template.jxtg.script.event.SubstituteAttribute;
 import org.apache.cocoon.xml.IncludeXMLConsumer;
 import org.apache.cocoon.xml.XMLConsumer;
 import org.apache.cocoon.xml.dom.DOMBuilder;
@@ -72,67 +63,10 @@
                     continue;
                 }
 
-                // this is a macro call
-                Map attributeMap = new HashMap();
-                Iterator i = startElement.getAttributeEvents().iterator();
-                while (i.hasNext()) {
-                    String attributeName;
-                    Object attributeValue;
-                    AttributeEvent attrEvent = (AttributeEvent) i.next();
-                    attributeName = attrEvent.getLocalName();
-                    if (attrEvent instanceof CopyAttribute) {
-                        CopyAttribute copy = (CopyAttribute) attrEvent;
-                        attributeValue = copy.getValue();
-                    } else if (attrEvent instanceof SubstituteAttribute) {
-                        SubstituteAttribute substEvent = (SubstituteAttribute) 
attrEvent;
-                        if (substEvent.getSubstitutions().size() == 1
-                                && substEvent.getSubstitutions().get(0) 
instanceof JXTExpression) {
-                            JXTExpression expr = (JXTExpression) substEvent
-                                    .getSubstitutions().get(0);
-                            Object val;
-                            try {
-                                val = expr.getNode(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));
-                            }
-                            attributeValue = val != null ? val : "";
-                        } else {
-                            attributeValue = substEvent.getSubstitutions()
-                                    .toString(ev.getLocation(),
-                                            expressionContext);
-                        }
-                    } else {
-                        throw new Error("this shouldn't have happened");
-                    }
-                    attributeMap.put(attributeName, attributeValue);
-                }
-                ExpressionContext localExpressionContext = new 
ExpressionContext(
-                        expressionContext);
-                HashMap macro = new HashMap();
-                macro.put("body", startElement);
-                macro.put("arguments", attributeMap);
-                localExpressionContext.put("macro", macro);
-                Iterator iter = def.getParameters().entrySet().iterator();
-                while (iter.hasNext()) {
-                    Map.Entry e = (Map.Entry) iter.next();
-                    String key = (String) e.getKey();
-                    StartParameter startParam = (StartParameter) e.getValue();
-                    Object default_ = startParam.getDefaultValue();
-                    Object val = attributeMap.get(key);
-                    if (val == null) {
-                        val = default_;
-                    }
-                    localExpressionContext.put(key, val);
-                }
-                call(ev.getLocation(), startElement, consumer,
-                        localExpressionContext, executionContext,
-                        def.getBody(), def.getEndInstruction());
-                ev = startElement.getEndElement().getNext();
-            } else 
+                StartCall call = new StartCall( def, startElement );
+                ev = call.execute(consumer, expressionContext,
+                        executionContext, macroCall, startEvent, endEvent);
+            } else
                 ev = ev.execute(consumer, expressionContext, executionContext,
                         macroCall, startEvent, endEvent);
         }
@@ -176,7 +110,7 @@
         streamer.stream(node);
     }
 
-    private static void call(Locator location, StartElement macroCall,
+    public static void call(Locator location, StartElement macroCall,
             final XMLConsumer consumer, ExpressionContext expressionContext,
             ExecutionContext executionContext, Event startEvent, Event 
endEvent)
             throws SAXException {

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&r1=155185&r2=155186
==============================================================================
--- 
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
 Thu Feb 24 06:11:29 2005
@@ -15,8 +15,13 @@
  */
 package org.apache.cocoon.template.jxtg.script.event;
 
+import org.xml.sax.Locator;
 
 public abstract class StartInstruction extends Event {
+    public StartInstruction(Locator locator) {
+        super(locator);
+        startElement = null;
+    }
 
     public StartInstruction(StartElement startElement) {
         super(startElement.getLocation());


Reply via email to