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/08/17 17:39:03 UTC

svn commit: r233189 - in /cocoon/branches/BRANCH_2_1_X/src: blocks/forms/java/org/apache/cocoon/forms/util/ java/org/apache/cocoon/components/treeprocessor/ java/org/apache/cocoon/generation/ java/org/apache/cocoon/util/location/ test/org/apache/cocoon...

Author: sylvain
Date: Wed Aug 17 08:38:52 2005
New Revision: 233189

URL: http://svn.apache.org/viewcvs?rev=233189&view=rev
Log:
Minor refactoring

Removed:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatorToAttributesPipe.java
Modified:
    cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/util/DomHelper.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/DefaultTreeBuilder.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationAttributes.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationImpl.java
    cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/util/location/LocationTestCase.java

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/util/DomHelper.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/util/DomHelper.java?rev=233189&r1=233188&r2=233189&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/util/DomHelper.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/forms/java/org/apache/cocoon/forms/util/DomHelper.java Wed Aug 17 08:38:52 2005
@@ -25,8 +25,8 @@
 
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.util.location.Location;
 import org.apache.cocoon.util.location.LocationAttributes;
-import org.apache.cocoon.util.location.LocatorToAttributesPipe;
 import org.apache.cocoon.xml.SaxBuffer;
 import org.apache.cocoon.xml.dom.DOMBuilder;
 import org.apache.cocoon.xml.dom.DOMStreamer;
@@ -63,6 +63,10 @@
 
     public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
 
