Path: blob/jdk8u272-b10-aarch32-20201026/jaxp/src/javax/xml/stream/XMLEventWriter.java
48792 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.stream.events.*;31import javax.xml.stream.util.XMLEventConsumer;32import javax.xml.namespace.NamespaceContext;3334/**35*36* This is the top level interface for writing XML documents.37*38* Instances of this interface are not required to validate the39* form of the XML.40*41* @version 1.042* @author Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.43* @see XMLEventReader44* @see javax.xml.stream.events.XMLEvent45* @see javax.xml.stream.events.Characters46* @see javax.xml.stream.events.ProcessingInstruction47* @see javax.xml.stream.events.StartElement48* @see javax.xml.stream.events.EndElement49* @since 1.650*/51public interface XMLEventWriter extends XMLEventConsumer {5253/**54* Writes any cached events to the underlying output mechanism55* @throws XMLStreamException56*/57public void flush() throws XMLStreamException;5859/**60* Frees any resources associated with this stream61* @throws XMLStreamException62*/63public void close() throws XMLStreamException;6465/**66* Add an event to the output stream67* Adding a START_ELEMENT will open a new namespace scope that68* will be closed when the corresponding END_ELEMENT is written.69* <table border="2" rules="all" cellpadding="4">70* <thead>71* <tr>72* <th align="center" colspan="2">73* Required and optional fields for events added to the writer74* </th>75* </tr>76* </thead>77* <tbody>78* <tr>79* <th>Event Type</th>80* <th>Required Fields</th>81* <th>Optional Fields</th>82* <th>Required Behavior</th>83* </tr>84* <tr>85* <td> START_ELEMENT </td>86* <td> QName name </td>87* <td> namespaces , attributes </td>88* <td> A START_ELEMENT will be written by writing the name,89* namespaces, and attributes of the event in XML 1.0 valid90* syntax for START_ELEMENTs.91* The name is written by looking up the prefix for92* the namespace uri. The writer can be configured to93* respect prefixes of QNames. If the writer is respecting94* prefixes it must use the prefix set on the QName. The95* default behavior is to lookup the value for the prefix96* on the EventWriter's internal namespace context.97* Each attribute (if any)98* is written using the behavior specified in the attribute99* section of this table. Each namespace (if any) is written100* using the behavior specified in the namespace section of this101* table.102* </td>103* </tr>104* <tr>105* <td> END_ELEMENT </td>106* <td> Qname name </td>107* <td> None </td>108* <td> A well formed END_ELEMENT tag is written.109* The name is written by looking up the prefix for110* the namespace uri. The writer can be configured to111* respect prefixes of QNames. If the writer is respecting112* prefixes it must use the prefix set on the QName. The113* default behavior is to lookup the value for the prefix114* on the EventWriter's internal namespace context.115* If the END_ELEMENT name does not match the START_ELEMENT116* name an XMLStreamException is thrown.117* </td>118* </tr>119* <tr>120* <td> ATTRIBUTE </td>121* <td> QName name , String value </td>122* <td> QName type </td>123* <td> An attribute is written using the same algorithm124* to find the lexical form as used in START_ELEMENT.125* The default is to use double quotes to wrap attribute126* values and to escape any double quotes found in the127* value. The type value is ignored.128* </td>129* </tr>130* <tr>131* <td> NAMESPACE </td>132* <td> String prefix, String namespaceURI,133* boolean isDefaultNamespaceDeclaration134* </td>135* <td> None </td>136* <td> A namespace declaration is written. If the137* namespace is a default namespace declaration138* (isDefault is true) then xmlns="$namespaceURI"139* is written and the prefix is optional. If140* isDefault is false, the prefix must be declared141* and the writer must prepend xmlns to the prefix142* and write out a standard prefix declaration.143* </td>144* </tr>145* <tr>146* <td> PROCESSING_INSTRUCTION </td>147* <td> None</td>148* <td> String target, String data</td>149* <td> The data does not need to be present and may be150* null. Target is required and many not be null.151* The writer152* will write data section153* directly after the target,154* enclosed in appropriate XML 1.0 syntax155* </td>156* </tr>157* <tr>158* <td> COMMENT </td>159* <td> None </td>160* <td> String comment </td>161* <td> If the comment is present (not null) it is written, otherwise an162* an empty comment is written163* </td>164* </tr>165* <tr>166* <td> START_DOCUMENT </td>167* <td> None </td>168* <td> String encoding , boolean standalone, String version </td>169* <td> A START_DOCUMENT event is not required to be written to the170* stream. If present the attributes are written inside171* the appropriate XML declaration syntax172* </td>173* </tr>174* <tr>175* <td> END_DOCUMENT </td>176* <td> None </td>177* <td> None </td>178* <td> Nothing is written to the output </td>179* </tr>180* <tr>181* <td> DTD </td>182* <td> String DocumentTypeDefinition </td>183* <td> None </td>184* <td> The DocumentTypeDefinition is written to the output </td>185* </tr>186* </tbody>187* </table>188* @param event the event to be added189* @throws XMLStreamException190*/191public void add(XMLEvent event) throws XMLStreamException;192193/**194* Adds an entire stream to an output stream,195* calls next() on the inputStream argument until hasNext() returns false196* This should be treated as a convenience method that will197* perform the following loop over all the events in an198* event reader and call add on each event.199*200* @param reader the event stream to add to the output201* @throws XMLStreamException202*/203204public void add(XMLEventReader reader) throws XMLStreamException;205206/**207* Gets the prefix the uri is bound to208* @param uri the uri to look up209* @throws XMLStreamException210*/211public String getPrefix(String uri) throws XMLStreamException;212213/**214* Sets the prefix the uri is bound to. This prefix is bound215* in the scope of the current START_ELEMENT / END_ELEMENT pair.216* If this method is called before a START_ELEMENT has been written217* the prefix is bound in the root scope.218* @param prefix the prefix to bind to the uri219* @param uri the uri to bind to the prefix220* @throws XMLStreamException221*/222public void setPrefix(String prefix, String uri) throws XMLStreamException;223224/**225* Binds a URI to the default namespace226* This URI is bound227* in the scope of the current START_ELEMENT / END_ELEMENT pair.228* If this method is called before a START_ELEMENT has been written229* the uri is bound in the root scope.230* @param uri the uri to bind to the default namespace231* @throws XMLStreamException232*/233public void setDefaultNamespace(String uri) throws XMLStreamException;234235/**236* Sets the current namespace context for prefix and uri bindings.237* This context becomes the root namespace context for writing and238* will replace the current root namespace context. Subsequent calls239* to setPrefix and setDefaultNamespace will bind namespaces using240* the context passed to the method as the root context for resolving241* namespaces.242* @param context the namespace context to use for this writer243* @throws XMLStreamException244*/245public void setNamespaceContext(NamespaceContext context)246throws XMLStreamException;247248/**249* Returns the current namespace context.250* @return the current namespace context251*/252public NamespaceContext getNamespaceContext();253254255}256257258