325N/A/*
325N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.txw2.output;
325N/A
325N/Aimport org.xml.sax.Attributes;
325N/Aimport org.xml.sax.ContentHandler;
325N/Aimport org.xml.sax.SAXException;
325N/Aimport org.xml.sax.ext.LexicalHandler;
325N/Aimport org.xml.sax.helpers.XMLFilterImpl;
325N/A
325N/Aimport java.util.Stack;
325N/A
325N/A/**
325N/A * {@link XMLFilterImpl} that does indentation to SAX events.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Apublic class IndentingXMLFilter extends XMLFilterImpl implements LexicalHandler {
325N/A private LexicalHandler lexical;
325N/A
325N/A public IndentingXMLFilter() {
325N/A }
325N/A
325N/A public IndentingXMLFilter(ContentHandler handler) {
325N/A setContentHandler(handler);
325N/A }
325N/A
325N/A public IndentingXMLFilter(ContentHandler handler, LexicalHandler lexical) {
325N/A setContentHandler(handler);
325N/A setLexicalHandler(lexical);
325N/A }
325N/A
325N/A public LexicalHandler getLexicalHandler() {
325N/A return lexical;
325N/A }
325N/A
325N/A public void setLexicalHandler(LexicalHandler lexical) {
325N/A this.lexical = lexical;
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Return the current indent step.
325N/A *
325N/A * <p>Return the current indent step: each start tag will be
325N/A * indented by this number of spaces times the number of
325N/A * ancestors that the element has.</p>
325N/A *
325N/A * @return The number of spaces in each indentation step,
325N/A * or 0 or less for no indentation.
325N/A * @see #setIndentStep(int)
325N/A *
325N/A * @deprecated
325N/A * Only return the length of the indent string.
325N/A */
325N/A public int getIndentStep ()
325N/A {
325N/A return indentStep.length();
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Set the current indent step.
325N/A *
325N/A * @param indentStep The new indent step (0 or less for no
325N/A * indentation).
325N/A * @see #getIndentStep()
325N/A *
325N/A * @deprecated
325N/A * Should use the version that takes string.
325N/A */
325N/A public void setIndentStep (int indentStep)
325N/A {
325N/A StringBuilder s = new StringBuilder();
325N/A for( ; indentStep>0; indentStep-- ) s.append(' ');
325N/A setIndentStep(s.toString());
325N/A }
325N/A
325N/A public void setIndentStep(String s) {
325N/A this.indentStep = s;
325N/A }
325N/A
325N/A
325N/A
325N/A ////////////////////////////////////////////////////////////////////
325N/A // Override methods from XMLWriter.
325N/A ////////////////////////////////////////////////////////////////////
325N/A
325N/A /**
325N/A * Write a start tag.
325N/A *
325N/A * <p>Each tag will begin on a new line, and will be
325N/A * indented by the current indent step times the number
325N/A * of ancestors that the element has.</p>
325N/A *
325N/A * <p>The newline and indentation will be passed on down
325N/A * the filter chain through regular characters events.</p>
325N/A *
325N/A * @param uri The element's Namespace URI.
325N/A * @param localName The element's local name.
325N/A * @param qName The element's qualified (prefixed) name.
325N/A * @param atts The element's attribute list.
325N/A * @exception org.xml.sax.SAXException If there is an error
325N/A * writing the start tag, or if a filter further
325N/A * down the chain raises an exception.
325N/A * @see XMLWriter#startElement(String, String, String,Attributes)
325N/A */
325N/A public void startElement (String uri, String localName,
325N/A String qName, Attributes atts)
325N/A throws SAXException {
325N/A stateStack.push(SEEN_ELEMENT);
325N/A state = SEEN_NOTHING;
325N/A if (depth > 0) {
325N/A writeNewLine();
325N/A }
325N/A doIndent();
325N/A super.startElement(uri, localName, qName, atts);
325N/A depth++;
325N/A }
325N/A
325N/A private void writeNewLine() throws SAXException {
325N/A super.characters(NEWLINE,0,NEWLINE.length);
325N/A }
325N/A
325N/A private static final char[] NEWLINE = {'\n'};
325N/A
325N/A
325N/A /**
325N/A * Write an end tag.
325N/A *
325N/A * <p>If the element has contained other elements, the tag
325N/A * will appear indented on a new line; otherwise, it will
325N/A * appear immediately following whatever came before.</p>
325N/A *
325N/A * <p>The newline and indentation will be passed on down
325N/A * the filter chain through regular characters events.</p>
325N/A *
325N/A * @param uri The element's Namespace URI.
325N/A * @param localName The element's local name.
325N/A * @param qName The element's qualified (prefixed) name.
325N/A * @exception org.xml.sax.SAXException If there is an error
325N/A * writing the end tag, or if a filter further
325N/A * down the chain raises an exception.
325N/A * @see XMLWriter#endElement(String, String, String)
325N/A */
325N/A public void endElement (String uri, String localName, String qName)
325N/A throws SAXException
325N/A {
325N/A depth--;
325N/A if (state == SEEN_ELEMENT) {
325N/A writeNewLine();
325N/A doIndent();
325N/A }
325N/A super.endElement(uri, localName, qName);
325N/A state = stateStack.pop();
325N/A }
325N/A
325N/A
325N/A// /**
325N/A// * Write a empty element tag.
325N/A// *
325N/A// * <p>Each tag will appear on a new line, and will be
325N/A// * indented by the current indent step times the number
325N/A// * of ancestors that the element has.</p>
325N/A// *
325N/A// * <p>The newline and indentation will be passed on down
325N/A// * the filter chain through regular characters events.</p>
325N/A// *
325N/A// * @param uri The element's Namespace URI.
325N/A// * @param localName The element's local name.
325N/A// * @param qName The element's qualified (prefixed) name.
325N/A// * @param atts The element's attribute list.
325N/A// * @exception org.xml.sax.SAXException If there is an error
325N/A// * writing the empty tag, or if a filter further
325N/A// * down the chain raises an exception.
325N/A// * @see XMLWriter#emptyElement(String, String, String, Attributes)
325N/A// */
325N/A// public void emptyElement (String uri, String localName,
325N/A// String qName, Attributes atts)
325N/A// throws SAXException
325N/A// {
325N/A// state = SEEN_ELEMENT;
325N/A// if (depth > 0) {
325N/A// super.characters("\n");
325N/A// }
325N/A// doIndent();
325N/A// super.emptyElement(uri, localName, qName, atts);
325N/A// }
325N/A
325N/A
325N/A /**
325N/A * Write a sequence of characters.
325N/A *
325N/A * @param ch The characters to write.
325N/A * @param start The starting position in the array.
325N/A * @param length The number of characters to use.
325N/A * @exception org.xml.sax.SAXException If there is an error
325N/A * writing the characters, or if a filter further
325N/A * down the chain raises an exception.
325N/A * @see XMLWriter#characters(char[], int, int)
325N/A */
325N/A public void characters (char ch[], int start, int length)
325N/A throws SAXException
325N/A {
325N/A state = SEEN_DATA;
325N/A super.characters(ch, start, length);
325N/A }
325N/A
325N/A public void comment(char ch[], int start, int length) throws SAXException {
325N/A if (depth > 0) {
325N/A writeNewLine();
325N/A }
325N/A doIndent();
325N/A if(lexical!=null)
325N/A lexical.comment(ch,start,length);
325N/A }
325N/A
325N/A public void startDTD(String name, String publicId, String systemId) throws SAXException {
325N/A if(lexical!=null)
325N/A lexical.startDTD(name, publicId, systemId);
325N/A }
325N/A
325N/A public void endDTD() throws SAXException {
325N/A if(lexical!=null)
325N/A lexical.endDTD();
325N/A }
325N/A
325N/A public void startEntity(String name) throws SAXException {
325N/A if(lexical!=null)
325N/A lexical.startEntity(name);
325N/A }
325N/A
325N/A public void endEntity(String name) throws SAXException {
325N/A if(lexical!=null)
325N/A lexical.endEntity(name);
325N/A }
325N/A
325N/A public void startCDATA() throws SAXException {
325N/A if(lexical!=null)
325N/A lexical.startCDATA();
325N/A }
325N/A
325N/A public void endCDATA() throws SAXException {
325N/A if(lexical!=null)
325N/A lexical.endCDATA();
325N/A }
325N/A
325N/A ////////////////////////////////////////////////////////////////////
325N/A // Internal methods.
325N/A ////////////////////////////////////////////////////////////////////
325N/A
325N/A
325N/A /**
325N/A * Print indentation for the current level.
325N/A *
325N/A * @exception org.xml.sax.SAXException If there is an error
325N/A * writing the indentation characters, or if a filter
325N/A * further down the chain raises an exception.
325N/A */
325N/A private void doIndent ()
325N/A throws SAXException
325N/A {
325N/A if (depth > 0) {
325N/A char[] ch = indentStep.toCharArray();
325N/A for( int i=0; i<depth; i++ )
325N/A characters(ch, 0, ch.length);
325N/A }
325N/A }
325N/A
325N/A
325N/A ////////////////////////////////////////////////////////////////////
325N/A // Constants.
325N/A ////////////////////////////////////////////////////////////////////
325N/A
325N/A private final static Object SEEN_NOTHING = new Object();
325N/A private final static Object SEEN_ELEMENT = new Object();
325N/A private final static Object SEEN_DATA = new Object();
325N/A
325N/A
325N/A ////////////////////////////////////////////////////////////////////
325N/A // Internal state.
325N/A ////////////////////////////////////////////////////////////////////
325N/A
325N/A private Object state = SEEN_NOTHING;
325N/A private Stack<Object> stateStack = new Stack<Object>();
325N/A
325N/A private String indentStep = "";
325N/A private int depth = 0;
325N/A}