resulttreehandler.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,929 行 · 第 1/4 页

JAVA
1,929
字号
            if (!rawName.equals("xmlns")) { // don't handle xmlns default namespace.                ensurePrefixIsDeclared(uri, rawName);            }            } catch (org.xml.sax.SAXException se) {            throw new TransformerException(se);        }              if (DEBUG) {            System.out.println("ResultTreeHandler#addAttribute Adding attr: "			   + localName + ", " + uri);        }                if (!isDefinedNSDecl(rawName, value)) {            m_attributes.addAttribute(uri, localName, rawName, type, value);        }    } else {        m_transformer.getMsgMgr().warn(m_stylesheetRoot,                                   XSLTErrorResources.WG_ILLEGAL_ATTRIBUTE_POSITION,                                   new Object[]{ localName });    }}  /**   * Return whether or not a namespace declaration is defined   *   *   * @param rawName Raw name of namespace element   * @param value URI of given namespace   *   * @return True if the namespace is already defined in list of   * namespaces   */  public boolean isDefinedNSDecl(String rawName, String value)  {    if (rawName.equals("xmlns") || rawName.startsWith("xmlns:"))    {      int index;      String prefix = (index = rawName.indexOf(":")) < 0                      ? "" : rawName.substring(0, index);      String definedURI = m_nsSupport.getURI(prefix);      if (null != definedURI)      {        if (definedURI.equals(value))        {          return true;        }        else          return false;      }      else        return false;    }    else      return false;  }  /**   * Returns whether a namespace is defined   *   *   * @param attr Namespace attribute node   *   * @return True if the namespace is already defined in   * list of namespaces   */  public boolean isDefinedNSDecl(int attr)  {    DTM dtm = m_transformer.getXPathContext().getDTM(attr);    if (DTM.NAMESPACE_NODE == dtm.getNodeType(attr))    {      // String prefix = dtm.getPrefix(attr);      String prefix = dtm.getNodeNameX(attr);      String uri = getURI(prefix);      if ((null != uri) && uri.equals(dtm.getStringValue(attr)))        return true;    }    return false;  }  /**   * Returns whether a namespace is defined   *   *   * @param attr Namespace attribute node   * @param dtm The DTM that owns attr.   *   * @return True if the namespace is already defined in   * list of namespaces   */  public boolean isDefinedNSDecl(int attr, DTM dtm)  {    if (DTM.NAMESPACE_NODE == dtm.getNodeType(attr))    {      // String prefix = dtm.getPrefix(attr);      String prefix = dtm.getNodeNameX(attr);      String uri = getURI(prefix);      if ((null != uri) && uri.equals(dtm.getStringValue(attr)))        return true;    }    return false;  }  /**   * Copy an DOM attribute to the created output element, executing   * attribute templates as need be, and processing the xsl:use   * attribute.   *   * @param attr Attribute node to add to result tree   *   * @throws TransformerException   */  public void addAttribute(int attr) throws TransformerException  {    DTM dtm = m_transformer.getXPathContext().getDTM(attr);    if (isDefinedNSDecl(attr, dtm))      return;    String ns = dtm.getNamespaceURI(attr);    if (ns == null)      ns = "";    // %OPT% ...can I just store the node handle?        addAttribute(ns, dtm.getLocalName(attr), dtm.getNodeName(attr), "CDATA",                 dtm.getNodeValue(attr));  }  // end copyAttributeToTarget method  /**   * Copy DOM attributes to the result element.   *   * @param src Source node with the attributes   *   * @throws TransformerException   */  public void addAttributes(int src) throws TransformerException  {    DTM dtm = m_transformer.getXPathContext().getDTM(src);    for (int node = dtm.getFirstAttribute(src); DTM.NULL != node;            node = dtm.getNextAttribute(node))    {      addAttribute(node);    }  }  /**   * Tell if an element is pending, to be output to the result tree.   *   * @return True if an element is pending   */  public final boolean isElementPending()  {        return m_elemIsPending;  }  /**   * Retrieves the stylesheet element that produced   * the SAX event.   *   * <p>Please note that the ElemTemplateElement returned may   * be in a default template, and thus may not be   * defined in the stylesheet.</p>   *   * @return the stylesheet element that produced the SAX event.   */  public ElemTemplateElement getCurrentElement()  {    if (m_elemIsPending)      return m_snapshot.m_currentElement;    else      return m_transformer.getCurrentElement();  }  /**   * This method retrieves the current context node   * in the source tree.   *   * @return the current context node in the source tree.   */  public org.w3c.dom.Node getCurrentNode()  {        if (m_snapshot.m_currentNode != null)    {      return m_snapshot.m_currentNode;    }    else    {      DTM dtm = m_transformer.getXPathContext().getDTM(m_transformer.getCurrentNode());      return dtm.getNode(m_transformer.getCurrentNode());    }  }  /**   * This method retrieves the xsl:template   * that is in effect, which may be a matched template   * or a named template.   *   * <p>Please note that the ElemTemplate returned may   * be a default template, and thus may not have a template   * defined in the stylesheet.</p>   *   * @return the xsl:template that is in effect   */  public ElemTemplate getCurrentTemplate()  {    if (m_elemIsPending)      return m_snapshot.m_currentTemplate;    else      return m_transformer.getCurrentTemplate();  }  /**   * This method retrieves the xsl:template   * that was matched.  Note that this may not be   * the same thing as the current template (which   * may be from getCurrentElement()), since a named   * template may be in effect.   *   * <p>Please note that the ElemTemplate returned may   * be a default template, and thus may not have a template   * defined in the stylesheet.</p>   *   * @return the xsl:template that was matched.   */  public ElemTemplate getMatchedTemplate()  {    if (m_elemIsPending)      return m_snapshot.m_matchedTemplate;    else      return m_transformer.getMatchedTemplate();  }  /**   * Retrieves the node in the source tree that matched   * the template obtained via getMatchedTemplate().   *   * @return the node in the source tree that matched   * the template obtained via getMatchedTemplate().   */  public org.w3c.dom.Node getMatchedNode()  {    if (m_elemIsPending)    {      DTM dtm = m_transformer.getXPathContext().getDTM(m_snapshot.m_matchedNode);      return dtm.getNode(m_snapshot.m_matchedNode);    }    else    {      DTM dtm = m_transformer.getXPathContext().getDTM(m_transformer.getMatchedNode());      return dtm.getNode(m_transformer.getMatchedNode());    }  }  /**   * Get the current context node list.   *   * @return the current context node list.   */  public org.w3c.dom.traversal.NodeIterator getContextNodeList()  {    if (m_elemIsPending)    {      return new org.apache.xml.dtm.ref.DTMNodeIterator(m_snapshot.m_contextNodeList);    }    else      return new org.apache.xml.dtm.ref.DTMNodeIterator(m_transformer.getContextNodeList());  }  /**   * Get the TrAX Transformer object in effect.   *   * @return the TrAX Transformer object in effect.   */  public Transformer getTransformer()  {    return m_transformer;  }      // Implement ErrorHandler    /**    * Receive notification of a warning.    *    * <p>SAX parsers will use this method to report conditions that    * are not errors or fatal errors as defined by the XML 1.0    * recommendation.  The default behaviour is to take no action.</p>    *    * <p>The SAX parser must continue to provide normal parsing events    * after invoking this method: it should still be possible for the    * application to process the document through to the end.</p>    *    * <p>Filters may use this method to report other, non-XML warnings    * as well.</p>    *    * @param exception The warning information encapsulated in a    *                  SAX parse exception.    * @exception org.xml.sax.SAXException Any SAX exception, possibly    *            wrapping another exception.    * @see org.xml.sax.SAXParseException     */  public void warning (SAXParseException exception)    throws SAXException  {    if (m_contentHandler instanceof ErrorHandler)      ((ErrorHandler)m_contentHandler).warning(exception);  }           /**    * Receive notification of a recoverable error.    *    * <p>This corresponds to the definition of "error" in section 1.2    * of the W3C XML 1.0 Recommendation.  For example, a validating    * parser would use this callback to report the violation of a    * validity constraint.  The default behaviour is to take no    * action.</p>    *    * <p>The SAX parser must continue to provide normal parsing events    * after invoking this method: it should still be possible for the    * application to process the document through to the end.  If the    * application cannot do so, then the parser should report a fatal    * error even if the XML 1.0 recommendation does not require it to    * do so.</p>    *    * <p>Filters may use this method to report other, non-XML errors    * as well.</p>    *    * @param exception The error information encapsulated in a    *                  SAX parse exception.    * @exception org.xml.sax.SAXException Any SAX exception, possibly    *            wrapping another exception.    * @see org.xml.sax.SAXParseException     */  public void error (SAXParseException exception)    throws SAXException  {    if (m_contentHandler instanceof ErrorHandler)      ((ErrorHandler)m_contentHandler).error(exception);  }           /**    * Receive notification of a non-recoverable error.    *    * <p>This corresponds to the definition of "fatal error" in    * section 1.2 of the W3C XML 1.0 Recommendation.  For example, a    * parser would use this callback to report the violation of a    * well-formedness constraint.</p>    *    * <p>The application must assume that the document is unusable    * after the parser has invoked this method, and should continue    * (if at all) only for the sake of collecting addition error    * messages: in fact, SAX parsers are free to stop reporting any    * other events once this method has been invoked.</p>    *    * @param exception The error information encapsulated in a    *                  SAX parse exception.      * @exception org.xml.sax.SAXException Any SAX exception, possibly    *            wrapping another exception.    * @see org.xml.sax.SAXParseException    */  public void fatalError (SAXParseException exception)    throws SAXException  {          m_elemIsPending = false;    m_docEnded = true;    m_docPending = false;        if (m_contentHandler instanceof ErrorHandler)      ((ErrorHandler)m_contentHandler).fatalError(exception);  }    boolean m_isTransformClient = false;  /**   * Use the SAX2 helper class to track result namespaces.   */  NamespaceSupport m_nsSupport = new NamespaceSupport2();  /**   * The transformer object.   */  private TransformerImpl m_transformer;  /**   * The content handler.  May be null, in which   * case, we'll defer to the content handler in the   * transformer.   */  private ContentHandler m_contentHandler;  /** The LexicalHandler */  private LexicalHandler m_lexicalHandler;  /**   * The root of a linked set of stylesheets.   */  private StylesheetRoot m_stylesheetRoot = null;  /**   * This is used whenever a unique namespace is needed.   */  private int m_uniqueNSValue = 0;  /** Prefix used to create unique prefix names */  private static final String S_NAMESPACEPREFIX = "ns";  /**   * This class clones nodes to the result tree.   */  public ClonerToResultTree m_cloner;  /**   * Trace manager for debug support.   */  private TraceManager m_tracer;    private QueuedStateSnapshot m_snapshot = new QueuedStateSnapshot();  // These are passed to flushPending, to help it decide if it   // should really flush.    class QueuedStateSnapshot  {    /**     * The stylesheet element that produced the SAX event.     */    ElemTemplateElement m_currentElement;        /**     * The current context node in the source tree.     */    org.w3c.dom.Node m_currentNode;        /**     * The xsl:template that is in effect, which may be a matched template     * or a named template.     */    ElemTemplate m_currentTemplate;        /**     * The xsl:template that was matched.     */    ElemTemplate m_matchedTemplate;        /**     * The node in the source tree that matched     * the template obtained via getMatchedTemplate().     */    int m_matchedNode;        /**     * The current context node list.     */    DTMIterator m_contextNodeList;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?