author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.xml.internal.txw2.output; |
|
27 |
||
28 |
import javax.xml.stream.XMLStreamException; |
|
29 |
import javax.xml.stream.XMLStreamWriter; |
|
30 |
import java.util.Stack; |
|
31 |
||
32 |
/** |
|
33 |
* @author Kohsuke Kawaguchi |
|
34 |
*/ |
|
35 |
public class IndentingXMLStreamWriter extends DelegatingXMLStreamWriter { |
|
36 |
private final static Object SEEN_NOTHING = new Object(); |
|
37 |
private final static Object SEEN_ELEMENT = new Object(); |
|
38 |
private final static Object SEEN_DATA = new Object(); |
|
39 |
||
40 |
private Object state = SEEN_NOTHING; |
|
41 |
private Stack<Object> stateStack = new Stack<Object>(); |
|
42 |
||
43 |
private String indentStep = " "; |
|
44 |
private int depth = 0; |
|
45 |
||
46 |
public IndentingXMLStreamWriter(XMLStreamWriter writer) { |
|
47 |
super(writer); |
|
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* Return the current indent step. |
|
52 |
* |
|
53 |
* <p>Return the current indent step: each start tag will be |
|
54 |
* indented by this number of spaces times the number of |
|
55 |
* ancestors that the element has.</p> |
|
56 |
* |
|
57 |
* @return The number of spaces in each indentation step, |
|
58 |
* or 0 or less for no indentation. |
|
59 |
* @see #setIndentStep(int) |
|
60 |
* |
|
61 |
* @deprecated |
|
62 |
* Only return the length of the indent string. |
|
63 |
*/ |
|
64 |
public int getIndentStep() { |
|
65 |
return indentStep.length(); |
|
66 |
} |
|
67 |
||
68 |
||
69 |
/** |
|
70 |
* Set the current indent step. |
|
71 |
* |
|
72 |
* @param indentStep The new indent step (0 or less for no |
|
73 |
* indentation). |
|
74 |
* @see #getIndentStep() |
|
75 |
* |
|
76 |
* @deprecated |
|
77 |
* Should use the version that takes string. |
|
78 |
*/ |
|
79 |
public void setIndentStep(int indentStep) { |
|
80 |
StringBuilder s = new StringBuilder(); |
|
81 |
for (; indentStep > 0; indentStep--) s.append(' '); |
|
82 |
setIndentStep(s.toString()); |
|
83 |
} |
|
84 |
||
85 |
public void setIndentStep(String s) { |
|
86 |
this.indentStep = s; |
|
87 |
} |
|
88 |
||
89 |
private void onStartElement() throws XMLStreamException { |
|
90 |
stateStack.push(SEEN_ELEMENT); |
|
91 |
state = SEEN_NOTHING; |
|
92 |
if (depth > 0) { |
|
93 |
super.writeCharacters("\n"); |
|
94 |
} |
|
95 |
doIndent(); |
|
96 |
depth++; |
|
97 |
} |
|
98 |
||
99 |
private void onEndElement() throws XMLStreamException { |
|
100 |
depth--; |
|
101 |
if (state == SEEN_ELEMENT) { |
|
102 |
super.writeCharacters("\n"); |
|
103 |
doIndent(); |
|
104 |
} |
|
105 |
state = stateStack.pop(); |
|
106 |
} |
|
107 |
||
108 |
private void onEmptyElement() throws XMLStreamException { |
|
109 |
state = SEEN_ELEMENT; |
|
110 |
if (depth > 0) { |
|
111 |
super.writeCharacters("\n"); |
|
112 |
} |
|
113 |
doIndent(); |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Print indentation for the current level. |
|
118 |
* |
|
119 |
* @exception org.xml.sax.SAXException If there is an error |
|
120 |
* writing the indentation characters, or if a filter |
|
121 |
* further down the chain raises an exception. |
|
122 |
*/ |
|
123 |
private void doIndent() throws XMLStreamException { |
|
124 |
if (depth > 0) { |
|
125 |
for (int i = 0; i < depth; i++) |
|
126 |
super.writeCharacters(indentStep); |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
||
131 |
public void writeStartDocument() throws XMLStreamException { |
|
132 |
super.writeStartDocument(); |
|
133 |
super.writeCharacters("\n"); |
|
134 |
} |
|
135 |
||
136 |
public void writeStartDocument(String version) throws XMLStreamException { |
|
137 |
super.writeStartDocument(version); |
|
138 |
super.writeCharacters("\n"); |
|
139 |
} |
|
140 |
||
141 |
public void writeStartDocument(String encoding, String version) throws XMLStreamException { |
|
142 |
super.writeStartDocument(encoding, version); |
|
143 |
super.writeCharacters("\n"); |
|
144 |
} |
|
145 |
||
146 |
public void writeStartElement(String localName) throws XMLStreamException { |
|
147 |
onStartElement(); |
|
148 |
super.writeStartElement(localName); |
|
149 |
} |
|
150 |
||
151 |
public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException { |
|
152 |
onStartElement(); |
|
153 |
super.writeStartElement(namespaceURI, localName); |
|
154 |
} |
|
155 |
||
156 |
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { |
|
157 |
onStartElement(); |
|
158 |
super.writeStartElement(prefix, localName, namespaceURI); |
|
159 |
} |
|
160 |
||
161 |
public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException { |
|
162 |
onEmptyElement(); |
|
163 |
super.writeEmptyElement(namespaceURI, localName); |
|
164 |
} |
|
165 |
||
166 |
public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException { |
|
167 |
onEmptyElement(); |
|
168 |
super.writeEmptyElement(prefix, localName, namespaceURI); |
|
169 |
} |
|
170 |
||
171 |
public void writeEmptyElement(String localName) throws XMLStreamException { |
|
172 |
onEmptyElement(); |
|
173 |
super.writeEmptyElement(localName); |
|
174 |
} |
|
175 |
||
176 |
public void writeEndElement() throws XMLStreamException { |
|
177 |
onEndElement(); |
|
178 |
super.writeEndElement(); |
|
179 |
} |
|
180 |
||
181 |
public void writeCharacters(String text) throws XMLStreamException { |
|
182 |
state = SEEN_DATA; |
|
183 |
super.writeCharacters(text); |
|
184 |
} |
|
185 |
||
186 |
public void writeCharacters(char[] text, int start, int len) throws XMLStreamException { |
|
187 |
state = SEEN_DATA; |
|
188 |
super.writeCharacters(text, start, len); |
|
189 |
} |
|
190 |
||
191 |
public void writeCData(String data) throws XMLStreamException { |
|
192 |
state = SEEN_DATA; |
|
193 |
super.writeCData(data); |
|
194 |
} |
|
195 |
} |