elemtemplateelement.java

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

JAVA
1,705
字号
   * The list of namespace declarations for this element only.   * @serial   */  private Vector m_declaredPrefixes;  /**   * Return a table that contains all prefixes available   * within this element context.   *   * @return Vector containing the prefixes available within this   * element context    */  public Vector getDeclaredPrefixes()  {    return m_declaredPrefixes;  }  /**   * From the SAX2 helper class, set the namespace table for   * this element.  Take care to call resolveInheritedNamespaceDecls.   * after all namespace declarations have been added.   *   * @param nsSupport non-null reference to NamespaceSupport from    * the ContentHandler.   *   * @throws TransformerException   */  public void setPrefixes(NamespaceSupport nsSupport) throws TransformerException  {    setPrefixes(nsSupport, false);  }  /**   * Copy the namespace declarations from the NamespaceSupport object.     * Take care to call resolveInheritedNamespaceDecls.   * after all namespace declarations have been added.   *   * @param nsSupport non-null reference to NamespaceSupport from    * the ContentHandler.   * @param excludeXSLDecl true if XSLT namespaces should be ignored.   *   * @throws TransformerException   */  public void setPrefixes(NamespaceSupport nsSupport, boolean excludeXSLDecl)          throws TransformerException  {    Enumeration decls = nsSupport.getDeclaredPrefixes();    while (decls.hasMoreElements())    {      String prefix = (String) decls.nextElement();      if (null == m_declaredPrefixes)        m_declaredPrefixes = new Vector();      String uri = nsSupport.getURI(prefix);      if (excludeXSLDecl && uri.equals(Constants.S_XSLNAMESPACEURL))        continue;      // System.out.println("setPrefixes - "+prefix+", "+uri);      XMLNSDecl decl = new XMLNSDecl(prefix, uri, false);      m_declaredPrefixes.addElement(decl);    }  }  /**   * Fullfill the PrefixResolver interface.  Calling this for this class    * will throw an error.   *   * @param prefix The prefix to look up, which may be an empty string ("")    *               for the default Namespace.   * @param context The node context from which to look up the URI.   *   * @return null if the error listener does not choose to throw an exception.   */  public String getNamespaceForPrefix(String prefix, org.w3c.dom.Node context)  {    this.error(XSLTErrorResources.ER_CANT_RESOLVE_NSPREFIX, null);    return null;  }  /**   * Given a namespace, get the corrisponding prefix.   * 9/15/00: This had been iteratively examining the m_declaredPrefixes   * field for this node and its parents. That makes life difficult for   * the compilation experiment, which doesn't have a static vector of   * local declarations. Replaced a recursive solution, which permits   * easier subclassing/overriding.   *   * @param prefix non-null reference to prefix string, which should map    *               to a namespace URL.   *   * @return The namespace URL that the prefix maps to, or null if no    *         mapping can be found.   */  public String getNamespaceForPrefix(String prefix)  {//    if (null != prefix && prefix.equals("xmlns"))//    {//      return Constants.S_XMLNAMESPACEURI;//    }    Vector nsDecls = m_declaredPrefixes;    if (null != nsDecls)    {      int n = nsDecls.size();      if(prefix.equals(Constants.ATTRVAL_DEFAULT_PREFIX))      {        prefix = "";      }      for (int i = 0; i < n; i++)      {        XMLNSDecl decl = (XMLNSDecl) nsDecls.elementAt(i);        if (prefix.equals(decl.getPrefix()))          return decl.getURI();      }    }    // Not found; ask our ancestors    if (null != m_parentNode)      return m_parentNode.getNamespaceForPrefix(prefix);    // JJK: No ancestors; try implicit    // %REVIEW% Are there literals somewhere that we should use instead?    // %REVIEW% Is this really the best place to patch?    if("xml".equals(prefix))      return "http://www.w3.org/XML/1998/namespace";    // No parent, so no definition    return null;  }  /**   * The table of {@link XMLNSDecl}s for this element   * and all parent elements, screened for excluded prefixes.   * @serial   */  Vector m_prefixTable;  /**   * Return a table that contains all prefixes available   * within this element context.   *   * @return reference to vector of {@link XMLNSDecl}s, which may be null.   */  public Vector getPrefixes()  {    return m_prefixTable;  }    /**   * Get whether or not the passed URL is contained flagged by   * the "extension-element-prefixes" property.  This method is overridden    * by {@link ElemLiteralResult#containsExcludeResultPrefix}.   * @see <a href="http://www.w3.org/TR/xslt#extension-element">extension-element in XSLT Specification</a>   *   * @param prefix non-null reference to prefix that might be excluded.   *   * @return true if the prefix should normally be excluded.   */  public boolean containsExcludeResultPrefix(String prefix, String uri)  {    ElemTemplateElement parent = this.getParentElem();    if(null != parent)      return parent.containsExcludeResultPrefix(prefix, uri);          return false;  }  /**   * Tell if the result namespace decl should be excluded.  Should be called before   * namespace aliasing (I think).   *   * @param prefix non-null reference to prefix.   * @param uri reference to namespace that prefix maps to, which is protected    *            for null, but should really never be passed as null.   *   * @return true if the given namespace should be excluded.   *   * @throws TransformerException   */  private boolean excludeResultNSDecl(String prefix, String uri)          throws TransformerException  {    if (uri != null)    {      if (uri.equals(Constants.S_XSLNAMESPACEURL)              || getStylesheet().containsExtensionElementURI(uri)              || uri.equals(Constants.S_BUILTIN_EXTENSIONS_URL)              || uri.equals(Constants.S_BUILTIN_OLD_EXTENSIONS_URL))        return true;      if (containsExcludeResultPrefix(prefix, uri))        return true;    }    return false;  }    /**   * Combine the parent's namespaces with this namespace   * for fast processing, taking care to reference the   * parent's namespace if this namespace adds nothing new.   * (Recursive method, walking the elements depth-first,   * processing parents before children).   * Note that this method builds m_prefixTable with aliased    * namespaces, *not* the original namespaces.   *   * @throws TransformerException   */  public void resolvePrefixTables() throws TransformerException  {    // Always start with a fresh prefix table!    m_prefixTable = null;    // If we have declared declarations, then we look for     // a parent that has namespace decls, and add them     // to this element's decls.  Otherwise we just point     // to the parent that has decls.    if (null != this.m_declaredPrefixes)    {      StylesheetRoot stylesheet = this.getStylesheetRoot();            // Add this element's declared prefixes to the       // prefix table.      int n = m_declaredPrefixes.size();      for (int i = 0; i < n; i++)      {        XMLNSDecl decl = (XMLNSDecl) m_declaredPrefixes.elementAt(i);        String prefix = decl.getPrefix();        String uri = decl.getURI();        if(null == uri)          uri = "";        boolean shouldExclude = excludeResultNSDecl(prefix, uri);        // Create a new prefix table if one has not already been created.        if (null == m_prefixTable)          m_prefixTable = new Vector();        NamespaceAlias nsAlias = stylesheet.getNamespaceAliasComposed(uri);        if(null != nsAlias)        {          // Should I leave the non-aliased element in the table as           // an excluded element?                    // The exclusion should apply to the non-aliased prefix, so           // we don't calculate it here.  -sb          // Use stylesheet prefix, as per xsl WG          decl = new XMLNSDecl(nsAlias.getStylesheetPrefix(),                               nsAlias.getResultNamespace(), shouldExclude);        }        else          decl = new XMLNSDecl(prefix, uri, shouldExclude);        m_prefixTable.addElement(decl);              }    }    ElemTemplateElement parent = this.getParentNodeElem();    if (null != parent)    {      // The prefix table of the parent should never be null!      Vector prefixes = parent.m_prefixTable;      if (null == m_prefixTable && !needToCheckExclude())      {        // Nothing to combine, so just use parent's table!        this.m_prefixTable = parent.m_prefixTable;      }      else      {        // Add the prefixes from the parent's prefix table.        int n = prefixes.size();                for (int i = 0; i < n; i++)        {          XMLNSDecl decl = (XMLNSDecl) prefixes.elementAt(i);          boolean shouldExclude = excludeResultNSDecl(decl.getPrefix(),                                                      decl.getURI());          if (shouldExclude != decl.getIsExcluded())          {            decl = new XMLNSDecl(decl.getPrefix(), decl.getURI(),                                 shouldExclude);          }                    //m_prefixTable.addElement(decl);          addOrReplaceDecls(decl);        }      }    }    else if (null == m_prefixTable)    {      // Must be stylesheet element without any result prefixes!      m_prefixTable = new Vector();    }  }    /**   * Add or replace this namespace declaration in list   * of namespaces in scope for this element.   *   * @param newDecl namespace declaration to add to list   */  void addOrReplaceDecls(XMLNSDecl newDecl)  {      int n = m_prefixTable.size();        for (int i = n - 1; i >= 0; i--)        {          XMLNSDecl decl = (XMLNSDecl) m_prefixTable.elementAt(i);          if (decl.getPrefix().equals(newDecl.getPrefix()))          {            return;          }        }      m_prefixTable.addElement(newDecl);          }    /**   * Return whether we need to check namespace prefixes    * against and exclude result prefixes list.   */  boolean needToCheckExclude()  {    return false;      }   /**   * Send startPrefixMapping events to the result tree handler   * for all declared prefix mappings in the stylesheet.   *   * @param transformer non-null reference to the the current transform-time state.   *   * @throws TransformerException   */  void executeNSDecls(TransformerImpl transformer) throws TransformerException  {       executeNSDecls(transformer, null);  }  /**   * Send startPrefixMapping events to the result tree handler   * for all declared prefix mappings in the stylesheet.   *   * @param transformer non-null reference to the the current transform-time state.   * @param ignorePrefix string prefix to not startPrefixMapping   *   * @throws TransformerException   */  void executeNSDecls(TransformerImpl transformer, String ignorePrefix) throws TransformerException  {      try    {      if (null != m_prefixTable)      {        ResultTreeHandler rhandler = transformer.getResultTreeHandler();        int n = m_prefixTable.size();        for (int i = n - 1; i >= 0; i--)        {          XMLNSDecl decl = (XMLNSDecl) m_prefixTable.elementAt(i);          if (!decl.getIsExcluded() && !(null != ignorePrefix && decl.getPrefix().equals(ignorePrefix)))          {            rhandler.startPrefixMapping(decl.getPrefix(), decl.getURI(), true);          }        }      }    }    catch(org.xml.sax.SAXException se)    {      throw new TransformerException(se);    }  }  /**   * Send endPrefixMapping events to the result tree handler   * for all declared prefix mappings in the stylesheet.   *   * @param transformer non-null reference to the the current transform-time state.   *   * @throws TransformerException   */  void unexecuteNSDecls(TransformerImpl transformer) throws TransformerException  {       unexecuteNSDecls(transformer, null);  }  /**   * Send endPrefixMapping events to the result tree handler   * for all declared prefix mappings in the stylesheet.   *   * @param transformer non-null reference to the the current transform-time state.   * @param ignorePrefix string prefix to not endPrefixMapping   *    * @throws TransformerException   */  void unexecuteNSDecls(TransformerImpl transformer, String ignorePrefix) throws TransformerException  {     try    {      if (null != m_prefixTable)      {        ResultTreeHandler rhandler = transformer.getResultTreeHandler();        int n = m_prefixTable.size();        for (int i = 0; i < n; i++)        {

⌨️ 快捷键说明

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