src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/ToStream.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 51786 c93f14a4ae29
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
     3  */
     3  */
     4 /*
     4 /*
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     6  * contributor license agreements.  See the NOTICE file distributed with
     6  * contributor license agreements.  See the NOTICE file distributed with
     7  * this work for additional information regarding copyright ownership.
     7  * this work for additional information regarding copyright ownership.
    49 /**
    49 /**
    50  * This abstract class is a base class for other stream
    50  * This abstract class is a base class for other stream
    51  * serializers (xml, html, text ...) that write output to a stream.
    51  * serializers (xml, html, text ...) that write output to a stream.
    52  *
    52  *
    53  * @xsl.usage internal
    53  * @xsl.usage internal
    54  * @LastModified: Sept 2018
    54  * @LastModified: Aug 2019
    55  */
    55  */
    56 abstract public class ToStream extends SerializerBase {
    56 abstract public class ToStream extends SerializerBase {
    57 
    57 
    58     private static final String COMMENT_BEGIN = "<!--";
    58     private static final String COMMENT_BEGIN = "<!--";
    59     private static final String COMMENT_END = "-->";
    59     private static final String COMMENT_END = "-->";
   196     private char m_highSurrogate = 0;
   196     private char m_highSurrogate = 0;
   197 
   197 
   198     /**
   198     /**
   199      * Default constructor
   199      * Default constructor
   200      */
   200      */
   201     public ToStream() { }
   201     public ToStream() {
       
   202         this(null);
       
   203     }
       
   204 
       
   205     public ToStream(ErrorListener l) {
       
   206         m_errListener = l;
       
   207     }
   202 
   208 
   203     /**
   209     /**
   204      * This helper method to writes out "]]>" when closing a CDATA section.
   210      * This helper method to writes out "]]>" when closing a CDATA section.
   205      *
   211      *
   206      * @throws org.xml.sax.SAXException
   212      * @throws org.xml.sax.SAXException
   420                             || ( !defaultVal && (oldExplicitEncoding == null || !oldExplicitEncoding.equalsIgnoreCase(newEncoding) ))) {
   426                             || ( !defaultVal && (oldExplicitEncoding == null || !oldExplicitEncoding.equalsIgnoreCase(newEncoding) ))) {
   421                        // We are trying to change the default or the non-default setting of the encoding to a different value
   427                        // We are trying to change the default or the non-default setting of the encoding to a different value
   422                        // from what it was
   428                        // from what it was
   423 
   429 
   424                        EncodingInfo encodingInfo = Encodings.getEncodingInfo(newEncoding);
   430                        EncodingInfo encodingInfo = Encodings.getEncodingInfo(newEncoding);
   425                        if (newEncoding != null && encodingInfo.name == null) {
   431                        if (encodingInfo.name == null) {
   426                         // We tried to get an EncodingInfo for Object for the given
   432                             // We tried to get an EncodingInfo for Object for the given
   427                         // encoding, but it came back with an internall null name
   433                             // encoding, but it came back with an internall null name
   428                         // so the encoding is not supported by the JDK, issue a message.
   434                             // so the encoding is not supported by the JDK, issue a message.
   429                         final String msg = Utils.messages.createMessage(
   435                             final String msg = Utils.messages.createMessage(
   430                                 MsgKey.ER_ENCODING_NOT_SUPPORTED,new Object[]{ newEncoding });
   436                                     MsgKey.ER_ENCODING_NOT_SUPPORTED,new Object[]{ newEncoding });
   431 
   437 
   432                         final String msg2 =
   438                             final String msg2 =
   433                             "Warning: encoding \"" + newEncoding + "\" not supported, using "
   439                                 "Warning: encoding \"" + newEncoding + "\" not supported, using "
   434                                    + Encodings.DEFAULT_MIME_ENCODING;
   440                                        + Encodings.DEFAULT_MIME_ENCODING;
   435                         try {
   441                             try {
   436                                 // Prepare to issue the warning message
   442                                 // refer to JDK-8229005, should throw Exception instead of warning and
   437                                 final Transformer tran = super.getTransformer();
   443                                 // then falling back to the default encoding. Keep it for now.
   438                                 if (tran != null) {
   444                                 if (m_errListener != null) {
   439                                     final ErrorListener errHandler = tran
   445                                     m_errListener.warning(new TransformerException(msg, m_sourceLocator));
   440                                             .getErrorListener();
   446                                     m_errListener.warning(new TransformerException(msg2, m_sourceLocator));
   441                                     // Issue the warning message
       
   442                                     if (null != errHandler
       
   443                                             && m_sourceLocator != null) {
       
   444                                         errHandler
       
   445                                                 .warning(new TransformerException(
       
   446                                                         msg, m_sourceLocator));
       
   447                                         errHandler
       
   448                                                 .warning(new TransformerException(
       
   449                                                         msg2, m_sourceLocator));
       
   450                                     } else {
       
   451                                         System.out.println(msg);
       
   452                                         System.out.println(msg2);
       
   453                                     }
       
   454                                 } else {
       
   455                                     System.out.println(msg);
       
   456                                     System.out.println(msg2);
       
   457                                 }
   447                                 }
   458                             } catch (Exception e) {
   448                             } catch (Exception e) {
   459                             }
   449                             }
   460 
   450 
   461                             // We said we are using UTF-8, so use it
   451                             // We said we are using UTF-8, so use it
   462                             newEncoding = Encodings.DEFAULT_MIME_ENCODING;
   452                             newEncoding = Encodings.DEFAULT_MIME_ENCODING;
   463                             val = Encodings.DEFAULT_MIME_ENCODING; // to store the modified value into the properties a little later
   453                             // to store the modified value into the properties a little later
       
   454                             val = Encodings.DEFAULT_MIME_ENCODING;
   464                             encodingInfo = Encodings.getEncodingInfo(newEncoding);
   455                             encodingInfo = Encodings.getEncodingInfo(newEncoding);
   465                         }
   456                         }
   466                        // The encoding was good, or was forced to UTF-8 above
   457                        // The encoding was good, or was forced to UTF-8 above
   467 
   458 
   468 
   459 
  1229             {
  1220             {
  1230                 closeStartTag();
  1221                 closeStartTag();
  1231                 m_elemContext.m_startTagOpen = false;
  1222                 m_elemContext.m_startTagOpen = false;
  1232             }
  1223             }
  1233 
  1224 
  1234             if (!m_cdataTagOpen && shouldIndent())
  1225             if (!m_cdataTagOpen && shouldIndentForText())
  1235                 indent();
  1226                 indent();
  1236 
  1227 
  1237             boolean writeCDataBrackets =
  1228             boolean writeCDataBrackets =
  1238                 (((length >= 1) && escapingNotNeeded(ch[start])));
  1229                 (((length >= 1) && escapingNotNeeded(ch[start])));
  1239 
  1230 
  1268                  */
  1259                  */
  1269                 if (ch[start + length - 1] == ']')
  1260                 if (ch[start + length - 1] == ']')
  1270                     closeCDATA();
  1261                     closeCDATA();
  1271             }
  1262             }
  1272 
  1263 
       
  1264             m_isprevtext = true;
  1273             // time to fire off CDATA event
  1265             // time to fire off CDATA event
  1274             if (m_tracer != null)
  1266             if (m_tracer != null)
  1275                 super.fireCDATAEvent(ch, old_start, length);
  1267                 super.fireCDATAEvent(ch, old_start, length);
  1276         }
  1268         }
  1277         catch (IOException ioe)
  1269         catch (IOException ioe)
  1534             throw new SAXException(e);
  1526             throw new SAXException(e);
  1535         }
  1527         }
  1536     }
  1528     }
  1537 
  1529 
  1538     /**
  1530     /**
  1539      * Used to flush the buffered characters when indentation is on, this method
  1531      * Flushes the buffered characters when indentation is on. This method
  1540      * will be called when the next node is traversed.
  1532      * is called before the next node is traversed.
  1541      *
  1533      *
  1542      */
  1534      * @param isText indicates whether the node to be traversed is text
  1543     final protected void flushCharactersBuffer() throws SAXException {
  1535      * @throws org.xml.sax.SAXException
       
  1536      */
       
  1537     final protected void flushCharactersBuffer(boolean isText) throws SAXException {
  1544         try {
  1538         try {
  1545             if (shouldFormatOutput() && m_charactersBuffer.isAnyCharactersBuffered()) {
  1539             if (shouldFormatOutput() && m_charactersBuffer.isAnyCharactersBuffered()) {
  1546                 if (m_elemContext.m_isCdataSection) {
  1540                 if (m_elemContext.m_isCdataSection) {
  1547                     /*
  1541                     /*
  1548                      * due to cdata-section-elements atribute, we need this as
  1542                      * due to cdata-section-elements atribute, we need this as
  1551                     char[] chars = m_charactersBuffer.toChars();
  1545                     char[] chars = m_charactersBuffer.toChars();
  1552                     cdata(chars, 0, chars.length);
  1546                     cdata(chars, 0, chars.length);
  1553                     return;
  1547                     return;
  1554                 }
  1548                 }
  1555 
  1549 
  1556                 m_childNodeNum++;
  1550                 if (!isText) {
       
  1551                     m_childNodeNum++;
       
  1552                 }
  1557                 boolean skipBeginningNewlines = false;
  1553                 boolean skipBeginningNewlines = false;
  1558                 if (shouldIndentForText()) {
  1554                 if (shouldIndentForText()) {
  1559                     indent();
  1555                     indent();
  1560                     m_startNewLine = true;
  1556                     m_startNewLine = true;
  1561                     // newline has always been added here because if this is the
  1557                     // newline has always been added here because if this is the
  1844         if (isInEntityRef())
  1840         if (isInEntityRef())
  1845             return;
  1841             return;
  1846 
  1842 
  1847         if (m_doIndent) {
  1843         if (m_doIndent) {
  1848             m_childNodeNum++;
  1844             m_childNodeNum++;
  1849             flushCharactersBuffer();
  1845             flushCharactersBuffer(false);
  1850         }
  1846         }
  1851 
  1847 
  1852         if (m_needToCallStartDocument)
  1848         if (m_needToCallStartDocument)
  1853         {
  1849         {
  1854             startDocumentInternal();
  1850             startDocumentInternal();
  2115 
  2111 
  2116         if (isInEntityRef())
  2112         if (isInEntityRef())
  2117             return;
  2113             return;
  2118 
  2114 
  2119         if (m_doIndent) {
  2115         if (m_doIndent) {
  2120             flushCharactersBuffer();
  2116             flushCharactersBuffer(false);
  2121         }
  2117         }
  2122         // namespaces declared at the current depth are no longer valid
  2118         // namespaces declared at the current depth are no longer valid
  2123         // so get rid of them
  2119         // so get rid of them
  2124         m_prefixMap.popNamespaces(m_elemContext.m_currentElemDepth, null);
  2120         m_prefixMap.popNamespaces(m_elemContext.m_currentElemDepth, null);
  2125 
  2121 
  2307         int start_old = start;
  2303         int start_old = start;
  2308         if (isInEntityRef())
  2304         if (isInEntityRef())
  2309             return;
  2305             return;
  2310         if (m_doIndent) {
  2306         if (m_doIndent) {
  2311             m_childNodeNum++;
  2307             m_childNodeNum++;
  2312             flushCharactersBuffer();
  2308             flushCharactersBuffer(false);
  2313         }
  2309         }
  2314         if (m_elemContext.m_startTagOpen)
  2310         if (m_elemContext.m_startTagOpen)
  2315         {
  2311         {
  2316             closeStartTag();
  2312             closeStartTag();
  2317             m_elemContext.m_startTagOpen = false;
  2313             m_elemContext.m_startTagOpen = false;
  2489      * @see #endCDATA
  2485      * @see #endCDATA
  2490      */
  2486      */
  2491     public void startCDATA() throws org.xml.sax.SAXException
  2487     public void startCDATA() throws org.xml.sax.SAXException
  2492     {
  2488     {
  2493         if (m_doIndent) {
  2489         if (m_doIndent) {
  2494             m_childNodeNum++;
  2490             flushCharactersBuffer(true);
  2495             flushCharactersBuffer();
       
  2496         }
  2491         }
  2497 
  2492 
  2498         m_cdataStartCalled = true;
  2493         m_cdataStartCalled = true;
  2499     }
  2494     }
  2500 
  2495