jaxp/src/com/sun/org/apache/xml/internal/serialize/XMLSerializer.java
changeset 23777 ce87cedb71cf
parent 12457 c348e06f0e82
child 25834 aba3efbf4ec5
--- a/jaxp/src/com/sun/org/apache/xml/internal/serialize/XMLSerializer.java	Tue Mar 25 14:51:51 2014 -0700
+++ b/jaxp/src/com/sun/org/apache/xml/internal/serialize/XMLSerializer.java	Mon Mar 31 19:03:41 2014 +0400
@@ -3,11 +3,12 @@
  * DO NOT REMOVE OR ALTER!
  */
 /*
- * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
@@ -18,8 +19,6 @@
  * limitations under the License.
  */
 
-
-
 // Sep 14, 2000:
 //  Fixed problem with namespace handling. Contributed by
 //  David Blondeau <blondeau@intalio.com>
@@ -33,14 +32,13 @@
 // Aug 21, 2000:
 //  Added ability to omit DOCTYPE declaration.
 
-
 package com.sun.org.apache.xml.internal.serialize;
 
-
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.Writer;
-import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Map;
 
 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
 import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
@@ -50,6 +48,7 @@
 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
 import org.w3c.dom.Attr;
 import org.w3c.dom.DOMError;
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
@@ -71,9 +70,9 @@
  * The serializer supports both DOM and SAX. SAX serializing is done by firing
  * SAX events and using the serializer as a document handler. DOM serializing is done
  * by calling {@link #serialize(Document)} or by using DOM Level 3
- * {@link org.w3c.dom.ls.DOMSerializer} and
- * serializing with {@link org.w3c.dom.ls.DOMSerializer#write},
- * {@link org.w3c.dom.ls.DOMSerializer#writeToString}.
+ * {@link org.w3c.dom.ls.LSSerializer} and
+ * serializing with {@link org.w3c.dom.ls.LSSerializer#write},
+ * {@link org.w3c.dom.ls.LSSerializer#writeToString}.
  * <p>
  * If an I/O exception occurs while serializing, the serializer
  * will not throw an exception directly, but only throw it
@@ -195,7 +194,7 @@
     /**
      * This methods turns on namespace fixup algorithm during
      * DOM serialization.
-     * @see org.w3c.dom.ls.DOMSerializer
+     * @see org.w3c.dom.ls.LSSerializer
      *
      * @param namespaces
      */
@@ -222,7 +221,6 @@
         ElementState state;
         String       name;
         String       value;
-        boolean      addNSAttr = false;
 
         if (DEBUG) {
             System.out.println("==>startElement("+namespaceURI+","+localName+
@@ -277,13 +275,16 @@
                 if (namespaceURI != null && ! namespaceURI.equals( "" )) {
                     String prefix;
                     prefix = getPrefix( namespaceURI );
-                    if (prefix != null && prefix.length() > 0)
+                    if (prefix != null && prefix.length() > 0) {
                         rawName = prefix + ":" + localName;
-                    else
+                    }
+                    else {
                         rawName = localName;
-                } else
+                    }
+                }
+                else {
                     rawName = localName;
-                addNSAttr = true;
+                }
             }
 
             _printer.printText( '<' );
@@ -334,18 +335,18 @@
             }
 
             if (_prefixes != null) {
-                Enumeration keys;
-
-                keys = _prefixes.keys();
-                while (keys.hasMoreElements()) {
+                Iterator entries = _prefixes.entrySet().iterator();
+                while (entries.hasNext()) {
                     _printer.printSpace();
-                    value = (String) keys.nextElement();
-                    name = (String) _prefixes.get( value );
+                    Map.Entry entry = (Map.Entry) entries.next();
+                    value = (String) entry.getKey();
+                    name = (String) entry.getValue();
                     if (name.length() == 0) {
                         _printer.printText( "xmlns=\"" );
                         printEscaped( value );
                         _printer.printText( '"' );
-                    } else {
+                    }
+                    else {
                         _printer.printText( "xmlns:" );
                         _printer.printText( name );
                         _printer.printText( "=\"" );
@@ -770,13 +771,11 @@
                                 //          xmlns:foo = ""
                             }
                             continue;
-                        } else { // xmlns
-                            // empty prefix is always bound ("" or some string)
-
-                            value = fSymbolTable.addSymbol(value);
-                            fNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, value);
-                            continue;
                         }
+                        // xmlns --- empty prefix is always bound ("" or some string)
+                        value = fSymbolTable.addSymbol(value);
+                        fNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, value);
+                        continue;
                     }  // end-else: valid declaration
                 } // end-if: namespace declaration
             }  // end-for
@@ -958,22 +957,20 @@
                                 //          xmlns:foo = ""
                             }
                             continue;
