Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/stream/XMLStreamWriter.java
48534 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.26*/2728package javax.xml.stream;2930import javax.xml.namespace.NamespaceContext;3132/**33* The XMLStreamWriter interface specifies how to write XML. The XMLStreamWriter does34* not perform well formedness checking on its input. However35* the writeCharacters method is required to escape & , < and >36* For attribute values the writeAttribute method will escape the37* above characters plus " to ensure that all character content38* and attribute values are well formed.39*40* Each NAMESPACE41* and ATTRIBUTE must be individually written.42*43* <table border="1" cellpadding="2" cellspacing="0">44* <thead>45* <tr>46* <th colspan="5">XML Namespaces, <code>javax.xml.stream.isRepairingNamespaces</code> and write method behaviour</th>47* </tr>48* <tr>49* <th>Method</th> <!-- method -->50* <th colspan="2"><code>isRepairingNamespaces</code> == true</th>51* <th colspan="2"><code>isRepairingNamespaces</code> == false</th>52* </tr>53* <tr>54* <th></th> <!-- method -->55* <th>namespaceURI bound</th>56* <th>namespaceURI unbound</th>57* <th>namespaceURI bound</th>58* <th>namespaceURI unbound</th>59* </tr>60* </thead>61*62* <tbody>63* <tr>64* <th><code>writeAttribute(namespaceURI, localName, value)</code></th>65* <!-- isRepairingNamespaces == true -->66* <td>67* <!-- namespaceURI bound -->68* prefix:localName="value" <sup>[1]</sup>69* </td>70* <td>71* <!-- namespaceURI unbound -->72* xmlns:{generated}="namespaceURI" {generated}:localName="value"73* </td>74* <!-- isRepairingNamespaces == false -->75* <td>76* <!-- namespaceURI bound -->77* prefix:localName="value" <sup>[1]</sup>78* </td>79* <td>80* <!-- namespaceURI unbound -->81* <code>XMLStreamException</code>82* </td>83* </tr>84*85* <tr>86* <th><code>writeAttribute(prefix, namespaceURI, localName, value)</code></th>87* <!-- isRepairingNamespaces == true -->88* <td>89* <!-- namespaceURI bound -->90* bound to same prefix:<br />91* prefix:localName="value" <sup>[1]</sup><br />92* <br />93* bound to different prefix:<br />94* xmlns:{generated}="namespaceURI" {generated}:localName="value"95* </td>96* <td>97* <!-- namespaceURI unbound -->98* xmlns:prefix="namespaceURI" prefix:localName="value" <sup>[3]</sup>99* </td>100* <!-- isRepairingNamespaces == false -->101* <td>102* <!-- namespaceURI bound -->103* bound to same prefix:<br />104* prefix:localName="value" <sup>[1][2]</sup><br />105* <br />106* bound to different prefix:<br />107* <code>XMLStreamException</code><sup>[2]</sup>108* </td>109* <td>110* <!-- namespaceURI unbound -->111* xmlns:prefix="namespaceURI" prefix:localName="value" <sup>[2][5]</sup>112* </td>113* </tr>114*115* <tr>116* <th><code>writeStartElement(namespaceURI, localName)</code><br />117* <br />118* <code>writeEmptyElement(namespaceURI, localName)</code></th>119* <!-- isRepairingNamespaces == true -->120* <td >121* <!-- namespaceURI bound -->122* <prefix:localName> <sup>[1]</sup>123* </td>124* <td>125* <!-- namespaceURI unbound -->126* <{generated}:localName xmlns:{generated}="namespaceURI">127* </td>128* <!-- isRepairingNamespaces == false -->129* <td>130* <!-- namespaceURI bound -->131* <prefix:localName> <sup>[1]</sup>132* </td>133* <td>134* <!-- namespaceURI unbound -->135* <code>XMLStreamException</code>136* </td>137* </tr>138*139* <tr>140* <th><code>writeStartElement(prefix, localName, namespaceURI)</code><br />141* <br />142* <code>writeEmptyElement(prefix, localName, namespaceURI)</code></th>143* <!-- isRepairingNamespaces == true -->144* <td>145* <!-- namespaceURI bound -->146* bound to same prefix:<br />147* <prefix:localName> <sup>[1]</sup><br />148* <br />149* bound to different prefix:<br />150* <{generated}:localName xmlns:{generated}="namespaceURI">151* </td>152* <td>153* <!-- namespaceURI unbound -->154* <prefix:localName xmlns:prefix="namespaceURI"> <sup>[4]</sup>155* </td>156* <!-- isRepairingNamespaces == false -->157* <td>158* <!-- namespaceURI bound -->159* bound to same prefix:<br />160* <prefix:localName> <sup>[1]</sup><br />161* <br />162* bound to different prefix:<br />163* <code>XMLStreamException</code>164* </td>165* <td>166* <!-- namespaceURI unbound -->167* <prefix:localName> 168* </td>169* </tr>170* </tbody>171* <tfoot>172* <tr>173* <td colspan="5">174* Notes:175* <ul>176* <li>[1] if namespaceURI == default Namespace URI, then no prefix is written</li>177* <li>[2] if prefix == "" || null && namespaceURI == "", then no prefix or Namespace declaration is generated or written</li>178* <li>[3] if prefix == "" || null, then a prefix is randomly generated</li>179* <li>[4] if prefix == "" || null, then it is treated as the default Namespace and no prefix is generated or written, an xmlns declaration is generated and written if the namespaceURI is unbound</li>180* <li>[5] if prefix == "" || null, then it is treated as an invalid attempt to define the default Namespace and an XMLStreamException is thrown</li>181* </ul>182* </td>183* </tr>184* </tfoot>185* </table>186*187* @version 1.0188* @author Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.189* @see XMLOutputFactory190* @see XMLStreamReader191* @since 1.6192*/193public interface XMLStreamWriter {194195/**196* Writes a start tag to the output. All writeStartElement methods197* open a new scope in the internal namespace context. Writing the198* corresponding EndElement causes the scope to be closed.199* @param localName local name of the tag, may not be null200* @throws XMLStreamException201*/202public void writeStartElement(String localName)203throws XMLStreamException;204205/**206* Writes a start tag to the output207* @param namespaceURI the namespaceURI of the prefix to use, may not be null208* @param localName local name of the tag, may not be null209* @throws XMLStreamException if the namespace URI has not been bound to a prefix and210* javax.xml.stream.isRepairingNamespaces has not been set to true211*/212public void writeStartElement(String namespaceURI, String localName)213throws XMLStreamException;214215/**216* Writes a start tag to the output217* @param localName local name of the tag, may not be null218* @param prefix the prefix of the tag, may not be null219* @param namespaceURI the uri to bind the prefix to, may not be null220* @throws XMLStreamException221*/222public void writeStartElement(String prefix,223String localName,224String namespaceURI)225throws XMLStreamException;226227/**228* Writes an empty element tag to the output229* @param namespaceURI the uri to bind the tag to, may not be null230* @param localName local name of the tag, may not be null231* @throws XMLStreamException if the namespace URI has not been bound to a prefix and232* javax.xml.stream.isRepairingNamespaces has not been set to true233*/234public void writeEmptyElement(String namespaceURI, String localName)235throws XMLStreamException;236237/**238* Writes an empty element tag to the output239* @param prefix the prefix of the tag, may not be null240* @param localName local name of the tag, may not be null241* @param namespaceURI the uri to bind the tag to, may not be null242* @throws XMLStreamException243*/244public void writeEmptyElement(String prefix, String localName, String namespaceURI)245throws XMLStreamException;246247/**248* Writes an empty element tag to the output249* @param localName local name of the tag, may not be null250* @throws XMLStreamException251*/252public void writeEmptyElement(String localName)253throws XMLStreamException;254255/**256* Writes string data to the output without checking for well formedness.257* The data is opaque to the XMLStreamWriter, i.e. the characters are written258* blindly to the underlying output. If the method cannot be supported259* in the currrent writing context the implementation may throw a260* UnsupportedOperationException. For example note that any261* namespace declarations, end tags, etc. will be ignored and could262* interfere with proper maintanence of the writers internal state.263*264* @param data the data to write265*/266// public void writeRaw(String data) throws XMLStreamException;267268/**269* Writes an end tag to the output relying on the internal270* state of the writer to determine the prefix and local name271* of the event.272* @throws XMLStreamException273*/274public void writeEndElement()275throws XMLStreamException;276277/**278* Closes any start tags and writes corresponding end tags.279* @throws XMLStreamException280*/281public void writeEndDocument()282throws XMLStreamException;283284/**285* Close this writer and free any resources associated with the286* writer. This must not close the underlying output stream.287* @throws XMLStreamException288*/289public void close()290throws XMLStreamException;291292/**293* Write any cached data to the underlying output mechanism.294* @throws XMLStreamException295*/296public void flush()297throws XMLStreamException;298299/**300* Writes an attribute to the output stream without301* a prefix.302* @param localName the local name of the attribute303* @param value the value of the attribute304* @throws IllegalStateException if the current state does not allow Attribute writing305* @throws XMLStreamException306*/307public void writeAttribute(String localName, String value)308throws XMLStreamException;309310/**311* Writes an attribute to the output stream312* @param prefix the prefix for this attribute313* @param namespaceURI the uri of the prefix for this attribute314* @param localName the local name of the attribute315* @param value the value of the attribute316* @throws IllegalStateException if the current state does not allow Attribute writing317* @throws XMLStreamException if the namespace URI has not been bound to a prefix and318* javax.xml.stream.isRepairingNamespaces has not been set to true319*/320321public void writeAttribute(String prefix,322String namespaceURI,323String localName,324String value)325throws XMLStreamException;326327/**328* Writes an attribute to the output stream329* @param namespaceURI the uri of the prefix for this attribute330* @param localName the local name of the attribute331* @param value the value of the attribute332* @throws IllegalStateException if the current state does not allow Attribute writing333* @throws XMLStreamException if the namespace URI has not been bound to a prefix and334* javax.xml.stream.isRepairingNamespaces has not been set to true335*/336public void writeAttribute(String namespaceURI,337String localName,338String value)339throws XMLStreamException;340341/**342* Writes a namespace to the output stream343* If the prefix argument to this method is the empty string,344* "xmlns", or null this method will delegate to writeDefaultNamespace345*346* @param prefix the prefix to bind this namespace to347* @param namespaceURI the uri to bind the prefix to348* @throws IllegalStateException if the current state does not allow Namespace writing349* @throws XMLStreamException350*/351public void writeNamespace(String prefix, String namespaceURI)352throws XMLStreamException;353354/**355* Writes the default namespace to the stream356* @param namespaceURI the uri to bind the default namespace to357* @throws IllegalStateException if the current state does not allow Namespace writing358* @throws XMLStreamException359*/360public void writeDefaultNamespace(String namespaceURI)361throws XMLStreamException;362363/**364* Writes an xml comment with the data enclosed365* @param data the data contained in the comment, may be null366* @throws XMLStreamException367*/368public void writeComment(String data)369throws XMLStreamException;370371/**372* Writes a processing instruction373* @param target the target of the processing instruction, may not be null374* @throws XMLStreamException375*/376public void writeProcessingInstruction(String target)377throws XMLStreamException;378379/**380* Writes a processing instruction381* @param target the target of the processing instruction, may not be null382* @param data the data contained in the processing instruction, may not be null383* @throws XMLStreamException384*/385public void writeProcessingInstruction(String target,386String data)387throws XMLStreamException;388389/**390* Writes a CData section391* @param data the data contained in the CData Section, may not be null392* @throws XMLStreamException393*/394public void writeCData(String data)395throws XMLStreamException;396397/**398* Write a DTD section. This string represents the entire doctypedecl production399* from the XML 1.0 specification.400*401* @param dtd the DTD to be written402* @throws XMLStreamException403*/404public void writeDTD(String dtd)405throws XMLStreamException;406407/**408* Writes an entity reference409* @param name the name of the entity410* @throws XMLStreamException411*/412public void writeEntityRef(String name)413throws XMLStreamException;414415/**416* Write the XML Declaration. Defaults the XML version to 1.0, and the encoding to utf-8417* @throws XMLStreamException418*/419public void writeStartDocument()420throws XMLStreamException;421422/**423* Write the XML Declaration. Defaults the XML version to 1.0424* @param version version of the xml document425* @throws XMLStreamException426*/427public void writeStartDocument(String version)428throws XMLStreamException;429430/**431* Write the XML Declaration. Note that the encoding parameter does432* not set the actual encoding of the underlying output. That must433* be set when the instance of the XMLStreamWriter is created using the434* XMLOutputFactory435* @param encoding encoding of the xml declaration436* @param version version of the xml document437* @throws XMLStreamException If given encoding does not match encoding438* of the underlying stream439*/440public void writeStartDocument(String encoding,441String version)442throws XMLStreamException;443444/**445* Write text to the output446* @param text the value to write447* @throws XMLStreamException448*/449public void writeCharacters(String text)450throws XMLStreamException;451452/**453* Write text to the output454* @param text the value to write455* @param start the starting position in the array456* @param len the number of characters to write457* @throws XMLStreamException458*/459public void writeCharacters(char[] text, int start, int len)460throws XMLStreamException;461462/**463* Gets the prefix the uri is bound to464* @return the prefix or null465* @throws XMLStreamException466*/467public String getPrefix(String uri)468throws XMLStreamException;469470/**471* Sets the prefix the uri is bound to. This prefix is bound472* in the scope of the current START_ELEMENT / END_ELEMENT pair.473* If this method is called before a START_ELEMENT has been written474* the prefix is bound in the root scope.475* @param prefix the prefix to bind to the uri, may not be null476* @param uri the uri to bind to the prefix, may be null477* @throws XMLStreamException478*/479public void setPrefix(String prefix, String uri)480throws XMLStreamException;481482483/**484* Binds a URI to the default namespace485* This URI is bound486* in the scope of the current START_ELEMENT / END_ELEMENT pair.487* If this method is called before a START_ELEMENT has been written488* the uri is bound in the root scope.489* @param uri the uri to bind to the default namespace, may be null490* @throws XMLStreamException491*/492public void setDefaultNamespace(String uri)493throws XMLStreamException;494495/**496* Sets the current namespace context for prefix and uri bindings.497* This context becomes the root namespace context for writing and498* will replace the current root namespace context. Subsequent calls499* to setPrefix and setDefaultNamespace will bind namespaces using500* the context passed to the method as the root context for resolving501* namespaces. This method may only be called once at the start of502* the document. It does not cause the namespaces to be declared.503* If a namespace URI to prefix mapping is found in the namespace504* context it is treated as declared and the prefix may be used505* by the StreamWriter.506* @param context the namespace context to use for this writer, may not be null507* @throws XMLStreamException508*/509public void setNamespaceContext(NamespaceContext context)510throws XMLStreamException;511512/**513* Returns the current namespace context.514* @return the current NamespaceContext515*/516public NamespaceContext getNamespaceContext();517518/**519* Get the value of a feature/property from the underlying implementation520* @param name The name of the property, may not be null521* @return The value of the property522* @throws IllegalArgumentException if the property is not supported523* @throws NullPointerException if the name is null524*/525public Object getProperty(java.lang.String name) throws IllegalArgumentException;526527}528529530