+    public static Location getLocationObject(Element element) {
+        return LocationAttributes.getLocation(element);
+    }
+
     /**
      * Retrieves the location of an element node in the source file from which
      * the Document was created. This will only work for Document's created
@@ -295,7 +299,7 @@
         DOMBuilder builder = new DOMBuilder();
         
         // Enhance the sax stream with location information
-        ContentHandler locationHandler = new LocatorToAttributesPipe(builder);
+        ContentHandler locationHandler = new LocationAttributes.Pipe(builder);
         
         try {
             parser.parse(inputSource, locationHandler);

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/DefaultTreeBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/DefaultTreeBuilder.java?rev=233189&r1=233188&r2=233189&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/DefaultTreeBuilder.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/DefaultTreeBuilder.java Wed Aug 17 08:38:52 2005
@@ -493,7 +493,7 @@
         }
         desc.append('>');
         
-        Location rawLoc = LocationImpl.valueOf(config.getLocation());
+        Location rawLoc = LocationImpl.get(config);
         return new LocationImpl(desc.toString(), rawLoc.getURI(), rawLoc.getLineNumber(), rawLoc.getColumnNumber());
     }
 

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java?rev=233189&r1=233188&r2=233189&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java Wed Aug 17 08:38:52 2005
@@ -58,6 +58,7 @@
 import org.apache.cocoon.util.location.LocatedRuntimeException;
 import org.apache.cocoon.util.location.Location;
 import org.apache.cocoon.util.location.LocationAttributes;
+import org.apache.cocoon.util.location.LocationImpl;
 import org.apache.cocoon.xml.IncludeXMLConsumer;
 import org.apache.cocoon.xml.NamespacesTable;
 import org.apache.cocoon.xml.RedundantNamespacesFilter;
@@ -1883,7 +1884,7 @@
         private void addEvent(Event ev) throws SAXException {
             if (ev != null) {
                 if (lastEvent == null) {
-                    lastEvent = startEvent = new StartDocument(LocationAttributes.getLocation(locator, "template"));
+                    lastEvent = startEvent = new StartDocument(LocationImpl.get(locator, "template"));
                 } else {
                     flushChars();
                 }
@@ -1910,14 +1911,14 @@
             throws SAXException {
             if (charBuf == null) {
                 charBuf = new StringBuffer(length);
-                charLocation = LocationAttributes.getLocation(locator, "[text]");
+                charLocation = LocationImpl.get(locator, "[text]");
             }
             charBuf.append(ch, start, length);
         }
 
         public void endDocument() throws SAXException {
             StartDocument startDoc = (StartDocument)stack.pop();
-            EndDocument endDoc = new EndDocument(LocationAttributes.getLocation(locator, "template"));
+            EndDocument endDoc = new EndDocument(LocationImpl.get(locator, "template"));
             startDoc.endDocument = endDoc;
             addEvent(endDoc);
         }
@@ -1927,7 +1928,7 @@
             Event newEvent = null;
             if (NS.equals(namespaceURI)) {
                 StartInstruction startInstruction = (StartInstruction)start;
-                EndInstruction endInstruction = new EndInstruction(LocationAttributes.getLocation(locator, "<"+raw+">"), startInstruction);
+                EndInstruction endInstruction = new EndInstruction(LocationImpl.get(locator, "<"+raw+">"), startInstruction);
                 newEvent = endInstruction;
                 if (start instanceof StartWhen) {
                     StartWhen startWhen = (StartWhen)start;
@@ -1948,7 +1949,7 @@
                 }
             } else {
                 StartElement startElement = (StartElement)start;
-                newEvent = startElement.endElement = new EndElement(LocationAttributes.getLocation(locator, "<"+raw+">"), startElement);
+                newEvent = startElement.endElement = new EndElement(LocationImpl.get(locator, "<"+raw+">"), startElement);
             }
             addEvent(newEvent);
             if (start instanceof StartDefine) {
@@ -1958,17 +1959,17 @@
         }
 
         public void endPrefixMapping(String prefix) throws SAXException {
-            EndPrefixMapping endPrefixMapping = new EndPrefixMapping(LocationAttributes.getLocation(locator, null), prefix);
+            EndPrefixMapping endPrefixMapping = new EndPrefixMapping(LocationImpl.get(locator, null), prefix);
             addEvent(endPrefixMapping);
         }
 
         public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
-            Event ev = new IgnorableWhitespace(LocationAttributes.getLocation(locator, null), ch, start, length);
+            Event ev = new IgnorableWhitespace(LocationImpl.get(locator, null), ch, start, length);
             addEvent(ev);
         }
 
         public void processingInstruction(String target, String data) throws SAXException {
-            Event pi = new ProcessingInstruction(LocationAttributes.getLocation(locator, null), target, data);
+            Event pi = new ProcessingInstruction(LocationImpl.get(locator, null), target, data);
             addEvent(pi);
         }
 
@@ -1977,18 +1978,18 @@
         }
 
         public void skippedEntity(String name) throws SAXException {
-            addEvent(new SkippedEntity(LocationAttributes.getLocation(locator, null), name));
+            addEvent(new SkippedEntity(LocationImpl.get(locator, null), name));
         }
 
         public void startDocument() {
-            startEvent = new StartDocument(LocationAttributes.getLocation(locator, null));
+            startEvent = new StartDocument(LocationImpl.get(locator, null));
             lastEvent = startEvent;
             stack.push(lastEvent);
         }
 
         public void startElement(String namespaceURI, String localName, String qname, Attributes attrs) throws SAXException {
             Event newEvent = null;
-            Location locator = LocationAttributes.getLocation(this.locator, "<"+qname+">");
+            Location locator = LocationImpl.get(this.locator, "<"+qname+">");
             AttributesImpl elementAttributes = new AttributesImpl(attrs);
             int attributeCount = elementAttributes.getLength();
             for (int i = 0; i < attributeCount; i++) {
@@ -2199,7 +2200,7 @@
         }
 
         public void startPrefixMapping(String prefix, String uri) throws SAXException {
-            addEvent(new StartPrefixMapping(LocationAttributes.getLocation(locator, null), prefix, uri));
+            addEvent(new StartPrefixMapping(LocationImpl.get(locator, null), prefix, uri));
         }
 
         public void comment(char ch[], int start, int length) throws SAXException {
@@ -2207,27 +2208,27 @@
         }
 
         public void endCDATA() throws SAXException {
-            addEvent(new EndCDATA(LocationAttributes.getLocation(locator, null)));
+            addEvent(new EndCDATA(LocationImpl.get(locator, null)));
         }
 
         public void endDTD() throws SAXException {
-            addEvent(new EndDTD(LocationAttributes.getLocation(locator, null)));
+            addEvent(new EndDTD(LocationImpl.get(locator, null)));
         }
 
         public void endEntity(String name) throws SAXException {
-            addEvent(new EndEntity(LocationAttributes.getLocation(locator, null), name));
+            addEvent(new EndEntity(LocationImpl.get(locator, null), name));
         }
 
         public void startCDATA() throws SAXException {
-            addEvent(new StartCDATA(LocationAttributes.getLocation(locator, null)));
+            addEvent(new StartCDATA(LocationImpl.get(locator, null)));
         }
 
         public void startDTD(String name, String publicId, String systemId) throws SAXException {
-            addEvent(new StartDTD(LocationAttributes.getLocation(locator, null), name, publicId, systemId));
+            addEvent(new StartDTD(LocationImpl.get(locator, null), name, publicId, systemId));
         }
 
         public void startEntity(String name) throws SAXException {
-            addEvent(new StartEntity(LocationAttributes.getLocation(locator, null), name));
+            addEvent(new StartEntity(LocationImpl.get(locator, null), name));
         }
     }
 
@@ -2483,8 +2484,10 @@
     private void performGeneration(final XMLConsumer consumer, MyJexlContext jexlContext, JXPathContext jxpathContext,
                 StartElement macroCall, Event startEvent, Event endEvent) throws SAXException {
         cocoon.put("consumer", consumer);
-        execute(new RedundantNamespacesFilter(this.xmlConsumer), globalJexlContext, jxpathContext, null,
-                startEvent, null);
+        RedundantNamespacesFilter filter = new RedundantNamespacesFilter(this.xmlConsumer);
+//        EventPrinterPipe log = new EventPrinterPipe();
+//        log.setConsumer(filter);
+        execute(filter, globalJexlContext, jxpathContext, null, startEvent, null);
     }
 
     interface CharHandler {

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationAttributes.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationAttributes.java?rev=233189&r1=233188&r2=233189&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationAttributes.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationAttributes.java Wed Aug 17 08:38:52 2005
@@ -15,17 +15,28 @@
  */
 package org.apache.cocoon.util.location;
 
