Path: blob/master/src/java.xml/share/classes/javax/xml/stream/XMLStreamWriter.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.namespace.NamespaceContext;2829/**30* The XMLStreamWriter interface specifies how to write XML. The XMLStreamWriter does31* not perform well formedness checking on its input. However32* the writeCharacters method is required to escape {@literal &, < and >}33* For attribute values the writeAttribute method will escape the34* above characters plus {@literal "} to ensure that all character content35* and attribute values are well formed.36*37* Each NAMESPACE38* and ATTRIBUTE must be individually written.39*40* <table class="striped">41* <caption>XML Namespaces, {@code javax.xml.stream.isRepairingNamespaces} and write method behaviour</caption>42* <thead>43* <tr style="border-bottom: 1px solid black">44* <th scope="col" rowspan="2">Method</th> <!-- method -->45* <th scope="col" colspan="2">{@code isRepairingNamespaces} == true</th>46* <th scope="col" colspan="2">{@code isRepairingNamespaces} == false</th>47* </tr>48* <tr>49* <!-- method -->50* <th scope="col">namespaceURI bound</th>51* <th scope="col">namespaceURI unbound</th>52* <th scope="col">namespaceURI bound</th>53* <th scope="col">namespaceURI unbound</th>54* </tr>55* </thead>56*57* <tbody>58* <tr>59* <th scope="row">{@code writeAttribute(namespaceURI, localName, value)}</th>60* <!-- isRepairingNamespaces == true -->61* <td>62* <!-- namespaceURI bound -->63* prefix:localName="value" <sup>[1]</sup>64* </td>65* <td>66* <!-- namespaceURI unbound -->67* xmlns:{generated}="namespaceURI" {generated}:localName="value"68* </td>69* <!-- isRepairingNamespaces == false -->70* <td>71* <!-- namespaceURI bound -->72* prefix:localName="value" <sup>[1]</sup>73* </td>74* <td>75* <!-- namespaceURI unbound -->76* {@code XMLStreamException}77* </td>78* </tr>79*80* <tr>81* <th scope="row">{@code writeAttribute(prefix, namespaceURI, localName, value)}</th>82* <!-- isRepairingNamespaces == true -->83* <td>84* <!-- namespaceURI bound -->85* bound to same prefix:<br>86* prefix:localName="value" <sup>[1]</sup><br>87* <br>88* bound to different prefix:<br>89* xmlns:{generated}="namespaceURI" {generated}:localName="value"90* </td>91* <td>92* <!-- namespaceURI unbound -->93* xmlns:prefix="namespaceURI" prefix:localName="value" <sup>[3]</sup>94* </td>95* <!-- isRepairingNamespaces == false -->96* <td>97* <!-- namespaceURI bound -->98* bound to same prefix:<br>99* prefix:localName="value" <sup>[1][2]</sup><br>100* <br>101* bound to different prefix:<br>102* {@code XMLStreamException}<sup>[2]</sup>103* </td>104* <td>105* <!-- namespaceURI unbound -->106* xmlns:prefix="namespaceURI" prefix:localName="value" <sup>[2][5]</sup>107* </td>108* </tr>109*110* <tr>111* <th scope="row">{@code writeStartElement(namespaceURI, localName)}<br>112* <br>113* {@code writeEmptyElement(namespaceURI, localName)}</th>114* <!-- isRepairingNamespaces == true -->115* <td>116* <!-- namespaceURI bound -->117* {@code <prefix:localName>} <sup>[1]</sup>118* </td>119* <td>120* <!-- namespaceURI unbound -->121* {@code <{generated}:localName xmlns:{generated}="namespaceURI">}122* </td>123* <!-- isRepairingNamespaces == false -->124* <td>125* <!-- namespaceURI bound -->126* {@code prefix:localName>} <sup>[1]</sup>127* </td>128* <td>129* <!-- namespaceURI unbound -->130* {@code XMLStreamException}131* </td>132* </tr>133*134* <tr>135* <th scope="row">{@code writeStartElement(prefix, localName, namespaceURI)}<br>136* <br>137* {@code writeEmptyElement(prefix, localName, namespaceURI)}</th>138* <!-- isRepairingNamespaces == true -->139* <td>140* <!-- namespaceURI bound -->141* bound to same prefix:<br>142* {@code <prefix:localName>} <sup>[1]</sup><br>143* <br>144* bound to different prefix:<br>145* {@code <{generated}:localName xmlns:{generated}="namespaceURI">}146* </td>147* <td>148* <!-- namespaceURI unbound -->149* {@code <prefix:localName xmlns:prefix="namespaceURI">} <sup>[4]</sup>150* </td>151* <!-- isRepairingNamespaces == false -->152* <td>153* <!-- namespaceURI bound -->154* bound to same prefix:<br>155* {@code <prefix:localName>} <sup>[1]</sup><br>156* <br>157* bound to different prefix:<br>158* {@code XMLStreamException}159* </td>160* <td>161* <!-- namespaceURI unbound -->162* {@code <prefix:localName>} 163* </td>164* </tr>165* </tbody>166* </table>167168* Notes:169* <ul>170* <li>[1] if namespaceURI == default Namespace URI, then no prefix is written</li>171* <li>[2] if prefix == "" || null {@literal &&} namespaceURI == "", then172* no prefix or Namespace declaration is generated or written</li>173* <li>[3] if prefix == "" || null, then a prefix is randomly generated</li>174* <li>[4] if prefix == "" || null, then it is treated as the default Namespace and175* no prefix is generated or written, an xmlns declaration is generated176* and written if the namespaceURI is unbound</li>177* <li>[5] if prefix == "" || null, then it is treated as an invalid attempt to178* define the default Namespace and an XMLStreamException is thrown</li>179* </ul>180*181* @version 1.0182* @author Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.183* @see XMLOutputFactory184* @see XMLStreamReader185* @since 1.6186*/187public interface XMLStreamWriter {188189/**190* Writes a start tag to the output. All writeStartElement methods191* open a new scope in the internal namespace context. Writing the192* corresponding EndElement causes the scope to be closed.193* @param localName local name of the tag, may not be null194* @throws XMLStreamException if an error occurs195*/196public void writeStartElement(String localName)197throws XMLStreamException;198199/**200* Writes a start tag to the output201* @param namespaceURI the namespaceURI of the prefix to use, may not be null202* @param localName local name of the tag, may not be null203* @throws XMLStreamException if the namespace URI has not been bound to a prefix and204* javax.xml.stream.isRepairingNamespaces has not been set to true205*/206public void writeStartElement(String namespaceURI, String localName)207throws XMLStreamException;208209/**210* Writes a start tag to the output211* @param localName local name of the tag, may not be null212* @param prefix the prefix of the tag, may not be null213* @param namespaceURI the uri to bind the prefix to, may not be null214* @throws XMLStreamException if an error occurs215*/216public void writeStartElement(String prefix,217String localName,218String namespaceURI)219throws XMLStreamException;220221/**222* Writes an empty element tag to the output223* @param namespaceURI the uri to bind the tag to, may not be null224* @param localName local name of the tag, may not be null225* @throws XMLStreamException if the namespace URI has not been bound to a prefix and226* javax.xml.stream.isRepairingNamespaces has not been set to true227*/228public void writeEmptyElement(String namespaceURI, String localName)229throws XMLStreamException;230231/**232* Writes an empty element tag to the output233* @param prefix the prefix of the tag, may not be null234* @param localName local name of the tag, may not be null235* @param namespaceURI the uri to bind the tag to, may not be null236* @throws XMLStreamException if an error occurs237*/238public void writeEmptyElement(String prefix, String localName, String namespaceURI)239throws XMLStreamException;240241/**242* Writes an empty element tag to the output243* @param localName local name of the tag, may not be null244* @throws XMLStreamException if an error occurs245*/246public void writeEmptyElement(String localName)247throws XMLStreamException;248249/**250* Writes string data to the output without checking for well formedness.251* The data is opaque to the XMLStreamWriter, i.e. the characters are written252* blindly to the underlying output. If the method cannot be supported253* in the currrent writing context the implementation may throw a254* UnsupportedOperationException. For example note that any255* namespace declarations, end tags, etc. will be ignored and could256* interfere with proper maintanence of the writers internal state.257*258* @param data the data to write259*/260// public void writeRaw(String data) throws XMLStreamException;261262/**263* Writes an end tag to the output relying on the internal264* state of the writer to determine the prefix and local name265* of the event.266* @throws XMLStreamException if an error occurs267*/268public void writeEndElement()269throws XMLStreamException;270271/**272* Closes any start tags and writes corresponding end tags.273* @throws XMLStreamException if an error occurs274*/275public void writeEndDocument()276throws XMLStreamException;277278/**279* Close this writer and free any resources associated with the280* writer. This must not close the underlying output stream.281* @throws XMLStreamException if an error occurs282*/283public void close()284throws XMLStreamException;285286/**287* Write any cached data to the underlying output mechanism.288* @throws XMLStreamException if an error occurs289*/290public void flush()291throws XMLStreamException;292293/**294* Writes an attribute to the output stream without295* a prefix.296* @param localName the local name of the attribute297* @param value the value of the attribute298* @throws IllegalStateException if the current state does not allow Attribute writing299* @throws XMLStreamException if an error occurs300*/301public void writeAttribute(String localName, String value)302throws XMLStreamException;303304/**305* Writes an attribute to the output stream306* @param prefix the prefix for this attribute307* @param namespaceURI the uri of the prefix for this attribute308* @param localName the local name of the attribute309* @param value the value of the attribute310* @throws IllegalStateException if the current state does not allow Attribute writing311* @throws XMLStreamException if the namespace URI has not been bound to a prefix and312* javax.xml.stream.isRepairingNamespaces has not been set to true313*/314315public void writeAttribute(String prefix,316String namespaceURI,317String localName,318String value)319throws XMLStreamException;320321/**322* Writes an attribute to the output stream323* @param namespaceURI the uri of the prefix for this attribute324* @param localName the local name of the attribute325* @param value the value of the attribute326* @throws IllegalStateException if the current state does not allow Attribute writing327* @throws XMLStreamException if the namespace URI has not been bound to a prefix and328* javax.xml.stream.isRepairingNamespaces has not been set to true329*/330public void writeAttribute(String namespaceURI,331String localName,332String value)333throws XMLStreamException;334335/**336* Writes a namespace to the output stream337* If the prefix argument to this method is the empty string,338* "xmlns", or null this method will delegate to writeDefaultNamespace339*340* @param prefix the prefix to bind this namespace to341* @param namespaceURI the uri to bind the prefix to342* @throws IllegalStateException if the current state does not allow Namespace writing343* @throws XMLStreamException if an error occurs344*/345public void writeNamespace(String prefix, String namespaceURI)346throws XMLStreamException;347348/**349* Writes the default namespace to the stream350* @param namespaceURI the uri to bind the default namespace to351* @throws IllegalStateException if the current state does not allow Namespace writing352* @throws XMLStreamException if an error occurs353*/354public void writeDefaultNamespace(String namespaceURI)355throws XMLStreamException;356357/**358* Writes an xml comment with the data enclosed359* @param data the data contained in the comment, may be null360* @throws XMLStreamException if an error occurs361*/362public void writeComment(String data)363throws XMLStreamException;364365/**366* Writes a processing instruction367* @param target the target of the processing instruction, may not be null368* @throws XMLStreamException if an error occurs369*/370public void writeProcessingInstruction(String target)371throws XMLStreamException;372373/**374* Writes a processing instruction375* @param target the target of the processing instruction, may not be null376* @param data the data contained in the processing instruction, may not be null377* @throws XMLStreamException if an error occurs378*/379public void writeProcessingInstruction(String target,380String data)381throws XMLStreamException;382383/**384* Writes a CData section385* @param data the data contained in the CData Section, may not be null386* @throws XMLStreamException if an error occurs387*/388public void writeCData(String data)389throws XMLStreamException;390391/**392* Write a DTD section. This string represents the entire doctypedecl production393* from the XML 1.0 specification.394*395* @param dtd the DTD to be written396* @throws XMLStreamException if an error occurs397*/398public void writeDTD(String dtd)399throws XMLStreamException;400401/**402* Writes an entity reference403* @param name the name of the entity404* @throws XMLStreamException if an error occurs405*/406public void writeEntityRef(String name)407throws XMLStreamException;408409/**410* Write the XML Declaration. Defaults the XML version to 1.0, and the encoding to utf-8411* @throws XMLStreamException if an error occurs412*/413public void writeStartDocument()414throws XMLStreamException;415416/**417* Write the XML Declaration. Defaults the XML version to 1.0418* @param version version of the xml document419* @throws XMLStreamException if an error occurs420*/421public void writeStartDocument(String version)422throws XMLStreamException;423424/**425* Write the XML Declaration. Note that the encoding parameter does426* not set the actual encoding of the underlying output. That must427* be set when the instance of the XMLStreamWriter is created using the428* XMLOutputFactory429* @param encoding encoding of the xml declaration430* @param version version of the xml document431* @throws XMLStreamException If given encoding does not match encoding432* of the underlying stream433*/434public void writeStartDocument(String encoding,435String version)436throws XMLStreamException;437438/**439* Write text to the output440* @param text the value to write441* @throws XMLStreamException if an error occurs442*/443public void writeCharacters(String text)444throws XMLStreamException;445446/**447* Write text to the output448* @param text the value to write449* @param start the starting position in the array450* @param len the number of characters to write451* @throws XMLStreamException if an error occurs452*/453public void writeCharacters(char[] text, int start, int len)454throws XMLStreamException;455456/**457* Gets the prefix the uri is bound to.458* @param uri the uri the prefix is bound to459* @return the prefix or null460* @throws XMLStreamException if an error occurs461*/462public String getPrefix(String uri)463throws XMLStreamException;464465/**466* Sets the prefix the uri is bound to. This prefix is bound467* in the scope of the current START_ELEMENT / END_ELEMENT pair.468* If this method is called before a START_ELEMENT has been written469* the prefix is bound in the root scope.470* @param prefix the prefix to bind to the uri, may not be null471* @param uri the uri to bind to the prefix, may be null472* @throws XMLStreamException if an error occurs473*/474public void setPrefix(String prefix, String uri)475throws XMLStreamException;476477478/**479* Binds a URI to the default namespace480* This URI is bound481* in the scope of the current START_ELEMENT / END_ELEMENT pair.482* If this method is called before a START_ELEMENT has been written483* the uri is bound in the root scope.484* @param uri the uri to bind to the default namespace, may be null485* @throws XMLStreamException if an error occurs486*/487public void setDefaultNamespace(String uri)488throws XMLStreamException;489490/**491* Sets the current namespace context for prefix and uri bindings.492* This context becomes the root namespace context for writing and493* will replace the current root namespace context. Subsequent calls494* to setPrefix and setDefaultNamespace will bind namespaces using495* the context passed to the method as the root context for resolving496* namespaces. This method may only be called once at the start of497* the document. It does not cause the namespaces to be declared.498* If a namespace URI to prefix mapping is found in the namespace499* context it is treated as declared and the prefix may be used500* by the StreamWriter.501* @param context the namespace context to use for this writer, may not be null502* @throws XMLStreamException if an error occurs503*/504public void setNamespaceContext(NamespaceContext context)505throws XMLStreamException;506507/**508* Returns the current namespace context.509* @return the current NamespaceContext510*/511public NamespaceContext getNamespaceContext();512513/**514* Get the value of a feature/property from the underlying implementation515* @param name The name of the property, may not be null516* @return The value of the property517* @throws IllegalArgumentException if the property is not supported518* @throws NullPointerException if the name is null519*/520public Object getProperty(java.lang.String name) throws IllegalArgumentException;521522}523524525