forrest-svn mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thors...@apache.org
Subject svn commit: r354057 - in /forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher: DispatcherHelper.java transformation/DispatcherTransformer.java
Date Mon, 05 Dec 2005 12:40:30 GMT
Author: thorsten
Date: Mon Dec  5 04:39:51 2005
New Revision: 354057

URL: http://svn.apache.org/viewcvs?rev=354057&view=rev
Log:
Enhanced hooks matching and fixed the xpath bug caused by the used method. Extracted helper
methods to the dispatcher helper class. Enhanced overall 
state of the transformer to allow/disallow markup within the structurer. 

Activated final transformation to transform hooks to format specific markup. Need to be passed
by the pipeline.

Modified:
    forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/DispatcherHelper.java
    forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java

Modified: forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/DispatcherHelper.java
URL: http://svn.apache.org/viewcvs/forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/DispatcherHelper.java?rev=354057&r1=354056&r2=354057&view=diff
==============================================================================
--- forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/DispatcherHelper.java
(original)
+++ forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/DispatcherHelper.java
Mon Dec  5 04:39:51 2005
@@ -20,10 +20,14 @@
 import java.io.IOException;
 
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerFactory;
 
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.cocoon.xml.IncludeXMLConsumer;
 import org.apache.cocoon.xml.XMLUtils;
 import org.apache.cocoon.xml.dom.DOMBuilder;
 import org.apache.excalibur.source.SourceNotFoundException;
@@ -66,9 +70,9 @@
      * @throws IOException
      *             if an error occurs during source access..
      */