+import org.apache.cocoon.xml.AbstractXMLPipe;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Element;
 import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
 import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
 import org.xml.sax.helpers.AttributesImpl;
 
 /**
  * A class to handle location information stored in attributes.
- * These attributes are typically setup using {@link LocatorToAttributesPipe}
+ * These attributes are typically setup using {@link org.apache.cocoon.util.location.LocationAttributes.Pipe}
+ * which augments the SAX stream with additional attributes, e.g.:
+ * <pre>
+ * &lt;root xmlns:loc="http://apache.org/cocoon/location"
+ *       loc:src="file://path/to/file.xml"
+ *       loc:line="1" loc:column="1"&gt;
+ *   &lt;foo loc:src="file://path/to/file.xml" loc:line="2" loc:column="3"/&gt;
+ * &lt;/root&gt;
+ * </pre>
  * 
- * @see LocatorToAttributesPipe
+ * @see org.apache.cocoon.util.location.LocationAttributes.Pipe
  * @since 2.1.8
  * @version $Id$
  */
@@ -79,21 +90,6 @@
     }
     
     /**
-     * Returns the {@link Location} pointed to by a SAX <code>Locator</code>.
-     * 
-     * @param locator the locator (can be null)
-     * @param description a description for the location (can be null)
-     * @return the location
-     */
-    public static Location getLocation(Locator locator, String description) {
-        if (locator == null || locator.getSystemId() == null) {
-            return Location.UNKNOWN;
-        }
-        
-        return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
-    }
-    
-    /**
      * Returns the {@link Location} of an element (SAX flavor).
      * 
      * @param attrs the element's attributes that hold the location information
@@ -238,5 +234,64 @@
     public static int getColumn(Element elem) {
         Attr attr = elem.getAttributeNodeNS(URI, COL_ATTR);
         return attr != null ? Integer.parseInt(attr.getValue()) : -1;
+    }
+
+    /**
+     * A SAX filter that adds the information available from the <code>Locator</code> as attributes.
+     * The purpose of having location as attributes is to allow this information to survive transformations
+     * of the document (an XSL could copy these attributes over) or conversion of SAX events to a DOM.
+     * <p>
+     * The location is added as 3 attributes in a specific namespace to each element.
+     * <pre>
+     * &lt;root xmlns:loc="http://apache.org/cocoon/location"
+     *       loc:src="file://path/to/file.xml"
+     *       loc:line="1" loc:column="1"&gt;
+     *   &lt;foo loc:src="file://path/to/file.xml" loc:line="2" loc:column="3"/&gt;
+     * &lt;/root&gt;
+     * </pre>
+     * <strong>Note:</strong> Although this adds a lot of information to the serialized form of the document,
+     * the overhead in SAX events is not that big, as attribute names are interned, and all <code>src</code>
+     * attributes point to the same string.
+     * 
+     * @see org.apache.cocoon.util.location.LocationAttributes
+     * @since 2.1.8
+     */
+    public static class Pipe extends AbstractXMLPipe {
+        
+        private Locator locator;
+        
+        /**
+         * Create a filter. It has to be chained to another handler to be really useful.
+         */
+        public Pipe() {
+        }
+
+        /**
+         * Create a filter that is chained to another handler.
+         * @param next the next handler in the chain.
+         */
+        public Pipe(ContentHandler next) {
+            setContentHandler(next);
+        }
+
+        public void setDocumentLocator(Locator locator) {
+            this.locator = locator;
+            super.setDocumentLocator(locator);
+        }
+        
+        public void startDocument() throws SAXException {
+            super.startDocument();
+            super.startPrefixMapping(LocationAttributes.PREFIX, LocationAttributes.URI);
+        }
+        
+        public void endDocument() throws SAXException {
+            endPrefixMapping(LocationAttributes.PREFIX);
+            super.endDocument();
+        }
+
+        public void startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException {
+            // Add location attributes to the element
+            super.startElement(uri, loc, raw, LocationAttributes.addLocationAttributes(locator, attrs));
+        }
     }
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationImpl.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationImpl.java?rev=233189&r1=233188&r2=233189&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationImpl.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationImpl.java Wed Aug 17 08:38:52 2005
@@ -17,7 +17,9 @@
 
 import java.io.Serializable;
 
