< prev index next >

src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/ToHTMLStream.java

Print this page

        

@@ -1,43 +1,38 @@
 /*
- * reserved comment block
- * DO NOT REMOVE OR ALTER!
+ * Copyright (c) 2014, 2016 Oracle and/or its affiliates. All rights reserved.
  */
 /*
- * 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
+ * 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
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/*
- * $Id: ToHTMLStream.java,v 1.2.4.1 2005/09/15 08:15:26 suresh_emailid Exp $
- */
+
 package com.sun.org.apache.xml.internal.serializer;
 
 import java.io.IOException;
-import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
 import java.util.Properties;
 
 import javax.xml.transform.Result;
 
-import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
-import com.sun.org.apache.xml.internal.serializer.utils.Utils;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 
+import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
+import com.sun.org.apache.xml.internal.serializer.utils.Utils;
+
 /**
  * This serializer takes a series of SAX or
  * SAX-like events and writes its output
  * to the given stream.
  *

@@ -50,13 +45,12 @@
 {
 
     /** This flag is set while receiving events from the DTD */
     protected boolean m_inDTD = false;
 
-    /** True if the current element is a block element.  (seems like
-     *  this needs to be a stack. -sb). */
-    private boolean m_inBlockElem = false;
+    /** True if the previous element is a block element. */
+    private boolean m_isprevblock = false;
 
     /**
      * Map that tells which XML characters should have special treatment, and it
      *  provides character to entity name lookup.
      */

