cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject svn commit: rev 36259 - in cocoon/branches/butterfly/src: java java/org/apache/butterfly/serialization java/org/apache/butterfly/xml webapp
Date Wed, 11 Aug 2004 22:12:55 GMT
Author: ugo
Date: Wed Aug 11 15:12:54 2004
New Revision: 36259

Added:
   cocoon/branches/butterfly/src/java/org/apache/butterfly/serialization/TextSerializer.java
   cocoon/branches/butterfly/src/webapp/sitemap.xmap
   cocoon/branches/butterfly/src/webapp/sitemap.xslt
Modified:
   cocoon/branches/butterfly/src/java/applicationContext.xml
   cocoon/branches/butterfly/src/java/org/apache/butterfly/xml/AbstractXMLProducer.java
   cocoon/branches/butterfly/src/webapp/sitemap.groovy
Log:
Text serializer

Modified: cocoon/branches/butterfly/src/java/applicationContext.xml
==============================================================================
--- cocoon/branches/butterfly/src/java/applicationContext.xml	(original)
+++ cocoon/branches/butterfly/src/java/applicationContext.xml	Wed Aug 11 15:12:54 2004
@@ -19,7 +19,7 @@
 <!--+
     | Spring's bean factory configuration file.
     |
-    |  $Id: beans.xml,v 1.1 2004/07/23 08:47:20 ugo Exp $
+    |  $Id$
     +-->
   
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
@@ -66,6 +66,15 @@
   </bean>
 
   <bean id="xmlSerializer" class="org.apache.butterfly.serialization.XMLSerializer" singleton="false">
+    <property name="traxTransformerFactory">
+      <ref bean="traxTransformerFactory"/>
+    </property>
+    <property name="encoding">
+      <value>UTF-8</value>
+    </property>
+  </bean>
+
+  <bean id="textSerializer" class="org.apache.butterfly.serialization.TextSerializer"
singleton="false">
     <property name="traxTransformerFactory">
       <ref bean="traxTransformerFactory"/>
     </property>

