1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/output/XmlOutput.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,175 @@ 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 +package com.sun.xml.internal.bind.v2.runtime.output; 1.30 + 1.31 +import java.io.IOException; 1.32 + 1.33 +import javax.xml.bind.JAXBContext; 1.34 +import javax.xml.stream.XMLStreamException; 1.35 + 1.36 +import com.sun.xml.internal.bind.v2.runtime.Name; 1.37 +import com.sun.xml.internal.bind.v2.runtime.NameList; 1.38 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; 1.39 + 1.40 +import org.xml.sax.SAXException; 1.41 + 1.42 +/** 1.43 + * Well-formed XML writer. 1.44 + * 1.45 + * <p> 1.46 + * Implementations of this interface is used to connect {@link XMLSerializer} 1.47 + * to the actual target. This allows {@link XMLSerializer} to be API agnostic. 1.48 + * 1.49 + * 1.50 + * <h2>Notes</h2> 1.51 + * <p> 1.52 + * {@link JAXBContext} assigns indices to URIs and local names 1.53 + * that are statically known by using {@link NameList}. 1.54 + * {@link XmlOutput} implementation can use these indices to improve 1.55 + * the performance. For example, those namespace URI indices can be 1.56 + * turned into prefixes quickly. 1.57 + * 1.58 + * <p> 1.59 + * {@link XmlOutput} still allows arbitrary namepsace URIs / local names 1.60 + * to be written. 1.61 + * 1.62 + * <p> 1.63 + * The {@link NamespaceContextImpl} object, which is shared between {@link XmlOutput} and 1.64 + * {@link XMLSerializer}, keeps track of the in-scope namespace bindings. By the time 1.65 + * the {@link #beginStartTag} method is called, all the namespace bindings for the new 1.66 + * element is already declared. Similarly, after the {@link #endTag} method is called, 1.67 + * in-scope bindings will be removed. This book keeping is all done outside {@link XmlOutput}. 1.68 + * 1.69 + * <p> 1.70 + * {@link XmlOutput} and {@link XMLSerializer} uses indices to 1.71 + * reference prefixes/URIs to be written. {@link NamespaceContextImpl} can 1.72 + * convert prefix indices to URIs and the string representations of prefixes. 1.73 + * Binding from indices to URIs and prefixes do not change while indices 1.74 + * are "in scope", so {@link XmlOutput} is again expected to take advantage of 1.75 + * this to improve the perofmrnace. 1.76 + * 1.77 + * <p> 1.78 + * prefix index 0 is reserved for "xml", and this binding is assumed to be always there. 1.79 + * {@link NamespaceContextImpl} can handle this index correctly, but this binding will never 1.80 + * be reported to {@link XmlOutput} through {@link #beginStartTag}. 1.81 + * 1.82 + * <p> 1.83 + * One pecurilar behavior of a {@link NamespaceContextImpl} object is that it tries 1.84 + * to define redundant xmlns="" on the root element. Implementations of {@link XmlOutput} 1.85 + * is encouraged to check for this and avoid generating redundant namespace declarations. 1.86 + * 1.87 + * 1.88 + * 1.89 + * <h2>Call Sequence</h2> 1.90 + * <p> 1.91 + * {@link XMLSerializer} calls the writer methods in the following order: 1.92 + * 1.93 + * <pre> 1.94 + * CALLSEQUENCE := {@link #startDocument startDocument} ELEMENT {@link #endDocument endDocument} 1.95 + * | ELEMENT // for fragment 1.96 + * 1.97 + * ELEMENT := {@link #beginStartTag beginStartTag} {@link #attribute attribute}* {@link #endStartTag endStartTag} CONTENTS {@link #endTag endTag} 1.98 + * 1.99 + * CONTENTS := (ELEMENT | {@link #text text})* 1.100 + * </pre> 1.101 + * 1.102 + * TODO: for FI, consider making attribute values from Strings to CharSequences. 1.103 + * 1.104 + * @author Kohsuke Kawaguchi 1.105 + */ 1.106 +public interface XmlOutput { 1.107 +// 1.108 +// 1.109 +// Contracts 1.110 +// 1.111 +// 1.112 + /** 1.113 + * Called at the very beginning. 1.114 + * 1.115 + * @param serializer 1.116 + * the {@link XMLSerializer} that coordinates this whole marshalling episode. 1.117 + * @param fragment 1.118 + * true if we are marshalling a fragment. 1.119 + */ 1.120 + public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException; 1.121 + 1.122 + /** 1.123 + * Called at the very end. This is the last method to be invoked. 1.124 + * 1.125 + * @param fragment 1.126 + * false if we are writing the whole document. 1.127 + */ 1.128 + public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException; 1.129 + 1.130 + /** 1.131 + * Writes a start tag. 1.132 + * 1.133 + * <p> 1.134 + * At this point {@link NamespaceContextImpl} holds namespace declarations needed for this 1.135 + * new element. 1.136 + * 1.137 + * <p> 1.138 + * This method is used for writing tags that are indexed. 1.139 + */ 1.140 + public void beginStartTag(Name name) throws IOException, XMLStreamException; 1.141 + 1.142 + public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException; 1.143 + 1.144 + public void attribute( Name name, String value ) throws IOException, XMLStreamException; 1.145 + 1.146 + /** 1.147 + * @param prefix 1.148 + * -1 if this attribute does not have a prefix 1.149 + * (this handling differs from that of elements.) 1.150 + */ 1.151 + public void attribute( int prefix, String localName, String value ) throws IOException, XMLStreamException; 1.152 + 1.153 + public void endStartTag() throws IOException, SAXException; 1.154 + 1.155 + public void endTag(Name name) throws IOException, SAXException, XMLStreamException; 1.156 + 1.157 + public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException; 1.158 + 1.159 + /** 1.160 + * Writes XML text with character escaping, if necessary. 1.161 + * 1.162 + * @param value 1.163 + * this string can contain characters that might need escaping 1.164 + * (such as '&' or '>') 1.165 + * @param needsSeparatingWhitespace 1.166 + */ 1.167 + public void text( String value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException; 1.168 + 1.169 + /** 1.170 + * Writes XML text with character escaping, if necessary. 1.171 + * 1.172 + * @param value 1.173 + * this string can contain characters that might need escaping 1.174 + * (such as '&' or '>') 1.175 + * @param needsSeparatingWhitespace 1.176 + */ 1.177 + public void text( Pcdata value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException; 1.178 +}