src/com/sun/org/apache/xml/internal/serializer/ToUnknownStream.java

Print this page




   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 /*
  21  * $Id: ToUnknownStream.java,v 1.3 2005/09/28 13:49:08 pvedula Exp $
  22  */
  23 package com.sun.org.apache.xml.internal.serializer;
  24 
  25 import java.io.IOException;
  26 import java.io.OutputStream;
  27 import java.io.Writer;
  28 import java.util.Properties;
  29 import java.util.Vector;
  30 
  31 import javax.xml.transform.SourceLocator;
  32 import javax.xml.transform.Transformer;
  33 
  34 import org.w3c.dom.Node;
  35 import org.xml.sax.Attributes;
  36 import org.xml.sax.ContentHandler;
  37 import org.xml.sax.Locator;
  38 import org.xml.sax.SAXException;
  39 
  40 
  41 /**
  42  *This class wraps another SerializationHandler. The wrapped object will either
  43  * handler XML or HTML, which is not known until a little later when the first XML
  44  * tag is seen.  If the first tag is <html> then the wrapped object is an HTML
  45  * handler, otherwise it is an XML handler.
  46  *
  47  * This class effectively caches the first few calls to it then passes them
  48  * on to the wrapped handler (once it exists).  After that subsequent calls a
  49  * simply passed directly to the wrapped handler.


  83 
  84     /**
  85      * the namespace URI associated with the first element
  86      */
  87     private String m_firstElementURI;
  88 
  89     /**
  90      * the local name (no prefix) associated with the first element
  91      */
  92     private String m_firstElementLocalName = null;
  93 
  94     /**
  95      * true if the first tag has been emitted to the wrapped handler
  96      */
  97     private boolean m_firstTagNotEmitted = true;
  98 
  99     /**
 100      * A collection of namespace URI's (only for first element).
 101      * _namespacePrefix has the matching prefix for these URI's
 102      */
 103     private Vector m_namespaceURI = null;
 104     /**
 105      * A collection of namespace Prefix (only for first element)
 106      * _namespaceURI has the matching URIs for these prefix'
 107      */
 108     private Vector m_namespacePrefix = null;
 109 
 110     /**
 111      * true if startDocument() was called before the underlying handler
 112      * was initialized
 113      */
 114     private boolean m_needToCallStartDocument = false;
 115     /**
 116      * true if setVersion() was called before the underlying handler
 117      * was initialized
 118      */
 119     private boolean m_setVersion_called = false;
 120     /**
 121      * true if setDoctypeSystem() was called before the underlying handler
 122      * was initialized
 123      */
 124     private boolean m_setDoctypeSystem_called = false;
 125     /**
 126      * true if setDoctypePublic() was called before the underlying handler
 127      * was initialized
 128      */


 404 
 405     public boolean startPrefixMapping(String prefix, String uri, boolean shouldFlush)
 406         throws SAXException
 407     {
 408         boolean pushed = false;
 409         if (m_firstTagNotEmitted)
 410         {
 411             if (m_firstElementName != null && shouldFlush)
 412             {
 413                 /* we've already seen a startElement, and this is a prefix mapping
 414                  * for the up coming element, so flush the old element
 415                  * then send this event on its way.
 416                  */
 417                 flush();
 418                 pushed = m_handler.startPrefixMapping(prefix, uri, shouldFlush);
 419             }
 420             else
 421             {
 422                 if (m_namespacePrefix == null)
 423                 {
 424                     m_namespacePrefix = new Vector();
 425                     m_namespaceURI = new Vector();
 426                 }
 427                 m_namespacePrefix.addElement(prefix);
 428                 m_namespaceURI.addElement(uri);
 429 
 430                 if (m_firstElementURI == null)
 431                 {
 432                     if (prefix.equals(m_firstElementPrefix))
 433                         m_firstElementURI = uri;
 434                 }
 435             }
 436 
 437         }
 438         else
 439         {
 440            pushed = m_handler.startPrefixMapping(prefix, uri, shouldFlush);
 441         }
 442         return pushed;
 443     }
 444 
 445     /**
 446       * This method cannot be cached because default is different in
 447       * HTML and XML (we need more than a boolean).
 448       */


