Fix for XALANJ-1497 which fixes the incorrect newline
after a PI, but also other incorrect whitespace around
PI and comments.

After this fix the newline after an XML header is almost
never written out because the generated XML could later be
used as an external general parsed entity and even that
extra newline could mess things up.
diff --git a/src/org/apache/xml/serializer/ToStream.java b/src/org/apache/xml/serializer/ToStream.java
index 5db982d..9afae5c 100644
--- a/src/org/apache/xml/serializer/ToStream.java
+++ b/src/org/apache/xml/serializer/ToStream.java
@@ -2402,13 +2402,14 @@
 
         try
         {
-            if (shouldIndent())
-                indent();
-
             final int limit = start + length;
             boolean wasDash = false;
             if (m_cdataTagOpen)
                 closeCDATA();
+            
+            if (shouldIndent())
+                indent();
+            
             final java.io.Writer writer = m_writer;    
             writer.write(COMMENT_BEGIN);
             // Detect occurrences of two consecutive dashes, handle as necessary.
@@ -2441,6 +2442,15 @@
             throw new SAXException(e);
         }
 
+        /*
+         * Don't write out any indentation whitespace now,
+         * because there may be non-whitespace text after this.
+         * 
+         * Simply mark that at this point if we do decide
+         * to indent that we should 
+         * add a newline on the end of the current line before
+         * the indentation at the start of the next line.
+         */ 
         m_startNewLine = true;
         // time to generate comment event
         if (m_tracer != null)
@@ -2684,7 +2694,7 @@
      */
     protected boolean shouldIndent()
     {
-        return m_doIndent && (!m_ispreserve && !m_isprevtext);
+        return m_doIndent && (!m_ispreserve && !m_isprevtext) && m_elemContext.m_currentElemDepth > 0;
     }
 
     /**
diff --git a/src/org/apache/xml/serializer/ToXMLStream.java b/src/org/apache/xml/serializer/ToXMLStream.java
index c28cb0f..7092923 100644
--- a/src/org/apache/xml/serializer/ToXMLStream.java
+++ b/src/org/apache/xml/serializer/ToXMLStream.java
@@ -145,8 +145,22 @@
                     writer.write('\"');
                     writer.write(standalone);
                     writer.write("?>");
-                    if (m_doIndent)
-                        writer.write(m_lineSep, 0, m_lineSepLen);
+                    if (m_doIndent) {
+                        if (m_standaloneWasSpecified
+                                || getDoctypePublic() != null
+                                || getDoctypeSystem() != null) {
+                            // We almost never put a newline after the XML
+                            // header because this XML could be used as
+                            // an extenal general parsed entity
+                            // and we don't know the context into which it
+                            // will be used in the future.  Only when
+                            // standalone, or a doctype system or public is
+                            // specified are we free to insert a new line
+                            // after the header.  Is it even worth bothering
+                            // in these rare cases?                           
+                            writer.write(m_lineSep, 0, m_lineSepLen);
+                        }
+                    }
                 } 
                 catch(IOException e)
                 {
@@ -296,13 +310,16 @@
 
                 writer.write('?');
                 writer.write('>');
-
-                // Always output a newline char if not inside of an
-                // element. The whitespace is not significant in that
-                // case.
-                if (m_elemContext.m_currentElemDepth <= 0)
-                    writer.write(m_lineSep, 0, m_lineSepLen);
-
+                
+                /*
+                 * Don't write out any indentation whitespace now,
+                 * because there may be non-whitespace text after this.
+                 * 
+                 * Simply mark that at this point if we do decide
+                 * to indent that we should 
+                 * add a newline on the end of the current line before
+                 * the indentation at the start of the next line.
+                 */ 
                 m_startNewLine = true;
             }
             catch(IOException e)