-                        } else { // xmlns
-                            // empty prefix is always bound ("" or some string)
-
-                            uri = fNSBinder.getURI(XMLSymbols.EMPTY_STRING);
-                            localUri=fLocalNSBinder.getURI(XMLSymbols.EMPTY_STRING);
-                            value = fSymbolTable.addSymbol(value);
-                            if (localUri == null ){
-                                // declaration was not printed while fixing element namespace binding
-                                if (fNamespacePrefixes) {
-                                    printNamespaceAttr(XMLSymbols.EMPTY_STRING, value);
-                                }
-                                // case 4 does not apply here since attributes can't use
-                                // default namespace
+                        }
+                        // xmlns --- empty prefix is always bound ("" or some string)
+                        uri = fNSBinder.getURI(XMLSymbols.EMPTY_STRING);
+                        localUri= fLocalNSBinder.getURI(XMLSymbols.EMPTY_STRING);
+                        value = fSymbolTable.addSymbol(value);
+                        if (localUri == null ) {
+                            // declaration was not printed while fixing element namespace binding
+                            if (fNamespacePrefixes) {
+                                printNamespaceAttr(XMLSymbols.EMPTY_STRING, value);
                             }
-                            continue;
+                            // case 4 does not apply here since attributes can't use
+                            // default namespace
                         }
+                        continue;
 
                     }
                     uri = fSymbolTable.addSymbol(uri);
@@ -1195,8 +1192,6 @@
         AttributesImpl attrsOnly;
         String         rawName;
         int            i;
-        int            indexColon;
-        String         prefix;
         int            length;
 
         if (attrs == null) {
@@ -1233,7 +1228,7 @@
             int ch = source.charAt(i);
             if (!XMLChar.isValid(ch)) {
                 if (++i < length) {
-                    surrogates(ch, source.charAt(i));
+                    surrogates(ch, source.charAt(i), false);
                 } else {
                     fatalError("The character '" + (char) ch + "' is an invalid XML character");
                 }
@@ -1291,16 +1286,17 @@
                 if (!XMLChar.isValid(ch)) {
                     // check if it is surrogate
                     if (++index <length) {
-                        surrogates(ch, text.charAt(index));
+                        surrogates(ch, text.charAt(index), true);
                     } else {
-                        fatalError("The character '"+(char)ch+"' is an invalid XML character");
+                        fatalError("The character '"+ch+"' is an invalid XML character");
                     }
                     continue;
                 }
                 if ( unescaped ) {
                     _printer.printText( ch );
-                } else
+                } else {
                     printXMLChar( ch );
+                }
             }
         } else {
             // Not preserving spaces: print one part at a time, and
@@ -1313,17 +1309,18 @@
                 if (!XMLChar.isValid(ch)) {
                     // check if it is surrogate
                     if (++index <length) {
-                        surrogates(ch, text.charAt(index));
+                        surrogates(ch, text.charAt(index), true);
                     } else {
-                        fatalError("The character '"+(char)ch+"' is an invalid XML character");
+                        fatalError("The character '"+ch+"' is an invalid XML character");
                     }
                     continue;
                 }
 
-                                if ( unescaped )
+                if ( unescaped ) {
                     _printer.printText( ch );
-                else
-                    printXMLChar( ch);
+                } else {
+                    printXMLChar( ch );
+                }
             }
         }
     }
@@ -1332,8 +1329,6 @@
 
     protected void printText( char[] chars, int start, int length,
                               boolean preserveSpace, boolean unescaped ) throws IOException {
-        int index;
-        char ch;
 
         if ( preserveSpace ) {
             // Preserving spaces: the text must print exactly as it is,
@@ -1341,13 +1336,13 @@
             // consolidating spaces. If a line terminator is used, a line
             // break will occur.
             while ( length-- > 0 ) {
-                ch = chars[start++];
+                char ch = chars[start++];
                 if (!XMLChar.isValid(ch)) {
                     // check if it is surrogate
                     if ( length-- > 0 ) {
-                        surrogates(ch, chars[start++]);
+                        surrogates(ch, chars[start++], true);
                     } else {
-                        fatalError("The character '"+(char)ch+"' is an invalid XML character");
+                        fatalError("The character '"+ch+"' is an invalid XML character");
                     }
                     continue;
                 }
@@ -1363,13 +1358,13 @@
             // by printing mechanism. Line terminator is treated
             // no different than other text part.
             while ( length-- > 0 ) {
-                ch = chars[start++];
+                char ch = chars[start++];
                 if (!XMLChar.isValid(ch)) {
                     // check if it is surrogate
                     if ( length-- > 0 ) {
-                        surrogates(ch, chars[start++]);
+                        surrogates(ch, chars[start++], true);
                     } else {
-                        fatalError("The character '"+(char)ch+"' is an invalid XML character");
+                        fatalError("The character '"+ch+"' is an invalid XML character");
                     }
                     continue;
                 }