+import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.commons.lang.ObjectUtils;
+import org.xml.sax.Locator;
 
 /**
  * A simple immutable and serializable implementation of {@link Location}.
@@ -108,7 +110,7 @@
      * @param text the text to parse
      * @return the location (possibly UNKNOWN if text was null or in an incorrect format)
      */
-    public static LocationImpl valueOf(String text) throws IllegalArgumentException {
+    public static LocationImpl get(String text) throws IllegalArgumentException {
         if (text == null || text.length() == 0) {
             return UNKNOWN;
         }
@@ -147,6 +149,42 @@
         return UNKNOWN;
     }
 
+    /**
+     * Returns the {@link Location} pointed to by a SAX <code>Locator</code>.
+     * 
+     * @param locator the locator (can be null)
+     * @param description a description for the location (can be null)
+     * @return the location (possibly UNKNOWN)
+     */
+    public static LocationImpl get(Locator locator, String description) {
+        if (locator == null || locator.getSystemId() == null) {
+            return UNKNOWN;
+        }
+        
+        return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
+    }
+    
+    /**
+     * Returns the {@link Location} of an Avalon <code>Configuration</code> object.
+     * 
+     * 
+     */
+    public static LocationImpl get(Configuration config) {
+        if (config == null) {
+            return UNKNOWN;
+        }
+        
+        // Why in hell is "if (config instanceof Locatable)" producing a compilation error???
+        Object obj = config;
+        // We may have a locatable implementation of configuration
+        if (obj instanceof Locatable) {
+            return get(((Locatable)obj).getLocation());
+        }
+        
+        String locString = config.getLocation();
+        return get(locString);
+    }
+    
     /**
      * Get the description of this location
      * 

Modified: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/util/location/LocationTestCase.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/util/location/LocationTestCase.java?rev=233189&r1=233188&r2=233189&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/util/location/LocationTestCase.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/util/location/LocationTestCase.java Wed Aug 17 08:38:52 2005
@@ -32,7 +32,7 @@
 
     public void testParse() throws Exception {
         String str = "<map:generate> - path/to/file.xml:1:40";
-        Location loc = LocationImpl.valueOf(str);
+        Location loc = LocationImpl.get(str);
         
         assertEquals("<map:generate>", loc.getDescription());
         assertEquals("URI", "path/to/file.xml", loc.getURI());
@@ -42,7 +42,7 @@
     }
     
     public void testEquals() throws Exception {
-        Location loc1 = LocationImpl.valueOf(str);
+        Location loc1 = LocationImpl.get(str);
         Location loc2 = new LocationImpl(null, "path/to/file.xml", 1, 40);
         
         assertEquals("locations", loc1, loc2);