jaxp/src/com/sun/org/apache/xml/internal/serialize/BaseMarkupSerializer.java
changeset 23777 ce87cedb71cf
parent 12457 c348e06f0e82
child 25834 aba3efbf4ec5
--- a/jaxp/src/com/sun/org/apache/xml/internal/serialize/BaseMarkupSerializer.java	Tue Mar 25 14:51:51 2014 -0700
+++ b/jaxp/src/com/sun/org/apache/xml/internal/serialize/BaseMarkupSerializer.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,7 +19,6 @@
  * limitations under the License.
  */
 
-
 // Sep 14, 2000:
 //  Fixed comments to preserve whitespaces and add a line break
 //  when indenting. Reported by Gervase Markham <gerv@gerv.net>
@@ -57,17 +57,13 @@
 import com.sun.org.apache.xerces.internal.dom.DOMLocatorImpl;
 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
 import com.sun.org.apache.xerces.internal.util.XMLChar;
-import org.w3c.dom.DOMImplementation;
+import org.w3c.dom.DOMError;
+import org.w3c.dom.DOMErrorHandler;
 import org.w3c.dom.Document;
 import org.w3c.dom.DocumentFragment;
 import org.w3c.dom.DocumentType;
-import org.w3c.dom.DOMError;
-import org.w3c.dom.DOMErrorHandler;
 import org.w3c.dom.Element;
-import org.w3c.dom.Entity;
-import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
-import org.w3c.dom.Notation;
 import org.w3c.dom.ls.LSException;
 import org.w3c.dom.ls.LSSerializerFilter;
 import org.w3c.dom.traversal.NodeFilter;
@@ -126,7 +122,7 @@
  * @author Elena Litani, IBM
  * @author Sunitha Reddy, Sun Microsystems
  * @see Serializer
- * @see LSSerializer
+ * @see org.w3c.dom.ls.LSSerializer
  */
 public abstract class BaseMarkupSerializer
     implements ContentHandler, DocumentHandler, LexicalHandler,
@@ -337,6 +333,9 @@
         return true;
     }
 
+    protected void cleanup() {
+        fCurrentNode = null;
+    }
 
     protected void prepare()
         throws IOException
@@ -409,6 +408,7 @@
         reset();
         prepare();
         serializeNode( elem );
+        cleanup();
         _printer.flush();
         if ( _printer.getException() != null )
             throw _printer.getException();
@@ -438,7 +438,7 @@
      * writer and output format. Throws an exception only if
      * an I/O exception occured while serializing.
      *
-     * @param elem The element to serialize
+     * @param frag The document fragment to serialize
      * @throws IOException An I/O exception occured while
      *   serializing
      */
@@ -448,6 +448,7 @@
         reset();
         prepare();
         serializeNode( frag );
+        cleanup();
         _printer.flush();
         if ( _printer.getException() != null )
             throw _printer.getException();
@@ -470,6 +471,7 @@
         prepare();
         serializeNode( doc );
         serializePreRoot();
+        cleanup();
         _printer.flush();
         if ( _printer.getException() != null )
             throw _printer.getException();
@@ -530,22 +532,22 @@
                 if (!XMLChar.isValid(ch)) {
                     // check if it is surrogate
                     if (++index < end) {
-                        surrogates(ch, chars[index]);
+                        surrogates(ch, chars[index],true);
                     }
                     else {
-                        fatalError("The character '"+(char)ch+"' is an invalid XML character");
+                        fatalError("The character '"+ch+"' is an invalid XML character");
                     }
                     continue;
-                } else {
-                    if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||
-                        ch == '\n' || ch == '\r' || ch == '\t' ) {
-                        _printer.printText((char)ch);
-                    } else {
-                        // The character is not printable -- split CDATA section
-                        _printer.printText("]]>&#x");
-                        _printer.printText(Integer.toHexString(ch));
-                        _printer.printText(";<![CDATA[");
-                    }
+                }
+                if ( ( ch >= ' ' && _encodingInfo.isPrintable(ch) && ch != 0x7F ) ||
+                    ch == '\n' || ch == '\r' || ch == '\t' ) {
+                    _printer.printText(ch);
+                }
+                else {
+                    // The character is not printable -- split CDATA section
+                    _printer.printText("]]>&#x");
+                    _printer.printText(Integer.toHexString(ch));
+                    _printer.printText(";<![CDATA[");
                 }
             }
             _printer.setNextIndent( saveIndent );
