cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ja...@apache.org
Subject svn commit: r933707 - in /cocoon/branches/BRANCH_2_1_X: ./ src/java/org/apache/cocoon/transformation/XIncludeTransformer.java src/java/org/apache/cocoon/xml/XMLBaseSupport.java src/test/org/apache/cocoon/transformation/xinclude-result-1.xml status.xml
Date Tue, 13 Apr 2010 17:17:47 GMT
Author: jasha
Date: Tue Apr 13 17:17:47 2010
New Revision: 933707

URL: http://svn.apache.org/viewvc?rev=933707&view=rev
Log:
COCOON-2104 Add base URI fixup support to XIncludeTransformer. Patch by Andrew Cave

Modified:
    cocoon/branches/BRANCH_2_1_X/   (props changed)
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/XIncludeTransformer.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/XMLBaseSupport.java
    cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/transformation/xinclude-result-1.xml
    cocoon/branches/BRANCH_2_1_X/status.xml

Propchange: cocoon/branches/BRANCH_2_1_X/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Tue Apr 13 17:17:47 2010
@@ -1,20 +1,19 @@
-bin
-build
-dist
-prj.el
-.project
-.classpath
-*.launch
-local.build.properties
-local.blocks.properties
-cocoon.sampledata
-.DS_Store
-*.ipr
-*.iws
-*.iml
-mount-table.xml
-tomcat
-tomcat.bat
-slide
-local.blocks.*.xconf
-.settings
+local.blocks.*.xconf
+mount-table.xml
+*.iws
+.DS_Store
+.project
+cocoon.sampledata
+build
+local.build.properties
+.settings
+*.ipr
+tomcat.bat
+tomcat
+.classpath
+*.iml
+slide
+bin
+*.launch
+prj.el
+dist

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/XIncludeTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/XIncludeTransformer.java?rev=933707&r1=933706&r2=933707&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/XIncludeTransformer.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/XIncludeTransformer.java
Tue Apr 13 17:17:47 2010
@@ -54,6 +54,7 @@ import org.xml.sax.ContentHandler;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.helpers.AttributesImpl;
 
 /**
  * @cocoon.sitemap.component.documentation
@@ -81,7 +82,9 @@ public class XIncludeTransformer extends
      * @deprecated Should be removed in cocoon 2.2. Use javax.xml.XMLConstants.XML_NS_URI
instead.
      */
     public static final String XMLBASE_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace";
+    public static final String XMLBASE_NAMESPACE_PREFIX = "xml";
     public static final String XMLBASE_ATTRIBUTE = "base";
+    public static final String XMLBASE_ATTRIBUTE_TYPE = "CDATA";
 
     public static final String XINCLUDE_NAMESPACE_URI = "http://www.w3.org/2001/XInclude";
     public static final String XINCLUDE_INCLUDE_ELEMENT = "include";
@@ -181,6 +184,18 @@ public class XIncludeTransformer extends
          * XIncludePipe. Used to detect loop inclusions.
          */
         private String xpointer;
+        
+        /**
+         * Value of the current element level. Used to determine when to insert
+         * xml:base attributes for base URI fixup.
+         */
+        private int level = 0;
+
+        /**
+         * Base URI of the parent of the current element. Used to determine
+         * if base URI fixup is necessary.
+         */
+        private String parentBaseURI = null;
 
         private XIncludePipe parent;
 
@@ -226,7 +241,9 @@ public class XIncludeTransformer extends
 
         public void startElement(String uri, String name, String raw, Attributes attr) throws
