You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by lg...@apache.org on 2005/09/03 16:51:18 UTC

svn commit: r267469 - in /cocoon/blocks/template/trunk: java/org/apache/cocoon/template/ java/org/apache/cocoon/template/environment/ java/org/apache/cocoon/template/instruction/ java/org/apache/cocoon/template/script/ test/org/apache/cocoon/template/j...

Author: lgawron
Date: Sat Sep  3 07:49:45 2005
New Revision: 267469

URL: http://svn.apache.org/viewcvs?rev=267469&view=rev
Log:
Out instruction extended with two additional attributes:
<jx:out value="${xmlString}" xmlize="true" strip-root="true"/>

put ServiceManager into ExecutionContext (jx:out needs to access xml parser)

testcase added

Added:
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut-output.xml   (with props)
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut.xml   (with props)
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOutInclude.xml   (with props)
Modified:
    cocoon/blocks/template/trunk/java/org/apache/cocoon/template/JXTemplateGenerator.java
    cocoon/blocks/template/trunk/java/org/apache/cocoon/template/environment/ExecutionContext.java
    cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/Out.java
    cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/Invoker.java
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxSet.xml

Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/JXTemplateGenerator.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/JXTemplateGenerator.java?rev=267469&r1=267468&r2=267469&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/JXTemplateGenerator.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/JXTemplateGenerator.java Sat Sep  3 07:49:45 2005
@@ -120,7 +120,7 @@
                 new RedundantNamespacesFilter(this.xmlConsumer));
         ((Map) expressionContext.get("cocoon")).put("consumer", consumer);
         Invoker.execute(consumer, this.expressionContext, new ExecutionContext(
-                this.definitions, this.scriptManager), null, startEvent, null);
+                this.definitions, this.scriptManager, this.manager), null, startEvent, null);
     }
 
     public Serializable getKey() {

Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/environment/ExecutionContext.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/environment/ExecutionContext.java?rev=267469&r1=267468&r2=267469&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/environment/ExecutionContext.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/environment/ExecutionContext.java Sat Sep  3 07:49:45 2005
@@ -17,6 +17,7 @@
 
 import java.util.Map;
 
+import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.cocoon.template.script.ScriptManager;
 
 /**
@@ -25,10 +26,12 @@
 public class ExecutionContext {
     private Map definitions;
     private ScriptManager scriptManager;
-
-    public ExecutionContext(Map definitions, ScriptManager scriptManager) {
+    private ServiceManager serviceManager;
+    
+    public ExecutionContext(Map definitions, ScriptManager scriptManager, ServiceManager serviceManager) {
         this.definitions = definitions;
         this.scriptManager = scriptManager;
+        this.serviceManager = serviceManager;
     }
 
     public Map getDefinitions() {
@@ -37,5 +40,9 @@
 
     public ScriptManager getScriptManager() {
         return this.scriptManager;
+    }
+    
+    public ServiceManager getServiceManager() {
+        return serviceManager;
     }
 }

Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/Out.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/Out.java?rev=267469&r1=267468&r2=267469&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/Out.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/Out.java Sat Sep  3 07:49:45 2005
@@ -15,16 +15,24 @@
  */
 package org.apache.cocoon.template.instruction;
 
+import java.io.ByteArrayInputStream;
 import java.util.Stack;
 
+import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.cocoon.components.expression.ExpressionContext;
 import org.apache.cocoon.template.environment.ExecutionContext;
 import org.apache.cocoon.template.expression.JXTExpression;
 import org.apache.cocoon.template.script.Invoker;
 import org.apache.cocoon.template.script.event.Event;
 import org.apache.cocoon.template.script.event.StartElement;
+import org.apache.cocoon.xml.IncludeXMLConsumer;
 import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.commons.lang.BooleanUtils;
+import org.apache.excalibur.xml.sax.SAXParser;
+import org.apache.excalibur.xml.sax.XMLizable;
+import org.w3c.dom.Node;
 import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
@@ -34,6 +42,8 @@
  */
 public class Out extends Instruction {
     private final JXTExpression compiledExpression;
+    private Boolean xmlize;
+    private Boolean stripRoot;
 
     public Out(StartElement raw, Attributes attrs, Stack stack)
         throws SAXException {
@@ -41,18 +51,22 @@
         super(raw);
         Locator locator = getLocation();
         String value = attrs.getValue("value");
-        if (value != null) {
-            this.compiledExpression =
-                JXTExpression.compileExpr(value, "out: \"value\": ", locator);
-            String lenientValue = attrs.getValue("lenient");
-            Boolean lenient = lenientValue == null ? null : Boolean.valueOf(lenientValue);
-
-            // Why can out be lenient?
-            if ( lenient != null)
-                this.compiledExpression.setLenient(lenient);
-        } else {
+        if (value == null)
             throw new SAXParseException("out: \"value\" is required", locator, null);
-        }
+
+        this.compiledExpression = JXTExpression.compileExpr(value, "out: \"value\": ", locator);
+        String lenientValue = attrs.getValue("lenient");
+        Boolean lenient = lenientValue == null ? null : Boolean.valueOf(lenientValue);
+
+        // Why can out be lenient?
+        if (lenient != null)
+            this.compiledExpression.setLenient(lenient);
+        
+        String xmlize = attrs.getValue("xmlize");
+        this.xmlize = ( xmlize == null ) ? null : Boolean.valueOf( xmlize );
+        
+        String stripRoot = attrs.getValue("strip-root");
+        this.stripRoot = ( stripRoot == null ) ? null : Boolean.valueOf( stripRoot );
     }
 
     public Event execute(final XMLConsumer consumer,
@@ -62,7 +76,26 @@
         Object val;
         try {
             val = this.compiledExpression.getNode(expressionContext);
-            Invoker.executeNode(consumer, val);
+            
+            //TODO: LG, I do not see a good way to do this.
+            if (BooleanUtils.isTrue(this.xmlize)) {
+                if (val instanceof Node || val instanceof Node[] || val instanceof XMLizable)
+                    Invoker.executeNode(consumer, val);
+                else {
+                    ServiceManager serviceManager = executionContext.getServiceManager();
+                    SAXParser parser = null;
+                    try {
+                        parser = (SAXParser) serviceManager.lookup(SAXParser.ROLE);
+                        InputSource source = new InputSource(new ByteArrayInputStream(val.toString().getBytes()));
+                        IncludeXMLConsumer includeConsumer = new IncludeXMLConsumer(consumer);
+                        includeConsumer.setIgnoreRootElement(BooleanUtils.isTrue(this.stripRoot));
+                        parser.parse(source, includeConsumer);
+                    } finally {
+                        serviceManager.release(parser);
+                    }
+                }
+            } else
+                Invoker.executeNode(consumer, val);
         } catch (Exception e) {
             throw new SAXParseException(e.getMessage(), getLocation(), e);
         }

Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/Invoker.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/Invoker.java?rev=267469&r1=267468&r2=267469&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/Invoker.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/script/Invoker.java Sat Sep  3 07:49:45 2005
@@ -74,16 +74,19 @@
         }
     }
 
-    public static void executeNode(final XMLConsumer consumer, Object val)
-            throws SAXException {
-
+    public static void executeNode(final XMLConsumer consumer, Object val) throws SAXException {
+        executeNode(consumer, val, false);
+    }
+    
+    public static void executeNode(final XMLConsumer consumer, Object val, boolean stripRoot) throws SAXException {
         if (val instanceof Node) {
-            executeDOM(consumer, (Node) val);
+            executeDOM(consumer, (Node) val, stripRoot);
         } 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);
+                //TODO: should this take stripRoot into account?
                 executeDOM(consumer, n);
             }
         } else if (val instanceof Node[]) {
@@ -91,10 +94,13 @@
             int len = nodeList.length;
             for (int i = 0; i < len; i++) {
                 Node n = nodeList[i];
+                //TODO: should this take stripRoot into account?
                 executeDOM(consumer, n);
             }
         } else if (val instanceof XMLizable) {
-            ((XMLizable) val).toSAX(new IncludeXMLConsumer(consumer));
+            IncludeXMLConsumer includer = new IncludeXMLConsumer(consumer);
+            includer.setIgnoreRootElement(stripRoot);
+            ((XMLizable) val).toSAX(includer);
         } else {
             char[] ch = val == null ? ArrayUtils.EMPTY_CHAR_ARRAY : val
                     .toString().toCharArray();
@@ -102,12 +108,18 @@
         }
     }
 
