You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by an...@apache.org on 2005/07/23 23:44:47 UTC

svn commit: r224555 - in /cocoon/blocks/xsp/trunk: java/org/apache/cocoon/components/language/markup/xsp/XSPExpressionFilter.java samples/java/interpolation.xsp

Author: anathaniel
Date: Sat Jul 23 14:44:42 2005
New Revision: 224555

URL: http://svn.apache.org/viewcvs?rev=224555&view=rev
Log:
Make @xsp:attr-interpolation and @xsp:text-interpolation nested properties.
That allows to set them to false in a local scope as alternative to {##expr} escaping.

Modified:
    cocoon/blocks/xsp/trunk/java/org/apache/cocoon/components/language/markup/xsp/XSPExpressionFilter.java
    cocoon/blocks/xsp/trunk/samples/java/interpolation.xsp

Modified: cocoon/blocks/xsp/trunk/java/org/apache/cocoon/components/language/markup/xsp/XSPExpressionFilter.java
URL: http://svn.apache.org/viewcvs/cocoon/blocks/xsp/trunk/java/org/apache/cocoon/components/language/markup/xsp/XSPExpressionFilter.java?rev=224555&r1=224554&r2=224555&view=diff
==============================================================================
--- cocoon/blocks/xsp/trunk/java/org/apache/cocoon/components/language/markup/xsp/XSPExpressionFilter.java (original)
+++ cocoon/blocks/xsp/trunk/java/org/apache/cocoon/components/language/markup/xsp/XSPExpressionFilter.java Sat Jul 23 14:44:42 2005
@@ -28,7 +28,14 @@
 import org.xml.sax.XMLReader;
 import org.xml.sax.ext.LexicalHandler;
 import org.xml.sax.helpers.XMLFilterImpl;
+import java.util.LinkedList;
 
+/**
+ * Filter attributes and text and expand {#expr} to xsp:attribute and xsp:expr
+ * elements.
+ *
+ * @version SVN $Id$
+ */
 public class XSPExpressionFilter implements ContentHandler, XSPExpressionParser.Handler {
 
     public static class XMLPipeAdapter extends AbstractXMLPipe {
@@ -81,31 +88,24 @@
     /** The markup language prefix */
     private String markupPrefix;
 
-    /** Set default processing of attribute templates */
-    private boolean defaultProcessAttribs;
-
-    /** Set processing of attribute templates */
-    private boolean processAttribs;
-
-    /** Set default processing of text templates */
-    private boolean defaultProcessText;
-
-    /** Set processing of text templates */
-    private boolean processText;
+    /** Interpolation settings as nested properties */
+    private LinkedList interpolationStack;
 
     /** The parser for XSP value templates */
     private XSPExpressionParser expressionParser = new XSPExpressionParser(this);
 
-    /** First element was processed */
-    private boolean firstElementProcessed;
-
     private ContentHandler contentHandler;
 
     public XSPExpressionFilter(XSPMarkupLanguage markup) {
         this.markupURI = markup.getURI();
         this.markupPrefix = markup.getPrefix();
-        this.defaultProcessAttribs = markup.hasAttrInterpolation();
-        this.defaultProcessText = markup.hasTextInterpolation();
+
+        // Initialize default interpolation settings.
+        boolean attrInterpolation = markup.hasAttrInterpolation();
+        boolean textInterpolation = markup.hasTextInterpolation();
+        interpolationStack = new LinkedList();
+        interpolationStack.addLast(new InterpolationSettings(attrInterpolation,
+                                                             textInterpolation));
     }
 
     public void setContentHandler(ContentHandler contentHandler) {
@@ -120,9 +120,6 @@
      * @param language
      */
     public void startDocument() throws SAXException {
-        processAttribs = defaultProcessAttribs;
-        processText = defaultProcessText;
-
         contentHandler.startDocument();
     }
 
@@ -137,13 +134,10 @@
             throws SAXException {
         expressionParser.flush();
 
-        // Check template for processing flags in page
-        if (!firstElementProcessed) {
-            initFromAttribs(attribs);
-            firstElementProcessed = true;
-        }
+        // Check template for interpolation flags
+        attribs = pushInterpolationStack(attribs);
 
-        if (processAttribs) {
+        if (getInterpolationSettings().attrInterpolation) {
             // Attribute value templates enabled => process attributes
             AttributesImpl staticAttribs = new AttributesImpl();
             AttributesImpl dynamicAttribs = new AttributesImpl();
@@ -194,18 +188,43 @@
         }
     }
 
-    protected void initFromAttribs(Attributes attribs) {
-        String value = attribs.getValue(markupURI, XSPMarkupLanguage.ATTR_INTERPOLATION);
+    /**
+     * Check attributes for presence of interpolation flags.
+     * Push current settings to stack.
+     * Remove interpolation attributes and return cleaned attribute list.
+     */
+    private Attributes pushInterpolationStack(Attributes attribs) {
+        String valueAttr = attribs.getValue(markupURI, XSPMarkupLanguage.ATTR_INTERPOLATION);
+        String valueText = attribs.getValue(markupURI, XSPMarkupLanguage.ATTR_INTERPOLATION);
 
-        if (value != null) {
-            processAttribs = Boolean.valueOf(value).booleanValue();
+        // Neither interpolation flag in attribute list: push tail to stack.
+        if (valueAttr == null && valueText == null ) {
+            interpolationStack.addLast(interpolationStack.getLast());
+            return attribs;
         }
 
-        value = attribs.getValue(markupURI, XSPMarkupLanguage.TEXT_INTERPOLATION);
+        // Push new interpolation settings to stack and remove attributes.
+
+        InterpolationSettings lastSettings = (InterpolationSettings)interpolationStack.getLast();
+        boolean attrInterpolation = lastSettings.attrInterpolation;
+        boolean textInterpolation = lastSettings.textInterpolation;
+
+        AttributesImpl cleanedAttribs = new AttributesImpl(attribs);
+
+        if (valueAttr != null) {
+            attrInterpolation = Boolean.valueOf(valueAttr).booleanValue();
+            cleanedAttribs.removeAttribute(cleanedAttribs.getIndex(markupURI, XSPMarkupLanguage.ATTR_INTERPOLATION));
+        }
 
-        if (value != null) {
-            processText = Boolean.valueOf(value).booleanValue();
+        if (valueText != null) {
+            textInterpolation = Boolean.valueOf(valueText).booleanValue();
+            cleanedAttribs.removeAttribute(cleanedAttribs.getIndex(markupURI, XSPMarkupLanguage.TEXT_INTERPOLATION));
         }
+
+        interpolationStack.addLast(new InterpolationSettings(attrInterpolation,
+                                                             textInterpolation));
+
+        return cleanedAttribs;
     }
 
     /**
@@ -214,6 +233,9 @@
     public void endElement(String uri, String loc, String raw) throws SAXException {
         expressionParser.flush();
         contentHandler.endElement(uri, loc, raw);
+
+        // Pop stack of interpolation settings.
+        interpolationStack.removeLast();
     }
 
     /**
@@ -223,7 +245,7 @@
      * @see org.xml.sax.contentHandler.#characters(char[], int, int)
      */
     public void characters(char[] ch, int start, int length) throws SAXException {
-        if (processText) {
+        if (getInterpolationSettings().textInterpolation) {
             // Text templated enabled => Replace text expressions
             expressionParser.consume(ch, start, length);
         }
@@ -294,5 +316,26 @@
 
     public void startPrefixMapping(String prefix, String uri) throws SAXException {
         contentHandler.startPrefixMapping(prefix, uri);
+    }
+
+    /**
+     * Return current interpolation settings.
+     */
+    private InterpolationSettings getInterpolationSettings() {
+        return (InterpolationSettings)interpolationStack.getLast();
+    }
+
+    /**
+     * Structure to hold settings for attribute and text interpolation.
+     */
+    private static class InterpolationSettings
+    {
+        boolean attrInterpolation;
+        boolean textInterpolation;
+
+        InterpolationSettings(boolean attrInterpolation, boolean textInterpolation) {
+            this.attrInterpolation = attrInterpolation;
+            this.textInterpolation = textInterpolation;
+        }
     }
 }

Modified: cocoon/blocks/xsp/trunk/samples/java/interpolation.xsp
URL: http://svn.apache.org/viewcvs/cocoon/blocks/xsp/trunk/samples/java/interpolation.xsp?rev=224555&r1=224554&r2=224555&view=diff
==============================================================================
--- cocoon/blocks/xsp/trunk/samples/java/interpolation.xsp (original)
+++ cocoon/blocks/xsp/trunk/samples/java/interpolation.xsp Sat Jul 23 14:44:42 2005
@@ -1,6 +1,6 @@
 <?xml version="1.0"?>
 <!--
-  Copyright 1999-2004 The Apache Software Foundation
+  Copyright 2005 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.
@@ -15,7 +15,7 @@
   limitations under the License.
 -->
 
-<!-- CVS $Id: hello.xsp 30932 2004-07-29 17:35:38Z vgritsenko $ -->
+<!-- SVN $Id$ -->
 
 <xsp:page
   language="java"
@@ -26,7 +26,8 @@
     <title>Attribute and Text Interpolation</title>
     <content>
       <p>
-        {#Constants.COMPLETE_NAME} allows to use <tt>{##expr}</tt>
+        {#Constants.COMPLETE_NAME} allows to use
+        <tt>{##expr}</tt><!-- Note ## to escape interpolation. -->
         to replace the value of the Java expression <tt>expr</tt>
         within attribute values and text nodes.
       </p>
@@ -54,8 +55,9 @@
       <p>
         Just compare this:
       </p>
-      <pre><![CDATA[
-        <center style="color:{##color}">Hello {##world}!</center>]]>
+      <!-- Use attributes to disable interpolation in nested content. -->
+      <pre xsp:attr-interpolation="false" xsp:text-interpolation="false"><![CDATA[
+        <center style="color:{#color}">Hello {#world}!</center>]]>
       </pre>
       <center style="color:{#color}">Hello {#world}!</center>