svn commit: r1226316 - in /cocoon/cocoon3/trunk/cocoon-sax/src: main/java/org/apache/cocoon/sax/component/TextSerializer.java test/java/org/apache/cocoon/sax/component/TextSerializerTest.java

[email protected]
Newsgroups gmane.text.xml.cocoon.cvs
Message-ID <[email protected]>
Author: thorsten
Date: Sun Jan  1 23:41:08 2012
New Revision: 1226316

URL: http://svn.apache.org/viewvc?rev=1226316&view=rev
Log:
Adding a generic text serializer extening the xml based

Added:
    cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/TextSerializer.java   (with props)
    cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/TextSerializerTest.java   (with props)

Added: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/TextSerializer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/TextSerializer.java?rev=1226316&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/TextSerializer.java (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/TextSerializer.java Sun Jan  1 23:41:08 2012
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.cocoon.sax.component;
+
+import org.apache.cocoon.sax.util.XMLUtils;
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+
+/**
+ * Converts XML into plain text. It omits all XML tags and writes only character
+ * events to the output. Input document must have at least one element - root
+ * element - which should wrap all the text inside it.
+ * 
+ */
+public class TextSerializer extends XMLSerializer {
+
+    /*
+     * Set to true after first XML element
+     */
+    private boolean hasRootElement;
+
+    private boolean hadNoRootElement;
+
+    private final static String ENCODING = "UTF-8";
+
+    private static final String TXT = "text";
+
+    public TextSerializer() {
+        super();
+        super.setOmitXmlDeclaration(true);
+        this.hasRootElement = false;
+    }
+    @Override
+    public void setDocumentLocator (Locator locator) { 
+        // nothing
+      }
+    @Override
+    public void processingInstruction (String target, String data)
+    throws SAXException{
+        // nothing
+    }
+
+
+    @Override
+    public void startElement(String uri, String loc, String raw, Attributes a)
+            throws SAXException {
+        this.hasRootElement = true;
+    }
+
+    @Override
+    public void endElement(final String uri, final String name, final String raw)
+            throws SAXException {
+        // nothing
+    }
+
+    @Override
+    public void endDocument() throws SAXException {
+        if (this.hadNoRootElement) {
+            super.endElement("", "text", "text");
+        }
+        super.endDocument();
+    }
+
+    /**
+     * @throws SAXException
+     *             if text is encountered before root element.
+     */
+    @Override
+    public void characters(char c[], int start, int len) throws SAXException {
+        if (!this.hasRootElement) {
+            this.hasRootElement = this.hadNoRootElement = true;
+            // getLogger().warn("Encountered text before root element. Creating <text> wrapper element.");
+            super.startElement("", "text", "text", XMLUtils.EMPTY_ATTRIBUTES);
+        }
+        super.characters(c, start, len);
+    }
+
+    @Override
+    public void recycle() {
+        this.hasRootElement = false;
+        super.recycle();
+    }
+
+    public static TextSerializer createPlainSerializer() {
+        TextSerializer serializer = null;
+        serializer = new TextSerializer();
+        serializer.setContentType("text/plain;charset=utf-8");
+        serializer.setEncoding(ENCODING);
+        serializer.setMethod(TXT);
+        return serializer;
+    }
+}

Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/TextSerializer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/TextSerializerTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/TextSerializerTest.java?rev=1226316&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/TextSerializerTest.java (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/TextSerializerTest.java Sun Jan  1 23:41:08 2012
@@ -0,0 +1,27 @@
+package org.apache.cocoon.sax.component;
+
+import java.io.ByteArrayOutputStream;
+
+import org.junit.Test;
+
+import static org.apache.cocoon.pipeline.builder.PipelineBuilder.*;
+import static org.junit.Assert.*;
+
+public class TextSerializerTest {
+    
+    @Test
+    public void functional() throws Exception{
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        
+        newNonCachingPipeline()
+        .setStarter(new XMLGenerator("<x><y>1</y><z>2</z></x>"))
+        .setFinisher(new TextSerializer())
+        .withEmptyConfiguration()
+        .setup(baos)
+        .execute();
+        
+        String data = new String(baos.toByteArray());
+        assertEquals("12",data);
+    }
+
+}

Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/TextSerializerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.