1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/marshaller/DataWriter.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,377 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +//@@3RD PARTY CODE@@ 1.30 + 1.31 +// DataWriter.java - XML writer for data-oriented files. 1.32 + 1.33 +package com.sun.xml.internal.bind.marshaller; 1.34 + 1.35 +import java.io.IOException; 1.36 +import java.io.Writer; 1.37 +import java.util.Stack; 1.38 + 1.39 +import org.xml.sax.Attributes; 1.40 +import org.xml.sax.SAXException; 1.41 + 1.42 + 1.43 +/** 1.44 + * Write data- or field-oriented XML. 1.45 + * 1.46 + * <p>This filter pretty-prints field-oriented XML without mixed content. 1.47 + * all added indentation and newlines will be passed on down 1.48 + * the filter chain (if any).</p> 1.49 + * 1.50 + * <p>In general, all whitespace in an XML document is potentially 1.51 + * significant, so a general-purpose XML writing tool like the 1.52 + * {@link XMLWriter} class cannot 1.53 + * add newlines or indentation.</p> 1.54 + * 1.55 + * <p>There is, however, a large class of XML documents where information 1.56 + * is strictly fielded: each element contains either character data 1.57 + * or other elements, but not both. For this special case, it is possible 1.58 + * for a writing tool to provide automatic indentation and newlines 1.59 + * without requiring extra work from the user. Note that this class 1.60 + * will likely not yield appropriate results for document-oriented 1.61 + * XML like XHTML pages, which mix character data and elements together.</p> 1.62 + * 1.63 + * <p>This writer will automatically place each start tag on a new line, 1.64 + * optionally indented if an indent step is provided (by default, there 1.65 + * is no indentation). If an element contains other elements, the end 1.66 + * tag will also appear on a new line with leading indentation. Consider, 1.67 + * for example, the following code:</p> 1.68 + * 1.69 + * <pre> 1.70 + * DataWriter w = new DataWriter(); 1.71 + * 1.72 + * w.setIndentStep(2); 1.73 + * w.startDocument(); 1.74 + * w.startElement("Person"); 1.75 + * w.dataElement("name", "Jane Smith"); 1.76 + * w.dataElement("date-of-birth", "1965-05-23"); 1.77 + * w.dataElement("citizenship", "US"); 1.78 + * w.endElement("Person"); 1.79 + * w.endDocument(); 1.80 + * </pre> 1.81 + * 1.82 + * <p>This code will produce the following document:</p> 1.83 + * 1.84 + * <pre> 1.85 + * <?xml version="1.0" standalone="yes"?> 1.86 + * 1.87 + * <Person> 1.88 + * <name>Jane Smith</name> 1.89 + * <date-of-birth>1965-05-23</date-of-birth> 1.90 + * <citizenship>US</citizenship> 1.91 + * </Person> 1.92 + * </pre> 1.93 + * 1.94 + * <p>This class inherits from {@link XMLWriter}, 1.95 + * and provides all of the same support for Namespaces.</p> 1.96 + * 1.97 + * @since 1.0 1.98 + * @author David Megginson, david@megginson.com 1.99 + * @version 0.2 1.100 + * @see XMLWriter 1.101 + */ 1.102 +public class DataWriter extends XMLWriter 1.103 +{ 1.104 + 1.105 + 1.106 + 1.107 + //////////////////////////////////////////////////////////////////// 1.108 + // Constructors. 1.109 + //////////////////////////////////////////////////////////////////// 1.110 + 1.111 + 1.112 + /** 1.113 + * Create a new data writer for the specified output. 1.114 + * 1.115 + * @param writer The character stream where the XML document 1.116 + * will be written. 1.117 + * @param encoding 1.118 + * If non-null string is specified, it is written as a part 1.119 + * of the XML declaration. 1.120 + */ 1.121 + public DataWriter ( Writer writer, String encoding, CharacterEscapeHandler _escapeHandler ) 1.122 + { 1.123 + super(writer,encoding,_escapeHandler); 1.124 + } 1.125 + 1.126 + 1.127 + public DataWriter (Writer writer, String encoding ) { 1.128 + this( writer, encoding, DumbEscapeHandler.theInstance ); 1.129 + } 1.130 + 1.131 + 1.132 + 1.133 + //////////////////////////////////////////////////////////////////// 1.134 + // Accessors and setters. 1.135 + //////////////////////////////////////////////////////////////////// 1.136 + 1.137 + 1.138 + /** 1.139 + * Return the current indent step. 1.140 + * 1.141 + * <p>Return the current indent step: each start tag will be 1.142 + * indented by this number of spaces times the number of 1.143 + * ancestors that the element has.</p> 1.144 + * 1.145 + * @return The number of spaces in each indentation step, 1.146 + * or 0 or less for no indentation. 1.147 + * @see #setIndentStep(int) 1.148 + * 1.149 + * @deprecated 1.150 + * Only return the length of the indent string. 1.151 + */ 1.152 + public int getIndentStep () 1.153 + { 1.154 + return indentStep.length(); 1.155 + } 1.156 + 1.157 + 1.158 + /** 1.159 + * Set the current indent step. 1.160 + * 1.161 + * @param indentStep The new indent step (0 or less for no 1.162 + * indentation). 1.163 + * @see #getIndentStep() 1.164 + * 1.165 + * @deprecated 1.166 + * Should use the version that takes string. 1.167 + */ 1.168 + public void setIndentStep (int indentStep) 1.169 + { 1.170 + StringBuilder buf = new StringBuilder(); 1.171 + for( ; indentStep>0; indentStep-- ) 1.172 + buf.append(' '); 1.173 + setIndentStep(buf.toString()); 1.174 + } 1.175 + 1.176 + public void setIndentStep(String s) { 1.177 + this.indentStep = s; 1.178 + } 1.179 + 1.180 + 1.181 + 1.182 + //////////////////////////////////////////////////////////////////// 1.183 + // Override methods from XMLWriter. 1.184 + //////////////////////////////////////////////////////////////////// 1.185 + 1.186 + 1.187 + /** 1.188 + * Reset the writer so that it can be reused. 1.189 + * 1.190 + * <p>This method is especially useful if the writer failed 1.191 + * with an exception the last time through.</p> 1.192 + * 1.193 + * @see XMLWriter#reset() 1.194 + */ 1.195 + public void reset () 1.196 + { 1.197 + depth = 0; 1.198 + state = SEEN_NOTHING; 1.199 + stateStack = new Stack<Object>(); 1.200 + super.reset(); 1.201 + } 1.202 + 1.203 + protected void writeXmlDecl(String decl) throws IOException { 1.204 + super.writeXmlDecl(decl); 1.205 + write('\n'); 1.206 + } 1.207 + 1.208 + 1.209 + /** 1.210 + * Write a start tag. 1.211 + * 1.212 + * <p>Each tag will begin on a new line, and will be 1.213 + * indented by the current indent step times the number 1.214 + * of ancestors that the element has.</p> 1.215 + * 1.216 + * <p>The newline and indentation will be passed on down 1.217 + * the filter chain through regular characters events.</p> 1.218 + * 1.219 + * @param uri The element's Namespace URI. 1.220 + * @param localName The element's local name. 1.221 + * @param qName The element's qualified (prefixed) name. 1.222 + * @param atts The element's attribute list. 1.223 + * @exception org.xml.sax.SAXException If there is an error 1.224 + * writing the start tag, or if a filter further 1.225 + * down the chain raises an exception. 1.226 + * @see XMLWriter#startElement(String, String, String, Attributes) 1.227 + */ 1.228 + public void startElement (String uri, String localName, 1.229 + String qName, Attributes atts) 1.230 + throws SAXException 1.231 + { 1.232 + stateStack.push(SEEN_ELEMENT); 1.233 + state = SEEN_NOTHING; 1.234 + if (depth > 0) { 1.235 + super.characters("\n"); 1.236 + } 1.237 + doIndent(); 1.238 + super.startElement(uri, localName, qName, atts); 1.239 + depth++; 1.240 + } 1.241 + 1.242 + 1.243 + /** 1.244 + * Write an end tag. 1.245 + * 1.246 + * <p>If the element has contained other elements, the tag 1.247 + * will appear indented on a new line; otherwise, it will 1.248 + * appear immediately following whatever came before.</p> 1.249 + * 1.250 + * <p>The newline and indentation will be passed on down 1.251 + * the filter chain through regular characters events.</p> 1.252 + * 1.253 + * @param uri The element's Namespace URI. 1.254 + * @param localName The element's local name. 1.255 + * @param qName The element's qualified (prefixed) name. 1.256 + * @exception org.xml.sax.SAXException If there is an error 1.257 + * writing the end tag, or if a filter further 1.258 + * down the chain raises an exception. 1.259 + * @see XMLWriter#endElement(String, String, String) 1.260 + */ 1.261 + public void endElement (String uri, String localName, String qName) 1.262 + throws SAXException 1.263 + { 1.264 + depth--; 1.265 + if (state == SEEN_ELEMENT) { 1.266 + super.characters("\n"); 1.267 + doIndent(); 1.268 + } 1.269 + super.endElement(uri, localName, qName); 1.270 + state = stateStack.pop(); 1.271 + } 1.272 + 1.273 + public void endDocument() throws SAXException { 1.274 + try { 1.275 + write('\n'); 1.276 + } catch( IOException e ) { 1.277 + throw new SAXException(e); 1.278 + } 1.279 + super.endDocument(); 1.280 + } 1.281 + 1.282 +// /** 1.283 +// * Write a empty element tag. 1.284 +// * 1.285 +// * <p>Each tag will appear on a new line, and will be 1.286 +// * indented by the current indent step times the number 1.287 +// * of ancestors that the element has.</p> 1.288 +// * 1.289 +// * <p>The newline and indentation will be passed on down 1.290 +// * the filter chain through regular characters events.</p> 1.291 +// * 1.292 +// * @param uri The element's Namespace URI. 1.293 +// * @param localName The element's local name. 1.294 +// * @param qName The element's qualified (prefixed) name. 1.295 +// * @param atts The element's attribute list. 1.296 +// * @exception org.xml.sax.SAXException If there is an error 1.297 +// * writing the empty tag, or if a filter further 1.298 +// * down the chain raises an exception. 1.299 +// * @see XMLWriter#emptyElement(String, String, String, Attributes) 1.300 +// */ 1.301 +// public void emptyElement (String uri, String localName, 1.302 +// String qName, Attributes atts) 1.303 +// throws SAXException 1.304 +// { 1.305 +// state = SEEN_ELEMENT; 1.306 +// if (depth > 0) { 1.307 +// super.characters("\n"); 1.308 +// } 1.309 +// doIndent(); 1.310 +// super.emptyElement(uri, localName, qName, atts); 1.311 +// } 1.312 + 1.313 + 1.314 + /** 1.315 + * Write a sequence of characters. 1.316 + * 1.317 + * @param ch The characters to write. 1.318 + * @param start The starting position in the array. 1.319 + * @param length The number of characters to use. 1.320 + * @exception org.xml.sax.SAXException If there is an error 1.321 + * writing the characters, or if a filter further 1.322 + * down the chain raises an exception. 1.323 + * @see XMLWriter#characters(char[], int, int) 1.324 + */ 1.325 + public void characters (char ch[], int start, int length) 1.326 + throws SAXException 1.327 + { 1.328 + state = SEEN_DATA; 1.329 + super.characters(ch, start, length); 1.330 + } 1.331 + 1.332 + 1.333 + 1.334 + //////////////////////////////////////////////////////////////////// 1.335 + // Internal methods. 1.336 + //////////////////////////////////////////////////////////////////// 1.337 + 1.338 + 1.339 + /** 1.340 + * Print indentation for the current level. 1.341 + * 1.342 + * @exception org.xml.sax.SAXException If there is an error 1.343 + * writing the indentation characters, or if a filter 1.344 + * further down the chain raises an exception. 1.345 + */ 1.346 + private void doIndent () 1.347 + throws SAXException 1.348 + { 1.349 + if (depth > 0) { 1.350 + char[] ch = indentStep.toCharArray(); 1.351 + for( int i=0; i<depth; i++ ) 1.352 + characters(ch, 0, ch.length); 1.353 + } 1.354 + } 1.355 + 1.356 + 1.357 + 1.358 + //////////////////////////////////////////////////////////////////// 1.359 + // Constants. 1.360 + //////////////////////////////////////////////////////////////////// 1.361 + 1.362 + private final static Object SEEN_NOTHING = new Object(); 1.363 + private final static Object SEEN_ELEMENT = new Object(); 1.364 + private final static Object SEEN_DATA = new Object(); 1.365 + 1.366 + 1.367 + 1.368 + //////////////////////////////////////////////////////////////////// 1.369 + // Internal state. 1.370 + //////////////////////////////////////////////////////////////////// 1.371 + 1.372 + private Object state = SEEN_NOTHING; 1.373 + private Stack<Object> stateStack = new Stack<Object>(); 1.374 + 1.375 + private String indentStep = ""; 1.376 + private int depth = 0; 1.377 + 1.378 +} 1.379 + 1.380 +// end of DataWriter.java