You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by sy...@apache.org on 2005/06/17 08:27:55 UTC

svn commit: r191091 - /cocoon/trunk/src/java/org/apache/cocoon/transformation/BrowserUpdateTransformer.java

Author: sylvain
Date: Thu Jun 16 23:27:52 2005
New Revision: 191091

URL: http://svn.apache.org/viewcvs?rev=191091&view=rev
Log:
CFroms tree widget: forgot a file in the previous commit...

Modified:
    cocoon/trunk/src/java/org/apache/cocoon/transformation/BrowserUpdateTransformer.java

Modified: cocoon/trunk/src/java/org/apache/cocoon/transformation/BrowserUpdateTransformer.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/transformation/BrowserUpdateTransformer.java?rev=191091&r1=191090&r2=191091&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/transformation/BrowserUpdateTransformer.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/transformation/BrowserUpdateTransformer.java Thu Jun 16 23:27:52 2005
@@ -11,7 +11,9 @@
 import org.apache.cocoon.xml.AttributesImpl;
 import org.apache.cocoon.xml.RedundantNamespacesFilter;
 import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 public class BrowserUpdateTransformer extends AbstractTransformer {
     
@@ -19,19 +21,30 @@
     
     public static final String BU_NSURI = "http://apache.org/cocoon/browser-update/1.0";
     
-    private boolean ajaxMode = false;
+    private boolean ajaxRequest = false;
     
     private int replaceDepth = 0;
+    
+    private boolean inUpdateTag = false;
+    private String updateTagId = null;
+    
+    Locator locator;
 
     public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) throws ProcessingException, SAXException, IOException {
 
         Request request = ObjectModelHelper.getRequest(objectModel);
-        this.ajaxMode = request.getParameter(AJAXMODE_PARAM) != null;
+        this.ajaxRequest = request.getParameter(AJAXMODE_PARAM) != null;
+    }
+    
+    public void setDocumentLocator(Locator locator) {
+        super.setDocumentLocator(locator);
+        this.locator = locator;
     }
 
+
     public void startDocument() throws SAXException {
         
-        if (ajaxMode) {
+        if (ajaxRequest) {
             // Add the namespace filter to our own output.
             // This is needed as flattening bu:* elements can lead to some weird reordering of
             // namespace declarations...
@@ -45,7 +58,7 @@
         }
         
         super.startDocument();
-        if (ajaxMode) {
+        if (ajaxRequest) {
             // Add a root element. The original one is very likely to be stripped
             super.startPrefixMapping("bu", BU_NSURI);
             super.startElement(BU_NSURI, "document", "bu:document", new AttributesImpl());
@@ -57,23 +70,58 @@
         super.startPrefixMapping(prefix, uri);
     }
 
-    public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException {
+    public void startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException {
         if (BU_NSURI.equals(uri) && "replace".equals(loc)) {
-            if (this.ajaxMode && this.replaceDepth == 0) {
+            // Keep the id attribute. It may be null, in which case the one of the
+            // child element will be used.
+            this.updateTagId = attrs.getValue("id");
+            this.inUpdateTag = true;
+            if (this.ajaxRequest && this.replaceDepth == 0) {
                 // Pass this element through
-                super.startElement(uri, loc, raw, a);
+                super.startElement(uri, loc, raw, attrs);
             }
             replaceDepth++;
         } else {
             // Passthrough if not in ajax mode or if in a bu:replace
-            if (!this.ajaxMode || this.replaceDepth > 0) {
-                super.startElement(uri, loc, raw, a);
+            
+            // Is the enclosing element a bu:replace?
+            if (this.inUpdateTag) {
+                this.inUpdateTag = false;
+                // Is there already an id?
+                String localId = attrs.getValue("id");
+                if (localId != null) {
+                    // Yes: check it's the same
+                    if (this.updateTagId != null && !localId.equals(this.updateTagId)) {
+                        throw new SAXParseException("Id on bu:replace (" + this.updateTagId + ") and " + raw + " (" +
+                                localId + ") don't match.", this.locator);
+                    }
+                } else {
+                    // No: add it
+                    if (this.updateTagId == null) {
+                        throw new SAXParseException("Neither bu:replace nor " + raw + " have an id attribute.", this.locator);
+                    }
+                    AttributesImpl newAttrs = new AttributesImpl(attrs);
+                    newAttrs.addCDATAAttribute("id", this.updateTagId);
+                    attrs = newAttrs;
+                }
+                this.updateTagId = null;
+            }
+            if (!this.ajaxRequest || this.replaceDepth > 0) {
+                super.startElement(uri, loc, raw, attrs);
             }
         }
     }
 
     public void characters(char[] buffer, int offset, int len) throws SAXException {
-        if (!this.ajaxMode || this.replaceDepth > 0) {
+        if (this.inUpdateTag) {
+            // Check that it's only spaces
+            for (int i = offset; i < len; i++) {
+                if (!Character.isWhitespace(buffer[i])) {
+                    throw new SAXParseException("bu:replace must include a single child element and no text.", this.locator);
+                }
+            }
+        }
+        if (!this.ajaxRequest || this.replaceDepth > 0) {
             super.characters(buffer, offset, len);
         }
     }
@@ -81,13 +129,13 @@
     public void endElement(String uri, String loc, String raw) throws SAXException {
         if (BU_NSURI.equals(uri) && "replace".equals(loc)) {
             replaceDepth--;
-            if (this.ajaxMode && this.replaceDepth == 0) {
+            if (this.ajaxRequest && this.replaceDepth == 0) {
                 // Pass this element through
                 super.endElement(uri, loc, raw);
             }
         } else {
             // Passthrough if not in ajax mode or if in a bu:replace
-            if (!this.ajaxMode || this.replaceDepth > 0) {
+            if (!this.ajaxRequest || this.replaceDepth > 0) {
                 super.endElement(uri, loc, raw);
             }
         }
@@ -99,7 +147,7 @@
     }
 
     public void endDocument() throws SAXException {
-        if (ajaxMode) {
+        if (ajaxRequest) {
             super.endElement(BU_NSURI, "document", "bu:document");
             super.endPrefixMapping("bu");
         }