1075     {
1076         if (m_firstElementName != null)
1077         {
1078             if (m_wrapped_handler_not_initialized)
1079             {
1080                 initStreamOutput();
1081                 m_wrapped_handler_not_initialized = false;
1082             }
1083             // Output first tag
1084             m_handler.startElement(m_firstElementURI, null, m_firstElementName, m_attributes);
1085             // don't need the collected attributes of the first element anymore.
1086             m_attributes = null;
1087 
1088             // Output namespaces of first tag
1089             if (m_namespacePrefix != null)
1090             {
1091                 final int n = m_namespacePrefix.size();
1092                 for (int i = 0; i < n; i++)
1093                 {
1094                     final String prefix =
1095                         (String) m_namespacePrefix.elementAt(i);
1096                     final String uri = (String) m_namespaceURI.elementAt(i);
1097                     m_handler.startPrefixMapping(prefix, uri, false);
1098                 }
1099                 m_namespacePrefix = null;
1100                 m_namespaceURI = null;
1101             }
1102             m_firstTagNotEmitted = false;
1103         }
1104     }
1105 
1106     /**
1107      * Utility function for calls to local-name().
1108      *
1109      * Don't want to override static function on SerializerBase
1110      * So added Unknown suffix to method name.
1111      */
1112     private String getLocalNameUnknown(String value)
1113     {
1114         int idx = value.lastIndexOf(':');
1115         if (idx >= 0)
1116             value = value.substring(idx + 1);


1148             getLocalNameUnknown(m_firstElementName).equalsIgnoreCase("html");
1149 
1150         // Try to rule out if this is not to be an HTML document based on URI
1151         if (isHTML
1152             && m_firstElementURI != null
1153             && !EMPTYSTRING.equals(m_firstElementURI))
1154         {
1155             // the <html> element has a non-trivial namespace
1156             isHTML = false;
1157         }
1158         // Try to rule out if this is an not to be an HTML document based on prefix
1159         if (isHTML && m_namespacePrefix != null)
1160         {
1161             /* the first element has a name of "html", but lets check the prefix.
1162              * If the prefix points to a namespace with a URL that is not ""
1163              * then the doecument doesn't start with an <html> tag, and isn't html
1164              */
1165             final int max = m_namespacePrefix.size();
1166             for (int i = 0; i < max; i++)
1167             {
1168                 final String prefix = (String) m_namespacePrefix.elementAt(i);
1169                 final String uri = (String) m_namespaceURI.elementAt(i);
1170 
1171                 if (m_firstElementPrefix != null
1172                     && m_firstElementPrefix.equals(prefix)
1173                     && !EMPTYSTRING.equals(uri))
1174                 {
1175                     // The first element has a prefix, so it can't be <html>
1176                     isHTML = false;
1177                     break;
1178                 }
1179             }
1180 
1181         }
1182         return isHTML;
1183     }
1184     /**
1185      * @see Serializer#asDOMSerializer()
1186      */
1187     public DOMSerializer asDOMSerializer() throws IOException
1188     {
1189         return m_handler.asDOMSerializer();
1190     }
1191 
1192     /**
1193      * @param URI_and_localNames Vector a list of pairs of URI/localName
1194      * specified in the cdata-section-elements attribute.
1195      * @see SerializationHandler#setCdataSectionElements(java.util.Vector)
1196      */
1197     public void setCdataSectionElements(Vector URI_and_localNames)
1198     {
1199         m_handler.setCdataSectionElements(URI_and_localNames);
1200     }
1201     /**
1202      * @see ExtendedContentHandler#addAttributes(org.xml.sax.Attributes)
1203      */
1204     public void addAttributes(Attributes atts) throws SAXException
1205     {
1206         m_handler.addAttributes(atts);
1207     }
1208 
1209     /**
1210      * Get the current namespace mappings.
1211      * Simply returns the mappings of the wrapped handler.
1212      * @see ExtendedContentHandler#getNamespaceMappings()
1213      */
1214     public NamespaceMappings getNamespaceMappings()
1215     {
1216         NamespaceMappings mappings = null;
1217         if (m_handler != null)




   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 /*
  21  * $Id: ToUnknownStream.java,v 1.3 2005/09/28 13:49:08 pvedula Exp $
  22  */
  23 package com.sun.org.apache.xml.internal.serializer;
  24 
  25 import java.io.IOException;
  26 import java.io.OutputStream;
  27 import java.io.Writer;
  28 import java.util.Properties;
  29 import java.util.ArrayList;
  30 
  31 import javax.xml.transform.SourceLocator;
  32 import javax.xml.transform.Transformer;
  33 
  34 import org.w3c.dom.Node;
  35 import org.xml.sax.Attributes;
  36 import org.xml.sax.ContentHandler;
  37 import org.xml.sax.Locator;
  38 import org.xml.sax.SAXException;
  39 
  40 
  41 /**
  42  *This class wraps another SerializationHandler. The wrapped object will either
  43  * handler XML or HTML, which is not known until a little later when the first XML
  44  * tag is seen.  If the first tag is <html> then the wrapped object is an HTML
  45  * handler, otherwise it is an XML handler.
  46  *
  47  * This class effectively caches the first few calls to it then passes them
  48  * on to the wrapped handler (once it exists).  After that subsequent calls a
  49  * simply passed directly to the wrapped handler.


  83 
  84     /**
  85      * the namespace URI associated with the first element
  86      */
  87     private String m_firstElementURI;
  88 
  89     /**
  90      * the local name (no prefix) associated with the first element
  91      */
  92     private String m_firstElementLocalName = null;
  93 
  94     /**
  95      * true if the first tag has been emitted to the wrapped handler
  96      */
  97     private boolean m_firstTagNotEmitted = true;
  98 
  99     /**
 100      * A collection of namespace URI's (only for first element).
 101      * _namespacePrefix has the matching prefix for these URI's
 102      */
 103     private ArrayList<String> m_namespaceURI = null;
 104     /**
 105      * A collection of namespace Prefix (only for first element)
 106      * _namespaceURI has the matching URIs for these prefix'
 107      */
 108     private ArrayList<String> m_namespacePrefix = null;
 109 
 110     /**
 111      * true if startDocument() was called before the underlying handler
 112      * was initialized
 113      */
 114     private boolean m_needToCallStartDocument = false;
 115     /**
 116      * true if setVersion() was called before the underlying handler
 117      * was initialized
 118      */
 119     private boolean m_setVersion_called = false;
 120     /**
 121      * true if setDoctypeSystem() was called before the underlying handler
 122      * was initialized
 123      */
 124     private boolean m_setDoctypeSystem_called = false;
 125     /**
 126      * true if setDoctypePublic() was called before the underlying handler
 127      * was initialized
 128      */


 404 
 405     public boolean startPrefixMapping(String prefix, String uri, boolean shouldFlush)
 406         throws SAXException
 407     {
 408         boolean pushed = false;
 409         if (m_firstTagNotEmitted)
 410         {
 411             if (m_firstElementName != null && shouldFlush)
 412             {
 413                 /* we've already seen a startElement, and this is a prefix mapping
 414                  * for the up coming element, so flush the old element
 415                  * then send this event on its way.
 416                  */
 417                 flush();
 418                 pushed = m_handler.startPrefixMapping(prefix, uri, shouldFlush);
 419             }
 420             else
 421             {
 422                 if (m_namespacePrefix == null)
 423                 {
 424                     m_namespacePrefix = new ArrayList<>();
 425                     m_namespaceURI = new ArrayList<>();
 426                 }
 427                 m_namespacePrefix.add(prefix);
 428                 m_namespaceURI.add(uri);
 429 
 430                 if (m_firstElementURI == null)
 431                 {
 432                     if (prefix.equals(m_firstElementPrefix))
 433                         m_firstElementURI = uri;
 434                 }
 435             }
 436 
 437         }
 438         else
 439         {
 440            pushed = m_handler.startPrefixMapping(prefix, uri, shouldFlush);
 441         }
 442         return pushed;
 443     }
 444 
 445     /**
 446       * This method cannot be cached because default is different in
 447       * HTML and XML (we need more than a boolean).
 448       */


1075     {
1076         if (m_firstElementName != null)
1077         {
1078             if (m_wrapped_handler_not_initialized)
1079             {
1080                 initStreamOutput();
1081                 m_wrapped_handler_not_initialized = false;
1082             }
1083             // Output first tag
1084             m_handler.startElement(m_firstElementURI, null, m_firstElementName, m_attributes);
1085             // don't need the collected attributes of the first element anymore.
1086             m_attributes = null;
1087 
1088             // Output namespaces of first tag
1089             if (m_namespacePrefix != null)
1090             {
1091                 final int n = m_namespacePrefix.size();
1092                 for (int i = 0; i < n; i++)
1093                 {
1094                     final String prefix =
1095                         (String) m_namespacePrefix.get(i);
1096                     final String uri = (String) m_namespaceURI.get(i);
1097                     m_handler.startPrefixMapping(prefix, uri, false);
1098                 }
1099                 m_namespacePrefix = null;
1100                 m_namespaceURI = null;
1101             }
1102             m_firstTagNotEmitted = false;
1103         }
1104     }
1105 
1106     /**
1107      * Utility function for calls to local-name().
1108      *
1109      * Don't want to override static function on SerializerBase
1110      * So added Unknown suffix to method name.
1111      */
1112     private String getLocalNameUnknown(String value)
1113     {
1114         int idx = value.lastIndexOf(':');
1115         if (idx >= 0)
1116             value = value.substring(idx + 1);


1148             getLocalNameUnknown(m_firstElementName).equalsIgnoreCase("html");
1149 
1150         // Try to rule out if this is not to be an HTML document based on URI
1151         if (isHTML
1152             && m_firstElementURI != null
1153             && !EMPTYSTRING.equals(m_firstElementURI))
1154         {
1155             // the <html> element has a non-trivial namespace
1156             isHTML = false;
1157         }
1158         // Try to rule out if this is an not to be an HTML document based on prefix
1159         if (isHTML && m_namespacePrefix != null)
1160         {
1161             /* the first element has a name of "html", but lets check the prefix.
1162              * If the prefix points to a namespace with a URL that is not ""
1163              * then the doecument doesn't start with an <html> tag, and isn't html
1164              */
1165             final int max = m_namespacePrefix.size();
1166             for (int i = 0; i < max; i++)
1167             {
1168                 final String prefix = m_namespacePrefix.get(i);
1169                 final String uri = m_namespaceURI.get(i);
1170 
1171                 if (m_firstElementPrefix != null
1172                     && m_firstElementPrefix.equals(prefix)
1173                     && !EMPTYSTRING.equals(uri))
1174                 {
1175                     // The first element has a prefix, so it can't be <html>
1176                     isHTML = false;
1177                     break;
1178                 }
1179             }
1180 
1181         }
1182         return isHTML;
1183     }
1184     /**
1185      * @see Serializer#asDOMSerializer()
1186      */
1187     public DOMSerializer asDOMSerializer() throws IOException
1188     {
1189         return m_handler.asDOMSerializer();
1190     }
1191 
1192     /**
1193      * @param URI_and_localNames Vector a list of pairs of URI/localName
1194      * specified in the cdata-section-elements attribute.
1195      * @see SerializationHandler#setCdataSectionElements(java.util.Vector)
1196      */
1197     public void setCdataSectionElements(ArrayList<String> URI_and_localNames)
1198     {
1199         m_handler.setCdataSectionElements(URI_and_localNames);
1200     }
1201     /**
1202      * @see ExtendedContentHandler#addAttributes(org.xml.sax.Attributes)
1203      */
1204     public void addAttributes(Attributes atts) throws SAXException
1205     {
1206         m_handler.addAttributes(atts);
1207     }
1208 
1209     /**
1210      * Get the current namespace mappings.
1211      * Simply returns the mappings of the wrapped handler.
1212      * @see ExtendedContentHandler#getNamespaceMappings()
1213      */
1214     public NamespaceMappings getNamespaceMappings()
1215     {
1216         NamespaceMappings mappings = null;
1217         if (m_handler != null)