+    public static void executeDOM(final XMLConsumer consumer, Node node)
+    throws SAXException {
+        executeDOM(consumer, node, false);
+    }
+    
     /**
      * dump a DOM document, using an IncludeXMLConsumer to filter out start/end document events
      */
-    public static void executeDOM(final XMLConsumer consumer, Node node)
+    public static void executeDOM(final XMLConsumer consumer, Node node, boolean stripRoot)
             throws SAXException {
         IncludeXMLConsumer includer = new IncludeXMLConsumer(consumer);
+        includer.setIgnoreRootElement(stripRoot);
         DOMStreamer streamer = new DOMStreamer(includer);
         streamer.stream(node);
     }

Modified: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java?rev=267469&r1=267468&r2=267469&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java (original)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java Sat Sep  3 07:49:45 2005
@@ -148,4 +148,15 @@
         getFlowContext().put("value", new Double(979.0101));
         assertEqual(load(outputURI), generate(JX, inputURI, EMPTY_PARAMS));
     }
+
+    public void testOut() throws Exception {
+        String inputURI = docBase + "jxOut.xml";
+        String outputURI = docBase + "jxOut-output.xml";
+        String includeURI = docBase + "jxOutInclude.xml";
+        
+        getFlowContext().put("value", "simple");
+        getFlowContext().put("xml", "<root><node>value</node></root>");
+        getFlowContext().put("document", load(includeURI));
+        assertEqual(load(outputURI), generate(JX, inputURI, EMPTY_PARAMS));
+    }
 }