@@ -1195,11 +1197,6 @@
         }
         case Node.DOCUMENT_NODE : {
             DocumentType      docType;
-            DOMImplementation domImpl;
-            NamedNodeMap      map;
-            Entity            entity;
-            Notation          notation;
-            int               i;
 
             serializeDocument();
 
@@ -1208,7 +1205,6 @@
             docType = ( (Document) node ).getDoctype();
             if (docType != null) {
                 // DOM Level 2 (or higher)
-                domImpl = ( (Document) node ).getImplementation();
                 try {
                     String internal;
 
@@ -1372,7 +1368,7 @@
      * state with <tt>empty</tt> and <tt>afterElement</tt> set to false.
      *
      * @return The current element state
-     * @throws IOException An I/O exception occured while
+     * @throws IOException An I/O exception occurred while
      *   serializing
      */
     protected ElementState content()
@@ -1415,7 +1411,6 @@
      * whether the text is printed as CDATA or unescaped.
      *
      * @param text The text to print
-     * @param unescaped True is should print unescaped
      * @throws IOException An I/O exception occured while
      *   serializing
      */
@@ -1430,9 +1425,6 @@
         // state) or whether we are inside a CDATA section or entity.
 
         if ( state.inCData || state.doCData ) {
-            int          index;
-            int          saveIndent;
-
             // Print a CDATA section. The text is not escaped, but ']]>'
             // appearing in the code must be identified and dealt with.
             // The contents of a text node is considered space preserving.
@@ -1440,7 +1432,7 @@
                 _printer.printText("<![CDATA[");
                 state.inCData = true;
             }
-            saveIndent = _printer.getNextIndent();
+            int saveIndent = _printer.getNextIndent();
             _printer.setNextIndent( 0 );
             printCDATAText( text);
             _printer.setNextIndent( saveIndent );
@@ -1543,12 +1535,10 @@
                             fDOMErrorHandler.handleError(fDOMError);
                             throw new LSException(LSException.SERIALIZE_ERR, msg);
                         }
-                        else {
-                            // issue error
-                            modifyDOMError(msg, DOMError.SEVERITY_ERROR, "cdata-section-not-splitted", fCurrentNode);
-                            if (!fDOMErrorHandler.handleError(fDOMError)) {
-                                throw new LSException(LSException.SERIALIZE_ERR, msg);
-                            }
+                        // issue error
+                        modifyDOMError(msg, DOMError.SEVERITY_ERROR, "cdata-section-not-splitted", fCurrentNode);
+                        if (!fDOMErrorHandler.handleError(fDOMError)) {
+                            throw new LSException(LSException.SERIALIZE_ERR, msg);
                         }
                     } else {
                         // issue warning
@@ -1573,29 +1563,29 @@
             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;
-            } else {
-                if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||
-                     ch == '\n' || ch == '\r' || ch == '\t' ) {
-                    _printer.printText((char)ch);
-                } else {
+            }
+            if ( ( ch >= ' ' && _encodingInfo.isPrintable(ch) && ch != 0x7F ) ||
+                 ch == '\n' || ch == '\r' || ch == '\t' ) {
+                _printer.printText(ch);
+            }
+            else {
 
-                    // The character is not printable -- split CDATA section
-                    _printer.printText("]]>&#x");
-                    _printer.printText(Integer.toHexString(ch));
-                    _printer.printText(";<![CDATA[");
-                }
+                // The character is not printable -- split CDATA section
+                _printer.printText("]]>&#x");
+                _printer.printText(Integer.toHexString(ch));
+                _printer.printText(";<![CDATA[");
             }
         }
     }
 
 
