Path: blob/master/src/java.xml/share/classes/javax/xml/stream/XMLEventWriter.java
40948 views
/*1* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.stream;2627import javax.xml.stream.events.*;28import javax.xml.stream.util.XMLEventConsumer;29import javax.xml.namespace.NamespaceContext;3031/**32*33* This is the top level interface for writing XML documents.34*35* Instances of this interface are not required to validate the36* form of the XML.37*38* @version 1.039* @author Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.40* @see XMLEventReader41* @see javax.xml.stream.events.XMLEvent42* @see javax.xml.stream.events.Characters43* @see javax.xml.stream.events.ProcessingInstruction44* @see javax.xml.stream.events.StartElement45* @see javax.xml.stream.events.EndElement46* @since 1.647*/48public interface XMLEventWriter extends XMLEventConsumer {4950/**51* Writes any cached events to the underlying output mechanism52* @throws XMLStreamException if an error occurs53*/54public void flush() throws XMLStreamException;5556/**57* Frees any resources associated with this stream58* @throws XMLStreamException if an error occurs59*/60public void close() throws XMLStreamException;6162/**63* Add an event to the output stream64* Adding a START_ELEMENT will open a new namespace scope that65* will be closed when the corresponding END_ELEMENT is written.66* <table class="striped">67* <caption>Required and optional fields for events added to the writer</caption>68* <thead>69* <tr>70* <th scope="col">Event Type</th>71* <th scope="col">Required Fields</th>72* <th scope="col">Optional Fields</th>73* <th scope="col">Required Behavior</th>74* </tr>75* </thead>76* <tbody>77* <tr>78* <th scope="row"> START_ELEMENT </th>79* <td> QName name </td>80* <td> namespaces , attributes </td>81* <td> A START_ELEMENT will be written by writing the name,82* namespaces, and attributes of the event in XML 1.0 valid83* syntax for START_ELEMENTs.84* The name is written by looking up the prefix for85* the namespace uri. The writer can be configured to86* respect prefixes of QNames. If the writer is respecting87* prefixes it must use the prefix set on the QName. The88* default behavior is to lookup the value for the prefix89* on the EventWriter's internal namespace context.90* Each attribute (if any)91* is written using the behavior specified in the attribute92* section of this table. Each namespace (if any) is written93* using the behavior specified in the namespace section of this94* table.95* </td>96* </tr>97* <tr>98* <th scope="row"> END_ELEMENT </th>99* <td> Qname name </td>100* <td> None </td>101* <td> A well formed END_ELEMENT tag is written.102* The name is written by looking up the prefix for103* the namespace uri. The writer can be configured to104* respect prefixes of QNames. If the writer is respecting105* prefixes it must use the prefix set on the QName. The106* default behavior is to lookup the value for the prefix107* on the EventWriter's internal namespace context.108* If the END_ELEMENT name does not match the START_ELEMENT109* name an XMLStreamException is thrown.110* </td>111* </tr>112* <tr>113* <th scope="row"> ATTRIBUTE </th>114* <td> QName name , String value </td>115* <td> QName type </td>116* <td> An attribute is written using the same algorithm117* to find the lexical form as used in START_ELEMENT.118* The default is to use double quotes to wrap attribute119* values and to escape any double quotes found in the120* value. The type value is ignored.121* </td>122* </tr>123* <tr>124* <th scope="row"> NAMESPACE </th>125* <td> String prefix, String namespaceURI,126* boolean isDefaultNamespaceDeclaration127* </td>128* <td> None </td>129* <td> A namespace declaration is written. If the130* namespace is a default namespace declaration131* (isDefault is true) then xmlns="$namespaceURI"132* is written and the prefix is optional. If133* isDefault is false, the prefix must be declared134* and the writer must prepend xmlns to the prefix135* and write out a standard prefix declaration.136* </td>137* </tr>138* <tr>139* <th scope="row"> PROCESSING_INSTRUCTION </th>140* <td> None</td>141* <td> String target, String data</td>142* <td> The data does not need to be present and may be143* null. Target is required and many not be null.144* The writer145* will write data section146* directly after the target,147* enclosed in appropriate XML 1.0 syntax148* </td>149* </tr>150* <tr>151* <th scope="row"> COMMENT </th>152* <td> None </td>153* <td> String comment </td>154* <td> If the comment is present (not null) it is written, otherwise an155* an empty comment is written156* </td>157* </tr>158* <tr>159* <th scope="row"> START_DOCUMENT </th>160* <td> None </td>161* <td> String encoding , boolean standalone, String version </td>162* <td> A START_DOCUMENT event is not required to be written to the163* stream. If present the attributes are written inside164* the appropriate XML declaration syntax165* </td>166* </tr>167* <tr>168* <th scope="row"> END_DOCUMENT </th>169* <td> None </td>170* <td> None </td>171* <td> Nothing is written to the output </td>172* </tr>173* <tr>174* <th scope="row"> DTD </th>175* <td> String DocumentTypeDefinition </td>176* <td> None </td>177* <td> The DocumentTypeDefinition is written to the output </td>178* </tr>179* </tbody>180* </table>181* @param event the event to be added182* @throws XMLStreamException if an error occurs183*/184public void add(XMLEvent event) throws XMLStreamException;185186/**187* Adds an entire stream to an output stream,188* calls next() on the inputStream argument until hasNext() returns false189* This should be treated as a convenience method that will190* perform the following loop over all the events in an191* event reader and call add on each event.192*193* @param reader the event stream to add to the output194* @throws XMLStreamException if an error occurs195*/196197public void add(XMLEventReader reader) throws XMLStreamException;198199/**200* Gets the prefix the uri is bound to201* @param uri the uri to look up202* @return the prefix203* @throws XMLStreamException if an error occurs204*/205public String getPrefix(String uri) throws XMLStreamException;206207/**208* Sets the prefix the uri is bound to. This prefix is bound209* in the scope of the current START_ELEMENT / END_ELEMENT pair.210* If this method is called before a START_ELEMENT has been written211* the prefix is bound in the root scope.212* @param prefix the prefix to bind to the uri213* @param uri the uri to bind to the prefix214* @throws XMLStreamException if an error occurs215*/216public void setPrefix(String prefix, String uri) throws XMLStreamException;217218/**219* Binds a URI to the default namespace220* This URI is bound221* in the scope of the current START_ELEMENT / END_ELEMENT pair.222* If this method is called before a START_ELEMENT has been written223* the uri is bound in the root scope.224* @param uri the uri to bind to the default namespace225* @throws XMLStreamException if an error occurs226*/227public void setDefaultNamespace(String uri) throws XMLStreamException;228229/**230* Sets the current namespace context for prefix and uri bindings.231* This context becomes the root namespace context for writing and232* will replace the current root namespace context. Subsequent calls233* to setPrefix and setDefaultNamespace will bind namespaces using234* the context passed to the method as the root context for resolving235* namespaces.236* @param context the namespace context to use for this writer237* @throws XMLStreamException if an error occurs238*/239public void setNamespaceContext(NamespaceContext context)240throws XMLStreamException;241242/**243* Returns the current namespace context.244* @return the current namespace context245*/246public NamespaceContext getNamespaceContext();247248249}250251252