tounknownstream.java
来自「JAVA 所有包」· Java 代码 · 共 1,314 行 · 第 1/3 页
JAVA
1,314 行
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed 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: ToUnknownStream.java,v 1.3 2005/09/28 13:49:08 pvedula Exp $ */package com.sun.org.apache.xml.internal.serializer;import java.io.IOException;import java.io.OutputStream;import java.io.Writer;import java.util.Properties;import java.util.Vector;import javax.xml.transform.SourceLocator;import javax.xml.transform.Transformer;import org.w3c.dom.Node;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.Locator;import org.xml.sax.SAXException;/** *This class wraps another SerializationHandler. The wrapped object will either * handler XML or HTML, which is not known until a little later when the first XML * tag is seen. If the first tag is <html> then the wrapped object is an HTML * handler, otherwise it is an XML handler. * * This class effectively caches the first few calls to it then passes them * on to the wrapped handler (once it exists). After that subsequent calls a * simply passed directly to the wrapped handler. * * The user of this class doesn't know if the output is ultimatley XML or HTML. * * This class is not a public API, it is public because it is used within Xalan. * @xsl.usage internal */public final class ToUnknownStream extends SerializerBase{ /** * The wrapped handler, initially XML but possibly switched to HTML */ private SerializationHandler m_handler; /** * A String with no characters */ private static final String EMPTYSTRING = ""; /** * true if the underlying handler (XML or HTML) is fully initialized */ private boolean m_wrapped_handler_not_initialized = false; /** * the prefix of the very first tag in the document */ private String m_firstElementPrefix; /** * the element name (including any prefix) of the very first tag in the document */ private String m_firstElementName; /** * the namespace URI associated with the first element */ private String m_firstElementURI; /** * the local name (no prefix) associated with the first element */ private String m_firstElementLocalName = null; /** * true if the first tag has been emitted to the wrapped handler */ private boolean m_firstTagNotEmitted = true; /** * A collection of namespace URI's (only for first element). * _namespacePrefix has the matching prefix for these URI's */ private Vector m_namespaceURI = null; /** * A collection of namespace Prefix (only for first element) * _namespaceURI has the matching URIs for these prefix' */ private Vector m_namespacePrefix = null; /** * true if startDocument() was called before the underlying handler * was initialized */ private boolean m_needToCallStartDocument = false; /** * true if setVersion() was called before the underlying handler * was initialized */ private boolean m_setVersion_called = false; /** * true if setDoctypeSystem() was called before the underlying handler * was initialized */ private boolean m_setDoctypeSystem_called = false; /** * true if setDoctypePublic() was called before the underlying handler * was initialized */ private boolean m_setDoctypePublic_called = false; /** * true if setMediaType() was called before the underlying handler * was initialized */ private boolean m_setMediaType_called = false; /** * Default constructor. * Initially this object wraps an XML Stream object, so _handler is never null. * That may change later to an HTML Stream object. */ public ToUnknownStream() { m_handler = new ToXMLStream(); } /** * @see Serializer#asContentHandler() * @return the wrapped XML or HTML handler */ public ContentHandler asContentHandler() throws IOException { /* don't return the real handler ( m_handler ) because * that would expose the real handler to the outside. * Keep m_handler private so it can be internally swapped * to an HTML handler. */ return this; } /** * @see SerializationHandler#close() */ public void close() { m_handler.close(); } /** * @see Serializer#getOutputFormat() * @return the properties of the underlying handler */ public Properties getOutputFormat() { return m_handler.getOutputFormat(); } /** * @see Serializer#getOutputStream() * @return the OutputStream of the underlying XML or HTML handler */ public OutputStream getOutputStream() { return m_handler.getOutputStream(); } /** * @see Serializer#getWriter() * @return the Writer of the underlying XML or HTML handler */ public Writer getWriter() { return m_handler.getWriter(); } /** * passes the call on to the underlying HTML or XML handler * @see Serializer#reset() * @return ??? */ public boolean reset() { return m_handler.reset(); } /** * Converts the DOM node to output * @param node the DOM node to transform to output * @see DOMSerializer#serialize(Node) * */ public void serialize(Node node) throws IOException { if (m_firstTagNotEmitted) { flush(); } m_handler.serialize(node); } /** * @see SerializationHandler#setEscaping(boolean) */ public boolean setEscaping(boolean escape) throws SAXException { return m_handler.setEscaping(escape); } /** * Set the properties of the handler * @param format the output properties to set * @see Serializer#setOutputFormat(Properties) */ public void setOutputFormat(Properties format) { m_handler.setOutputFormat(format); } /** * Sets the output stream to write to * @param output the OutputStream to write to * @see Serializer#setOutputStream(OutputStream) */ public void setOutputStream(OutputStream output) { m_handler.setOutputStream(output); } /** * Sets the writer to write to * @param writer the writer to write to * @see Serializer#setWriter(Writer) */ public void setWriter(Writer writer) { m_handler.setWriter(writer); } /** * Adds an attribute to the currenly open tag * @param uri the URI of a namespace * @param localName the attribute name, without prefix * @param rawName the attribute name, with prefix (if any) * @param type the type of the attribute, typically "CDATA" * @param value the value of the parameter * @param XSLAttribute true if this attribute is coming from an xsl:attribute element * @see ExtendedContentHandler#addAttribute(String, String, String, String, String) */ public void addAttribute( String uri, String localName, String rawName, String type, String value, boolean XSLAttribute) throws SAXException { if (m_firstTagNotEmitted) { flush(); } m_handler.addAttribute(uri, localName, rawName, type, value, XSLAttribute); } /** * Adds an attribute to the currenly open tag * @param rawName the attribute name, with prefix (if any) * @param value the value of the parameter * @see ExtendedContentHandler#addAttribute(String, String) */ public void addAttribute(String rawName, String value) { if (m_firstTagNotEmitted) { flush(); } m_handler.addAttribute(rawName, value); } /** * Adds a unique attribute to the currenly open tag */ public void addUniqueAttribute(String rawName, String value, int flags) throws SAXException { if (m_firstTagNotEmitted) { flush(); } m_handler.addUniqueAttribute(rawName, value, flags); } /** * Converts the String to a character array and calls the SAX method * characters(char[],int,int); * * @see ExtendedContentHandler#characters(String) */ public void characters(String chars) throws SAXException { final int length = chars.length(); if (length > m_charsBuff.length) { m_charsBuff = new char[length*2 + 1]; } chars.getChars(0, length, m_charsBuff, 0); this.characters(m_charsBuff, 0, length); } /** * Pass the call on to the underlying handler * @see ExtendedContentHandler#endElement(String) */ public void endElement(String elementName) throws SAXException { if (m_firstTagNotEmitted) { flush(); } m_handler.endElement(elementName); } /** * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String) * @param prefix The prefix that maps to the URI * @param uri The URI for the namespace */ public void startPrefixMapping(String prefix, String uri) throws SAXException { this.startPrefixMapping(prefix,uri, true); } /** * This method is used when a prefix/uri namespace mapping * is indicated after the element was started with a * startElement() and before and endElement(). * startPrefixMapping(prefix,uri) would be used before the * startElement() call. * @param uri the URI of the namespace * @param prefix the prefix associated with the given URI. * * @see ExtendedContentHandler#namespaceAfterStartElement(String, String) */ public void namespaceAfterStartElement(String prefix, String uri) throws SAXException { // hack for XSLTC with finding URI for default namespace if (m_firstTagNotEmitted && m_firstElementURI == null && m_firstElementName != null) { String prefix1 = getPrefixPart(m_firstElementName); if (prefix1 == null && EMPTYSTRING.equals(prefix)) { // the elements URI is not known yet, and it // doesn't have a prefix, and we are currently // setting the uri for prefix "", so we have // the uri for the element... lets remember it m_firstElementURI = uri; } } startPrefixMapping(prefix,uri, false); } public boolean startPrefixMapping(String prefix, String uri, boolean shouldFlush) throws SAXException { boolean pushed = false; if (m_firstTagNotEmitted) { if (m_firstElementName != null && shouldFlush) { /* we've already seen a startElement, and this is a prefix mapping * for the up coming element, so flush the old element * then send this event on its way. */ flush(); pushed = m_handler.startPrefixMapping(prefix, uri, shouldFlush); } else { if (m_namespacePrefix == null) { m_namespacePrefix = new Vector(); m_namespaceURI = new Vector(); } m_namespacePrefix.addElement(prefix); m_namespaceURI.addElement(uri); if (m_firstElementURI == null) { if (prefix.equals(m_firstElementPrefix)) m_firstElementURI = uri; } } } else { pushed = m_handler.startPrefixMapping(prefix, uri, shouldFlush); } return pushed; } /** * This method cannot be cached because default is different in * HTML and XML (we need more than a boolean). */ public void setVersion(String version) { m_handler.setVersion(version); // Cache call to setVersion() // super.setVersion(version); m_setVersion_called = true; } /** * @see org.xml.sax.ContentHandler#startDocument() */ public void startDocument() throws SAXException {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?