Added: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut-output.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut-output.xml?rev=267469&view=auto
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut-output.xml (added)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut-output.xml Sat Sep  3 07:49:45 2005
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<root xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
+	<simple>simple</simple>
+	<nonxmlized>&lt;root&gt;&lt;node&gt;value&lt;/node&gt;&lt;/root&gt;</nonxmlized>
+	<xmlized><root><node>value</node></root></xmlized>
+	<xmlized-noroot><node>value</node></xmlized-noroot>
+	<document>
+		<root>
+			<p>content</p>
+			<p>content2</p>
+		</root>
+	</document>
+</root>

Propchange: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut-output.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut.xml?rev=267469&view=auto
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut.xml (added)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut.xml Sat Sep  3 07:49:45 2005
@@ -0,0 +1,8 @@
+<?xml version="1.0"?>
+<root xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
+	<simple><jx:out value="${value}"/></simple>
+	<nonxmlized><jx:out value="${xml}"/></nonxmlized>
+	<xmlized><jx:out value="${xml}" xmlize="true"/></xmlized>
+	<xmlized-noroot><jx:out value="${xml}" xmlize="true" strip-root="true"/></xmlized-noroot>
+	<document><jx:out value="${document}"/></document>
+</root>

Propchange: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOut.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOutInclude.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOutInclude.xml?rev=267469&view=auto
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOutInclude.xml (added)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOutInclude.xml Sat Sep  3 07:49:45 2005
@@ -0,0 +1,5 @@
+<?xml version="1.0"?>
+<root>
+	<p>content</p>
+	<p>content2</p>
+</root>

Propchange: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxOutInclude.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxSet.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxSet.xml?rev=267469&r1=267468&r2=267469&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxSet.xml (original)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxSet.xml Sat Sep  3 07:49:45 2005
@@ -1,7 +1,5 @@
 <?xml version="1.0"?>
-
 <root xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
-
   <test1>
     <jx:set var="var1" value="foo"/>
     <jx:out value="${var1}"/>