SAXException {
             // Track xml:base context:
+            parentBaseURI = xmlBaseSupport.getCurrentBase();
             xmlBaseSupport.startElement(uri, name, raw, attr);
+            this.level++;
             // Handle elements in xinclude namespace:
             if (XINCLUDE_NAMESPACE_URI.equals(uri)) {
                 // Handle xi:include:
@@ -256,15 +273,56 @@ public class XIncludeTransformer extends
                     throw new SAXException("Unknown XInclude element " + raw + " at " + getLocation());
                 }
             } else if (isEvaluatingContent()) {
-                // Copy other elements through when appropriate:
-                super.startElement(uri, name, raw, attr);
-            }
+                // Copy other elements through when appropriate,
+                // performing base URI fixup when necessary.
+                if(mustAddBaseAttr())
+                    super.startElement(uri, name, raw, addBaseURI(attr));
+                else
+                    super.startElement(uri, name, raw, attr);
+            }
+        }
+       
+        private boolean mustAddBaseAttr(){
+            if(level != 1)
+                return false;
+            if(this.parent == null)
+                return false;
+            String parentBase = this.parent.parentBaseURI;
+            String currentBase = xmlBaseSupport.getCurrentBase();
+            if(currentBase == null)
+                return false;
+            if(parentBase == null || !parentBase.equals(currentBase))
+                return true;
+            return false;
+        }
+
+        /**
+         * Adds xml:base attribute as per the XInclude spec.
+         */
+        private Attributes addBaseURI(Attributes oldAttr) throws SAXException {         

+            String currentBaseURI = xmlBaseSupport.getCurrentBase();
+
+            AttributesImpl fixedAttr = new AttributesImpl(oldAttr);
+                
+            // Old xml:base attributes are removed.
+            int xmlBaseAttrIdx = fixedAttr.getIndex(XMLBASE_NAMESPACE_URI, XMLBASE_ATTRIBUTE);
+            if(xmlBaseAttrIdx != -1)
+                fixedAttr.removeAttribute(xmlBaseAttrIdx);
+            
+            fixedAttr.addAttribute(
+                    XMLBASE_NAMESPACE_URI, XMLBASE_ATTRIBUTE,
+                    XMLBASE_NAMESPACE_PREFIX + ":" + XMLBASE_ATTRIBUTE,
+                    XMLBASE_ATTRIBUTE_TYPE,
+                    currentBaseURI
+            );
+            return fixedAttr;
         }
 
         public void endElement(String uri, String name, String raw) throws SAXException {
             // Track xml:base context:
             xmlBaseSupport.endElement(uri, name, raw);
-
+            this.level--;
+            
             // Handle elements in xinclude namespace:
             if (XINCLUDE_NAMESPACE_URI.equals(uri)) {
                 // Handle xi:include:

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/XMLBaseSupport.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/XMLBaseSupport.java?rev=933707&r1=933706&r2=933707&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/XMLBaseSupport.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/xml/XMLBaseSupport.java Tue Apr
13 17:17:47 2010
@@ -117,7 +117,10 @@ public class XMLBaseSupport {
         return resolve(getCurrentBase(), spec);
     }
 
-    private String getCurrentBase() {
+    /**
+     * Returns the base URI currently in effect, or null if unknown.
+     */
+    public String getCurrentBase() {
         if (bases.size() > 0) {
             BaseInfo baseInfo = (BaseInfo)bases.peek();
             return baseInfo.getUrl();

Modified: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/transformation/xinclude-result-1.xml
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/transformation/xinclude-result-1.xml?rev=933707&r1=933706&r2=933707&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/transformation/xinclude-result-1.xml
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/transformation/xinclude-result-1.xml
Tue Apr 13 17:17:47 2010
@@ -21,7 +21,7 @@
   <p>P1</p>
   <p>P2</p>
 
-<root-include>
+<root-include xml:base="resource://org/apache/cocoon/transformation/xinclude-incl.xml">
   <br/>
   <p>include 1</p>
   <p>include 2</p>
@@ -31,4 +31,4 @@
 
   <p>P3</p>
   <p>P4</p>
-</root>
\ No newline at end of file
+</root>

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/status.xml?rev=933707&r1=933706&r2=933707&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Tue Apr 13 17:17:47 2010
@@ -187,6 +187,9 @@
     <action dev="all" type="update">
       Starting with 2.1.12 the minimum required Java version will be 1.4.2.
     </action>
+    <action dev="JSJ" type="fix" fixes-bug="COCOON-2104" due-to="Andrew Cave" due-to-email="ajcave@ualberta.ca">
+      Add base URI fixup support to XIncludeTransformer
+    </action>
     <action dev="JSJ" type="fix" fixes-bug="COCOON-2041" due-to="Edward Riede" due-to-email="edriede@yahoo.com">
       Repository block: WebDAV Returns improper status on PUT
     </action>



Mime
View raw message