Sun, 18 Jun 2017 23:18:45 +0100
8172297: In java 8, the marshalling with JAX-WS does not escape carriage return
Reviewed-by: lancea
1 /*
2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
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 */
26 package com.sun.xml.internal.txw2.output;
28 import org.xml.sax.Attributes;
29 import org.xml.sax.ContentHandler;
30 import org.xml.sax.SAXException;
31 import org.xml.sax.ext.LexicalHandler;
32 import org.xml.sax.helpers.XMLFilterImpl;
34 import java.util.Stack;
36 /**
37 * {@link XMLFilterImpl} that does indentation to SAX events.
38 *
39 * @author Kohsuke Kawaguchi
40 */
41 public class IndentingXMLFilter extends XMLFilterImpl implements LexicalHandler {
42 private LexicalHandler lexical;
44 public IndentingXMLFilter() {
45 }
47 public IndentingXMLFilter(ContentHandler handler) {
48 setContentHandler(handler);
49 }
51 public IndentingXMLFilter(ContentHandler handler, LexicalHandler lexical) {
52 setContentHandler(handler);
53 setLexicalHandler(lexical);
54 }
56 public LexicalHandler getLexicalHandler() {
57 return lexical;
58 }
60 public void setLexicalHandler(LexicalHandler lexical) {
61 this.lexical = lexical;
62 }
65 /**
66 * Return the current indent step.
67 *
68 * <p>Return the current indent step: each start tag will be
69 * indented by this number of spaces times the number of
70 * ancestors that the element has.</p>
71 *
72 * @return The number of spaces in each indentation step,
73 * or 0 or less for no indentation.
74 * @see #setIndentStep(int)
75 *
76 * @deprecated
77 * Only return the length of the indent string.
78 */
79 public int getIndentStep ()
80 {
81 return indentStep.length();
82 }
85 /**
86 * Set the current indent step.
87 *
88 * @param indentStep The new indent step (0 or less for no
89 * indentation).
90 * @see #getIndentStep()
91 *
92 * @deprecated
93 * Should use the version that takes string.
94 */
95 public void setIndentStep (int indentStep)
96 {
97 StringBuilder s = new StringBuilder();
98 for( ; indentStep>0; indentStep-- ) s.append(' ');
99 setIndentStep(s.toString());
100 }
102 public void setIndentStep(String s) {
103 this.indentStep = s;
104 }
108 ////////////////////////////////////////////////////////////////////
109 // Override methods from XMLWriter.
110 ////////////////////////////////////////////////////////////////////
112 /**
113 * Write a start tag.
114 *
115 * <p>Each tag will begin on a new line, and will be
116 * indented by the current indent step times the number
117 * of ancestors that the element has.</p>
118 *
119 * <p>The newline and indentation will be passed on down
120 * the filter chain through regular characters events.</p>
121 *
122 * @param uri The element's Namespace URI.
123 * @param localName The element's local name.
124 * @param qName The element's qualified (prefixed) name.
125 * @param atts The element's attribute list.
126 * @exception org.xml.sax.SAXException If there is an error
127 * writing the start tag, or if a filter further
128 * down the chain raises an exception.
129 * @see XMLWriter#startElement(String, String, String,Attributes)
130 */
131 public void startElement (String uri, String localName,
132 String qName, Attributes atts)
133 throws SAXException {
134 stateStack.push(SEEN_ELEMENT);
135 state = SEEN_NOTHING;
136 if (depth > 0) {
137 writeNewLine();
138 }
139 doIndent();
140 super.startElement(uri, localName, qName, atts);
141 depth++;
142 }
144 private void writeNewLine() throws SAXException {
145 super.characters(NEWLINE,0,NEWLINE.length);
146 }
148 private static final char[] NEWLINE = {'\n'};
151 /**
152 * Write an end tag.
153 *
154 * <p>If the element has contained other elements, the tag
155 * will appear indented on a new line; otherwise, it will
156 * appear immediately following whatever came before.</p>
157 *
158 * <p>The newline and indentation will be passed on down
159 * the filter chain through regular characters events.</p>
160 *
161 * @param uri The element's Namespace URI.
162 * @param localName The element's local name.
163 * @param qName The element's qualified (prefixed) name.
164 * @exception org.xml.sax.SAXException If there is an error
165 * writing the end tag, or if a filter further
166 * down the chain raises an exception.
167 * @see XMLWriter#endElement(String, String, String)
168 */
169 public void endElement (String uri, String localName, String qName)
170 throws SAXException
171 {
172 depth--;
173 if (state == SEEN_ELEMENT) {
174 writeNewLine();
175 doIndent();
176 }
177 super.endElement(uri, localName, qName);
178 state = stateStack.pop();
179 }
182 // /**
183 // * Write a empty element tag.
184 // *
185 // * <p>Each tag will appear on a new line, and will be
186 // * indented by the current indent step times the number
187 // * of ancestors that the element has.</p>
188 // *
189 // * <p>The newline and indentation will be passed on down
190 // * the filter chain through regular characters events.</p>
191 // *
192 // * @param uri The element's Namespace URI.
193 // * @param localName The element's local name.
194 // * @param qName The element's qualified (prefixed) name.
195 // * @param atts The element's attribute list.
196 // * @exception org.xml.sax.SAXException If there is an error
197 // * writing the empty tag, or if a filter further
198 // * down the chain raises an exception.
199 // * @see XMLWriter#emptyElement(String, String, String, Attributes)
200 // */
201 // public void emptyElement (String uri, String localName,
202 // String qName, Attributes atts)
203 // throws SAXException
204 // {
205 // state = SEEN_ELEMENT;
206 // if (depth > 0) {
207 // super.characters("\n");
208 // }
209 // doIndent();
210 // super.emptyElement(uri, localName, qName, atts);
211 // }
214 /**
215 * Write a sequence of characters.
216 *
217 * @param ch The characters to write.
218 * @param start The starting position in the array.
219 * @param length The number of characters to use.
220 * @exception org.xml.sax.SAXException If there is an error
221 * writing the characters, or if a filter further
222 * down the chain raises an exception.
223 * @see XMLWriter#characters(char[], int, int)
224 */
225 public void characters (char ch[], int start, int length)
226 throws SAXException
227 {
228 state = SEEN_DATA;
229 super.characters(ch, start, length);
230 }
232 public void comment(char ch[], int start, int length) throws SAXException {
233 if (depth > 0) {
234 writeNewLine();
235 }
236 doIndent();
237 if(lexical!=null)
238 lexical.comment(ch,start,length);
239 }
241 public void startDTD(String name, String publicId, String systemId) throws SAXException {
242 if(lexical!=null)
243 lexical.startDTD(name, publicId, systemId);
244 }
246 public void endDTD() throws SAXException {
247 if(lexical!=null)
248 lexical.endDTD();
249 }
251 public void startEntity(String name) throws SAXException {
252 if(lexical!=null)
253 lexical.startEntity(name);
254 }
256 public void endEntity(String name) throws SAXException {
257 if(lexical!=null)
258 lexical.endEntity(name);
259 }
261 public void startCDATA() throws SAXException {
262 if(lexical!=null)
263 lexical.startCDATA();
264 }
266 public void endCDATA() throws SAXException {
267 if(lexical!=null)
268 lexical.endCDATA();
269 }
271 ////////////////////////////////////////////////////////////////////
272 // Internal methods.
273 ////////////////////////////////////////////////////////////////////
276 /**
277 * Print indentation for the current level.
278 *
279 * @exception org.xml.sax.SAXException If there is an error
280 * writing the indentation characters, or if a filter
281 * further down the chain raises an exception.
282 */
283 private void doIndent ()
284 throws SAXException
285 {
286 if (depth > 0) {
287 char[] ch = indentStep.toCharArray();
288 for( int i=0; i<depth; i++ )
289 characters(ch, 0, ch.length);
290 }
291 }
294 ////////////////////////////////////////////////////////////////////
295 // Constants.
296 ////////////////////////////////////////////////////////////////////
298 private final static Object SEEN_NOTHING = new Object();
299 private final static Object SEEN_ELEMENT = new Object();
300 private final static Object SEEN_DATA = new Object();
303 ////////////////////////////////////////////////////////////////////
304 // Internal state.
305 ////////////////////////////////////////////////////////////////////
307 private Object state = SEEN_NOTHING;
308 private Stack<Object> stateStack = new Stack<Object>();
310 private String indentStep = "";
311 private int depth = 0;
312 }