-    protected Document getDocument(String uri) throws Exception {
-        Document doc = org.apache.forrest.dispatcher.util.SourceUtil.readDOM(uri,
-                this.manager);
+    public Document getDocument(String uri) throws Exception {
+        Document doc = org.apache.forrest.dispatcher.util.SourceUtil.readDOM(
+                uri, this.manager);
         if (doc != null) {
             this.namespaceHelper = new NamespaceHelper(
                     DISPATCHER_NAMESPACE_URI, DISPATCHER_PREFIX, doc);
@@ -92,10 +96,34 @@
         return builder.getDocument();
 
     }
-    
+
+    public Transformer createTransformer()
+            throws TransformerConfigurationException {
+        TransformerFactory tFactory = TransformerFactory.newInstance();
+        Transformer transformer = tFactory.newTransformer();
+        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
+        // "yes");
+        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+        return transformer;
+
+    }
+
+    public Transformer createTransformer(Source source)
+            throws TransformerConfigurationException {
+        TransformerFactory tFactory = TransformerFactory.newInstance();
+        Transformer transformer = tFactory.newTransformer(source);
+        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
+        // "yes");
+        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+        return transformer;
+
+    }
+
     /**
-     * void setAttributesDOM(Attributes attr, Node xpathNode)
-     * sets all attributes to the node (like <xsl:copy-of select="@*"/>)
+     * void setAttributesDOM(Attributes attr, Node xpathNode) sets all
+     * attributes to the node (like <xsl:copy-of select="@*"/>)
      * 
      * @param attr
      * @param xpathNode
@@ -108,6 +136,25 @@
             String value = attr.getValue(i);
             ((Element) xpathNode).setAttribute(localName, value);
         }
+    }
+
+    public String setAttributesXPath(Attributes attr, String path)
+            throws DOMException {
+        if (attr.getLength() > 0) {
+            String xpath = "[";
+            for (int i = 0; i < attr.getLength(); i++) {
+                String localName = attr.getLocalName(i);
+                String value = attr.getValue(i);
+                xpath = xpath + "@" + localName + "='" + value + "'";
+                if (i < (attr.getLength() - 1)) {
+                    xpath = xpath + " and ";
+                }
+            }
+            xpath = xpath + "]";
+            path = path + xpath;
+        }
+        return path;
+
     }
 
     public DispatcherHelper(ServiceManager manager)

Modified: forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java
URL: http://svn.apache.org/viewcvs/forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java?rev=354057&r1=354056&r2=354057&view=diff
==============================================================================
--- forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java
(original)
+++ forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java
Mon Dec  5 04:39:51 2005
@@ -24,6 +24,9 @@
 
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
 
 import org.apache.avalon.framework.activity.Disposable;
 import org.apache.avalon.framework.parameters.Parameters;
@@ -62,7 +65,7 @@
      * format for the request.
      * 
      * <pre>
-     *                                              &lt;&lt;strong&gt;forrest:view&lt;/strong&gt;
format=&quot;html&quot;/&gt;
+     *       &lt;&lt;strong&gt;forrest:view&lt;/strong&gt; format=&quot;html&quot;/&gt;
      * </pre>
      */
     public static final String STRUCTURER_ELEMENT = "view";
@@ -74,7 +77,7 @@
      * <strong>format</strong>.
      * 
      * <pre>
-     *                                              &lt;forrest:view &lt;strong&gt;format&lt;/strong&gt;=&quot;html&quot;/&gt;
+     *       &lt;forrest:view &lt;strong&gt;format&lt;/strong&gt;=&quot;html&quot;/&gt;
      * </pre>
      */
     public static final String STRUCTURER_FORMAT_ATTRIBUTE = "type";
@@ -91,7 +94,7 @@
      * layout via e.g. css. In html for example
      * 
      * <pre>
-     *                                              &lt;forrest:hook name=&quot;test&quot;/&gt;
+     *       &lt;forrest:hook name=&quot;test&quot;/&gt;
      * </pre>
      * 
      * <p>
@@ -99,7 +102,7 @@
      * </p>
      * 
      * <pre>
-     *                                              &lt;div id=&quot;test&quot;/&gt;
+     *       &lt;div id=&quot;test&quot;/&gt;
      * </pre>
      */
     public static final String DISPATCHER_HOOK_ELEMENT = "hook";
@@ -109,7 +112,7 @@
      * css. In html for example
      * 
      * <pre>
-     *                                              &lt;forrest:css url=&quot;pelt.basic.css&quot;
media=&quot;screen&quot; theme=&quot;Pelt&quot;/&gt;
+     *       &lt;forrest:css url=&quot;pelt.basic.css&quot; media=&quot;screen&quot;
theme=&quot;Pelt&quot;/&gt;
      * </pre>
      * 
      * <p>
@@ -117,7 +120,7 @@
      * </p>
      * 
      * <pre>
-     *                                              &lt;link media=&quot;screen&quot;
href=&quot;../themes/pelt.basic.css&quot; title=&quot;Pelt&quot; rel=&quot;stylesheet&quot;
type=&quot;text/css&quot; /&gt;
+     *       &lt;link media=&quot;screen&quot; href=&quot;../themes/pelt.basic.css&quot;
title=&quot;Pelt&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;
/&gt;
      * </pre>
      */
     public static final String DISPATCHER_CSS_ELEMENT = "css";
@@ -164,11 +167,15 @@
 
     private String path = null;
 
-    private boolean recording;
+    private boolean recording,allowMarkup;
+    public static final String DISPATCHER_ALLOW_MARKUP="allowMarkup";
 
     private DispatcherHelper dispatcherHelper;
 
-    private boolean insideStructurer=false;
+    private boolean insideStructurer = false;
+
+    private String hooksXSL;
+    public static final String HOOKS_TRANSFORMER_PARAMETER="hooksTransformer";
 
     /**
      * Constructor Set the namespace
@@ -236,6 +243,7 @@
         this.processor = null;
         this.dispatcherHelper = null;
         this.contract = null;
+        this.hooksXSL=null;
         try {
             this.dispatcherHelper = new DispatcherHelper(manager);
             this.processor = (XPathProcessor) this.manager
@@ -248,6 +256,8 @@
         }
         storedPrefixMap = new HashMap();
         insideProperties = false;
+        this.allowMarkup = Boolean.getBoolean(parameters.getParameter(
+                DISPATCHER_ALLOW_MARKUP, null));
         this.requestedFormat = parameters.getParameter(
                 STRUCTURER_FORMAT_ATTRIBUTE, null);
         if (requestedFormat == null) {
@@ -256,6 +266,14 @@
             getLogger().error(error);
             throw new ProcessingException(error);
         }
+        this.hooksXSL=parameters.getParameter(HOOKS_TRANSFORMER_PARAMETER,null);
+        if (this.hooksXSL.equals("")||this.hooksXSL==null){
+            String error = "dispatcherError:\n"
+                + "You have to set the \"hooksTransformer\" parameter in the sitemap!";
+        getLogger().error(error);
+        throw new ProcessingException(error);
+        }
+        
     }
 
     public void startElement(String uri, String name, String raw,
@@ -296,7 +314,9 @@
                 propertyProcessingStart(uri, name, raw, attr);
             }
         } else {
-            if (!this.insideProperties & this.includeNodes)
+            if (!this.insideProperties & this.includeNodes&this.insideStructurer&this.allowMarkup)
+                super.startElement(uri, name, raw, attr);
+            if (!this.insideProperties & this.includeNodes&!this.insideStructurer)
                 super.startElement(uri, name, raw, attr);
         }
     }
@@ -313,17 +333,16 @@
         /* create a DOM node from the current sax event */
         Element currentElement = dispatcher.getDocument().createElement(name);
         dispatcherHelper.setAttributesDOM(attr, currentElement);
+        String tempPath = path + "/" + name;
+        tempPath=dispatcherHelper.setAttributesXPath(attr,tempPath);
         if (path == null || path.equals("")) {
-            path = raw;
+            path = name;
             this.rootNode.appendChild(currentElement);
         } else {
             /* calculate, prepare and add node to the dispatcher */
             try {
-                String tempPath = path + "/" + name;
                 Node xpathNode;
-                String[] xpath = DOMUtil.buildPathArray(path);
-                xpathNode = DOMUtil
-                        .getFirstNodeFromPath(rootNode, xpath, false);
+                xpathNode =DOMUtil.getSingleNode(rootNode,path,this.processor);
                 if (xpathNode == null)
                     createXpathNode(attr, tempPath);
                 else
@@ -335,7 +354,7 @@
                 getLogger().error(error);
                 throw new SAXException(error);
             }
-            path = path + "/" + name;
+            path = tempPath;
         }
     }
 
@@ -388,7 +407,9 @@
                 else
                     path = null;
         } else {
-            if (!this.insideProperties & this.includeNodes)
+            if (!this.insideProperties & this.includeNodes&this.insideStructurer&this.allowMarkup)
+                super.endElement(uri, name, raw);
+            if (!this.insideProperties & this.includeNodes&!this.insideStructurer)
                 super.endElement(uri, name, raw);
         }
     }
