transformerimpl.java

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

JAVA
2,145
字号
   * Get a SAX2 ContentHandler for the input.   *   * @return A valid ContentHandler, which should never be null, as   * long as getFeature("http://xml.org/trax/features/sax/input")   * returns true.   */  public ContentHandler getInputContentHandler()  {    return getInputContentHandler(false);  }  /**   * Get a SAX2 ContentHandler for the input.   *   * @param doDocFrag true if a DocumentFragment should be created as   * the root, rather than a Document.   *   * @return A valid ContentHandler, which should never be null, as   * long as getFeature("http://xml.org/trax/features/sax/input")   * returns true.   */  public ContentHandler getInputContentHandler(boolean doDocFrag)  {    if (null == m_inputContentHandler)    {      //      if(null == m_urlOfSource && null != m_stylesheetRoot)      //        m_urlOfSource = m_stylesheetRoot.getBaseIdentifier();      m_inputContentHandler = new TransformerHandlerImpl(this, doDocFrag,              m_urlOfSource);    }    return m_inputContentHandler;  }  /**   * Get a SAX2 DeclHandler for the input.   * @return A valid DeclHandler, which should never be null, as   * long as getFeature("http://xml.org/trax/features/sax/input")   * returns true.   */  public DeclHandler getInputDeclHandler()  {    if (m_inputContentHandler instanceof DeclHandler)      return (DeclHandler) m_inputContentHandler;    else      return null;  }  /**   * Get a SAX2 LexicalHandler for the input.   * @return A valid LexicalHandler, which should never be null, as   * long as getFeature("http://xml.org/trax/features/sax/input")   * returns true.   */  public LexicalHandler getInputLexicalHandler()  {    if (m_inputContentHandler instanceof LexicalHandler)      return (LexicalHandler) m_inputContentHandler;    else      return null;  }  /**   * Set the output properties for the transformation.  These   * properties will override properties set in the templates   * with xsl:output.   *   * @param oformat A valid OutputProperties object (which will   * not be mutated), or null.   */  public void setOutputFormat(OutputProperties oformat)  {    m_outputFormat = oformat;  }  /**   * Get the output properties used for the transformation.   *   * @return the output format that was set by the user,   * otherwise the output format from the stylesheet.   */  public OutputProperties getOutputFormat()  {    // Get the output format that was set by the user, otherwise get the     // output format from the stylesheet.    OutputProperties format = (null == m_outputFormat)                              ? getStylesheet().getOutputComposed()                              : m_outputFormat;    return format;  }  /**   * <meta name="usage" content="internal"/>   * Get the current serializer in use, which may well not   * be the main serializer (for instance, this may well be   * a text serializer for string creation from templates).   *   * @return The current serializer, or null if there is none.   */  public Serializer getSerializer()  {    return m_serializer;  }  /**   * <meta name="usage" content="internal"/>   * Set the current serializer.   *   * @param s The current serializer, or null.   */  public void setSerializer(Serializer s)  {    m_serializer = s;  }    /**   * Set a parameter for the templates.   *    * @param name The name of the parameter.   * @param namespace The namespace of the parameter.   * @param value The value object.  This can be any valid Java object   * -- it's up to the processor to provide the proper   * coersion to the object, or simply pass it on for use   * in extensions.   */  public void setParameter(String name, String namespace, Object value)  {    VariableStack varstack = getXPathContext().getVarStack();    QName qname = new QName(namespace, name);    XObject xobject = XObject.create(value, getXPathContext());        StylesheetRoot sroot = m_stylesheetRoot;    Vector vars = sroot.getVariablesAndParamsComposed();    int i = vars.size();    while (--i >= 0)    {      ElemVariable variable = (ElemVariable)vars.elementAt(i);      if(variable.getXSLToken() == Constants.ELEMNAME_PARAMVARIABLE &&          variable.getName().equals(qname))      {          varstack.setGlobalVariable(i, xobject);      }    }  }  /** NEEDSDOC Field m_userParams          */  Vector m_userParams;  /**   * Set a parameter for the transformation.   *   * @param name The name of the parameter,   *             which may have a namespace URI.   * @param value The value object.  This can be any valid Java object   * -- it's up to the processor to provide the proper   * coersion to the object, or simply pass it on for use   * in extensions.   */  public void setParameter(String name, Object value)  {    StringTokenizer tokenizer = new StringTokenizer(name, "{}", false);    try    {      // The first string might be the namespace, or it might be       // the local name, if the namespace is null.      String s1 = tokenizer.nextToken();      String s2 = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null;      if (null == m_userParams)        m_userParams = new Vector();      if (null == s2)      {        replaceOrPushUserParam(new QName(s1), XObject.create(value, getXPathContext()));        setParameter(s1, null, value);      }      else      {        replaceOrPushUserParam(new QName(s1, s2), XObject.create(value, getXPathContext()));        setParameter(s2, s1, value);      }    }    catch (java.util.NoSuchElementException nsee)    {      // Should throw some sort of an error.    }  }  /**   * NEEDSDOC Method replaceOrPushUserParam    *   *   * NEEDSDOC @param qname   * NEEDSDOC @param xval   */  private void replaceOrPushUserParam(QName qname, XObject xval)  {    int n = m_userParams.size();    for (int i = n - 1; i >= 0; i--)    {      Arg arg = (Arg) m_userParams.elementAt(i);      if (arg.getQName().equals(qname))      {        m_userParams.setElementAt(new Arg(qname, xval, true), i);        return;      }    }    m_userParams.addElement(new Arg(qname, xval, true));  }  /**   * Get a parameter that was explicitly set with setParameter   * or setParameters.   *   *   * NEEDSDOC @param name   * @return A parameter that has been set with setParameter   * or setParameters,   * *not* all the xsl:params on the stylesheet (which require   * a transformation Source to be evaluated).   */  public Object getParameter(String name)  {    try    {      // VariableStack varstack = getXPathContext().getVarStack();      // The first string might be the namespace, or it might be       // the local name, if the namespace is null.      QName qname = QName.getQNameFromString(name);      if (null == m_userParams)        return null;      int n = m_userParams.size();      for (int i = n - 1; i >= 0; i--)      {        Arg arg = (Arg) m_userParams.elementAt(i);        if (arg.getQName().equals(qname))        {          return arg.getVal().object();        }      }      return null;    }    catch (java.util.NoSuchElementException nsee)    {      // Should throw some sort of an error.      return null;    }  }    /**   * Reset parameters that the user specified for the transformation.   * Called during transformer.reset() after we have cleared the    * variable stack. We need to make sure that user params are   * reset so that the transformer object can be reused.    */  private void resetUserParameters()  {    try    {            if (null == m_userParams)        return;      int n = m_userParams.size();      for (int i = n - 1; i >= 0; i--)      {        Arg arg = (Arg) m_userParams.elementAt(i);        QName name = arg.getQName();        // The first string might be the namespace, or it might be         // the local name, if the namespace is null.        String s1 = name.getNamespace();        String s2 = name.getLocalPart();        setParameter(s2, s1, arg.getVal().object());              }          }    catch (java.util.NoSuchElementException nsee)    {      // Should throw some sort of an error.          }  }  /**   * Set a bag of parameters for the transformation. Note that   * these will not be additive, they will replace the existing   * set of parameters.   *   * @param name The name of the parameter,   *             which may have a namespace URI.   * @param value The value object.  This can be any valid Java object   * -- it's up to the processor to provide the proper   * coersion to the object, or simply pass it on for use   * in extensions.   *   * NEEDSDOC @param params   */  public void setParameters(Properties params)  {    clearParameters();    Enumeration names = params.propertyNames();    while (names.hasMoreElements())    {      String name = params.getProperty((String) names.nextElement());      StringTokenizer tokenizer = new StringTokenizer(name, "{}", false);      try      {        // The first string might be the namespace, or it might be         // the local name, if the namespace is null.        String s1 = tokenizer.nextToken();        String s2 = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null;        if (null == s2)          setParameter(s1, null, params.getProperty(name));        else          setParameter(s2, s1, params.getProperty(name));      }      catch (java.util.NoSuchElementException nsee)      {        // Should throw some sort of an error.      }    }  }  /**   * Reset the parameters to a null list.   */  public void clearParameters()  {    synchronized (m_reentryGuard)    {      VariableStack varstack = new VariableStack();      m_xcontext.setVarStack(varstack);      m_userParams = null;    }  }  /**   * Internal -- push the global variables from the Stylesheet onto   * the context's runtime variable stack.   * <p>If we encounter a variable   * that is already defined in the variable stack, we ignore it.  This   * is because the second variable definition will be at a lower import   * precedence.  Presumably, global"variables at the same import precedence   * with the same name will have been caught during the recompose process.   * <p>However, if we encounter a parameter that is already defined in the   * variable stack, we need to see if this is a parameter whose value was   * supplied by a setParameter call.  If so, we need to "receive" the one   * already in the stack, ignoring this one.  If it is just an earlier   * xsl:param or xsl:variable definition, we ignore it using the same   * reasoning as explained above for the variable.   *   * @param contextNode The root of the source tree, can't be null.   *   * @throws TransformerException   */  protected void pushGlobalVars(int contextNode) throws TransformerException  {    XPathContext xctxt = m_xcontext;    VariableStack vs = xctxt.getVarStack();    StylesheetRoot sr = getStylesheet();    Vector vars = sr.getVariablesAndParamsComposed();        int i = vars.size();    vs.link(i);    while (--i >= 0)    {      ElemVariable v = (ElemVariable) vars.elementAt(i);      // XObject xobj = v.getValue(this, contextNode);      XObject xobj = new XUnresolvedVariable(v, contextNode, this,                                     vs.getStackFrame(), 0, true);            if(null == vs.elementAt(i))                                       vs.setGlobalVariable(i, xobj);    }  }  /**   * Set an object that will be used to resolve URIs used in   * document(), etc.   * @param resolver An object that implements the URIResolver interface,   * or null.   */  public void setURIResolver(URIResolver resolver)  {    synchronized (m_reentryGuard)    {

⌨️ 快捷键说明

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