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/12/25 14:45:57 UTC

svn commit: r358972 - in /cocoon/blocks/template/trunk: java/org/apache/cocoon/template/instruction/ test/org/apache/cocoon/template/jxtg/

Author: lgawron
Date: Sun Dec 25 05:42:48 2005
New Revision: 358972

URL: http://svn.apache.org/viewcvs?rev=358972&view=rev
Log:
apart from <jx:atttribute name="text" value="abc"/> you can also do:
<jx:atttribute name="text">abc</jx:attribute>
This is most useful for nesting jx:* tags:
<jx:attribute name="value"><jx:formatDate value="${date}" pattern="dd MMMM yyyy" locale="pl_PL"/></jx:attribute>

Modified:
    cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/Attribute.java
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/JXTemplateGeneratorTestCase.java
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxAttribute-output.xml
    cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxAttribute.xml

Modified: cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/Attribute.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/Attribute.java?rev=358972&r1=358971&r2=358972&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/Attribute.java (original)
+++ cocoon/blocks/template/trunk/java/org/apache/cocoon/template/instruction/Attribute.java Sun Dec 25 05:42:48 2005
@@ -3,20 +3,26 @@
  */
 package org.apache.cocoon.template.instruction;
 
+import java.io.StringWriter;
 import java.util.Stack;
 
 import org.apache.cocoon.components.expression.ExpressionContext;
+import org.apache.cocoon.template.JXTemplateGenerator;
 import org.apache.cocoon.template.environment.ExecutionContext;
 import org.apache.cocoon.template.environment.ParsingContext;
 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.template.xml.AttributeAwareXMLConsumer;
+import org.apache.cocoon.xml.ContentHandlerWrapper;
 import org.apache.cocoon.xml.XMLConsumer;
