templatelist.java

来自「java jdk 1.4的源码」· Java 代码 · 共 958 行 · 第 1/2 页

JAVA
958
字号
      break;    case DTM.ENTITY_REFERENCE_NODE :    case DTM.ENTITY_NODE :      head = (TemplateSubPatternAssociation) m_patternTable.get(        dtm.getNodeName(targetNode)); // %REVIEW% I think this is right      break;    case DTM.PROCESSING_INSTRUCTION_NODE :      head = (TemplateSubPatternAssociation) m_patternTable.get(        dtm.getLocalName(targetNode));      break;    case DTM.COMMENT_NODE :      head = m_commentPatterns;      break;    case DTM.DOCUMENT_NODE :    case DTM.DOCUMENT_FRAGMENT_NODE :      head = m_docPatterns;      break;    case DTM.NOTATION_NODE :    default :      head = (TemplateSubPatternAssociation) m_patternTable.get(        dtm.getNodeName(targetNode)); // %REVIEW% I think this is right    }    return (null == head) ? m_wildCardPatterns : head;  }    /**   * Given a target element, find the template that best   * matches in the given XSL document, according   * to the rules specified in the xsl draft.  This variation of getTemplate    * assumes the current node and current expression node have already been    * pushed.    *   * @param xctxt   * @param targetNode   * @param mode A string indicating the display mode.   * @param maxImportLevel The maximum importCountComposed that we should consider or -1   *        if we should consider all import levels.  This is used by apply-imports to   *        access templates that have been overridden.   * @param quietConflictWarnings   * @return Rule that best matches targetElem.   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide   * the error condition is severe enough to halt processing.   *   * @throws TransformerException   */  public ElemTemplate getTemplateFast(XPathContext xctxt,                                int targetNode,                                int expTypeID,                                QName mode,                                int maxImportLevel,                                boolean quietConflictWarnings,                                DTM dtm)            throws TransformerException  {        TemplateSubPatternAssociation head;    switch (dtm.getNodeType(targetNode))    {    case DTM.ELEMENT_NODE :    case DTM.ATTRIBUTE_NODE :      head = (TemplateSubPatternAssociation) m_patternTable.get(        dtm.getLocalNameFromExpandedNameID(expTypeID));      break;    case DTM.TEXT_NODE :    case DTM.CDATA_SECTION_NODE :      head = m_textPatterns;      break;    case DTM.ENTITY_REFERENCE_NODE :    case DTM.ENTITY_NODE :      head = (TemplateSubPatternAssociation) m_patternTable.get(        dtm.getNodeName(targetNode)); // %REVIEW% I think this is right      break;    case DTM.PROCESSING_INSTRUCTION_NODE :      head = (TemplateSubPatternAssociation) m_patternTable.get(        dtm.getLocalName(targetNode));      break;    case DTM.COMMENT_NODE :      head = m_commentPatterns;      break;    case DTM.DOCUMENT_NODE :    case DTM.DOCUMENT_FRAGMENT_NODE :      head = m_docPatterns;      break;    case DTM.NOTATION_NODE :    default :      head = (TemplateSubPatternAssociation) m_patternTable.get(        dtm.getNodeName(targetNode)); // %REVIEW% I think this is right    }    if(null == head)    {      head = m_wildCardPatterns;      if(null == head)        return null;    }                                                  // XSLT functions, such as xsl:key, need to be able to get to     // current ElemTemplateElement via a cast to the prefix resolver.    // Setting this fixes bug idkey03.    xctxt.pushNamespaceContextNull();    try    {      do      {        if ( (maxImportLevel > -1) && (head.getImportLevel() > maxImportLevel) )        {          continue;        }        ElemTemplate template = head.getTemplate();                xctxt.setNamespaceContext(template);                if ((head.m_stepPattern.execute(xctxt, targetNode, dtm, expTypeID) != NodeTest.SCORE_NONE)                && head.matchMode(mode))        {          if (quietConflictWarnings)            checkConflicts(head, xctxt, targetNode, mode);          return template;        }      }      while (null != (head = head.getNext()));    }    finally    {      xctxt.popNamespaceContext();    }    return null;  }  // end findTemplate  /**   * Given a target element, find the template that best   * matches in the given XSL document, according   * to the rules specified in the xsl draft.   *   * @param xctxt   * @param targetNode   * @param mode A string indicating the display mode.   * @param quietConflictWarnings   * @return Rule that best matches targetElem.   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide   * the error condition is severe enough to halt processing.   *   * @throws TransformerException   */  public ElemTemplate getTemplate(XPathContext xctxt,                                int targetNode,                                QName mode,                                boolean quietConflictWarnings,                                DTM dtm)            throws TransformerException  {    TemplateSubPatternAssociation head = getHead(xctxt, targetNode, dtm);    if (null != head)    {      // XSLT functions, such as xsl:key, need to be able to get to       // current ElemTemplateElement via a cast to the prefix resolver.      // Setting this fixes bug idkey03.      xctxt.pushNamespaceContextNull();      xctxt.pushCurrentNodeAndExpression(targetNode, targetNode);      try      {        do        {          ElemTemplate template = head.getTemplate();                  xctxt.setNamespaceContext(template);                    if ((head.m_stepPattern.execute(xctxt, targetNode) != NodeTest.SCORE_NONE)                  && head.matchMode(mode))          {            if (quietConflictWarnings)              checkConflicts(head, xctxt, targetNode, mode);            return template;          }        }        while (null != (head = head.getNext()));      }      finally      {        xctxt.popCurrentNodeAndExpression();        xctxt.popNamespaceContext();      }    }    return null;  }  // end findTemplate    /**   * Given a target element, find the template that best   * matches in the given XSL document, according   * to the rules specified in the xsl draft.   *   * @param xctxt   * @param targetNode   * @param mode A string indicating the display mode.   * @param maxImportLevel The maximum importCountComposed that we should consider or -1   *        if we should consider all import levels.  This is used by apply-imports to   *        access templates that have been overridden.   * @param maxEndImportLevel The count of composed imports   * @param quietConflictWarnings   * @return Rule that best matches targetElem.   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide   * the error condition is severe enough to halt processing.   *   * @throws TransformerException   */  public ElemTemplate getTemplate(XPathContext xctxt,                                int targetNode,                                QName mode,                                int maxImportLevel, int endImportLevel,                                boolean quietConflictWarnings,                                DTM dtm)            throws TransformerException  {    TemplateSubPatternAssociation head = getHead(xctxt, targetNode, dtm);    if (null != head)    {      // XSLT functions, such as xsl:key, need to be able to get to       // current ElemTemplateElement via a cast to the prefix resolver.      // Setting this fixes bug idkey03.      xctxt.pushNamespaceContextNull();      xctxt.pushCurrentNodeAndExpression(targetNode, targetNode);      try      {        do        {          if ( (maxImportLevel > -1) && (head.getImportLevel() > maxImportLevel))          {            continue;          }          if (head.getImportLevel()<= maxImportLevel - endImportLevel)            return null;          ElemTemplate template = head.getTemplate();                  xctxt.setNamespaceContext(template);                    if ((head.m_stepPattern.execute(xctxt, targetNode) != NodeTest.SCORE_NONE)                  && head.matchMode(mode))          {            if (quietConflictWarnings)              checkConflicts(head, xctxt, targetNode, mode);            return template;          }        }        while (null != (head = head.getNext()));      }      finally      {        xctxt.popCurrentNodeAndExpression();        xctxt.popNamespaceContext();      }    }    return null;  }  // end findTemplate  /**   * Get a TemplateWalker for use by a compiler.  See the documentation for   * the TreeWalker inner class for further details.   */  public TemplateWalker getWalker()  {    return new TemplateWalker();  }  /**   * Check for match conflicts, and warn the stylesheet author.   *   * @param head Template pattern   * @param xctxt Current XPath context   * @param targetNode Node matching the pattern   * @param mode reference, which may be null, to the <a href="http://www.w3.org/TR/xslt#modes">current mode</a>.   */  private void checkConflicts(TemplateSubPatternAssociation head,                              XPathContext xctxt, int targetNode, QName mode)  {    // TODO: Check for conflicts.  }  /**   * Add object to vector if not already there.   *   * @param obj   * @param v   */  private void addObjectIfNotFound(Object obj, Vector v)  {    int n = v.size();    boolean addIt = true;    for (int i = 0; i < n; i++)    {      if (v.elementAt(i) == obj)      {        addIt = false;        break;      }    }    if (addIt)    {      v.addElement(obj);    }  }  /**   * Keyed on string macro names, and holding values   * that are macro elements in the XSL DOM tree.   * Initialized in initMacroLookupTable, and used in   * findNamedTemplate.   * @serial   */  private Hashtable m_namedTemplates = new Hashtable(89);  /**   * This table is keyed on the target elements   * of patterns, and contains linked lists of   * the actual patterns that match the target element   * to some degree of specifity.   * @serial   */  private Hashtable m_patternTable = new Hashtable(89);  /** Wildcard patterns.   *  @serial          */  private TemplateSubPatternAssociation m_wildCardPatterns = null;  /** Text Patterns.   *  @serial          */  private TemplateSubPatternAssociation m_textPatterns = null;  /** Root document Patterns.   *  @serial          */  private TemplateSubPatternAssociation m_docPatterns = null;  /** Comment Patterns.   *  @serial          */  private TemplateSubPatternAssociation m_commentPatterns = null;  /**   * Get table of named Templates.   * These are keyed on template names, and holding values   * that are template elements.   *   * @return A Hashtable dictionary that contains {@link java.lang.String}s    * as the keys, and {@link org.apache.xalan.templates.ElemTemplate}s as the    * values.    */  private Hashtable getNamedTemplates()  {    return m_namedTemplates;  }  /**   * Set table of named Templates.   * These are keyed on string macro names, and holding values   * that are template elements in the XSL DOM tree.   *   * @param v Hashtable dictionary that contains {@link java.lang.String}s    * as the keys, and {@link org.apache.xalan.templates.ElemTemplate}s as the    * values.   */  private void setNamedTemplates(Hashtable v)  {    m_namedTemplates = v;  }  /**   * Get the head of the assocation list that is keyed by target.   *   * @param key The name of a node.    *   * @return The head of a linked list that contains all possible match pattern to    * template associations for the given key.   */  private TemplateSubPatternAssociation getHead(String key)  {    return (TemplateSubPatternAssociation) m_patternTable.get(key);  }  /**   * Get the head of the assocation list that is keyed by target.   *   * @param key   * @param assoc   */  private void putHead(String key, TemplateSubPatternAssociation assoc)  {    if (key.equals(PsuedoNames.PSEUDONAME_TEXT))      m_textPatterns = assoc;    else if (key.equals(PsuedoNames.PSEUDONAME_ROOT))      m_docPatterns = assoc;    else if (key.equals(PsuedoNames.PSEUDONAME_COMMENT))      m_commentPatterns = assoc;    m_patternTable.put(key, assoc);  }  /**   * An inner class used by a compiler to iterate over all of the ElemTemplates   * stored in this TemplateList.  The compiler can replace returned templates   * with their compiled equivalent.   */  public class TemplateWalker  {    private Enumeration hashIterator;    private boolean inPatterns;    private TemplateSubPatternAssociation curPattern;    private Hashtable m_compilerCache = new Hashtable();    private TemplateWalker()    {      hashIterator = m_patternTable.elements();      inPatterns = true;      curPattern = null;    }    public ElemTemplate next()    {      ElemTemplate retValue = null;      ElemTemplate ct;      while (true)      {        if (inPatterns)        {          if (null != curPattern)            curPattern = curPattern.getNext();          if (null != curPattern)            retValue = curPattern.getTemplate();          else          {            if (hashIterator.hasMoreElements())            {              curPattern = (TemplateSubPatternAssociation) hashIterator.nextElement();              retValue =  curPattern.getTemplate();            }            else            {              inPatterns = false;              hashIterator = m_namedTemplates.elements();            }          }        }        if (!inPatterns)        {          if (hashIterator.hasMoreElements())            retValue = (ElemTemplate) hashIterator.nextElement();          else            return null;        }        ct = (ElemTemplate) m_compilerCache.get(new Integer(retValue.getUid()));        if (null == ct)        {          m_compilerCache.put(new Integer(retValue.getUid()), retValue);          return retValue;        }      }    }  }}

⌨️ 快捷键说明

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