resulttreehandler.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,929 行 · 第 1/4 页
JAVA
1,929 行
/** * Given a result tree fragment, walk the tree and * output it to the result stream. * * @param obj Result tree fragment object * @param support XPath context for the result tree fragment * * @throws org.xml.sax.SAXException */ public void outputResultTreeFragment(XObject obj, XPathContext support) throws org.xml.sax.SAXException { int doc = obj.rtf(); DTM dtm = support.getDTM(doc); if(null != dtm) { for (int n = dtm.getFirstChild(doc); DTM.NULL != n; n = dtm.getNextSibling(n)) { flushPending(true); // I think. startPrefixMapping("",""); dtm.dispatchToEvents(n, this); } } } /** * To fullfill the FormatterListener interface... no action * for the moment. * * @param locator Document locator */ public void setDocumentLocator(Locator locator){} /** * This function checks to make sure a given prefix is really * declared. It might not be, because it may be an excluded prefix. * If it's not, it still needs to be declared at this point. * TODO: This needs to be done at an earlier stage in the game... -sb * * @param ns Namespace URI of the element * @param rawName Raw name of element (with prefix) * * @throws org.xml.sax.SAXException */ public void ensurePrefixIsDeclared(String ns, String rawName) throws org.xml.sax.SAXException { if (ns != null && ns.length() > 0) { int index; String prefix = (index = rawName.indexOf(":")) < 0 ? "" : rawName.substring(0, index); if (null != prefix) { String foundURI = m_nsSupport.getURI(prefix); if ((null == foundURI) ||!foundURI.equals(ns)) { startPrefixMapping(prefix, ns, false); // Bugzilla1133: Generate attribute as well as namespace event. // SAX does expect both. m_attributes.addAttribute("http://www.w3.org/2000/xmlns/", prefix, "xmlns"+(prefix.length()==0 ? "" : ":")+prefix, "CDATA", ns); } } } } /** * This function checks to make sure a given prefix is really * declared. It might not be, because it may be an excluded prefix. * If it's not, it still needs to be declared at this point. * TODO: This needs to be done at an earlier stage in the game... -sb * * @param ns Namespace URI of the element * @param rawName Raw name of element (with prefix) * * NEEDSDOC @param dtm * NEEDSDOC @param namespace * * @throws org.xml.sax.SAXException */ public void ensureNamespaceDeclDeclared(DTM dtm, int namespace) throws org.xml.sax.SAXException { String uri = dtm.getNodeValue(namespace); String prefix = dtm.getNodeNameX(namespace); if ((uri != null && uri.length() > 0) && (null != prefix)) { String foundURI = m_nsSupport.getURI(prefix); if ((null == foundURI) ||!foundURI.equals(uri)) { startPrefixMapping(prefix, uri, false); } } } /** * Add the attributes that have been declared to the attribute list. * (Seems like I shouldn't have to do this...) * Internally deprecated in favor of combined startAndAddPrefixMappings(); * * * @throws org.xml.sax.SAXException */ protected void sendStartPrefixMappings() throws org.xml.sax.SAXException { Enumeration prefixes = m_nsSupport.getDeclaredPrefixes(); ContentHandler handler = m_contentHandler; while (prefixes.hasMoreElements()) { String prefix = (String) prefixes.nextElement(); handler.startPrefixMapping(prefix, m_nsSupport.getURI(prefix)); } } /** * Combination of sendStartPrefixMappings and * addNSDeclsToAttrs() (which it mostly replaces). Merging the two * loops is significantly more efficient. * * @throws org.xml.sax.SAXException */ protected void startAndAddPrefixMappings() throws org.xml.sax.SAXException { Enumeration prefixes = m_nsSupport.getDeclaredPrefixes(); ContentHandler handler = m_contentHandler; while (prefixes.hasMoreElements()) { String prefix = (String) prefixes.nextElement(); String uri=m_nsSupport.getURI(prefix); // Send event handler.startPrefixMapping(prefix, uri); // Set attribute boolean isDefault = (prefix.length() == 0); String name; if (isDefault) { //prefix = "xml"; name = "xmlns"; } else name = "xmlns:" + prefix; if (null == uri) uri = ""; m_attributes.addAttribute("http://www.w3.org/2000/xmlns/", prefix, name, "CDATA", uri); } m_nsDeclsHaveBeenAdded=true; } /** * Add the attributes that have been declared to the attribute list. * (Seems like I shouldn't have to do this...) * * @throws org.xml.sax.SAXException */ protected void sendEndPrefixMappings() throws org.xml.sax.SAXException { Enumeration prefixes = m_nsSupport.getDeclaredPrefixes(); ContentHandler handler = m_contentHandler; while (prefixes.hasMoreElements()) { String prefix = (String) prefixes.nextElement(); handler.endPrefixMapping(prefix); } } /** * Check to see if we should switch serializers based on the * first output element being an HTML element. * * @param ns Namespace URI of the element * @param localName Local part of name of the element * * @throws org.xml.sax.SAXException */ private void checkForSerializerSwitch(String ns, String localName) throws org.xml.sax.SAXException { try { if (m_docPending) { SerializerSwitcher.switchSerializerIfHTML(m_transformer, ns, localName); } } catch (TransformerException te) { throw new org.xml.sax.SAXException(te); } } /** * Add the attributes that have been declared to the attribute list. * * %REVIEW% This should have been done automatically during * flushPending(boolean); is it ever explicitly reinvoked? */ public void addNSDeclsToAttrs() { Enumeration prefixes = m_nsSupport.getDeclaredPrefixes(); while (prefixes.hasMoreElements()) { String prefix = (String) prefixes.nextElement(); boolean isDefault = (prefix.length() == 0); String name; if (isDefault) { //prefix = "xml"; name = "xmlns"; } else name = "xmlns:" + prefix; String uri = m_nsSupport.getURI(prefix); if (null == uri) uri = ""; m_attributes.addAttribute("http://www.w3.org/2000/xmlns/", prefix, name, "CDATA", uri); m_nsDeclsHaveBeenAdded = true; } } /** * Copy <KBD>xmlns:</KBD> attributes in if not already in scope. * * As a quick hack to support ClonerToResultTree, this can also be used * to copy an individual namespace node. * * @param src Source Node * NEEDSDOC @param type * NEEDSDOC @param dtm * * @throws TransformerException */ public void processNSDecls(int src, int type, DTM dtm) throws TransformerException { try { if (type == DTM.ELEMENT_NODE) { for (int namespace = dtm.getFirstNamespaceNode(src, true); DTM.NULL != namespace; namespace = dtm.getNextNamespaceNode(src, namespace, true)) { // String prefix = dtm.getPrefix(namespace); String prefix = dtm.getNodeNameX(namespace); String desturi = getURI(prefix); String srcURI = dtm.getNodeValue(namespace); if (!srcURI.equalsIgnoreCase(desturi)) { this.startPrefixMapping(prefix, srcURI, false); } } } else if (type == DTM.NAMESPACE_NODE) { String prefix = dtm.getNodeNameX(src); String desturi = getURI(prefix); String srcURI = dtm.getNodeValue(src); if (!srcURI.equalsIgnoreCase(desturi)) { this.startPrefixMapping(prefix, srcURI, false); } } } catch (org.xml.sax.SAXException se) { throw new TransformerException(se); } } /** * Given a prefix, return the namespace, * * @param prefix Given prefix name * * @return Namespace associated with the given prefix, or null */ public String getURI(String prefix) { return m_nsSupport.getURI(prefix); } /** * Given a namespace, try and find a prefix. * * @param namespace Given namespace URI * * @return Prefix name associated with namespace URI */ public String getPrefix(String namespace) { // This Enumeration business may be too slow for our purposes... Enumeration enum = m_nsSupport.getPrefixes(); while (enum.hasMoreElements()) { String prefix = (String) enum.nextElement(); if (m_nsSupport.getURI(prefix).equals(namespace)) return prefix; } return null; } /** * Get the NamespaceSupport object. * * @return NamespaceSupport object. */ public NamespaceSupport getNamespaceSupport() { return m_nsSupport; } // /** // * Override QueuedEvents#initQSE. // * // * @param qse Give queued Sax event // */ // protected void initQSE(QueuedSAXEvent qse) // { // // // qse.setContentHandler(m_contentHandler); // // qse.setTransformer(m_transformer); // // qse.setTraceManager(m_tracer); // } /** * Return the current content handler. * * @return The current content handler, or null if none * has been registered. * @see #setContentHandler */ public final ContentHandler getContentHandler() { return m_contentHandler; } /** * Set the current content handler. * * * @param ch Content Handler to be set * @return The current content handler, or null if none * has been registered. * @see #getContentHandler */ public void setContentHandler(ContentHandler ch) { m_contentHandler = ch; m_isTransformClient = (m_contentHandler instanceof TransformerClient); if (m_contentHandler instanceof LexicalHandler) m_lexicalHandler = (LexicalHandler) m_contentHandler; else m_lexicalHandler = null; reInitEvents(); } /** * Get a unique namespace value. * * @return a unique namespace value to be used with a * fabricated prefix */ public int getUniqueNSValue() { return m_uniqueNSValue++; } /** * Get new unique namespace prefix. * * @return Unique fabricated prefix. */ public String getNewUniqueNSPrefix() { return S_NAMESPACEPREFIX + String.valueOf(getUniqueNSValue()); } /** * Get the pending attributes. We have to delay the call to * m_flistener.startElement(name, atts) because of the * xsl:attribute and xsl:copy calls. In other words, * the attributes have to be fully collected before you * can call startElement. * * @return the pending attributes. */ public MutableAttrListImpl getPendingAttributes() { return m_attributes; } /** * Add an attribute to the end of the list. * * <p>Do not pass in xmlns decls to this function! * * <p>For the sake of speed, this method does no checking * to see if the attribute is already in the list: that is * the responsibility of the application.</p> * * @param uri The Namespace URI, or the empty string if * none is available or Namespace processing is not * being performed. * @param localName The local name, or the empty string if * Namespace processing is not being performed. * @param rawName The raw XML 1.0 name, or the empty string * if raw names are not available. * @param type The attribute type as a string. * @param value The attribute value. * * @throws TransformerException */ public void addAttribute( String uri, String localName, String rawName, String type, String value) throws TransformerException { // %REVIEW% See Bugzilla 4344. Do we need an "else" that announces // an error? Technically, this can't happen unless the stylesheet // is unreasonable... but it's unclear whether silent or noisy // failure is called for. // Will add an "else" and emit a warning message. This should // cover testcases such as copyerr04-07, attribset19,34,35, // attribseterr08...(is) if (m_elemIsPending) { // %REVIEW% %OPT% Is this ever needed????? // The check is not needed. See Bugzilla 10306. // if (!m_nsDeclsHaveBeenAdded) addNSDeclsToAttrs(); if (null == uri) { // defensive, should not really need this. uri = ""; } try {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?