~ubuntu-branches/ubuntu/vivid/jaxb/vivid-proposed

« back to all changes in this revision

Viewing changes to runtime/src/com/sun/xml/bind/v2/runtime/output/XmlOutput.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2014-09-01 14:26:44 UTC
  • Revision ID: package-import@ubuntu.com-20140901142644-nox3y6t2unq2wxjf
Tags: upstream-2.2.5
ImportĀ upstreamĀ versionĀ 2.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright (c) 1997-2011 Oracle and/or its affiliates. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common Development
 
8
 * and Distribution License("CDDL") (collectively, the "License").  You
 
9
 * may not use this file except in compliance with the License.  You can
 
10
 * obtain a copy of the License at
 
11
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 
12
 * or packager/legal/LICENSE.txt.  See the License for the specific
 
13
 * language governing permissions and limitations under the License.
 
14
 *
 
15
 * When distributing the software, include this License Header Notice in each
 
16
 * file and include the License file at packager/legal/LICENSE.txt.
 
17
 *
 
18
 * GPL Classpath Exception:
 
19
 * Oracle designates this particular file as subject to the "Classpath"
 
20
 * exception as provided by Oracle in the GPL Version 2 section of the License
 
21
 * file that accompanied this code.
 
22
 *
 
23
 * Modifications:
 
24
 * If applicable, add the following below the License Header, with the fields
 
25
 * enclosed by brackets [] replaced by your own identifying information:
 
26
 * "Portions Copyright [year] [name of copyright owner]"
 
27
 *
 
28
 * Contributor(s):
 
29
 * If you wish your version of this file to be governed by only the CDDL or
 
30
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 
31
 * elects to include this software in this distribution under the [CDDL or GPL
 
32
 * Version 2] license."  If you don't indicate a single choice of license, a
 
33
 * recipient has the option to distribute your version of this file under
 
34
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 
35
 * its licensees as provided above.  However, if you add GPL Version 2 code
 
36
 * and therefore, elected the GPL Version 2 license, then the option applies
 
37
 * only if the new code is made subject to such option by the copyright
 
38
 * holder.
 
39
 */
 
40
 
 
41
package com.sun.xml.bind.v2.runtime.output;
 
42
 
 
43
import java.io.IOException;
 
44
 
 
45
import javax.xml.bind.JAXBContext;
 
46
import javax.xml.stream.XMLStreamException;
 
47
 
 
48
import com.sun.xml.bind.v2.runtime.Name;
 
49
import com.sun.xml.bind.v2.runtime.NameList;
 
50
import com.sun.xml.bind.v2.runtime.XMLSerializer;
 
51
 
 
52
import org.xml.sax.SAXException;
 
53
 
 
54
/**
 
55
 * Well-formed XML writer.
 
56
 *
 
57
 * <p>
 
58
 * Implementations of this interface is used to connect {@link XMLSerializer}
 
59
 * to the actual target. This allows {@link XMLSerializer} to be API agnostic.
 
60
 *
 
61
 *
 
62
 * <h2>Notes</h2>
 
63
 * <p>
 
64
 * {@link JAXBContext} assigns indices to URIs and local names
 
65
 * that are statically known by using {@link NameList}.
 
66
 * {@link XmlOutput} implementation can use these indices to improve
 
67
 * the performance. For example, those namespace URI indices can be
 
68
 * turned into prefixes quickly.
 
69
 *
 
70
 * <p>
 
71
 * {@link XmlOutput} still allows arbitrary namepsace URIs / local names
 
72
 * to be written.
 
73
 *
 
74
 * <p>
 
75
 * The {@link NamespaceContextImpl} object, which is shared between {@link XmlOutput} and
 
76
 * {@link XMLSerializer}, keeps track of the in-scope namespace bindings. By the time
 
77
 * the {@link #beginStartTag} method is called, all the namespace bindings for the new
 
78
 * element is already declared. Similarly, after the {@link #endTag} method is called,
 
79
 * in-scope bindings will be removed. This book keeping is all done outside {@link XmlOutput}.
 
80
 *
 
81
 * <p>
 
82
 * {@link XmlOutput} and {@link XMLSerializer} uses indices to
 
83
 * reference prefixes/URIs to be written. {@link NamespaceContextImpl} can
 
84
 * convert prefix indices to URIs and the string representations of prefixes.
 
85
 * Binding from indices to URIs and prefixes do not change while indices
 
86
 * are "in scope", so {@link XmlOutput} is again expected to take advantage of
 
87
 * this to improve the perofmrnace.
 
88
 *
 
89
 * <p>
 
90
 * prefix index 0 is reserved for "xml", and this binding is assumed to be always there.
 
91
 * {@link NamespaceContextImpl} can handle this index correctly, but this binding will never
 
92
 * be reported to {@link XmlOutput} through {@link #beginStartTag}.
 
93
 *
 
94
 * <p>
 
95
 * One pecurilar behavior of a {@link NamespaceContextImpl} object is that it tries
 
96
 * to define redundant xmlns="" on the root element. Implementations of {@link XmlOutput}
 
97
 * is encouraged to check for this and avoid generating redundant namespace declarations.
 
98
 *
 
99
 *
 
100
 *
 
101
 * <h2>Call Sequence</h2>
 
102
 * <p>
 
103
 * {@link XMLSerializer} calls the writer methods in the following order:
 
104
 *
 
105
 * <pre>
 
106
 * CALLSEQUENCE  :=  {@link #startDocument startDocument} ELEMENT {@link #endDocument endDocument}
 
107
 *               |   ELEMENT   // for fragment
 
108
 *
 
109
 * ELEMENT       :=  {@link #beginStartTag beginStartTag} {@link #attribute attribute}* {@link #endStartTag endStartTag} CONTENTS {@link #endTag endTag}
 
110
 *
 
111
 * CONTENTS      :=  (ELEMENT | {@link #text text})*
 
112
 * </pre>
 
113
 *
 
114
 * TODO: for FI, consider making attribute values from Strings to CharSequences.
 
115
 *
 
116
 * @author Kohsuke Kawaguchi
 
117
 */
 
