001: package com.sun.xml.txw2.output;
002:
003: import javax.xml.stream.XMLStreamException;
004: import javax.xml.stream.XMLStreamWriter;
005: import java.util.Stack;
006:
007: /**
008: * @author Kohsuke Kawaguchi
009: */
010: public class IndentingXMLStreamWriter extends DelegatingXMLStreamWriter {
011: private final static Object SEEN_NOTHING = new Object();
012: private final static Object SEEN_ELEMENT = new Object();
013: private final static Object SEEN_DATA = new Object();
014:
015: private Object state = SEEN_NOTHING;
016: private Stack<Object> stateStack = new Stack<Object>();
017:
018: private String indentStep = " ";
019: private int depth = 0;
020:
021: public IndentingXMLStreamWriter(XMLStreamWriter writer) {
022: super (writer);
023: }
024:
025: /**
026: * Return the current indent step.
027: *
028: * <p>Return the current indent step: each start tag will be
029: * indented by this number of spaces times the number of
030: * ancestors that the element has.</p>
031: *
032: * @return The number of spaces in each indentation step,
033: * or 0 or less for no indentation.
034: * @see #setIndentStep(int)
035: *
036: * @deprecated
037: * Only return the length of the indent string.
038: */
039: public int getIndentStep() {
040: return indentStep.length();
041: }
042:
043: /**
044: * Set the current indent step.
045: *
046: * @param indentStep The new indent step (0 or less for no
047: * indentation).
048: * @see #getIndentStep()
049: *
050: * @deprecated
051: * Should use the version that takes string.
052: */
053: public void setIndentStep(int indentStep) {
054: StringBuilder s = new StringBuilder();
055: for (; indentStep > 0; indentStep--)
056: s.append(' ');
057: setIndentStep(s.toString());
058: }
059:
060: public void setIndentStep(String s) {
061: this .indentStep = s;
062: }
063:
064: private void onStartElement() throws XMLStreamException {
065: stateStack.push(SEEN_ELEMENT);
066: state = SEEN_NOTHING;
067: if (depth > 0) {
068: super .writeCharacters("\n");
069: }
070: doIndent();
071: depth++;
072: }
073:
074: private void onEndElement() throws XMLStreamException {
075: depth--;
076: if (state == SEEN_ELEMENT) {
077: super .writeCharacters("\n");
078: doIndent();
079: }
080: state = stateStack.pop();
081: }
082:
083: private void onEmptyElement() throws XMLStreamException {
084: state = SEEN_ELEMENT;
085: if (depth > 0) {
086: super .writeCharacters("\n");
087: }
088: doIndent();
089: }
090:
091: /**
092: * Print indentation for the current level.
093: *
094: * @exception org.xml.sax.SAXException If there is an error
095: * writing the indentation characters, or if a filter
096: * further down the chain raises an exception.
097: */
098: private void doIndent() throws XMLStreamException {
099: if (depth > 0) {
100: for (int i = 0; i < depth; i++)
101: super .writeCharacters(indentStep);
102: }
103: }
104:
105: public void writeStartDocument() throws XMLStreamException {
106: super .writeStartDocument();
107: super .writeCharacters("\n");
108: }
109:
110: public void writeStartDocument(String version)
111: throws XMLStreamException {
112: super .writeStartDocument(version);
113: super .writeCharacters("\n");
114: }
115:
116: public void writeStartDocument(String encoding, String version)
117: throws XMLStreamException {
118: super .writeStartDocument(encoding, version);
119: super .writeCharacters("\n");
120: }
121:
122: public void writeStartElement(String localName)
123: throws XMLStreamException {
124: onStartElement();
125: super .writeStartElement(localName);
126: }
127:
128: public void writeStartElement(String namespaceURI, String localName)
129: throws XMLStreamException {
130: onStartElement();
131: super .writeStartElement(namespaceURI, localName);
132: }
133:
134: public void writeStartElement(String prefix, String localName,
135: String namespaceURI) throws XMLStreamException {
136: onStartElement();
137: super .writeStartElement(prefix, localName, namespaceURI);
138: }
139:
140: public void writeEmptyElement(String namespaceURI, String localName)
141: throws XMLStreamException {
142: onEmptyElement();
143: super .writeEmptyElement(namespaceURI, localName);
144: }
145:
146: public void writeEmptyElement(String prefix, String localName,
147: String namespaceURI) throws XMLStreamException {
148: onEmptyElement();
149: super .writeEmptyElement(prefix, localName, namespaceURI);
150: }
151:
152: public void writeEmptyElement(String localName)
153: throws XMLStreamException {
154: onEmptyElement();
155: super .writeEmptyElement(localName);
156: }
157:
158: public void writeEndElement() throws XMLStreamException {
159: onEndElement();
160: super .writeEndElement();
161: }
162:
163: public void writeCharacters(String text) throws XMLStreamException {
164: state = SEEN_DATA;
165: super .writeCharacters(text);
166: }
167:
168: public void writeCharacters(char[] text, int start, int len)
169: throws XMLStreamException {
170: state = SEEN_DATA;
171: super .writeCharacters(text, start, len);
172: }
173:
174: public void writeCData(String data) throws XMLStreamException {
175: state = SEEN_DATA;
176: super.writeCData(data);
177: }
178: }
|