Added: cocoon/branches/butterfly/src/java/org/apache/butterfly/serialization/TextSerializer.java
==============================================================================
--- (empty file)
+++ cocoon/branches/butterfly/src/java/org/apache/butterfly/serialization/TextSerializer.java
Wed Aug 11 15:12:54 2004
@@ -0,0 +1,145 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.butterfly.serialization;
+
+import java.io.OutputStream;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.butterfly.xml.AbstractXMLPipe;
+import org.apache.butterfly.xml.xslt.TraxTransformerFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+/**
+ * Text serializer converts XML into plain text.
+ * It omits all XML tags and writes only character events to the output.
+ * Internally, text serializer uses XML serializer with {@link OutputKeys#METHOD}
+ * set to <code>text</code>.
+ *
+ * <p>Input document must have at least one element - root element - which
+ * should wrap all the text inside it.
+ *
+ * @version CVS $Id$
+ */
+public class TextSerializer extends AbstractXMLPipe implements Serializer {
+
+    protected static final Log logger = LogFactory.getLog(TextSerializer.class);
+    protected OutputStream output;
+    private Map objectModel;
+    protected TraxTransformerFactory transformerFactory;
+
+    /**
+     * Set to true after first XML element
+     */
+    private boolean hasRootElement;
+
+    /**
+     * Set to true after first XML element
+     */
+    private boolean hadNoRootElement;
+
+    /**
+     * The <code>Properties</code> used by this serializer.
+     */
+    protected Properties format = new Properties();
+
+    public TextSerializer() {
+        this.format.put(OutputKeys.METHOD, "text");
+    }
+
+    /**
+     * @param transformerFactory The transformerFactory to set.
+     */
+    public void setTraxTransformerFactory(TraxTransformerFactory transformerFactory) {
+        this.transformerFactory = transformerFactory;
+    }
+    
+    /**
+     * Set the {@link OutputStream} where the requested resource should
+     * be serialized.
+     */
+    public void setOutputStream(OutputStream output) {
+        this.output = output;
+        TransformerHandler handler = this.transformerFactory.getTransformerHandler();
+        handler.getTransformer().setOutputProperties(format);
+        handler.setResult(new StreamResult(this.output));
+        this.setContentHandler(handler);
+        this.setLexicalHandler(handler);
+    }
+
+    public void startElement(String uri, String loc, String raw, Attributes a)
+            throws SAXException {
+        this.hasRootElement = true;
+        super.startElement(uri, loc, raw, a);
+    }
+
+    public void characters(char c[], int start, int len)
+            throws SAXException {
+        if (!this.hasRootElement) {
+            this.hasRootElement = this.hadNoRootElement = true;
+            logger.warn("Encountered text before root element. Creating <text> wrapper
element.");
+            super.startElement("", "text", "text", new AttributesImpl());
+        }
+        super.characters(c, start, len);
+    }
+
+    public void endDocument() throws SAXException {
+        if (this.hadNoRootElement) {
+            super.endElement("", "text", "text");
+        }
+        super.endDocument();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.butterfly.sitemap.SitemapOutputComponent#getEncoding()
+     */
+    public String getEncoding() {
+        return (String) format.get(OutputKeys.ENCODING);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.butterfly.sitemap.SitemapOutputComponent#getMimeType()
+     */
+    public String getMimeType() {
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.butterfly.sitemap.SitemapOutputComponent#shouldSetContentLength()
+     */
+    public boolean shouldSetContentLength() {
+        return false;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.butterfly.sitemap.SitemapOutputComponent#setObjectModel(java.util.Map)
+     */
+    public void setObjectModel(Map objectModel) {
+        this.objectModel = objectModel;
+    }
+    
+    public void setEncoding(String encoding) {
+        format.put(OutputKeys.ENCODING, encoding);
+    }
+}

Modified: cocoon/branches/butterfly/src/java/org/apache/butterfly/xml/AbstractXMLProducer.java
==============================================================================
--- cocoon/branches/butterfly/src/java/org/apache/butterfly/xml/AbstractXMLProducer.java	(original)
+++ cocoon/branches/butterfly/src/java/org/apache/butterfly/xml/AbstractXMLProducer.java	Wed
Aug 11 15:12:54 2004
@@ -24,7 +24,7 @@
  *
  * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
  *         (Apache Software Foundation)
- * @version CVS $Id: AbstractXMLProducer.java,v 1.1 2004/07/23 08:47:20 ugo Exp $
+ * @version CVS $Id$
  */
 public abstract class AbstractXMLProducer
 implements XMLProducer {
@@ -68,14 +68,5 @@
      */
     public void setLexicalHandler(LexicalHandler handler) {
         this.lexicalHandler = handler;
-    }
-
-    /**
-     * Recycle the producer by removing references
-     */
-    public void recycle() {
-        this.xmlConsumer = null;
-        this.contentHandler = null;
-        this.lexicalHandler = null;
     }
 }

Modified: cocoon/branches/butterfly/src/webapp/sitemap.groovy
==============================================================================
--- cocoon/branches/butterfly/src/webapp/sitemap.groovy	(original)
+++ cocoon/branches/butterfly/src/webapp/sitemap.groovy	Wed Aug 11 15:12:54 2004
@@ -35,6 +35,10 @@
             read "resources/images/" + m.group(1) + ".gif", "image/gif", []
         } else if (m = (uri =~ "styles/(.*)\.css")) {
             read "resources/styles/" + m.group(1) + ".css", "text/css", []
+        } else if (m = (uri == "sitemap.groovy")) {
+            generate "file", "sitemap.xmap", []
+            transform "trax", "sitemap.xslt", [] 
+            serialize "text", []
         } else {
             return false;
         }

Added: cocoon/branches/butterfly/src/webapp/sitemap.xmap
==============================================================================
--- (empty file)
+++ cocoon/branches/butterfly/src/webapp/sitemap.xmap	Wed Aug 11 15:12:54 2004
@@ -0,0 +1,35 @@
+<?xml version="1.0"?>
+
+<!-- CVS $Id$ -->
+
+<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
+
+  <map:pipeline>
+    
+    <map:match pattern="">
+      <map:generate type="file" src="welcome.xml"/>
+      <map:transform type="trax" src="welcome.xslt"/>
+      <map:serialize type="xml">
+        <map:parameter name="encoding" value="UTF-8"/>
+      </map:serialize>
+    </map:match>
+    
+    <map:match pattern="(.*)\.html">
+      <map:generate type="file" src="{1}.xml"/>
+      <map:transform type="trax" src="welcome.xslt"/>
+      <map:serialize type="xml">
+        <map:parameter name="encoding" value="UTF-8"/>
+      </map:serialize>
+    </map:match>
+    
+    <map:match pattern="styles/(.*)\.css">
+      <map:read src="{1}" mime-type="text/css"/>
+    </map:match>
+    
+    <map:match pattern="images/(.*)\.gif">
+      <map:read src="{1}" mime-type="image/gif"/>
+    </map:match>
+    
+  </map:pipeline>
+
+</map:sitemap>
\ No newline at end of file

Added: cocoon/branches/butterfly/src/webapp/sitemap.xslt
==============================================================================
--- (empty file)
+++ cocoon/branches/butterfly/src/webapp/sitemap.xslt	Wed Aug 11 15:12:54 2004
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Copyright 1999-2004 The Apache Software Foundation
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<!-- CVS $Id$ -->
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+  
+  <xsl:template match="/">
+class Sitemap extends Pipeline {
+  
+    boolean setup(environment) {
+        uri = environment.uri
+        ...
+    }
+}    
+  </xsl:template>
+  
+</xsl:stylesheet>

Mime
View raw message