118
public interface XmlOutput {
 
119
//
 
120
//
 
121
// Contracts
 
122
//
 
123
//
 
124
    /**
 
125
     * Called at the very beginning.
 
126
     *
 
127
     * @param serializer
 
128
     *      the {@link XMLSerializer} that coordinates this whole marshalling episode.
 
129
     * @param fragment
 
130
     *      true if we are marshalling a fragment.
 
131
     */
 
132
    public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws IOException, SAXException, XMLStreamException;
 
133
 
 
134
    /**
 
135
     * Called at the very end. This is the last method to be invoked.
 
136
     *
 
137
     * @param fragment
 
138
     *      false if we are writing the whole document.
 
139
     */
 
140
    public void endDocument(boolean fragment) throws IOException, SAXException, XMLStreamException;
 
141
    
 
142
    /**
 
143
     * Writes a start tag.
 
144
     *
 
145
     * <p>
 
146
     * At this point {@link NamespaceContextImpl} holds namespace declarations needed for this
 
147
     * new element.
 
148
     *
 
149
     * <p>
 
150
     * This method is used for writing tags that are indexed.
 
151
     */
 
152
    public void beginStartTag(Name name) throws IOException, XMLStreamException;
 
153
    
 
154
    public void beginStartTag(int prefix, String localName) throws IOException, XMLStreamException;
 
155
 
 
156
    public void attribute( Name name, String value ) throws IOException, XMLStreamException;
 
157
    
 
158
    /**
 
159
     * @param prefix
 
160
     *      -1 if this attribute does not have a prefix
 
161
     *      (this handling differs from that of elements.)
 
162
     */
 
163
    public void attribute( int prefix, String localName, String value ) throws IOException, XMLStreamException;
 
164
 
 
165
    public void endStartTag() throws IOException, SAXException;
 
166
 
 
167
    public void endTag(Name name) throws IOException, SAXException, XMLStreamException;
 
168
    
 
169
    public void endTag(int prefix, String localName) throws IOException, SAXException, XMLStreamException;
 
170
 
 
171
    /**
 
172
     * Writes XML text with character escaping, if necessary.
 
173
     *
 
174
     * @param value
 
175
     *      this string can contain characters that might need escaping
 
176
     *      (such as '&amp;' or '>')
 
177
     * @param needsSeparatingWhitespace
 
178
     */
 
179
    public void text( String value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException;
 
180
 
 
181
    /**
 
182
     * Writes XML text with character escaping, if necessary.
 
183
     *
 
184
     * @param value
 
185
     *      this string can contain characters that might need escaping
 
186
     *      (such as '&amp;' or '>')
 
187
     * @param needsSeparatingWhitespace
 
188
     */
 
189
    public void text( Pcdata value, boolean needsSeparatingWhitespace ) throws IOException, SAXException, XMLStreamException;
 
190
}