cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ja...@apache.org
Subject svn commit: r933386 - in /cocoon/trunk/core: cocoon-core/src/test/resources/org/apache/cocoon/transformation/ cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/transformation/ cocoon-pipeline/cocoon-pipeline-impl/src/main/java/...
Date Mon, 12 Apr 2010 19:49:09 GMT
Author: jasha
Date: Mon Apr 12 19:49:09 2010
New Revision: 933386

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

Modified:
    cocoon/trunk/core/cocoon-core/src/test/resources/org/apache/cocoon/transformation/xinclude-result-1.xml
    cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/transformation/XIncludeTransformer.java
    cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-impl/src/main/java/org/apache/cocoon/xml/XMLBaseSupport.java

Modified: cocoon/trunk/core/cocoon-core/src/test/resources/org/apache/cocoon/transformation/xinclude-result-1.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/test/resources/org/apache/cocoon/transformation/xinclude-result-1.xml?rev=933386&r1=933385&r2=933386&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/test/resources/org/apache/cocoon/transformation/xinclude-result-1.xml
(original)
+++ cocoon/trunk/core/cocoon-core/src/test/resources/org/apache/cocoon/transformation/xinclude-result-1.xml
Mon Apr 12 19:49:09 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/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/transformation/XIncludeTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/transformation/XIncludeTransformer.java?rev=933386&r1=933385&r2=933386&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/transformation/XIncludeTransformer.java
(original)
+++ cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-components/src/main/java/org/apache/cocoon/transformation/XIncludeTransformer.java
Mon Apr 12 19:49:09 2010
@@ -56,6 +56,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;
 
 /**
  * Implementation of an XInclude transformer. It supports xml:base attributes,
@@ -78,7 +79,10 @@ public class XIncludeTransformer extends
     protected ServiceManager manager;
     private XIncludePipe xIncludePipe;
 
+    public static final String XMLBASE_NAMESPACE_URI = javax.xml.XMLConstants.XML_NS_URI;
+    public static final String XMLBASE_NAMESPACE_PREFIX = javax.xml.XMLConstants.XML_NS_PREFIX;
     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";
@@ -184,6 +188,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;
 
@@ -229,7 +245,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:
@@ -259,15 +277,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/trunk/core/cocoon-pipeline/cocoon-pipeline-impl/src/main/java/org/apache/cocoon/xml/XMLBaseSupport.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-impl/src/main/java/org/apache/cocoon/xml/XMLBaseSupport.java?rev=933386&r1=933385&r2=933386&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-impl/src/main/java/org/apache/cocoon/xml/XMLBaseSupport.java
(original)
+++ cocoon/trunk/core/cocoon-pipeline/cocoon-pipeline-impl/src/main/java/org/apache/cocoon/xml/XMLBaseSupport.java
Mon Apr 12 19:49:09 2010
@@ -119,7 +119,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();



Mime
View raw message