+import org.apache.xml.serialize.TextSerializer;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
+import org.xml.sax.helpers.AttributesImpl;
 
 /**
  * @version SVN $Id$
@@ -32,41 +38,53 @@
         Locator locator = getLocation();
         String name = attrs.getValue("name");
         if (name == null) {
-            throw new SAXParseException("parameter: \"name\" is required",
-                    locator, null);
+            throw new SAXParseException("parameter: \"name\" is required", locator, null);
         }
-        this.name = parsingContext.getStringTemplateParser().compileExpr(name, "parameter: \"name\": ",
-                locator);
+        this.name = parsingContext.getStringTemplateParser().compileExpr(name, "parameter: \"name\": ", locator);
 
         String value = attrs.getValue("value");
-        if (value == null)
-            throw new SAXParseException("parameter: \"value\" is required",
-                    locator, null);
 
-        this.value = parsingContext.getStringTemplateParser().compileExpr(value, "parameter: \"value\": ",
-                locator);
+        this.value = parsingContext.getStringTemplateParser().compileExpr(value, "parameter: \"value\": ", locator);
     }
 
-    public Event execute(final XMLConsumer consumer,
-            ExpressionContext expressionContext,
-            ExecutionContext executionContext, MacroContext macroContext,
-            Event startEvent, Event endEvent) throws SAXException {
+    public Event execute(final XMLConsumer consumer, ExpressionContext expressionContext,
+            ExecutionContext executionContext, MacroContext macroContext, Event startEvent, Event endEvent)
+            throws SAXException {
 
         String nameStr = null;
         String valueStr = "";
         try {
             nameStr = this.name.getStringValue(expressionContext);
-            valueStr = this.value.getStringValue(expressionContext);
+
+            if (this.value != null)
+                valueStr = this.value.getStringValue(expressionContext);
+            else {
+                final Attributes EMPTY_ATTRS = new AttributesImpl();
+                String elementName = "attribute";
+
+                TextSerializer serializer = new TextSerializer();
+                StringWriter writer = new StringWriter();
+                serializer.setOutputCharStream(writer);
+
+                ContentHandlerWrapper contentHandler = new ContentHandlerWrapper(serializer, serializer);
+                contentHandler.startDocument();
+
+                // TODO is root element necessary for TextSerializer?
+                contentHandler.startElement(JXTemplateGenerator.NS, elementName, elementName, EMPTY_ATTRS);
+                Invoker.execute(contentHandler, expressionContext, executionContext, macroContext, this.getNext(), this
+                        .getEndInstruction());
+                contentHandler.endElement(JXTemplateGenerator.NS, elementName, elementName);
+                contentHandler.endDocument();
+                valueStr = writer.toString();
+            }
         } catch (Exception exc) {
             throw new SAXParseException(exc.getMessage(), getLocation(), exc);
         }
         if (consumer instanceof AttributeAwareXMLConsumer) {
             AttributeAwareXMLConsumer c = (AttributeAwareXMLConsumer) consumer;
-            c.attribute("", nameStr, nameStr, "CDATA", valueStr == null ? ""
-                    : valueStr);
+            c.attribute("", nameStr, nameStr, "CDATA", valueStr == null ? "" : valueStr);
         } else
-            throw new SAXParseException("consumer is not attribute aware",
-                    getLocation());
+            throw new SAXParseException("consumer is not attribute aware", getLocation());
         return getEndInstruction().getNext();
     }
 

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=358972&r1=358971&r2=358972&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 Sun Dec 25 05:42:48 2005
@@ -129,6 +129,8 @@
         String inputURI = docBase + "jxAttribute.xml";
         String outputURI = docBase + "jxAttribute-output.xml";
 
+        Calendar cal = new GregorianCalendar(1979, 0, 1, 10, 21, 33);
+        getFlowContext().put("date", cal.getTime());
         assertEqual(load(outputURI), generate(JX, inputURI, EMPTY_PARAMS));
     }
     

Modified: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxAttribute-output.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxAttribute-output.xml?rev=358972&r1=358971&r2=358972&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxAttribute-output.xml (original)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxAttribute-output.xml Sun Dec 25 05:42:48 2005
@@ -1,11 +1,9 @@
 <?xml version="1.0"?>
 <root xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
-	<one foo="bar">
-	</one>
-	<two second="twoAttr">
-	  xyz
-	</two>
+	<one foo="bar"/>
+	<two second="twoAttr"> xyz </two>
 	<three foo="bar" foo2="bar2" dd="dd">
-	  <abc>def</abc>
+		<abc>def</abc>
 	</three>
+	<nestedjx value="01 styczeń 1979"/>
 </root>

Modified: cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxAttribute.xml
URL: http://svn.apache.org/viewcvs/cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxAttribute.xml?rev=358972&r1=358971&r2=358972&view=diff
==============================================================================
--- cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxAttribute.xml (original)
+++ cocoon/blocks/template/trunk/test/org/apache/cocoon/template/jxtg/jxAttribute.xml Sun Dec 25 05:42:48 2005
@@ -1,16 +1,19 @@
 <?xml version="1.0"?>
 <root xmlns:jx="http://apache.org/cocoon/templates/jx/1.0">
-    <one>
-        <jx:attribute name="foo" value="bar"/>
-    </one>
-    <jx:set var="a" value="dd"/>
-    <two>
-        <jx:if test="${a == 'dd'}">
-            <jx:attribute name="second" value="twoAttr"/>
-        </jx:if> xyz </two>
-    <three foo="bar">
-        <jx:attribute name="foo2" value="bar2"/>
-        <jx:attribute name="${a}" value="${a}"/>
-        <abc>def</abc>
-    </three>
+	<one>
+		<jx:attribute name="foo" value="bar"/>
+	</one>
+	<jx:set var="a" value="dd"/>
+	<two>
+		<jx:if test="${a == 'dd'}">
+			<jx:attribute name="second" value="twoAttr"/>
+		</jx:if> xyz </two>
+	<three foo="bar">
+		<jx:attribute name="foo2" value="bar2"/>
+		<jx:attribute name="${a}" value="${a}"/>
+		<abc>def</abc>
+	</three>
+	<nestedjx>
+		<jx:attribute name="value"><jx:formatDate value="${date}" pattern="dd MMMM yyyy" locale="pl_PL"/></jx:attribute>
+	</nestedjx>
 </root>