-    protected void surrogates(int high, int low) throws IOException{
+    protected void surrogates(int high, int low, boolean inContent) throws IOException{
         if (XMLChar.isHighSurrogate(high)) {
             if (!XMLChar.isLowSurrogate(low)) {
                 //Invalid XML
@@ -1608,7 +1598,7 @@
                     fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
                 }
                 else {
-                    if (content().inCData ) {
+                    if (inContent && content().inCData) {
                         _printer.printText("]]>&#x");
                         _printer.printText(Integer.toHexString(supplemental));
                         _printer.printText(";<![CDATA[");
@@ -1633,7 +1623,9 @@
      * Multiple spaces are printed as such, but spaces at beginning
      * of line are removed.
      *
-     * @param text The text to print
+     * @param chars The text to print
+     * @param start The start offset
+     * @param length The number of characters
      * @param preserveSpace Space preserving flag
      * @param unescaped Print unescaped
      */
@@ -1641,8 +1633,6 @@
                                     boolean preserveSpace, boolean unescaped )
         throws IOException
     {
-        int index;
-        char ch;
 
         if ( preserveSpace ) {
             // Preserving spaces: the text must print exactly as it is,
@@ -1650,12 +1640,14 @@
             // consolidating spaces. If a line terminator is used, a line
             // break will occur.
             while ( length-- > 0 ) {
-                ch = chars[ start ];
+                char ch = chars[ start ];
                 ++start;
-                if ( ch == '\n' || ch == '\r' || unescaped )
+                if ( ch == '\n' || ch == '\r' || unescaped ) {
                     _printer.printText( ch );
-                else
+                }
+                else {
                     printEscaped( ch );
+                }
             }
         } else {
             // Not preserving spaces: print one part at a time, and
@@ -1664,14 +1656,17 @@
             // by printing mechanism. Line terminator is treated
             // no different than other text part.
             while ( length-- > 0 ) {
-                ch = chars[ start ];
+                char ch = chars[ start ];
                 ++start;
-                if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )
+                if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' ) {
                     _printer.printSpace();
-                else if ( unescaped )
+                }
+                else if ( unescaped ) {
                     _printer.printText( ch );
-                else
+                }
+                else {
                     printEscaped( ch );
+                }
             }
         }
     }
@@ -1703,12 +1698,15 @@
             // no different than other text part.
             for ( index = 0 ; index < text.length() ; ++index ) {
                 ch = text.charAt( index );
-                if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )
+                if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' ) {
                     _printer.printSpace();
-                else if ( unescaped )
+                }
+                else if ( unescaped ) {
                     _printer.printText( ch );
-                else
+                }
+                else {
                     printEscaped( ch );
+                }
             }
         }
     }
@@ -1751,7 +1749,7 @@
             _printer.printText( '&' );
             _printer.printText( charRef );
             _printer.printText( ';' );
-        } else if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||
+        } else if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0x7F ) ||
                     ch == '\n' || ch == '\r' || ch == '\t' ) {
             // Non printables are below ASCII space but not tab or line
             // terminator, ASCII delete, or above a certain Unicode threshold.
@@ -1872,14 +1870,13 @@
     {
         if ( _elementStateCount > 0 ) {
             /*Corrected by David Blondeau (blondeau@intalio.com)*/
-                _prefixes = null;
-                //_prefixes = _elementStates[ _elementStateCount ].prefixes;
+            _prefixes = null;
+            //_prefixes = _elementStates[ _elementStateCount ].prefixes;
             -- _elementStateCount;
             return _elementStates[ _elementStateCount ];
-        } else {
-            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "Internal", null);
-            throw new IllegalStateException(msg);
         }
+        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "Internal", null);
+        throw new IllegalStateException(msg);
     }
 
 
@@ -1890,11 +1887,14 @@
      *
      * @return True if in the state of the document
      */
-    protected boolean isDocumentState()
-    {
+    protected boolean isDocumentState() {
         return _elementStateCount == 0;
     }
 
+    /** Clears document state. **/
+    final void clearDocumentState() {
+        _elementStateCount = 0;
+    }
 
     /**
      * Returns the namespace prefix for the specified URI.
@@ -1913,15 +1913,14 @@
             if ( prefix != null )
                 return prefix;
         }
-        if ( _elementStateCount == 0 )
+        if ( _elementStateCount == 0 ) {
             return null;
-        else {
-            for ( int i = _elementStateCount ; i > 0 ; --i ) {
-                if ( _elementStates[ i ].prefixes != null ) {
-                    prefix = (String) _elementStates[ i ].prefixes.get( namespaceURI );
-                    if ( prefix != null )
-                        return prefix;
-                }
+        }
+        for ( int i = _elementStateCount ; i > 0 ; --i ) {
+            if ( _elementStates[ i ].prefixes != null ) {
+                prefix = (String) _elementStates[ i ].prefixes.get( namespaceURI );
+                if ( prefix != null )
+                    return prefix;
             }
         }
         return null;