tcurdt 2004/06/01 10:16:28
Modified: src/blocks/serializers/java/org/apache/cocoon/components/serializers EncodingSerializer.java XMLSerializer.java . status.xml Log: added indentation support Revision Changes Path 1.3 +67 -4 cocoon-2.1/src/blocks/serializers/java/org/apache/cocoon/components/serializers/EncodingSerializer.java Index: EncodingSerializer.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/serializers/java/org/apache/cocoon/components/serializers/EncodingSerializer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- EncodingSerializer.java 30 Apr 2004 22:52:43 -0000 1.2 +++ EncodingSerializer.java 1 Jun 2004 17:16:28 -0000 1.3 @@ -19,6 +19,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; +import java.util.Arrays; import org.apache.avalon.excalibur.pool.Recyclable; import org.apache.avalon.framework.configuration.Configurable; @@ -39,8 +40,7 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>, February 2003 * @version CVS $Id$ */ -public abstract class EncodingSerializer -implements Serializer, Locator, Recyclable, Configurable { +public abstract class EncodingSerializer implements Serializer, Locator, Recyclable, Configurable { /** The line separator string */ private static final char S_EOL[] = @@ -80,6 +80,12 @@ /** Flag indicating if the document is being processed. */ private boolean processing = false; + /** Current nesting level */ + private int level = 0; + + /** Whitespace buffer for indentation */ + private char[] indentBuffer = null; + /* ====================================================================== */ /** The <code>Charset</code> associated with the character encoding. */ @@ -88,6 +94,8 @@ /** The <code>Namespace</code> associated with this instance. */ protected Namespaces namespaces = new Namespaces(); + /** Per level indent spaces */ + protected int indentPerLevel = 0; /* ====================================================================== */ /** @@ -144,10 +152,23 @@ throw new ConfigurationException("Encoding not supported: " + encoding, exception); } + + indentPerLevel = conf.getChild("indent").getValueAsInteger(0); + if (indentPerLevel > 0) { + assureIndentBuffer(indentPerLevel * 6); + } } /* ====================================================================== */ - + + private char[] assureIndentBuffer( int size ) { + if (indentBuffer == null || indentBuffer.length < size) { + indentBuffer = new char[size]; + Arrays.fill(indentBuffer,' '); + } + return indentBuffer; + } + /** * Encode and write a <code>String</code> */ @@ -355,6 +376,19 @@ } } + /** + * Write out character to indent the output according + * to the level of nesting + * @param indent + * @throws SAXException + */ + protected void writeIndent(int indent) throws SAXException { + this.charactersImpl("\n".toCharArray(),0,1); + if (indent > 0) { + this.charactersImpl(assureIndentBuffer(indent),0,indent); + } + } + /* ====================================================================== */ /** @@ -363,6 +397,7 @@ public void startDocument() throws SAXException { this.processing = true; + this.level = 0; } /** @@ -396,6 +431,11 @@ public void startElement(String nsuri, String local, String qual, Attributes attributes) throws SAXException { + if (indentPerLevel > 0) { + this.writeIndent(indentPerLevel*level); + level++; + } + String name = this.namespaces.qualify(nsuri, local, qual); if (this.prolog) { @@ -419,11 +459,24 @@ this.startElementImpl(nsuri, local, name, ns, at); } + public void characters (char ch[], int start, int length) + throws SAXException { + if (indentPerLevel > 0) { + this.writeIndent(indentPerLevel*level + 1); + } + this.charactersImpl(ch, start, length); + } + /** * Receive notification of the end of an element. */ public void endElement(String nsuri, String local, String qual) throws SAXException { + if (indentPerLevel > 0) { + level--; + this.writeIndent(indentPerLevel*level); + } + String name = this.namespaces.qualify(nsuri, local, qual); this.endElementImpl(nsuri, local, name); } @@ -451,6 +504,16 @@ */ public abstract void startElementImpl(String uri, String local, String qual, String namespaces[][], String attributes[][]) + throws SAXException; + + /** + * Receive character notifications + * @param ch + * @param start + * @param length + * @throws SAXException + */ + public abstract void charactersImpl (char ch[], int start, int length) throws SAXException; /** 1.6 +3 -3 cocoon-2.1/src/blocks/serializers/java/org/apache/cocoon/components/serializers/XMLSerializer.java Index: XMLSerializer.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/serializers/java/org/apache/cocoon/components/serializers/XMLSerializer.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- XMLSerializer.java 30 Apr 2004 22:57:22 -0000 1.5 +++ XMLSerializer.java 1 Jun 2004 17:16:28 -0000 1.6 @@ -341,7 +341,7 @@ /** * Receive notification of character data. */ - public void characters(char data[], int start, int length) + public void charactersImpl(char data[], int start, int length) throws SAXException { if (this.processing_prolog) return; this.closeElement(false); @@ -353,7 +353,7 @@ */ public void ignorableWhitespace(char data[], int start, int length) throws SAXException { - this.characters(data, start, length); + this.charactersImpl(data, start, length); } /** 1.344 +4 -1 cocoon-2.1/status.xml Index: status.xml =================================================================== RCS file: /home/cvs/cocoon-2.1/status.xml,v retrieving revision 1.343 retrieving revision 1.344 diff -u -r1.343 -r1.344 --- status.xml 27 May 2004 13:30:59 -0000 1.343 +++ status.xml 1 Jun 2004 17:16:28 -0000 1.344 @@ -205,6 +205,9 @@ <changes> <release version="@version@" date="@date@"> + <action dev="TC" type="add"> + Added support for indentation to the new XML Serializer. + </action> <action dev="CZ" type="update"> Remove deprecated methods from SimpleCharStream, Notifier, AbstractComplementaryConfigurableAction, StringUtils. Remove deprecated parser constant from Constants and remove