@@ -401,6 +422,7 @@
      * @throws SAXException
      */
     private void structurerProcessingStart(Attributes attr) throws SAXException {
+        this.insideStructurer = true;
         for (int i = 0; i < attr.getLength(); i++) {
             String localName = attr.getLocalName(i);
             String value = attr.getValue(i);
@@ -419,7 +441,6 @@
             if (path == null)
                 path = "result/";
             this.includeNodes = true;
-            this.insideStructurer=true;
             this.recording = true;
             try {
                 dispatcherHelper.setNamespaceHelper(
@@ -458,10 +479,27 @@
      */
     private void structurerProcessingEnd(String raw) throws SAXException {
         if (this.recording) {
-            XMLUtils.valueOf(new IncludeXMLConsumer(super.xmlConsumer),
-                    this.rootNode);
-            this.recording = false;
-            this.insideStructurer=false;
+            try {
+                DOMSource stylesheet = new DOMSource(dispatcherHelper.getDocument(this.hooksXSL));
+                Transformer transformer = dispatcherHelper.createTransformer(stylesheet);
+                DOMSource source = new DOMSource(this.rootNode.getFirstChild().getFirstChild());
+                DOMResult result = new DOMResult();
+                transformer.transform(source, result);
+                XMLUtils.valueOf(new IncludeXMLConsumer(super.xmlConsumer),
+                        result.getNode());
+            } catch (Exception e) {
+                String error = "dispatcherError: "
+                        + DispatcherException.ERROR_500
+                        + "\n"
+                        + "Could not setup final transformer!\n\n DispatcherStack:\n "
+                        + e;
+                getLogger().error(error);
+                this.recording = false;
+                throw new SAXException(error);
+            } finally {
+                this.recording = false;
+                this.insideStructurer = false;
+            }
         }
         if (getLogger().isDebugEnabled()) {
             getLogger().debug(
@@ -590,12 +628,10 @@
             /*
              * debug code - uncomment it if you need it! will output the
              * contract resulting data to sysout
-             * 
-             * DOMSource source = new DOMSource(node); StreamResult result = new
-             * StreamResult(System.out);
-             * contract.getContractTransformer().transform(source, result);
              */
-
+            // DOMSource source = new DOMSource(node);
+            // StreamResult result = new StreamResult(System.out);
+            // contract.getContractTransformer().transform(source, result);
             /*
              * append this node to the current path after testing where there is
              * a fixed location for the contract content. If so then add it
@@ -605,18 +641,18 @@
                     DispatcherHelper.DISPATCHER_NAMESPACE_URI, "part");
             for (int i = 0; i < contentChildren.getLength(); i++) {
                 Element contentChild = (Element) contentChildren.item(i);
+                NodeList finalContent = contentChild.getChildNodes();
                 if (contentChild != null) {
                     String location = contentChild.getAttribute("xpath");
                     if (location.equals("") | location == null) {
-                        String[] xpath = DOMUtil.buildPathArray(path);
-                        Node xpathNode = DOMUtil.getFirstNodeFromPath(rootNode,
-                                xpath, false);
+                        Node xpathNode =DOMUtil.getSingleNode(rootNode,path,this.processor);
+                        //String[] xpath = DOMUtil.buildPathArray(path);
+                     //   Node xpathNode = DOMUtil.getFirstNodeFromPath(rootNode,
+                    //            xpath, false);
                         if (xpathNode != null) {
-                            if (node.hasChildNodes()) {
-                                Node toMove = root.importNode(contentChild,
-                                        true);
-                                xpathNode.appendChild(toMove);
-                            }
+                            // add everything *within* the forrest:part element
+                            appendChildToResultIterator(root, finalContent,
+                                    xpathNode);
                         }
                     } else {
                         if (location.charAt(0) == '/')
@@ -628,18 +664,14 @@
                         xpathNode = DOMUtil.getFirstNodeFromPath(rootNode,
                                 xpath, false);
                         if (xpathNode != null) {
-                            if (node.hasChildNodes()) {
-                                Node toMove = root.importNode(contentChild,
-                                        true);
-                                xpathNode.appendChild(toMove);
-                            }
+                            // add everything *within* the forrest:part element
+                            appendChildToResultIterator(root, finalContent,
+                                    xpathNode);
                         } else {
                             xpathNode = createXpathNode(location);
-                            if (node.hasChildNodes()) {
-                                Node toMove = root.importNode(contentChild,
-                                        true);
-                                xpathNode.appendChild(toMove);
-                            }
+                            // add everything *within* the forrest:part element
+                            appendChildToResultIterator(root, finalContent,
+                                    xpathNode);
                         }
                     }
                 }
@@ -660,6 +692,33 @@
     }
 
     /**
+     * @param root
+     * @param finalContent
+     * @param xpathNode
+     * @throws DOMException
+     */
+    private void appendChildToResultIterator(Document root,
+            NodeList finalContent, Node xpathNode) throws DOMException {
+        for (int j = 0; j < finalContent.getLength(); j++) {
+            appendChildToResult(root, finalContent, xpathNode, j);
+        }
+    }
+
+    /**
+     * @param root
+     * @param finalContent
+     * @param xpathNode
+     * @param j
+     * @throws DOMException
+     */
+    private void appendChildToResult(Document root, NodeList finalContent,
+            Node xpathNode, int j) throws DOMException {
+        Node contractData = finalContent.item(j);
+        Node toMove = root.importNode(contractData, true);
+        xpathNode.appendChild(toMove);
+    }
+
+    /**
      * @param uri
      * @param name
      * @param raw
@@ -671,7 +730,7 @@
         for (int i = 0; i < attr.getLength(); i++) {
             String localName = attr.getLocalName(i);
             String value = attr.getValue(i);
-            if (ContractBean.PROPERTY_ID_ATTRIBUTE.equals(localName)) 
+            if (ContractBean.PROPERTY_ID_ATTRIBUTE.equals(localName))
                 this.propertyID = value;
         }
         if (this.propertyID.equals("") | this.propertyID == null) {
@@ -728,21 +787,24 @@
     public void characters(char c[], int start, int len) throws SAXException {
         if (this.insideProperties & this.includeNodes)
             this.builder.characters(c, start, len);
-        else if (!this.insideProperties & this.includeNodes&!this.insideStructurer)
+        else if (!this.insideProperties & this.includeNodes
+                & !this.insideStructurer)
             super.contentHandler.characters(c, start, len);
     }
 
     public void startCDATA() throws SAXException {
         if (this.insideProperties & this.includeNodes)
             this.builder.startCDATA();
-        else if (!this.insideProperties & this.includeNodes&!this.insideStructurer)
+        else if (!this.insideProperties & this.includeNodes
+                & !this.insideStructurer)
             super.lexicalHandler.startCDATA();
     }
 
     public void endCDATA() throws SAXException {
         if (this.insideProperties & this.includeNodes)
             this.builder.endCDATA();
-        else if (!this.insideProperties & this.includeNodes&!this.insideStructurer)
+        else if (!this.insideProperties & this.includeNodes
+                & !this.insideStructurer)
             super.lexicalHandler.endCDATA();
     }
 



Mime
View raw message