[ https://issues.apache.org/jira/browse/COCOON3-77?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13144969#comment-13144969 ]
Andre Juffer commented on COCOON3-77: ------------------------------------- # This patch file was generated by NetBeans IDE # It uses platform neutral UTF-8 encoding and \n newlines. --- Locally New +++ Locally New @@ -0,0 +1,235 @@ +/* + * 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.components.serializers.util; + +import org.apache.cocoon.components.serializers.encoding.TextEncoder; +import org.apache.cocoon.components.serializers.encoding.XMLEncoder; + +import org.xml.sax.SAXException; +import org.xml.sax.Attributes; + +/** + * 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. + * + * @author André Juffer, Triacle Biocomputing + */ +public class TextSerializer extends XMLSerializer { + + private static final XMLEncoder XML_ENCODER = new TextEncoder(); + + /* + * Set to true after first XML element + */ + private boolean hasRootElement; + + private final static String CHARSET = "UTF-8"; + + public TextSerializer() + throws Exception + { + super(XML_ENCODER); + super.setEncoding(CHARSET); + this.hasRootElement = false; + } + + @Override + public void head() + { + // Do nothing. + } + + @Override + public void startElement(String uri, + String loc, + String raw, + Attributes a) + throws SAXException + { + this.hasRootElement = true; + } + + /** + * @throws SAXException if text is encountered before root element. + */ + @Override + public void characters(char c[], int start, int len) + throws SAXException + { + if ( !this.hasRootElement ) + throw new SAXException("Encountered text before root element"); + super.characters(c, start, len); + } + + @Override + public void recycle() + { + this.hasRootElement = false; + super.recycle(); + } + + /** + * @return text/plain; charset= followed by character set, by default UTF-8 + */ + @Override + public String getMimeType() + { + return("text/plain; charset=" + this.charset.getName()); + } + + @Override + public void startElementImpl(String uri, + String local, + String qual, + String namespaces[][], + String attributes[][]) + throws SAXException + { + // Do nothing. + } + + @Override + public void endElementImpl(String uri, + String local, + String qual) + throws SAXException + { + // Do nothing. + } + + + @Override + public void charactersImpl(char[] chars, + int start, + int len) + throws SAXException + { + super.encode(chars, start, len); + } + + + @Override + public void body(String uri, + String local, + String qual) + { + // Do nothing. + } + + @Override + public void skippedEntity(String name) + throws SAXException + { + // Do nothing. + } + + @Override + public void processingInstruction(String target, String data) + throws SAXException + { + // Do nothing. + } + + @Override + public void ignorableWhitespace(char[] ch, + int start, + int length) + throws SAXException + { + // Do nothing. + } + + @Override + public void comment(char[] ch, + int start, + int length) + throws SAXException + { + // Do nothing. + } + + @Override + public void startCDATA() + throws SAXException + { + // Do nothing. + } + + @Override + public void endCDATA() + throws SAXException + { + // Do nothing. + } + + @Override + public void startEntity(String name) + throws SAXException + { + // Do nothing. + } + + @Override + public void endEntity(String name) + throws SAXException + { + // Do nothing. + } + + @Override + public void startDTD(String name, + String publicId, + String systemId) + throws SAXException + { + // Do nothing. + } + + @Override + public void endDTD() + throws SAXException + { + // Do nothing. + } + + @Override + public String toString() + { + String newline = System.getProperty("line.separator"); + + StringBuilder s = new StringBuilder(this.getClass().getName() + " : [" + newline); + if ( this.charset != null ) + s.append("charset - ").append(this.charset.toString()).append(newline); + if ( this.doctype != null ) + s.append("doctype - ").append(this.doctype.toString()).append(newline); + s.append("hasRootElement - ").append(this.hasRootElement).append(newline); + s.append("indentPerLevel - ").append(this.indentPerLevel).append(newline); + if ( this.namespaces != null ) + s.append("namespaces - ").append(this.namespaces.toString()).append(newline); + if ( this.request != null ) + s.append("request - ").append(this.request.toString()).append(newline); + s.append("mime-type - ").append(this.getMimeType()).append(newline); + s.append("]").append(newline); + + return s.toString(); + } +} > Text and JSON serializers > ------------------------- > > Key: COCOON3-77 > URL: https://issues.apache.org/jira/browse/COCOON3-77 > Project: Cocoon 3 > Issue Type: Improvement > Components: cocoon-optional > Affects Versions: 3.0.0-alpha-3 > Reporter: Andre Juffer > Priority: Minor > Attachments: EncodingJsonSerializer.java, > EncodingTextSerializer.java, JsonSerializer.java, TextEncoder.java, > TextSerializer.java, pom.xml, tribc-cocoon-3.xml > > > Serveral classes have been created for serializing text and JSON in the > sitemap. The JsonSerializer also checks whether the JSON text actually is > valid. The organization of the classes follows the encoding serializers (such > as the EncodingHTMLSerializer). -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa For more information on JIRA, see: http://www.atlassian.com/software/jira