@@ -721,11 +715,11 @@
      *
      * @throws org.xml.sax.SAXException
      */
     public final void endDocument() throws org.xml.sax.SAXException
     {
-
+        flushCharactersBuffer();
         flushPending();
         if (m_doIndent && !m_isprevtext)
         {
             try
             {

@@ -741,30 +735,52 @@
         if (m_tracer != null)
             super.fireEndDoc();
     }
 
     /**
+     * If the previous is an inline element, won't insert a new line before the
+     * text.
+     *
+     */
+    protected boolean shouldIndentForText() {
+        return super.shouldIndentForText() && m_isprevblock;
+    }
+
+    /**
+     * Only check m_doIndent, disregard m_ispreserveSpace.
+     *
+     * @return True if the content should be formatted.
+     */
+    protected boolean shouldFormatOutput() {
+        return m_doIndent;
+    }
+
+    /**
      *  Receive notification of the beginning of an element.
      *
      *
      *  @param namespaceURI
      *  @param localName
-     *  @param name The element type name.
-     *  @param atts The attributes attached to the element, if any.
-     *  @throws org.xml.sax.SAXException Any SAX exception, possibly
-     *             wrapping another exception.
+     * @param name
+     *            The element type name.
+     * @param atts
+     *            The attributes attached to the element, if any.
+     * @throws org.xml.sax.SAXException
+     *             Any SAX exception, possibly wrapping another exception.
      *  @see #endElement
      *  @see org.xml.sax.AttributeList
      */
     public void startElement(
         String namespaceURI,
         String localName,
         String name,
         Attributes atts)
-        throws org.xml.sax.SAXException
+        throws SAXException
     {
-
+        // will add extra one if having namespace but no matter
+        m_childNodeNum++;
+        flushCharactersBuffer();
         ElemContext elemContext = m_elemContext;
 
         // clean up any pending things first
         if (elemContext.m_startTagOpen)
         {

@@ -798,26 +814,22 @@
             int elemFlags = elemDesc.getFlags();
 
             // deal with indentation issues first
             if (m_doIndent)
             {
-
                 boolean isBlockElement = (elemFlags & ElemDesc.BLOCK) != 0;
-                if (m_ispreserve)
-                    m_ispreserve = false;
-                else if (
-                    (null != elemContext.m_elementName)
-                    && (!m_inBlockElem
-                        || isBlockElement) /* && !isWhiteSpaceSensitive */
-                    )
+                if ((elemContext.m_elementName != null)
+                        // If this element is a block element,
+                        // or if this is not a block element, then if the
+                        // previous is neither a text nor an inline
+                        && (isBlockElement || (!(m_isprevtext || !m_isprevblock))))
                 {
                     m_startNewLine = true;
 
                     indent();
-
                 }
-                m_inBlockElem = !isBlockElement;
+                m_isprevblock = isBlockElement;
             }
 
             // save any attributes for later processing
             if (atts != null)
                 addAttributes(atts);

@@ -825,11 +837,12 @@
             m_isprevtext = false;
             final java.io.Writer writer = m_writer;
             writer.write('<');
             writer.write(name);
 
-
+            m_childNodeNumStack.push(m_childNodeNum);
+            m_childNodeNum = 0;
 
             if (m_tracer != null)
                 firePseudoAttributes();
 
             if ((elemFlags & ElemDesc.EMPTY) != 0)

@@ -848,10 +861,19 @@
             {
                 elemContext = elemContext.push(namespaceURI,localName,name);
                 m_elemContext = elemContext;
                 elemContext.m_elementDesc = elemDesc;
                 elemContext.m_isRaw = (elemFlags & ElemDesc.RAW) != 0;
+
+                // set m_startNewLine for the next element
+                if (m_doIndent) {
+                    // elemFlags is equivalent to m_elemContext.m_elementDesc.getFlags(),
+                    // in this branch m_elemContext.m_elementName is not null
+                    boolean isBlockElement = (elemFlags & ElemDesc.BLOCK) != 0;
+                    if (isBlockElement)
+                        m_startNewLine = true;
+                }
             }
 
 
             if ((elemFlags & ElemDesc.HEADELEM) != 0)
             {

@@ -891,10 +913,11 @@
         final String namespaceURI,
         final String localName,
         final String name)
         throws org.xml.sax.SAXException
     {
+        flushCharactersBuffer();
         // deal with any pending issues
         if (m_cdataTagOpen)
             closeCDATA();
 
         // if the element has a namespace, treat it like XML, not HTML

@@ -917,22 +940,22 @@
             if (m_doIndent)
             {
                 final boolean isBlockElement = (elemFlags&ElemDesc.BLOCK) != 0;
                 boolean shouldIndent = false;
 
-                if (m_ispreserve)
-                {
-                    m_ispreserve = false;
-                }
-                else if (m_doIndent && (!m_inBlockElem || isBlockElement))
+                // If this element is a block element,
+                // or if this is not a block element, then if the previous is
+                // neither a text nor an inline
+                if (isBlockElement || (!(m_isprevtext || !m_isprevblock)))
                 {
                     m_startNewLine = true;
                     shouldIndent = true;
                 }
-                if (!elemContext.m_startTagOpen && shouldIndent)
+                if (!elemContext.m_startTagOpen && shouldIndent && (m_childNodeNum > 1 || !m_isprevtext))
                     indent(elemContext.m_currentElemDepth - 1);
-                m_inBlockElem = !isBlockElement;
+
+                m_isprevblock = isBlockElement;
             }
 
             final java.io.Writer writer = m_writer;
             if (!elemContext.m_startTagOpen)
             {

@@ -972,10 +995,11 @@
                 {
                     writer.write('>');
                 }
             }
 
+            m_childNodeNum = m_childNodeNumStack.pop();
             // clean up because the element has ended
             if ((elemFlags & ElemDesc.WHITESPACESENSITIVE) != 0)
                 m_ispreserve = true;
             m_isprevtext = false;
 

@@ -1509,11 +1533,11 @@
 //                  indent();
 
                 // writer.write("<![CDATA[");
                 // writer.write(chars, start, length);
                 writeNormalizedChars(chars, start, length, false, m_lineSepUse);
-
+                m_isprevtext = true;
                 // writer.write("]]>");
 
                 // time to generate characters event
                 if (m_tracer != null)
                     super.fireCharEvent(chars, start, length);

@@ -1564,11 +1588,10 @@
      * @throws org.xml.sax.SAXException
      */
     public final void cdata(char ch[], int start, int length)
         throws org.xml.sax.SAXException
     {
-
         if ((null != m_elemContext.m_elementName)
             && (m_elemContext.m_elementName.equalsIgnoreCase("SCRIPT")
                 || m_elemContext.m_elementName.equalsIgnoreCase("STYLE")))
         {
             try

@@ -1615,11 +1638,12 @@
      * @throws org.xml.sax.SAXException
      */
     public void processingInstruction(String target, String data)
         throws org.xml.sax.SAXException
     {
-
+        m_childNodeNum++;
+        flushCharactersBuffer();
         // Process any pending starDocument and startElement first.
         flushPending();
 
         // Use a fairly nasty hack to tell if the next node is supposed to be
         // unescaped text.

@@ -1943,14 +1967,12 @@
         return true;
     }
 
     private void initToHTMLStream()
     {
-//        m_elementDesc = null;
-        m_inBlockElem = false;
+        m_isprevblock = false;
         m_inDTD = false;
-//        m_isRawStack.clear();
         m_omitMetaTag = false;
         m_specialEscapeURLs = true;
     }
 
     static class Trie
< prev index next >