< prev index next >

src/java.base/share/classes/jdk/internal/util/xml/impl/XMLStreamWriterImpl.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 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


  49     static final int ELEMENT_STARTTAG_OPEN = 10;
  50     static final int ELEMENT_STARTTAG_CLOSE = 11;
  51     static final int ELEMENT_ENDTAG_OPEN = 12;
  52     static final int ELEMENT_ENDTAG_CLOSE = 13;
  53     public static final char CLOSE_START_TAG = '>';
  54     public static final char OPEN_START_TAG = '<';
  55     public static final String OPEN_END_TAG = "</";
  56     public static final char CLOSE_END_TAG = '>';
  57     public static final String START_CDATA = "<![CDATA[";
  58     public static final String END_CDATA = "]]>";
  59     public static final String CLOSE_EMPTY_ELEMENT = "/>";
  60     public static final String ENCODING_PREFIX = "&#x";
  61     public static final char SPACE = ' ';
  62     public static final char AMPERSAND = '&';
  63     public static final char DOUBLEQUOT = '"';
  64     public static final char SEMICOLON = ';';
  65     //current state
  66     private int _state = 0;
  67     private Element _currentEle;
  68     private XMLWriter _writer;
  69     private String _encoding;
  70     /**
  71      * This flag can be used to turn escaping off for content. It does
  72      * not apply to attribute content.
  73      */
  74     boolean _escapeCharacters = true;
  75     //pretty print by default
  76     private boolean _doIndent = true;
  77     //The system line separator for writing out line breaks.
  78     private char[] _lineSep =
  79             System.getProperty("line.separator").toCharArray();
  80 
  81     public XMLStreamWriterImpl(OutputStream os) throws XMLStreamException {
  82         this(os, XMLStreamWriter.DEFAULT_ENCODING);
  83     }
  84 
  85     public XMLStreamWriterImpl(OutputStream os, String encoding)
  86         throws XMLStreamException
  87     {
  88         Charset cs = null;
  89         if (encoding == null) {
  90             _encoding = XMLStreamWriter.DEFAULT_ENCODING;
  91         } else {
  92             try {
  93                 cs = getCharset(encoding);
  94             } catch (UnsupportedEncodingException e) {
  95                 throw new XMLStreamException(e);
  96             }
  97 
  98             this._encoding = encoding;
  99         }
 100 
 101         _writer = new XMLWriter(os, encoding, cs);
 102     }
 103 
 104     /**
 105      * Write the XML Declaration. Defaults the XML version to 1.0, and the
 106      * encoding to utf-8.
 107      *
 108      * @throws XMLStreamException
 109      */
 110     public void writeStartDocument() throws XMLStreamException {
 111         writeStartDocument(_encoding, XMLStreamWriter.DEFAULT_XML_VERSION);
 112     }
 113 
 114     /**
 115      * Write the XML Declaration. Defaults the encoding to utf-8
 116      *
 117      * @param version version of the xml document
 118      * @throws XMLStreamException
 119      */
 120     public void writeStartDocument(String version) throws XMLStreamException {
 121         writeStartDocument(_encoding, version, null);
 122     }
 123 
 124     /**
 125      * Write the XML Declaration. Note that the encoding parameter does not set
 126      * the actual encoding of the underlying output. That must be set when the
 127      * instance of the XMLStreamWriter is created
 128      *
 129      * @param encoding encoding of the xml declaration
 130      * @param version version of the xml document
 131      * @throws XMLStreamException If given encoding does not match encoding of the
 132      * underlying stream
 133      */
 134     public void writeStartDocument(String encoding, String version) throws XMLStreamException {
 135         writeStartDocument(encoding, version, null);
 136     }
 137 
 138     /**
 139      * Write the XML Declaration. Note that the encoding parameter does not set
 140      * the actual encoding of the underlying output. That must be set when the
 141      * instance of the XMLStreamWriter is created
 142      *
 143      * @param encoding encoding of the xml declaration
 144      * @param version version of the xml document
 145      * @param standalone indicate if the xml document is standalone
 146      * @throws XMLStreamException If given encoding does not match encoding of the
 147      * underlying stream
 148      */
 149     public void writeStartDocument(String encoding, String version, String standalone)
 150         throws XMLStreamException
 151     {
 152         if (_state > 0) {
 153             throw new XMLStreamException("XML declaration must be as the first line in the XML document.");
 154         }
 155         _state = STATE_XML_DECL;
 156         String enc = encoding;
 157         if (enc == null) {
 158             enc = _encoding;
 159         } else {
 160             //check if the encoding is supported
 161             try {
 162                 getCharset(encoding);
 163             } catch (UnsupportedEncodingException e) {
 164                 throw new XMLStreamException(e);
 165             }
 166         }
 167 
 168         if (version == null) {
 169             version = XMLStreamWriter.DEFAULT_XML_VERSION;
 170         }
 171 
 172         _writer.write("<?xml version=\"");
 173         _writer.write(version);
 174         _writer.write(DOUBLEQUOT);
 175 
 176         if (enc != null) {
 177             _writer.write(" encoding=\"");
 178             _writer.write(enc);


 545 
 546     /**
 547      * Returns a charset object for the specified encoding
 548      * @param encoding
 549      * @return a charset object
 550      * @throws UnsupportedEncodingException if the encoding is not supported
 551      */
 552     private Charset getCharset(String encoding) throws UnsupportedEncodingException {
 553         if (encoding.equalsIgnoreCase("UTF-32")) {
 554             throw new UnsupportedEncodingException("The basic XMLWriter does "
 555                     + "not support " + encoding);
 556         }
 557 
 558         Charset cs;
 559         try {
 560             cs = Charset.forName(encoding);
 561         } catch (IllegalCharsetNameException | UnsupportedCharsetException ex) {
 562             throw new UnsupportedEncodingException(encoding);
 563         }
 564         return cs;














 565     }
 566 
 567     /*
 568      * Start of Internal classes.
 569      *
 570      */
 571     protected class Element {
 572 
 573         /**
 574          * the parent element
 575          */
 576         protected Element _parent;
 577         /**
 578          * The size of the stack.
 579          */
 580         protected short _Depth;
 581         /**
 582          * indicate if an element is an empty one
 583          */
 584         boolean _isEmptyElement = false;


   1 /*
   2  * Copyright (c) 2012, 2017, 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


  49     static final int ELEMENT_STARTTAG_OPEN = 10;
  50     static final int ELEMENT_STARTTAG_CLOSE = 11;
  51     static final int ELEMENT_ENDTAG_OPEN = 12;
  52     static final int ELEMENT_ENDTAG_CLOSE = 13;
  53     public static final char CLOSE_START_TAG = '>';
  54     public static final char OPEN_START_TAG = '<';
  55     public static final String OPEN_END_TAG = "</";
  56     public static final char CLOSE_END_TAG = '>';
  57     public static final String START_CDATA = "<![CDATA[";
  58     public static final String END_CDATA = "]]>";
  59     public static final String CLOSE_EMPTY_ELEMENT = "/>";
  60     public static final String ENCODING_PREFIX = "&#x";
  61     public static final char SPACE = ' ';
  62     public static final char AMPERSAND = '&';
  63     public static final char DOUBLEQUOT = '"';
  64     public static final char SEMICOLON = ';';
  65     //current state
  66     private int _state = 0;
  67     private Element _currentEle;
  68     private XMLWriter _writer;
  69     private Charset _charset;
  70     /**
  71      * This flag can be used to turn escaping off for content. It does
  72      * not apply to attribute content.
  73      */
  74     boolean _escapeCharacters = true;
  75     //pretty print by default
  76     private boolean _doIndent = true;
  77     //The system line separator for writing out line breaks.
  78     private char[] _lineSep =
  79             System.getProperty("line.separator").toCharArray();
  80 
  81     public XMLStreamWriterImpl(OutputStream os) throws XMLStreamException {
  82         this(os, XMLStreamWriter.DEFAULT_CHARSET);
  83     }
  84 
  85     public XMLStreamWriterImpl(OutputStream os, Charset cs)
  86         throws XMLStreamException
  87     {
  88         if (cs == null) {
  89             _charset = XMLStreamWriter.DEFAULT_CHARSET;

  90         } else {
  91             try {
  92                 _charset = checkCharset(cs);
  93             } catch (UnsupportedEncodingException e) {
  94                 throw new XMLStreamException(e);
  95             }


  96         }
  97 
  98         _writer = new XMLWriter(os, null, _charset);
  99     }
 100 
 101     /**
 102      * Write the XML Declaration. Defaults the XML version to 1.0, and the
 103      * encoding to utf-8.
 104      *
 105      * @throws XMLStreamException
 106      */
 107     public void writeStartDocument() throws XMLStreamException {
 108         writeStartDocument(_charset.name(), XMLStreamWriter.DEFAULT_XML_VERSION);
 109     }
 110 
 111     /**
 112      * Write the XML Declaration. Defaults the encoding to utf-8
 113      *
 114      * @param version version of the xml document
 115      * @throws XMLStreamException
 116      */
 117     public void writeStartDocument(String version) throws XMLStreamException {
 118         writeStartDocument(_charset.name(), version, null);
 119     }
 120 
 121     /**
 122      * Write the XML Declaration. Note that the encoding parameter does not set
 123      * the actual encoding of the underlying output. That must be set when the
 124      * instance of the XMLStreamWriter is created
 125      *
 126      * @param encoding encoding of the xml declaration
 127      * @param version version of the xml document
 128      * @throws XMLStreamException If given encoding does not match encoding of the
 129      * underlying stream
 130      */
 131     public void writeStartDocument(String encoding, String version) throws XMLStreamException {
 132         writeStartDocument(encoding, version, null);
 133     }
 134 
 135     /**
 136      * Write the XML Declaration. Note that the encoding parameter does not set
 137      * the actual encoding of the underlying output. That must be set when the
 138      * instance of the XMLStreamWriter is created
 139      *
 140      * @param encoding encoding of the xml declaration
 141      * @param version version of the xml document
 142      * @param standalone indicate if the xml document is standalone
 143      * @throws XMLStreamException If given encoding does not match encoding of the
 144      * underlying stream
 145      */
 146     public void writeStartDocument(String encoding, String version, String standalone)
 147         throws XMLStreamException
 148     {
 149         if (_state > 0) {
 150             throw new XMLStreamException("XML declaration must be as the first line in the XML document.");
 151         }
 152         _state = STATE_XML_DECL;
 153         String enc = encoding;
 154         if (enc == null) {
 155             enc = _charset.name();
 156         } else {
 157             //check if the encoding is supported
 158             try {
 159                 getCharset(encoding);
 160             } catch (UnsupportedEncodingException e) {
 161                 throw new XMLStreamException(e);
 162             }
 163         }
 164 
 165         if (version == null) {
 166             version = XMLStreamWriter.DEFAULT_XML_VERSION;
 167         }
 168 
 169         _writer.write("<?xml version=\"");
 170         _writer.write(version);
 171         _writer.write(DOUBLEQUOT);
 172 
 173         if (enc != null) {
 174             _writer.write(" encoding=\"");
 175             _writer.write(enc);


 542 
 543     /**
 544      * Returns a charset object for the specified encoding
 545      * @param encoding
 546      * @return a charset object
 547      * @throws UnsupportedEncodingException if the encoding is not supported
 548      */
 549     private Charset getCharset(String encoding) throws UnsupportedEncodingException {
 550         if (encoding.equalsIgnoreCase("UTF-32")) {
 551             throw new UnsupportedEncodingException("The basic XMLWriter does "
 552                     + "not support " + encoding);
 553         }
 554 
 555         Charset cs;
 556         try {
 557             cs = Charset.forName(encoding);
 558         } catch (IllegalCharsetNameException | UnsupportedCharsetException ex) {
 559             throw new UnsupportedEncodingException(encoding);
 560         }
 561         return cs;
 562     }
 563 
 564     /**
 565      * Checks for charset support.
 566      * @param charset the specified charset
 567      * @return the charset
 568      * @throws UnsupportedEncodingException if the charset is not supported
 569      */
 570     private Charset checkCharset(Charset charset) throws UnsupportedEncodingException {
 571         if (charset.name().equalsIgnoreCase("UTF-32")) {
 572             throw new UnsupportedEncodingException("The basic XMLWriter does "
 573                     + "not support " + charset.name());
 574         }
 575         return charset;
 576     }
 577 
 578     /*
 579      * Start of Internal classes.
 580      *
 581      */
 582     protected class Element {
 583 
 584         /**
 585          * the parent element
 586          */
 587         protected Element _parent;
 588         /**
 589          * The size of the stack.
 590          */
 591         protected short _Depth;
 592         /**
 593          * indicate if an element is an empty one
 594          */
 595         boolean _isEmptyElement = false;


< prev index next >