View Javadoc
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   */
25  
26  package com.sun.xml.internal.txw2;
27  
28  import com.sun.xml.internal.txw2.annotation.XmlElement;
29  import com.sun.xml.internal.txw2.output.XmlSerializer;
30  
31  import javax.xml.namespace.QName;
32  
33  /**
34   * Defines common operations for all typed XML writers.
35   * Root of all typed XML writer interfaces.
36   *
37   * <p>
38   * This interface defines a series of methods to allow client applications
39   * to write arbitrary well-formed documents.
40   *
41   * @author Kohsuke Kawaguchi
42   */
43  public interface TypedXmlWriter {
44      /**
45       * Commits this element (and all its descendants) to the output.
46       *
47       * <p>
48       * Short for <tt>_commit(true)</tt>.
49       */
50      void commit();
51  
52      /**
53       * Commits this element (and all its descendants) to the output.
54       *
55       * <p>
56       * Once a writer is committed, nothing can be added to it further.
57       * Committing allows TXW to output a part of the document even
58       * if the rest has not yet been written.
59       *
60       * @param includingAllPredecessors
61       *      if false, this operation will _commit this writer and all its
62       *      descendants writers. If true, in addition to those writers,
63       *      this operation will close all the writers before this writer
64       *      in the document order.
65       */
66      void commit(boolean includingAllPredecessors);
67  
68      /**
69       * Blocks the writing of the start tag so that
70       * new attributes can be added even after child
71       * elements are appended.
72       *
73       * <p>
74       * This blocks the output at the token before the start tag until
75       * the {@link #commit()} method is called to _commit this element.
76       *
77       * <p>
78       * For more information, see the TXW documentation.
79       */
80      void block();
81  
82      /**
83       * Gets the {@link Document} object that this writer is writing to.
84       *
85       * @return
86       *      always non-null.
87       */
88      Document getDocument();
89  
90      /**
91       * Adds an attribute of the given name and the value.
92       *
93       * <p>
94       * Short for <pre>_attribute("",localName,value);</pre>
95       *
96       * @see #_attribute(String, String, Object)
97       */
98      void _attribute( String localName, Object value );
99  
100     /**
101      * Adds an attribute of the given name and the value.
102      *
103      * <p>
104      * Short for <pre>_attribute(new QName(nsUri,localName),value);</pre>
105      *
106      * @see #_attribute(QName, Object)
107      */
108     void _attribute( String nsUri, String localName, Object value );
109 
110     /**
111      * Adds an attribute of the given name and the value.
112      *
113      * @param attributeName
114      *      must not be null.
115      * @param value
116      *      value of the attribute.
117      *      must not be null.
118      *      See the documentation for the conversion rules.
119      */
120     void _attribute( QName attributeName, Object value );
121 
122     /**
123      * Declares a new namespace URI on this element.
124      *
125      * <p>
126      * The runtime system will assign an unique prefix for the URI.
127      *
128      * @param uri
129      *      can be empty, but must not be null.
130      */
131     void _namespace( String uri );
132 
133     /**
134      * Declares a new namespace URI on this element to
135      * a specific prefix.
136      *
137      * @param uri
138      *      can be empty, but must not be null.
139      * @param prefix
140      *      If non-empty, this prefix is bound to the URI
141      *      on this element. If empty, then the runtime will still try to
142      *      use the URI as the default namespace, but it may fail to do so
143      *      because of the constraints in the XML.
144      *
145      * @throws IllegalArgumentException
146      *      if the same prefix is already declared on this element.
147      */
148     void _namespace( String uri, String prefix );
149 
150     /**
151      * Declares a new namespace URI on this element.
152      *
153      * <p>
154      * The runtime system will assign an unique prefix for the URI.
155      *
156      * @param uri
157      *      can be empty, but must not be null.
158      * @param requirePrefix
159      *      if false, this method behaves just like {@link #_namespace(String)}.
160      *      if true, this guarantees that the URI is bound to a non empty prefix.
161      */
162     void _namespace( String uri, boolean requirePrefix );
163 
164     /**
165      * Appends text data.
166      *
167      * @param value
168      *      must not be null.
169      *      See the documentation for the conversion rules.
170      */
171     void _pcdata( Object value );
172 
173     /**
174      * Appends CDATA section.
175      *
176      * @param value
177      *      must not be null.
178      *      See the documentation for the conversion rules.
179      */
180     void _cdata( Object value );
181 
182     /**
183      * Appends a comment.
184      *
185      * @param value
186      *      must not be null.
187      *      See the documentation for the conversion rules.
188      *
189      * @throws UnsupportedOperationException
190      *      if the underlying {@link XmlSerializer} does not support
191      *      writing comments, this exception can be thrown.
192      */
193     void _comment( Object value ) throws UnsupportedOperationException;
194 
195     /**
196      * Appends a new child element.
197      *
198      * <p>
199      * Short for <pre>_element(<i>URI of this element</i>,localName,contentModel);</pre>
200      *
201      * <p>
202      * The namespace URI will be inherited from the parent element.
203      *
204      * @see #_element(String, String, Class)
205      */
206     <T extends TypedXmlWriter> T _element( String localName, Class<T> contentModel );
207 
208     /**
209      * Appends a new child element.
210      *
211      * <p>
212      * The newly created child element is appended at the end of the children.
213      *
214      * @param nsUri
215      *      The namespace URI of the newly created element.
216      * @param localName
217      *      The local name of the newly created element.
218      * @param contentModel
219      *      The typed XML writer interface used to write the children of
220      *      the new child element.
221      *
222      * @return
223      *      always return non-null {@link TypedXmlWriter} that can be used
224      *      to write the contents of the newly created child element.
225      */
226     <T extends TypedXmlWriter> T _element( String nsUri, String localName, Class<T> contentModel );
227 
228     /**
229      * Appends a new child element.
230      *
231      * <p>
232      * Short for <pre>_element(tagName.getNamespaceURI(),tagName.getLocalPart(),contentModel);</pre>
233      *
234      * @see #_element(String, String, Class)
235      */
236     <T extends TypedXmlWriter> T _element( QName tagName, Class<T> contentModel );
237 
238     /**
239      * Appends a new child element.
240      *
241      * <p>
242      * This version of the _element method requires the <i>T</i> class to be
243      * annotated with {@link XmlElement} annotation. The element name will be
244      * taken from there.
245      *
246      * @see #_element(String, String, Class)
247      */
248     <T extends TypedXmlWriter> T _element( Class<T> contentModel );
249 
250     /**
251      * Returns a different interface for this typed XML Writer.
252      *
253      * <p>
254      * Semantically, this operation is a 'cast' --- it returns the same underlying
255      * writer in a different interface. The returned new writer and the current writer
256      * will write to the same element.
257      *
258      * <p>
259      * But this is different from Java's ordinary cast because the returned object
260      * is not always the same as the current object.
261      *
262      * @return
263      *      always return non-null.
264      */
265     <T extends TypedXmlWriter> T _cast( Class<T> targetInterface );
266 }