Return-Path: Delivered-To: apmail-cocoon-cvs-archive@www.apache.org Received: (qmail 75627 invoked from network); 7 Aug 2007 18:17:02 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 7 Aug 2007 18:17:02 -0000 Received: (qmail 72992 invoked by uid 500); 7 Aug 2007 18:17:01 -0000 Delivered-To: apmail-cocoon-cvs-archive@cocoon.apache.org Received: (qmail 72865 invoked by uid 500); 7 Aug 2007 18:17:01 -0000 Mailing-List: contact cvs-help@cocoon.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@cocoon.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list cvs@cocoon.apache.org Received: (qmail 72845 invoked by uid 99); 7 Aug 2007 18:17:01 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Aug 2007 11:17:01 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Aug 2007 18:16:54 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E9ACB1A981D; Tue, 7 Aug 2007 11:16:37 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r563600 [2/2] - in /cocoon/trunk/blocks: cocoon-portal/cocoon-portal-portlet-impl/src/main/java/org/apache/cocoon/portal/pluto/adapter/ cocoon-serializers/cocoon-serializers-charsets/src/main/java/org/apache/cocoon/components/serializers/ut... Date: Tue, 07 Aug 2007 18:16:37 -0000 To: cvs@cocoon.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070807181637.E9ACB1A981D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: cocoon/trunk/blocks/cocoon-serializers/cocoon-serializers-impl/src/main/java/org/apache/cocoon/components/serializers/XMLSerializer.java URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-serializers/cocoon-serializers-impl/src/main/java/org/apache/cocoon/components/serializers/XMLSerializer.java?view=diff&rev=563600&r1=563599&r2=563600 ============================================================================== --- cocoon/trunk/blocks/cocoon-serializers/cocoon-serializers-impl/src/main/java/org/apache/cocoon/components/serializers/XMLSerializer.java (original) +++ cocoon/trunk/blocks/cocoon-serializers/cocoon-serializers-impl/src/main/java/org/apache/cocoon/components/serializers/XMLSerializer.java Tue Aug 7 11:16:36 2007 @@ -5,9 +5,9 @@ * 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. @@ -16,434 +16,70 @@ */ package org.apache.cocoon.components.serializers; -import java.io.CharArrayWriter; - -import org.apache.cocoon.components.serializers.encoding.XMLEncoder; -import org.apache.cocoon.components.serializers.util.DocType; -import org.apache.cocoon.components.serializers.util.Namespaces; -import org.apache.commons.lang.SystemUtils; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.avalon.excalibur.pool.Recyclable; +import org.apache.avalon.framework.configuration.Configurable; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.environment.SourceResolver; +import org.apache.cocoon.environment.http.HttpEnvironment; +import org.apache.cocoon.serialization.Serializer; +import org.apache.cocoon.sitemap.SitemapModelComponent; import org.xml.sax.SAXException; + /** - *

A fancy XML serializer not relying on the JAXP API.

- * + *

A fancy XML serializer not relying on the JAXP API.

+ * *

For configuration options of this serializer, please look at the * {@link EncodingSerializer}.

* * @version CVS $Id$ */ -public class XMLSerializer extends EncodingSerializer { - - private static final XMLEncoder XML_ENCODER = new XMLEncoder(); - - private static final char S_EOL[] = SystemUtils.LINE_SEPARATOR.toCharArray(); - - private static final char S_DOCUMENT_1[] = "".toCharArray(); - - private static final char S_ELEMENT_1[] = "=\"".toCharArray(); - private static final char S_ELEMENT_2[] = "".toCharArray(); - private static final char S_ELEMENT_4[] = " xmlns".toCharArray(); - - private static final char S_CDATA_1[] = "<[CDATA[".toCharArray(); - private static final char S_CDATA_2[] = "]]>".toCharArray(); - - private static final char S_COMMENT_1[] = "".toCharArray(); - - private static final char S_PROCINSTR_1[] = "".toCharArray(); - - private static final char C_LT = '<'; - private static final char C_GT = '>'; - private static final char C_SPACE = ' '; - private static final char C_QUOTE = '"'; - private static final char C_NSSEP = ':'; - - private static final boolean DEBUG = false; - - /* ====================================================================== */ - - /** Whether an element is left open like "<name ". */ - private boolean hanging_element = false; - - /** True if we are processing the prolog. */ - private boolean processing_prolog = true; - - /** True if we are processing the DTD. */ - private boolean processing_dtd = false; - - /** A Writer for prolog elements. */ - private PrologWriter prolog = new PrologWriter(); - - /* ====================================================================== */ - - /** The DocType instance representing the document. */ - protected DocType doctype = null; - - /* ====================================================================== */ - - /** - * Create a new instance of this XMLSerializer - */ - public XMLSerializer() { - super(XML_ENCODER); - } - - /** - * Create a new instance of this XMLSerializer - */ - protected XMLSerializer(XMLEncoder encoder) { - super(encoder); - } - - /** - * Reset this XMLSerializer. - */ - public void recycle() { - super.recycle(); - this.doctype = null; - this.hanging_element = false; - this.processing_prolog = true; - this.processing_dtd = false; - if (this.prolog != null) this.prolog.reset(); - } +public class XMLSerializer + extends org.apache.cocoon.components.serializers.util.XMLSerializer + implements Serializer, SitemapModelComponent, Recyclable, Configurable { /** - * Return the MIME Content-Type produced by this serializer. + * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters) */ - public String getMimeType() { - if (this.charset == null) return("text/xml"); - return("text/xml; charset=" + this.charset.getName()); + public void setup(SourceResolver resolver, + Map objectModel, + String src, + Parameters par) + throws ProcessingException, SAXException, IOException { + this.setup((HttpServletRequest) objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT)); } - /* ====================================================================== */ - - /** - * Receive notification of the beginning of a document. - */ - public void startDocument() - throws SAXException { - super.startDocument(); - this.head(); - } /** - * Receive notification of the end of a document. + * Test if the component wants to set the content length. */ - public void endDocument() - throws SAXException { - this.writeln(); - super.endDocument(); + public boolean shouldSetContentLength() { + return false; } /** - * Write the XML document header. - *

- * This method will write out the <?xml version="1.0" - * ...> header. - *

+ * Set the configurations for this serializer. */ - protected void head() - throws SAXException { - this.write(S_DOCUMENT_1); // [] - this.writeln(); - } - - /** - * Report the start of DTD declarations, if any. - */ - public void startDTD(String name, String public_id, String system_id) - throws SAXException { - this.processing_dtd = true; - this.doctype = new DocType(name, public_id, system_id); - } - /** - * Report the start of DTD declarations, if any. - */ - public void endDTD() - throws SAXException { - this.processing_dtd = false; + this.setIndentPerLevel(conf.getChild("indent").getValueAsInteger(0)); } - /** - * Receive notification of the beginning of the document body. - * - * @param uri The namespace URI of the root element. - * @param local The local name of the root element. - * @param qual The fully-qualified name of the root element. - */ - public void body(String uri, String local, String qual) - throws SAXException { - this.processing_prolog = false; - this.writeln(); - - /* We have a document type. */ - if (this.doctype != null) { - - String root_name = this.doctype.getName(); - /* Check the DTD and the root element */ - if (!root_name.equals(qual)) { - throw new SAXException("Root element name \"" + root_name - + "\" declared by document type declaration differs " - + "from actual root element name \"" + qual + "\""); - } - /* Output the declaration. */ - this.write(this.doctype.toString()); - } - - /* Output all PIs and comments we cached in the prolog */ - this.prolog.writeTo(this); - this.writeln(); - } - - /** - * Receive notification of the beginning of an element. - * - * @param uri The namespace URI of the root element. - * @param local The local name of the root element. - * @param qual The fully-qualified name of the root element. - * @param namespaces An array of String objects containing - * the namespaces to be declared by this element. - * @param attributes An array of String objects containing - * all attributes of this element. - */ - public void startElementImpl(String uri, String local, String qual, - String namespaces[][], String attributes[][]) - throws SAXException { - this.closeElement(false); - this.write(C_LT); // [<] - if (DEBUG) { - this.write('['); - this.write(uri); - this.write(']'); - } - this.write(qual); - - for (int x = 0; x < namespaces.length; x++) { - this.write(S_ELEMENT_4); // [ xmlns] - if (namespaces[x][Namespaces.NAMESPACE_PREFIX].length() > 0) { - this.write(C_NSSEP); // [:] - this.write(namespaces[x][Namespaces.NAMESPACE_PREFIX]); - } - this.write(S_ELEMENT_1); // [="] - this.encode(namespaces[x][Namespaces.NAMESPACE_URI]); - this.write(C_QUOTE); // ["] - } - - for (int x = 0; x < attributes.length; x++) { - this.write(C_SPACE); // [ ] - if (DEBUG) { - this.write('['); - this.write(attributes[x][ATTRIBUTE_NSURI]); - this.write(']'); - } - this.write(attributes[x][ATTRIBUTE_QNAME]); - this.write(S_ELEMENT_1); // [="] - this.encode(attributes[x][ATTRIBUTE_VALUE]); - this.write(C_QUOTE); // ["] - } - - this.hanging_element = true; - } - - /** - * Receive notification of the end of an element. - * - * @param uri The namespace URI of the root element. - * @param local The local name of the root element. - * @param qual The fully-qualified name of the root element. - */ - public void endElementImpl(String uri, String local, String qual) - throws SAXException { - if (closeElement(true)) return; - this.write(S_ELEMENT_2); // [] - } - - /** - * Write the end part of a start element (if necessary). - * - * @param end_element Whether this method was called because an element - * is being closed or not. - * @return true if this call successfully closed the element (and - * no further </element> is required. - */ - protected boolean closeElement(boolean end_element) - throws SAXException { - if (!hanging_element) return false; - if (end_element) this.write(S_ELEMENT_3); // [ />] - else this.write(C_GT); // [>] - this.hanging_element = false; - return true; - } - - /** - * Report the start of a CDATA section. - */ - public void startCDATA() - throws SAXException { - if (this.processing_prolog) return; - this.closeElement(false); - this.write(S_CDATA_1); // [<[CDATA[] - } - - /** - * Report the end of a CDATA section. - */ - public void endCDATA() - throws SAXException { - if (this.processing_prolog) return; - this.closeElement(false); - this.write(S_CDATA_2); // []]>] - } - - /** - * Receive notification of character data. - */ - public void charactersImpl(char data[], int start, int length) - throws SAXException { - if (this.processing_prolog) return; - this.closeElement(false); - this.encode(data, start, length); - } - - /** - * Receive notification of ignorable whitespace in element content. - */ - public void ignorableWhitespace(char data[], int start, int length) - throws SAXException { - this.charactersImpl(data, start, length); - } - - /** - * Report an XML comment anywhere in the document. - */ - public void comment(char data[], int start, int length) - throws SAXException { - if (this.processing_dtd) return; - - if (this.processing_prolog) { - this.prolog.write(S_COMMENT_1); // [] - this.prolog.write(S_EOL); - return; - } - - this.closeElement(false); - this.write(S_COMMENT_1); // [] - } - - /** - * Receive notification of a processing instruction. - */ - public void processingInstruction(String target, String data) - throws SAXException { - if (this.processing_dtd) return; - - if (this.processing_prolog) { - this.prolog.write(S_PROCINSTR_1); // [] - this.prolog.write(S_EOL); - return; - } - - this.closeElement(false); - - this.write(S_PROCINSTR_1); // [] - } - - /** - * Report the beginning of some internal and external XML entities. - */ - public void startEntity(String name) - throws SAXException { - } - - /** - * Report the end of an entity. - */ - public void endEntity(String name) - throws SAXException { - } - - /** - * Receive notification of a skipped entity. - */ - public void skippedEntity(String name) - throws SAXException { - } - - /* ====================================================================== */ - - /** - * The PrologWriter is a simple extension to a - * CharArrayWriter. - */ - private static final class PrologWriter extends CharArrayWriter { - - /** Create a new PrologWriter instance. */ - private PrologWriter() { - super(); - } - - /** - * Write an array of characters. - *

- * The CharArrayWriter implementation of this method - * throws an unwanted IOException. - *

- */ - public void write(char c[]) { - this.write(c, 0, c.length); - } - - /** - * Write a String. - *

- * The CharArrayWriter implementation of this method - * throws an unwanted IOException. - *

- */ - public void write(String str) { - this.write(str, 0, str.length()); - } - - /** - * Write our contents to a BaseSerializer without - * copying the buffer. - */ - public void writeTo(XMLSerializer serializer) - throws SAXException { - serializer.write(this.buf, 0, this.count); - } - } }