src/share/jaxws_classes/com/sun/xml/internal/txw2/output/IndentingXMLFilter.java

Sun, 18 Jun 2017 23:18:45 +0100

author
aefimov
date
Sun, 18 Jun 2017 23:18:45 +0100
changeset 1443
dffc222439a1
parent 0
373ffda63c9a
permissions
-rw-r--r--

8172297: In java 8, the marshalling with JAX-WS does not escape carriage return
Reviewed-by: lancea

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.txw2.output;
aoqi@0 27
aoqi@0 28 import org.xml.sax.Attributes;
aoqi@0 29 import org.xml.sax.ContentHandler;
aoqi@0 30 import org.xml.sax.SAXException;
aoqi@0 31 import org.xml.sax.ext.LexicalHandler;
aoqi@0 32 import org.xml.sax.helpers.XMLFilterImpl;
aoqi@0 33
aoqi@0 34 import java.util.Stack;
aoqi@0 35
aoqi@0 36 /**
aoqi@0 37 * {@link XMLFilterImpl} that does indentation to SAX events.
aoqi@0 38 *
aoqi@0 39 * @author Kohsuke Kawaguchi
aoqi@0 40 */
aoqi@0 41 public class IndentingXMLFilter extends XMLFilterImpl implements LexicalHandler {
aoqi@0 42 private LexicalHandler lexical;
aoqi@0 43
aoqi@0 44 public IndentingXMLFilter() {
aoqi@0 45 }
aoqi@0 46
aoqi@0 47 public IndentingXMLFilter(ContentHandler handler) {
aoqi@0 48 setContentHandler(handler);
aoqi@0 49 }
aoqi@0 50
aoqi@0 51 public IndentingXMLFilter(ContentHandler handler, LexicalHandler lexical) {
aoqi@0 52 setContentHandler(handler);
aoqi@0 53 setLexicalHandler(lexical);
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 public LexicalHandler getLexicalHandler() {
aoqi@0 57 return lexical;
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 public void setLexicalHandler(LexicalHandler lexical) {
aoqi@0 61 this.lexical = lexical;
aoqi@0 62 }
aoqi@0 63
aoqi@0 64
aoqi@0 65 /**
aoqi@0 66 * Return the current indent step.
aoqi@0 67 *
aoqi@0 68 * <p>Return the current indent step: each start tag will be
aoqi@0 69 * indented by this number of spaces times the number of
aoqi@0 70 * ancestors that the element has.</p>
aoqi@0 71 *
aoqi@0 72 * @return The number of spaces in each indentation step,
aoqi@0 73 * or 0 or less for no indentation.
aoqi@0 74 * @see #setIndentStep(int)
aoqi@0 75 *
aoqi@0 76 * @deprecated
aoqi@0 77 * Only return the length of the indent string.
aoqi@0 78 */
aoqi@0 79 public int getIndentStep ()
aoqi@0 80 {
aoqi@0 81 return indentStep.length();
aoqi@0 82 }
aoqi@0 83
aoqi@0 84
aoqi@0 85 /**
aoqi@0 86 * Set the current indent step.
aoqi@0 87 *
aoqi@0 88 * @param indentStep The new indent step (0 or less for no
aoqi@0 89 * indentation).
aoqi@0 90 * @see #getIndentStep()
aoqi@0 91 *
aoqi@0 92 * @deprecated
aoqi@0 93 * Should use the version that takes string.
aoqi@0 94 */
aoqi@0 95 public void setIndentStep (int indentStep)
aoqi@0 96 {
aoqi@0 97 StringBuilder s = new StringBuilder();
aoqi@0 98 for( ; indentStep>0; indentStep-- ) s.append(' ');
aoqi@0 99 setIndentStep(s.toString());
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 public void setIndentStep(String s) {
aoqi@0 103 this.indentStep = s;
aoqi@0 104 }
aoqi@0 105
aoqi@0 106
aoqi@0 107
aoqi@0 108 ////////////////////////////////////////////////////////////////////
aoqi@0 109 // Override methods from XMLWriter.
aoqi@0 110 ////////////////////////////////////////////////////////////////////
aoqi@0 111
aoqi@0 112 /**
aoqi@0 113 * Write a start tag.
aoqi@0 114 *
aoqi@0 115 * <p>Each tag will begin on a new line, and will be
aoqi@0 116 * indented by the current indent step times the number
aoqi@0 117 * of ancestors that the element has.</p>
aoqi@0 118 *
aoqi@0 119 * <p>The newline and indentation will be passed on down
aoqi@0 120 * the filter chain through regular characters events.</p>
aoqi@0 121 *
aoqi@0 122 * @param uri The element's Namespace URI.
aoqi@0 123 * @param localName The element's local name.
aoqi@0 124 * @param qName The element's qualified (prefixed) name.
aoqi@0 125 * @param atts The element's attribute list.
aoqi@0 126 * @exception org.xml.sax.SAXException If there is an error
aoqi@0 127 * writing the start tag, or if a filter further
aoqi@0 128 * down the chain raises an exception.
aoqi@0 129 * @see XMLWriter#startElement(String, String, String,Attributes)
aoqi@0 130 */
aoqi@0 131 public void startElement (String uri, String localName,
aoqi@0 132 String qName, Attributes atts)
aoqi@0 133 throws SAXException {
aoqi@0 134 stateStack.push(SEEN_ELEMENT);
aoqi@0 135 state = SEEN_NOTHING;
aoqi@0 136 if (depth > 0) {
aoqi@0 137 writeNewLine();
aoqi@0 138 }
aoqi@0 139 doIndent();
aoqi@0 140 super.startElement(uri, localName, qName, atts);
aoqi@0 141 depth++;
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 private void writeNewLine() throws SAXException {
aoqi@0 145 super.characters(NEWLINE,0,NEWLINE.length);
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 private static final char[] NEWLINE = {'\n'};
aoqi@0 149
aoqi@0 150
aoqi@0 151 /**
aoqi@0 152 * Write an end tag.
aoqi@0 153 *
aoqi@0 154 * <p>If the element has contained other elements, the tag
aoqi@0 155 * will appear indented on a new line; otherwise, it will
aoqi@0 156 * appear immediately following whatever came before.</p>
aoqi@0 157 *
aoqi@0 158 * <p>The newline and indentation will be passed on down
aoqi@0 159 * the filter chain through regular characters events.</p>
aoqi@0 160 *
aoqi@0 161 * @param uri The element's Namespace URI.
aoqi@0 162 * @param localName The element's local name.
aoqi@0 163 * @param qName The element's qualified (prefixed) name.
aoqi@0 164 * @exception org.xml.sax.SAXException If there is an error
aoqi@0 165 * writing the end tag, or if a filter further
aoqi@0 166 * down the chain raises an exception.
aoqi@0 167 * @see XMLWriter#endElement(String, String, String)
aoqi@0 168 */
aoqi@0 169 public void endElement (String uri, String localName, String qName)
aoqi@0 170 throws SAXException
aoqi@0 171 {
aoqi@0 172 depth--;
aoqi@0 173 if (state == SEEN_ELEMENT) {
aoqi@0 174 writeNewLine();
aoqi@0 175 doIndent();
aoqi@0 176 }
aoqi@0 177 super.endElement(uri, localName, qName);
aoqi@0 178 state = stateStack.pop();
aoqi@0 179 }
aoqi@0 180
aoqi@0 181
aoqi@0 182 // /**
aoqi@0 183 // * Write a empty element tag.
aoqi@0 184 // *
aoqi@0 185 // * <p>Each tag will appear on a new line, and will be
aoqi@0 186 // * indented by the current indent step times the number
aoqi@0 187 // * of ancestors that the element has.</p>
aoqi@0 188 // *
aoqi@0 189 // * <p>The newline and indentation will be passed on down
aoqi@0 190 // * the filter chain through regular characters events.</p>
aoqi@0 191 // *
aoqi@0 192 // * @param uri The element's Namespace URI.
aoqi@0 193 // * @param localName The element's local name.
aoqi@0 194 // * @param qName The element's qualified (prefixed) name.
aoqi@0 195 // * @param atts The element's attribute list.
aoqi@0 196 // * @exception org.xml.sax.SAXException If there is an error
aoqi@0 197 // * writing the empty tag, or if a filter further
aoqi@0 198 // * down the chain raises an exception.
aoqi@0 199 // * @see XMLWriter#emptyElement(String, String, String, Attributes)
aoqi@0 200 // */
aoqi@0 201 // public void emptyElement (String uri, String localName,
aoqi@0 202 // String qName, Attributes atts)
aoqi@0 203 // throws SAXException
aoqi@0 204 // {
aoqi@0 205 // state = SEEN_ELEMENT;
aoqi@0 206 // if (depth > 0) {
aoqi@0 207 // super.characters("\n");
aoqi@0 208 // }
aoqi@0 209 // doIndent();
aoqi@0 210 // super.emptyElement(uri, localName, qName, atts);
aoqi@0 211 // }
aoqi@0 212
aoqi@0 213
aoqi@0 214 /**
aoqi@0 215 * Write a sequence of characters.
aoqi@0 216 *
aoqi@0 217 * @param ch The characters to write.
aoqi@0 218 * @param start The starting position in the array.
aoqi@0 219 * @param length The number of characters to use.
aoqi@0 220 * @exception org.xml.sax.SAXException If there is an error
aoqi@0 221 * writing the characters, or if a filter further
aoqi@0 222 * down the chain raises an exception.
aoqi@0 223 * @see XMLWriter#characters(char[], int, int)
aoqi@0 224 */
aoqi@0 225 public void characters (char ch[], int start, int length)
aoqi@0 226 throws SAXException
aoqi@0 227 {
aoqi@0 228 state = SEEN_DATA;
aoqi@0 229 super.characters(ch, start, length);
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 public void comment(char ch[], int start, int length) throws SAXException {
aoqi@0 233 if (depth > 0) {
aoqi@0 234 writeNewLine();
aoqi@0 235 }
aoqi@0 236 doIndent();
aoqi@0 237 if(lexical!=null)
aoqi@0 238 lexical.comment(ch,start,length);
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 public void startDTD(String name, String publicId, String systemId) throws SAXException {
aoqi@0 242 if(lexical!=null)
aoqi@0 243 lexical.startDTD(name, publicId, systemId);
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 public void endDTD() throws SAXException {
aoqi@0 247 if(lexical!=null)
aoqi@0 248 lexical.endDTD();
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 public void startEntity(String name) throws SAXException {
aoqi@0 252 if(lexical!=null)
aoqi@0 253 lexical.startEntity(name);
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 public void endEntity(String name) throws SAXException {
aoqi@0 257 if(lexical!=null)
aoqi@0 258 lexical.endEntity(name);
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 public void startCDATA() throws SAXException {
aoqi@0 262 if(lexical!=null)
aoqi@0 263 lexical.startCDATA();
aoqi@0 264 }
aoqi@0 265
aoqi@0 266 public void endCDATA() throws SAXException {
aoqi@0 267 if(lexical!=null)
aoqi@0 268 lexical.endCDATA();
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 ////////////////////////////////////////////////////////////////////
aoqi@0 272 // Internal methods.
aoqi@0 273 ////////////////////////////////////////////////////////////////////
aoqi@0 274
aoqi@0 275
aoqi@0 276 /**
aoqi@0 277 * Print indentation for the current level.
aoqi@0 278 *
aoqi@0 279 * @exception org.xml.sax.SAXException If there is an error
aoqi@0 280 * writing the indentation characters, or if a filter
aoqi@0 281 * further down the chain raises an exception.
aoqi@0 282 */
aoqi@0 283 private void doIndent ()
aoqi@0 284 throws SAXException
aoqi@0 285 {
aoqi@0 286 if (depth > 0) {
aoqi@0 287 char[] ch = indentStep.toCharArray();
aoqi@0 288 for( int i=0; i<depth; i++ )
aoqi@0 289 characters(ch, 0, ch.length);
aoqi@0 290 }
aoqi@0 291 }
aoqi@0 292
aoqi@0 293
aoqi@0 294 ////////////////////////////////////////////////////////////////////
aoqi@0 295 // Constants.
aoqi@0 296 ////////////////////////////////////////////////////////////////////
aoqi@0 297
aoqi@0 298 private final static Object SEEN_NOTHING = new Object();
aoqi@0 299 private final static Object SEEN_ELEMENT = new Object();
aoqi@0 300 private final static Object SEEN_DATA = new Object();
aoqi@0 301
aoqi@0 302
aoqi@0 303 ////////////////////////////////////////////////////////////////////
aoqi@0 304 // Internal state.
aoqi@0 305 ////////////////////////////////////////////////////////////////////
aoqi@0 306
aoqi@0 307 private Object state = SEEN_NOTHING;
aoqi@0 308 private Stack<Object> stateStack = new Stack<Object>();
aoqi@0 309
aoqi@0 310 private String indentStep = "";
aoqi@0 311 private int depth = 0;
aoqi@0 312 }

mercurial