2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 * $Id: ToUnknownStream.java 471981 2006-11-07 04:28:00Z minchau $
21 package org.apache.xml.serializer;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.Writer;
26 import java.util.Properties;
27 import java.util.Vector;
29 import javax.xml.transform.SourceLocator;
30 import javax.xml.transform.Transformer;
32 import org.w3c.dom.Node;
33 import org.xml.sax.Attributes;
34 import org.xml.sax.ContentHandler;
35 import org.xml.sax.Locator;
36 import org.xml.sax.SAXException;
40 *This class wraps another SerializationHandler. The wrapped object will either
41 * handler XML or HTML, which is not known until a little later when the first XML
42 * tag is seen. If the first tag is <html> then the wrapped object is an HTML
43 * handler, otherwise it is an XML handler.
45 * This class effectively caches the first few calls to it then passes them
46 * on to the wrapped handler (once it exists). After that subsequent calls a
47 * simply passed directly to the wrapped handler.
49 * The user of this class doesn't know if the output is ultimatley XML or HTML.
51 * This class is not a public API, it is public because it is used within Xalan.
54 public final class ToUnknownStream extends SerializerBase
58 * The wrapped handler, initially XML but possibly switched to HTML
60 private SerializationHandler m_handler;
63 * A String with no characters
65 private static final String EMPTYSTRING = "";
68 * true if the underlying handler (XML or HTML) is fully initialized
70 private boolean m_wrapped_handler_not_initialized = false;
74 * the prefix of the very first tag in the document
76 private String m_firstElementPrefix;
78 * the element name (including any prefix) of the very first tag in the document
80 private String m_firstElementName;
83 * the namespace URI associated with the first element
85 private String m_firstElementURI;
88 * the local name (no prefix) associated with the first element
90 private String m_firstElementLocalName = null;
93 * true if the first tag has been emitted to the wrapped handler
95 private boolean m_firstTagNotEmitted = true;
98 * A collection of namespace URI's (only for first element).
99 * _namespacePrefix has the matching prefix for these URI's
101 private Vector m_namespaceURI = null;
103 * A collection of namespace Prefix (only for first element)
104 * _namespaceURI has the matching URIs for these prefix'
106 private Vector m_namespacePrefix = null;
109 * true if startDocument() was called before the underlying handler
112 private boolean m_needToCallStartDocument = false;
114 * true if setVersion() was called before the underlying handler
117 private boolean m_setVersion_called = false;
119 * true if setDoctypeSystem() was called before the underlying handler
122 private boolean m_setDoctypeSystem_called = false;
124 * true if setDoctypePublic() was called before the underlying handler
127 private boolean m_setDoctypePublic_called = false;
129 * true if setMediaType() was called before the underlying handler
132 private boolean m_setMediaType_called = false;
135 * Default constructor.
136 * Initially this object wraps an XML Stream object, so _handler is never null.
137 * That may change later to an HTML Stream object.
139 public ToUnknownStream()
141 m_handler = new ToXMLStream();
145 * @see Serializer#asContentHandler()
146 * @return the wrapped XML or HTML handler
148 public ContentHandler asContentHandler() throws IOException
150 /* don't return the real handler ( m_handler ) because
151 * that would expose the real handler to the outside.
152 * Keep m_handler private so it can be internally swapped
153 * to an HTML handler.
159 * @see SerializationHandler#close()
167 * @see Serializer#getOutputFormat()
168 * @return the properties of the underlying handler
170 public Properties getOutputFormat()
172 return m_handler.getOutputFormat();
176 * @see Serializer#getOutputStream()
177 * @return the OutputStream of the underlying XML or HTML handler
179 public OutputStream getOutputStream()
181 return m_handler.getOutputStream();
185 * @see Serializer#getWriter()
186 * @return the Writer of the underlying XML or HTML handler
188 public Writer getWriter()
190 return m_handler.getWriter();
194 * passes the call on to the underlying HTML or XML handler
195 * @see Serializer#reset()
198 public boolean reset()
200 return m_handler.reset();
204 * Converts the DOM node to output
205 * @param node the DOM node to transform to output
206 * @see DOMSerializer#serialize(Node)
209 public void serialize(Node node) throws IOException
211 if (m_firstTagNotEmitted)
215 m_handler.serialize(node);
219 * @see SerializationHandler#setEscaping(boolean)
221 public boolean setEscaping(boolean escape) throws SAXException
223 return m_handler.setEscaping(escape);
227 * Set the properties of the handler
228 * @param format the output properties to set
229 * @see Serializer#setOutputFormat(Properties)
231 public void setOutputFormat(Properties format)
233 m_handler.setOutputFormat(format);
237 * Sets the output stream to write to
238 * @param output the OutputStream to write to
239 * @see Serializer#setOutputStream(OutputStream)
241 public void setOutputStream(OutputStream output)
243 m_handler.setOutputStream(output);
247 * Sets the writer to write to
248 * @param writer the writer to write to
249 * @see Serializer#setWriter(Writer)
251 public void setWriter(Writer writer)
253 m_handler.setWriter(writer);
257 * Adds an attribute to the currenly open tag
258 * @param uri the URI of a namespace
259 * @param localName the attribute name, without prefix
260 * @param rawName the attribute name, with prefix (if any)
261 * @param type the type of the attribute, typically "CDATA"
262 * @param value the value of the parameter
263 * @param XSLAttribute true if this attribute is coming from an xsl:attribute element
264 * @see ExtendedContentHandler#addAttribute(String, String, String, String, String)
266 public void addAttribute(
272 boolean XSLAttribute)
275 if (m_firstTagNotEmitted)
279 m_handler.addAttribute(uri, localName, rawName, type, value, XSLAttribute);
282 * Adds an attribute to the currenly open tag
283 * @param rawName the attribute name, with prefix (if any)
284 * @param value the value of the parameter
285 * @see ExtendedContentHandler#addAttribute(String, String)
287 public void addAttribute(String rawName, String value)
289 if (m_firstTagNotEmitted)
293 m_handler.addAttribute(rawName, value);
298 * Adds a unique attribute to the currenly open tag
300 public void addUniqueAttribute(String rawName, String value, int flags)
303 if (m_firstTagNotEmitted)
307 m_handler.addUniqueAttribute(rawName, value, flags);
312 * Converts the String to a character array and calls the SAX method
313 * characters(char[],int,int);
315 * @see ExtendedContentHandler#characters(String)
317 public void characters(String chars) throws SAXException
319 final int length = chars.length();
320 if (length > m_charsBuff.length)
322 m_charsBuff = new char[length*2 + 1];
324 chars.getChars(0, length, m_charsBuff, 0);
325 this.characters(m_charsBuff, 0, length);
329 * Pass the call on to the underlying handler
330 * @see ExtendedContentHandler#endElement(String)
332 public void endElement(String elementName) throws SAXException
334 if (m_firstTagNotEmitted)
338 m_handler.endElement(elementName);
343 * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)
344 * @param prefix The prefix that maps to the URI
345 * @param uri The URI for the namespace
347 public void startPrefixMapping(String prefix, String uri) throws SAXException
349 this.startPrefixMapping(prefix,uri, true);
353 * This method is used when a prefix/uri namespace mapping
354 * is indicated after the element was started with a
355 * startElement() and before and endElement().
356 * startPrefixMapping(prefix,uri) would be used before the
357 * startElement() call.
358 * @param uri the URI of the namespace
359 * @param prefix the prefix associated with the given URI.
361 * @see ExtendedContentHandler#namespaceAfterStartElement(String, String)
363 public void namespaceAfterStartElement(String prefix, String uri)
366 // hack for XSLTC with finding URI for default namespace
367 if (m_firstTagNotEmitted && m_firstElementURI == null && m_firstElementName != null)
369 String prefix1 = getPrefixPart(m_firstElementName);
370 if (prefix1 == null && EMPTYSTRING.equals(prefix))
372 // the elements URI is not known yet, and it
373 // doesn't have a prefix, and we are currently
374 // setting the uri for prefix "", so we have
375 // the uri for the element... lets remember it
376 m_firstElementURI = uri;
379 startPrefixMapping(prefix,uri, false);
382 public boolean startPrefixMapping(String prefix, String uri, boolean shouldFlush)
385 boolean pushed = false;
386 if (m_firstTagNotEmitted)
388 if (m_firstElementName != null && shouldFlush)
390 /* we've already seen a startElement, and this is a prefix mapping
391 * for the up coming element, so flush the old element
392 * then send this event on its way.
395 pushed = m_handler.startPrefixMapping(prefix, uri, shouldFlush);
399 if (m_namespacePrefix == null)
401 m_namespacePrefix = new Vector();
402 m_namespaceURI = new Vector();
404 m_namespacePrefix.addElement(prefix);
405 m_namespaceURI.addElement(uri);
407 if (m_firstElementURI == null)
409 if (prefix.equals(m_firstElementPrefix))
410 m_firstElementURI = uri;
417 pushed = m_handler.startPrefixMapping(prefix, uri, shouldFlush);
423 * This method cannot be cached because default is different in
424 * HTML and XML (we need more than a boolean).
427 public void setVersion(String version)
429 m_handler.setVersion(version);
431 // Cache call to setVersion()
432 // super.setVersion(version);
433 m_setVersion_called = true;
437 * @see org.xml.sax.ContentHandler#startDocument()
439 public void startDocument() throws SAXException
441 m_needToCallStartDocument = true;
446 public void startElement(String qName) throws SAXException
448 this.startElement(null, null, qName, null);
451 public void startElement(String namespaceURI, String localName, String qName) throws SAXException
453 this.startElement(namespaceURI, localName, qName, null);
456 public void startElement(
460 Attributes atts) throws SAXException
462 /* we are notified of the start of an element */
463 if (m_firstTagNotEmitted)
465 /* we have not yet sent the first element on its way */
466 if (m_firstElementName != null)
468 /* this is not the first element, but a later one.
469 * But we have the old element pending, so flush it out,
470 * then send this one on its way.
473 m_handler.startElement(namespaceURI, localName, elementName, atts);
477 /* this is the very first element that we have seen,
478 * so save it for flushing later. We may yet get to know its
479 * URI due to added attributes.
482 m_wrapped_handler_not_initialized = true;
483 m_firstElementName = elementName;
486 m_firstElementPrefix = getPrefixPartUnknown(elementName);
489 m_firstElementURI = namespaceURI;
492 m_firstElementLocalName = localName;
494 if (m_tracer != null)
495 firePseudoElement(elementName);
497 /* we don't want to call our own addAttributes, which
498 * merely delegates to the wrapped handler, but we want to
499 * add these attributes to m_attributes. So me must call super.
500 * addAttributes() In this case m_attributes is only used for the
501 * first element, after that this class totally delegates to the
502 * wrapped handler which is either XML or HTML.
505 super.addAttributes(atts);
507 // if there are attributes, then lets make the flush()
508 // call the startElement on the handler and send the
509 // attributes on their way.
517 // this is not the first element, but a later one, so just
518 // send it on its way.
519 m_handler.startElement(namespaceURI, localName, elementName, atts);
524 * Pass the call on to the underlying handler
525 * @see ExtendedLexicalHandler#comment(String)
527 public void comment(String comment) throws SAXException
529 if (m_firstTagNotEmitted && m_firstElementName != null)
533 else if (m_needToCallStartDocument)
535 m_handler.startDocument();
536 m_needToCallStartDocument = false;
539 m_handler.comment(comment);
543 * Pass the call on to the underlying handler
544 * @see XSLOutputAttributes#getDoctypePublic()
546 public String getDoctypePublic()
549 return m_handler.getDoctypePublic();
553 * Pass the call on to the underlying handler
554 * @see XSLOutputAttributes#getDoctypeSystem()
556 public String getDoctypeSystem()
558 return m_handler.getDoctypeSystem();
562 * Pass the call on to the underlying handler
563 * @see XSLOutputAttributes#getEncoding()
565 public String getEncoding()
567 return m_handler.getEncoding();
571 * Pass the call on to the underlying handler
572 * @see XSLOutputAttributes#getIndent()
574 public boolean getIndent()
576 return m_handler.getIndent();
580 * Pass the call on to the underlying handler
581 * @see XSLOutputAttributes#getIndentAmount()
583 public int getIndentAmount()
585 return m_handler.getIndentAmount();
589 * Pass the call on to the underlying handler
590 * @see XSLOutputAttributes#getMediaType()
592 public String getMediaType()
594 return m_handler.getMediaType();
598 * Pass the call on to the underlying handler
599 * @see XSLOutputAttributes#getOmitXMLDeclaration()
601 public boolean getOmitXMLDeclaration()
603 return m_handler.getOmitXMLDeclaration();
607 * Pass the call on to the underlying handler
608 * @see XSLOutputAttributes#getStandalone()
610 public String getStandalone()
612 return m_handler.getStandalone();
616 * Pass the call on to the underlying handler
617 * @see XSLOutputAttributes#getVersion()
619 public String getVersion()
621 return m_handler.getVersion();
625 * @see XSLOutputAttributes#setDoctype(String, String)
627 public void setDoctype(String system, String pub)
629 m_handler.setDoctypePublic(pub);
630 m_handler.setDoctypeSystem(system);
634 * Set the doctype in the underlying XML handler. Remember that this method
635 * was called, just in case we need to transfer this doctype to an HTML handler
636 * @param doctype the public doctype to set
637 * @see XSLOutputAttributes#setDoctypePublic(String)
639 public void setDoctypePublic(String doctype)
641 m_handler.setDoctypePublic(doctype);
642 m_setDoctypePublic_called = true;
646 * Set the doctype in the underlying XML handler. Remember that this method
647 * was called, just in case we need to transfer this doctype to an HTML handler
648 * @param doctype the system doctype to set
649 * @see XSLOutputAttributes#setDoctypeSystem(String)
651 public void setDoctypeSystem(String doctype)
653 m_handler.setDoctypeSystem(doctype);
654 m_setDoctypeSystem_called = true;
658 * Pass the call on to the underlying handler
659 * @see XSLOutputAttributes#setEncoding(String)
661 public void setEncoding(String encoding)
663 m_handler.setEncoding(encoding);
667 * Pass the call on to the underlying handler
668 * @see XSLOutputAttributes#setIndent(boolean)
670 public void setIndent(boolean indent)
672 m_handler.setIndent(indent);
676 * Pass the call on to the underlying handler
678 public void setIndentAmount(int value)
680 m_handler.setIndentAmount(value);
684 * @see XSLOutputAttributes#setMediaType(String)
686 public void setMediaType(String mediaType)
688 m_handler.setMediaType(mediaType);
689 m_setMediaType_called = true;
693 * Pass the call on to the underlying handler
694 * @see XSLOutputAttributes#setOmitXMLDeclaration(boolean)
696 public void setOmitXMLDeclaration(boolean b)
698 m_handler.setOmitXMLDeclaration(b);
702 * Pass the call on to the underlying handler
703 * @see XSLOutputAttributes#setStandalone(String)
705 public void setStandalone(String standalone)
707 m_handler.setStandalone(standalone);
711 * @see XSLOutputAttributes#setVersion(String)
715 * Pass the call on to the underlying handler
716 * @see org.xml.sax.ext.DeclHandler#attributeDecl(String, String, String, String, String)
718 public void attributeDecl(
726 m_handler.attributeDecl(arg0, arg1, arg2, arg3, arg4);
730 * Pass the call on to the underlying handler
731 * @see org.xml.sax.ext.DeclHandler#elementDecl(String, String)
733 public void elementDecl(String arg0, String arg1) throws SAXException
735 if (m_firstTagNotEmitted)
739 m_handler.elementDecl(arg0, arg1);
743 * Pass the call on to the underlying handler
744 * @see org.xml.sax.ext.DeclHandler#externalEntityDecl(String, String, String)
746 public void externalEntityDecl(
752 if (m_firstTagNotEmitted)
756 m_handler.externalEntityDecl(name, publicId, systemId);
760 * Pass the call on to the underlying handler
761 * @see org.xml.sax.ext.DeclHandler#internalEntityDecl(String, String)
763 public void internalEntityDecl(String arg0, String arg1)
766 if (m_firstTagNotEmitted)
770 m_handler.internalEntityDecl(arg0, arg1);
774 * Pass the call on to the underlying handler
775 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
777 public void characters(char[] characters, int offset, int length)
780 if (m_firstTagNotEmitted)
785 m_handler.characters(characters, offset, length);
790 * Pass the call on to the underlying handler
791 * @see org.xml.sax.ContentHandler#endDocument()
793 public void endDocument() throws SAXException
795 if (m_firstTagNotEmitted)
800 m_handler.endDocument();
806 * Pass the call on to the underlying handler
807 * @see org.xml.sax.ContentHandler#endElement(String, String, String)
809 public void endElement(String namespaceURI, String localName, String qName)
812 if (m_firstTagNotEmitted)
815 if (namespaceURI == null && m_firstElementURI != null)
816 namespaceURI = m_firstElementURI;
819 if (localName == null && m_firstElementLocalName != null)
820 localName = m_firstElementLocalName;
823 m_handler.endElement(namespaceURI, localName, qName);
827 * Pass the call on to the underlying handler
828 * @see org.xml.sax.ContentHandler#endPrefixMapping(String)
830 public void endPrefixMapping(String prefix) throws SAXException
832 m_handler.endPrefixMapping(prefix);
836 * Pass the call on to the underlying handler
837 * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
839 public void ignorableWhitespace(char[] ch, int start, int length)
842 if (m_firstTagNotEmitted)
846 m_handler.ignorableWhitespace(ch, start, length);
850 * Pass the call on to the underlying handler
851 * @see org.xml.sax.ContentHandler#processingInstruction(String, String)
853 public void processingInstruction(String target, String data)
856 if (m_firstTagNotEmitted)
861 m_handler.processingInstruction(target, data);
865 * Pass the call on to the underlying handler
866 * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator)
868 public void setDocumentLocator(Locator locator)
870 m_handler.setDocumentLocator(locator);
874 * Pass the call on to the underlying handler
875 * @see org.xml.sax.ContentHandler#skippedEntity(String)
877 public void skippedEntity(String name) throws SAXException
879 m_handler.skippedEntity(name);
885 * Pass the call on to the underlying handler
886 * @see org.xml.sax.ext.LexicalHandler#comment(char[], int, int)
888 public void comment(char[] ch, int start, int length) throws SAXException
890 if (m_firstTagNotEmitted)
895 m_handler.comment(ch, start, length);
899 * Pass the call on to the underlying handler
900 * @see org.xml.sax.ext.LexicalHandler#endCDATA()
902 public void endCDATA() throws SAXException
905 m_handler.endCDATA();
909 * Pass the call on to the underlying handler
910 * @see org.xml.sax.ext.LexicalHandler#endDTD()
912 public void endDTD() throws SAXException
919 * Pass the call on to the underlying handler
920 * @see org.xml.sax.ext.LexicalHandler#endEntity(String)
922 public void endEntity(String name) throws SAXException
924 if (m_firstTagNotEmitted)
928 m_handler.endEntity(name);
932 * Pass the call on to the underlying handler
933 * @see org.xml.sax.ext.LexicalHandler#startCDATA()
935 public void startCDATA() throws SAXException
937 m_handler.startCDATA();
941 * Pass the call on to the underlying handler
942 * @see org.xml.sax.ext.LexicalHandler#startDTD(String, String, String)
944 public void startDTD(String name, String publicId, String systemId)
947 m_handler.startDTD(name, publicId, systemId);
951 * Pass the call on to the underlying handler
952 * @see org.xml.sax.ext.LexicalHandler#startEntity(String)
954 public void startEntity(String name) throws SAXException
956 m_handler.startEntity(name);
960 * Initialize the wrapped output stream (XML or HTML).
961 * If the stream handler should be HTML, then replace the XML handler with
962 * an HTML handler. After than send the starting method calls that were cached
963 * to the wrapped handler.
966 private void initStreamOutput() throws SAXException
969 // Try to rule out if this is an not to be an HTML document based on prefix
970 boolean firstElementIsHTML = isFirstElemHTML();
972 if (firstElementIsHTML)
974 // create an HTML output handler, and initialize it
976 // keep a reference to the old handler, ... it will soon be gone
977 SerializationHandler oldHandler = m_handler;
979 /* We have to make sure we get an output properties with the proper
980 * defaults for the HTML method. The easiest way to do this is to
981 * have the OutputProperties class do it.
984 Properties htmlProperties =
985 OutputPropertiesFactory.getDefaultMethodProperties(Method.HTML);
986 Serializer serializer =
987 SerializerFactory.getSerializer(htmlProperties);
989 // The factory should be returning a ToStream
990 // Don't know what to do if it doesn't
991 // i.e. the user has over-ridden the content-handler property
993 m_handler = (SerializationHandler) serializer;
994 //m_handler = new ToHTMLStream();
996 Writer writer = oldHandler.getWriter();
999 m_handler.setWriter(writer);
1002 OutputStream os = oldHandler.getOutputStream();
1005 m_handler.setOutputStream(os);
1008 // need to copy things from the old handler to the new one here
1010 // if (_setVersion_called)
1012 m_handler.setVersion(oldHandler.getVersion());
1014 // if (_setDoctypeSystem_called)
1016 m_handler.setDoctypeSystem(oldHandler.getDoctypeSystem());
1018 // if (_setDoctypePublic_called)
1020 m_handler.setDoctypePublic(oldHandler.getDoctypePublic());
1022 // if (_setMediaType_called)
1024 m_handler.setMediaType(oldHandler.getMediaType());
1027 m_handler.setTransformer(oldHandler.getTransformer());
1030 /* Now that we have a real wrapped handler (XML or HTML) lets
1031 * pass any cached calls to it
1033 // Call startDocument() if necessary
1034 if (m_needToCallStartDocument)
1036 m_handler.startDocument();
1037 m_needToCallStartDocument = false;
1040 // the wrapped handler is now fully initialized
1041 m_wrapped_handler_not_initialized = false;
1044 private void emitFirstTag() throws SAXException
1046 if (m_firstElementName != null)
1048 if (m_wrapped_handler_not_initialized)
1051 m_wrapped_handler_not_initialized = false;
1054 m_handler.startElement(m_firstElementURI, null, m_firstElementName, m_attributes);
1055 // don't need the collected attributes of the first element anymore.
1056 m_attributes = null;
1058 // Output namespaces of first tag
1059 if (m_namespacePrefix != null)
1061 final int n = m_namespacePrefix.size();
1062 for (int i = 0; i < n; i++)
1064 final String prefix =
1065 (String) m_namespacePrefix.elementAt(i);
1066 final String uri = (String) m_namespaceURI.elementAt(i);
1067 m_handler.startPrefixMapping(prefix, uri, false);
1069 m_namespacePrefix = null;
1070 m_namespaceURI = null;
1072 m_firstTagNotEmitted = false;
1077 * Utility function for calls to local-name().
1079 * Don't want to override static function on SerializerBase
1080 * So added Unknown suffix to method name.
1082 private String getLocalNameUnknown(String value)
1084 int idx = value.lastIndexOf(':');
1086 value = value.substring(idx + 1);
1087 idx = value.lastIndexOf('@');
1089 value = value.substring(idx + 1);
1094 * Utility function to return prefix
1096 * Don't want to override static function on SerializerBase
1097 * So added Unknown suffix to method name.
1099 private String getPrefixPartUnknown(String qname)
1101 final int index = qname.indexOf(':');
1102 return (index > 0) ? qname.substring(0, index) : EMPTYSTRING;
1106 * Determine if the firts element in the document is <html> or <HTML>
1107 * This uses the cached first element name, first element prefix and the
1108 * cached namespaces from previous method calls
1110 * @return true if the first element is an opening <html> tag
1112 private boolean isFirstElemHTML()
1116 // is the first tag html, not considering the prefix ?
1118 getLocalNameUnknown(m_firstElementName).equalsIgnoreCase("html");
1120 // Try to rule out if this is not to be an HTML document based on URI
1122 && m_firstElementURI != null
1123 && !EMPTYSTRING.equals(m_firstElementURI))
1125 // the <html> element has a non-trivial namespace
1128 // Try to rule out if this is an not to be an HTML document based on prefix
1129 if (isHTML && m_namespacePrefix != null)
1131 /* the first element has a name of "html", but lets check the prefix.
1132 * If the prefix points to a namespace with a URL that is not ""
1133 * then the doecument doesn't start with an <html> tag, and isn't html
1135 final int max = m_namespacePrefix.size();
1136 for (int i = 0; i < max; i++)
1138 final String prefix = (String) m_namespacePrefix.elementAt(i);
1139 final String uri = (String) m_namespaceURI.elementAt(i);
1141 if (m_firstElementPrefix != null
1142 && m_firstElementPrefix.equals(prefix)
1143 && !EMPTYSTRING.equals(uri))
1145 // The first element has a prefix, so it can't be <html>
1155 * @see Serializer#asDOMSerializer()
1157 public DOMSerializer asDOMSerializer() throws IOException
1159 return m_handler.asDOMSerializer();
1163 * @param URI_and_localNames Vector a list of pairs of URI/localName
1164 * specified in the cdata-section-elements attribute.
1165 * @see SerializationHandler#setCdataSectionElements(java.util.Vector)
1167 public void setCdataSectionElements(Vector URI_and_localNames)
1169 m_handler.setCdataSectionElements(URI_and_localNames);
1172 * @see ExtendedContentHandler#addAttributes(org.xml.sax.Attributes)
1174 public void addAttributes(Attributes atts) throws SAXException
1176 m_handler.addAttributes(atts);
1180 * Get the current namespace mappings.
1181 * Simply returns the mappings of the wrapped handler.
1182 * @see ExtendedContentHandler#getNamespaceMappings()
1184 public NamespaceMappings getNamespaceMappings()
1186 NamespaceMappings mappings = null;
1187 if (m_handler != null)
1189 mappings = m_handler.getNamespaceMappings();
1194 * @see SerializationHandler#flushPending()
1196 public void flushPending() throws SAXException
1201 m_handler.flushPending();
1204 private void flush()
1208 if (m_firstTagNotEmitted)
1212 if (m_needToCallStartDocument)
1214 m_handler.startDocument();
1215 m_needToCallStartDocument = false;
1218 catch(SAXException e)
1220 throw new RuntimeException(e.toString());
1227 * @see ExtendedContentHandler#getPrefix
1229 public String getPrefix(String namespaceURI)
1231 return m_handler.getPrefix(namespaceURI);
1234 * @see ExtendedContentHandler#entityReference(java.lang.String)
1236 public void entityReference(String entityName) throws SAXException
1238 m_handler.entityReference(entityName);
1242 * @see ExtendedContentHandler#getNamespaceURI(java.lang.String, boolean)
1244 public String getNamespaceURI(String qname, boolean isElement)
1246 return m_handler.getNamespaceURI(qname, isElement);
1249 public String getNamespaceURIFromPrefix(String prefix)
1251 return m_handler.getNamespaceURIFromPrefix(prefix);
1254 public void setTransformer(Transformer t)
1256 m_handler.setTransformer(t);
1257 if ((t instanceof SerializerTrace) &&
1258 (((SerializerTrace) t).hasTraceListeners())) {
1259 m_tracer = (SerializerTrace) t;
1264 public Transformer getTransformer()
1266 return m_handler.getTransformer();
1270 * @see SerializationHandler#setContentHandler(org.xml.sax.ContentHandler)
1272 public void setContentHandler(ContentHandler ch)
1274 m_handler.setContentHandler(ch);
1277 * This method is used to set the source locator, which might be used to
1278 * generated an error message.
1279 * @param locator the source locator
1281 * @see ExtendedContentHandler#setSourceLocator(javax.xml.transform.SourceLocator)
1283 public void setSourceLocator(SourceLocator locator)
1285 m_handler.setSourceLocator(locator);
1288 protected void firePseudoElement(String elementName)
1291 if (m_tracer != null) {
1292 StringBuffer sb = new StringBuffer();
1295 sb.append(elementName);
1297 // convert the StringBuffer to a char array and
1298 // emit the trace event that these characters "might"
1300 char ch[] = sb.toString().toCharArray();
1301 m_tracer.fireGenerateEvent(
1302 SerializerTrace.EVENTTYPE_OUTPUT_PSEUDO_CHARACTERS,
1310 * @see org.apache.xml.serializer.Serializer#asDOM3Serializer()
1312 public Object asDOM3Serializer() throws IOException
1314 return m_handler.asDOM3Serializer();