tounknownstream.java
来自「JAVA 所有包」· Java 代码 · 共 1,314 行 · 第 1/3 页
JAVA
1,314 行
/** * Pass the call on to the underlying handler * @see org.xml.sax.ContentHandler#skippedEntity(String) */ public void skippedEntity(String name) throws SAXException { m_handler.skippedEntity(name); } /** * Pass the call on to the underlying handler * @see org.xml.sax.ext.LexicalHandler#comment(char[], int, int) */ public void comment(char[] ch, int start, int length) throws SAXException { if (m_firstTagNotEmitted) { flush(); } m_handler.comment(ch, start, length); } /** * Pass the call on to the underlying handler * @see org.xml.sax.ext.LexicalHandler#endCDATA() */ public void endCDATA() throws SAXException { m_handler.endCDATA(); } /** * Pass the call on to the underlying handler * @see org.xml.sax.ext.LexicalHandler#endDTD() */ public void endDTD() throws SAXException { m_handler.endDTD(); } /** * Pass the call on to the underlying handler * @see org.xml.sax.ext.LexicalHandler#endEntity(String) */ public void endEntity(String name) throws SAXException { if (m_firstTagNotEmitted) { emitFirstTag(); } m_handler.endEntity(name); } /** * Pass the call on to the underlying handler * @see org.xml.sax.ext.LexicalHandler#startCDATA() */ public void startCDATA() throws SAXException { m_handler.startCDATA(); } /** * Pass the call on to the underlying handler * @see org.xml.sax.ext.LexicalHandler#startDTD(String, String, String) */ public void startDTD(String name, String publicId, String systemId) throws SAXException { m_handler.startDTD(name, publicId, systemId); } /** * Pass the call on to the underlying handler * @see org.xml.sax.ext.LexicalHandler#startEntity(String) */ public void startEntity(String name) throws SAXException { m_handler.startEntity(name); } /** * Initialize the wrapped output stream (XML or HTML). * If the stream handler should be HTML, then replace the XML handler with * an HTML handler. After than send the starting method calls that were cached * to the wrapped handler. * */ private void initStreamOutput() throws SAXException { // Try to rule out if this is an not to be an HTML document based on prefix boolean firstElementIsHTML = isFirstElemHTML(); if (firstElementIsHTML) { // create an HTML output handler, and initialize it // keep a reference to the old handler, ... it will soon be gone SerializationHandler oldHandler = m_handler; /* We have to make sure we get an output properties with the proper * defaults for the HTML method. The easiest way to do this is to * have the OutputProperties class do it. */ Properties htmlProperties = OutputPropertiesFactory.getDefaultMethodProperties(Method.HTML); Serializer serializer = SerializerFactory.getSerializer(htmlProperties); // The factory should be returning a ToStream // Don't know what to do if it doesn't // i.e. the user has over-ridden the content-handler property // for html m_handler = (SerializationHandler) serializer; //m_handler = new ToHTMLStream(); Writer writer = oldHandler.getWriter(); if (null != writer) m_handler.setWriter(writer); else { OutputStream os = oldHandler.getOutputStream(); if (null != os) m_handler.setOutputStream(os); } // need to copy things from the old handler to the new one here // if (_setVersion_called) // { m_handler.setVersion(oldHandler.getVersion()); // } // if (_setDoctypeSystem_called) // { m_handler.setDoctypeSystem(oldHandler.getDoctypeSystem()); // } // if (_setDoctypePublic_called) // { m_handler.setDoctypePublic(oldHandler.getDoctypePublic()); // } // if (_setMediaType_called) // { m_handler.setMediaType(oldHandler.getMediaType()); // } m_handler.setTransformer(oldHandler.getTransformer()); } /* Now that we have a real wrapped handler (XML or HTML) lets * pass any cached calls to it */ // Call startDocument() if necessary if (m_needToCallStartDocument) { m_handler.startDocument(); m_needToCallStartDocument = false; } // the wrapped handler is now fully initialized m_wrapped_handler_not_initialized = false; } private void emitFirstTag() throws SAXException { if (m_firstElementName != null) { if (m_wrapped_handler_not_initialized) { initStreamOutput(); m_wrapped_handler_not_initialized = false; } // Output first tag m_handler.startElement(m_firstElementURI, null, m_firstElementName, m_attributes); // don't need the collected attributes of the first element anymore. m_attributes = null; // Output namespaces of first tag if (m_namespacePrefix != null) { final int n = m_namespacePrefix.size(); for (int i = 0; i < n; i++) { final String prefix = (String) m_namespacePrefix.elementAt(i); final String uri = (String) m_namespaceURI.elementAt(i); m_handler.startPrefixMapping(prefix, uri, false); } m_namespacePrefix = null; m_namespaceURI = null; } m_firstTagNotEmitted = false; } } /** * Utility function for calls to local-name(). * * Don't want to override static function on SerializerBase * So added Unknown suffix to method name. */ private String getLocalNameUnknown(String value) { int idx = value.lastIndexOf(':'); if (idx >= 0) value = value.substring(idx + 1); idx = value.lastIndexOf('@'); if (idx >= 0) value = value.substring(idx + 1); return (value); } /** * Utility function to return prefix * * Don't want to override static function on SerializerBase * So added Unknown suffix to method name. */ private String getPrefixPartUnknown(String qname) { final int index = qname.indexOf(':'); return (index > 0) ? qname.substring(0, index) : EMPTYSTRING; } /** * Determine if the firts element in the document is <html> or <HTML> * This uses the cached first element name, first element prefix and the * cached namespaces from previous method calls * * @return true if the first element is an opening <html> tag */ private boolean isFirstElemHTML() { boolean isHTML; // is the first tag html, not considering the prefix ? isHTML = getLocalNameUnknown(m_firstElementName).equalsIgnoreCase("html"); // Try to rule out if this is not to be an HTML document based on URI if (isHTML && m_firstElementURI != null && !EMPTYSTRING.equals(m_firstElementURI)) { // the <html> element has a non-trivial namespace isHTML = false; } // Try to rule out if this is an not to be an HTML document based on prefix if (isHTML && m_namespacePrefix != null) { /* the first element has a name of "html", but lets check the prefix. * If the prefix points to a namespace with a URL that is not "" * then the doecument doesn't start with an <html> tag, and isn't html */ final int max = m_namespacePrefix.size(); for (int i = 0; i < max; i++) { final String prefix = (String) m_namespacePrefix.elementAt(i); final String uri = (String) m_namespaceURI.elementAt(i); if (m_firstElementPrefix != null && m_firstElementPrefix.equals(prefix) && !EMPTYSTRING.equals(uri)) { // The first element has a prefix, so it can't be <html> isHTML = false; break; } } } return isHTML; } /** * @see Serializer#asDOMSerializer() */ public DOMSerializer asDOMSerializer() throws IOException { return m_handler.asDOMSerializer(); } /** * @param URI_and_localNames Vector a list of pairs of URI/localName * specified in the cdata-section-elements attribute. * @see SerializationHandler#setCdataSectionElements(java.util.Vector) */ public void setCdataSectionElements(Vector URI_and_localNames) { m_handler.setCdataSectionElements(URI_and_localNames); } /** * @see ExtendedContentHandler#addAttributes(org.xml.sax.Attributes) */ public void addAttributes(Attributes atts) throws SAXException { m_handler.addAttributes(atts); } /** * Get the current namespace mappings. * Simply returns the mappings of the wrapped handler. * @see ExtendedContentHandler#getNamespaceMappings() */ public NamespaceMappings getNamespaceMappings() { NamespaceMappings mappings = null; if (m_handler != null) { mappings = m_handler.getNamespaceMappings(); } return mappings; } /** * @see SerializationHandler#flushPending() */ public void flushPending() throws SAXException { flush(); m_handler.flushPending(); } private void flush() { try { if (m_firstTagNotEmitted) { emitFirstTag(); } if (m_needToCallStartDocument) { m_handler.startDocument(); m_needToCallStartDocument = false; } } catch(SAXException e) { throw new RuntimeException(e.toString()); } } /** * @see ExtendedContentHandler#getPrefix */ public String getPrefix(String namespaceURI) { return m_handler.getPrefix(namespaceURI); } /** * @see ExtendedContentHandler#entityReference(java.lang.String) */ public void entityReference(String entityName) throws SAXException { m_handler.entityReference(entityName); } /** * @see ExtendedContentHandler#getNamespaceURI(java.lang.String, boolean) */ public String getNamespaceURI(String qname, boolean isElement) { return m_handler.getNamespaceURI(qname, isElement); } public String getNamespaceURIFromPrefix(String prefix) { return m_handler.getNamespaceURIFromPrefix(prefix); } public void setTransformer(Transformer t) { m_handler.setTransformer(t); if ((t instanceof SerializerTrace) && (((SerializerTrace) t).hasTraceListeners())) { m_tracer = (SerializerTrace) t; } else { m_tracer = null; } } public Transformer getTransformer() { return m_handler.getTransformer(); } /** * @see SerializationHandler#setContentHandler(org.xml.sax.ContentHandler) */ public void setContentHandler(ContentHandler ch) { m_handler.setContentHandler(ch); } /** * This method is used to set the source locator, which might be used to * generated an error message. * @param locator the source locator * * @see ExtendedContentHandler#setSourceLocator(javax.xml.transform.SourceLocator) */ public void setSourceLocator(SourceLocator locator) { m_handler.setSourceLocator(locator); } protected void firePseudoElement(String elementName) { if (m_tracer != null) { StringBuffer sb = new StringBuffer(); sb.append('<'); sb.append(elementName); // convert the StringBuffer to a char array and // emit the trace event that these characters "might" // be written char ch[] = sb.toString().toCharArray(); m_tracer.fireGenerateEvent( SerializerTrace.EVENTTYPE_OUTPUT_PSEUDO_CHARACTERS, ch, 0, ch.length); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?