< prev index next >

src/java.xml/share/classes/com/sun/xml/internal/stream/writers/XMLStreamWriterImpl.java

Print this page

        

@@ -69,11 +69,11 @@
  * @author Neeraj Bajaj
  * @author K.Venugopal
  * @author Santiago.Pericas-Geertsen@sun.com
  * @author Sunitha.Reddy@sun.com
  */
-public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamWriter {
+public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamWriterBase {
 
     public static final String START_COMMENT = "<!--";
     public static final String END_COMMENT = "-->";
     public static final String DEFAULT_ENCODING = " encoding=\"utf-8\"";
     public static final String DEFAULT_XMLDECL = "<?xml version=\"1.0\" ?>";

@@ -1110,63 +1110,95 @@
             throw new XMLStreamException(e);
         }
     }
 
     /**
-     * @throws XMLStreamException
+     * Writes the XML declaration.
+     *
+     * @throws XMLStreamException in case of an IOException
      */
     public void writeStartDocument() throws XMLStreamException {
-        try {
-            fWriter.write(DEFAULT_XMLDECL);
-        } catch (IOException ex) {
-            throw new XMLStreamException(ex);
-        }
+        writeStartDocument(null, null, false, false);
     }
 
     /**
-     * @param version
-     * @throws XMLStreamException
+     * Writes the XML declaration.
+     *
+     * @param version the specified version
+     * @throws XMLStreamException in case of an IOException
      */
     public void writeStartDocument(String version) throws XMLStreamException {
-        try {
-            if ((version == null) || version.equals("")) {
-                writeStartDocument();
+        writeStartDocument(null, version, false, false);
+    }
+
+    /**
+     * Writes the XML declaration.
+     *
+     * @param encoding the specified encoding
+     * @param version the specified version
+     * @throws XMLStreamException in case of an IOException
+     */
+    @Override
+    public void writeStartDocument(String encoding, String version)
+        throws XMLStreamException {
+        writeStartDocument(encoding, version, false, false);
+    }
+
+    @Override
+    public void writeStartDocument(String encoding, String version,
+            boolean standalone, boolean standaloneSet)
+        throws XMLStreamException {
 
+        try {
+            if ((encoding == null || encoding.length() == 0)
+                    && (version == null || version.length() == 0)
+                    && (!standaloneSet)) {
+                fWriter.write(DEFAULT_XMLDECL);
                 return;
             }
 
+            // Verify the encoding before writing anything
+            if (encoding != null && !encoding.equals("")) {
+                verifyEncoding(encoding);
+            }
+
             fWriter.write("<?xml version=\"");
+
+            if ((version == null) || version.equals("")) {
+                fWriter.write(DEFAULT_XML_VERSION);
+            } else {
             fWriter.write(version);
-            fWriter.write("\"");
+            }
 
-            //fWriter.write(DEFAULT_ENCODING);
-            fWriter.write("?>");
+            if (encoding != null && !encoding.equals("")) {
+                fWriter.write("\" encoding=\"");
+                fWriter.write(encoding);
+            }
+
+            if (standaloneSet) {
+                fWriter.write("\" standalone=\"");
+                if (standalone) {
+                    fWriter.write("yes");
+                } else {
+                    fWriter.write("no");
+                }
+            }
+
+            fWriter.write("\"?>");
         } catch (IOException ex) {
             throw new XMLStreamException(ex);
         }
     }
 
     /**
-     * @param encoding
-     * @param version
-     * @throws XMLStreamException
+     * Verifies that the encoding is consistent between the underlying encoding
+     * and that specified.
+     *
+     * @param encoding the specified encoding
+     * @throws XMLStreamException if they do not match
      */
-    public void writeStartDocument(String encoding, String version)
-        throws XMLStreamException {
-        //Revisit : What about standalone ?
-        try {
-            if ((encoding == null) && (version == null)) {
-                writeStartDocument();
-
-                return;
-            }
-
-            if (encoding == null) {
-                writeStartDocument(version);
-
-                return;
-            }
+    private void verifyEncoding(String encoding) throws XMLStreamException {
 
             String streamEncoding = null;
             if (fWriter instanceof OutputStreamWriter) {
                 streamEncoding = ((OutputStreamWriter) fWriter).getEncoding();
             }

@@ -1192,29 +1224,10 @@
                             + streamEncoding
                             + "' and input paramter for writeStartDocument() method '"
                             + encoding + "' do not match.");
                 }
             }
-
-
-            fWriter.write("<?xml version=\"");
-
-            if ((version == null) || version.equals("")) {
-                fWriter.write(DEFAULT_XML_VERSION);
-            } else {
-                fWriter.write(version);
-            }
-
-            if (!encoding.equals("")) {
-                fWriter.write("\" encoding=\"");
-                fWriter.write(encoding);
-            }
-
-            fWriter.write("\"?>");
-        } catch (IOException ex) {
-            throw new XMLStreamException(ex);
-        }
     }
 
     /**
      * @param localName
      * @throws XMLStreamException
< prev index next >