transformerfactoryimpl.java

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

JAVA
1,073
字号
    }    else    {      isource = SAXSource.sourceToInputSource(source);      baseID = isource.getSystemId();    }    // What I try to do here is parse until the first startElement    // is found, then throw a special exception in order to terminate     // the parse.    StylesheetPIHandler handler = new StylesheetPIHandler(baseID, media,                                    title, charset);        // Use URIResolver. Patch from Dmitri Ilyin     if (m_uriResolver != null)     {      handler.setURIResolver(m_uriResolver);     }    try    {      if (null != node)      {        TreeWalker walker = new TreeWalker(handler, new org.apache.xpath.DOM2Helper(), baseID);        walker.traverse(node);      }      else      {        // Use JAXP1.1 ( if possible )        try        {          javax.xml.parsers.SAXParserFactory factory =            javax.xml.parsers.SAXParserFactory.newInstance();          factory.setNamespaceAware(true);          javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();          reader = jaxpParser.getXMLReader();        }        catch (javax.xml.parsers.ParserConfigurationException ex)        {          throw new org.xml.sax.SAXException(ex);        }        catch (javax.xml.parsers.FactoryConfigurationError ex1)        {          throw new org.xml.sax.SAXException(ex1.toString());        }        catch (NoSuchMethodError ex2){}        catch (AbstractMethodError ame){}        if (null == reader)        {          reader = XMLReaderFactory.createXMLReader();        }        // Need to set options!        reader.setContentHandler(handler);        reader.parse(isource);      }    }    catch (StopParseException spe)    {      // OK, good.    }    catch (org.xml.sax.SAXException se)    {      throw new TransformerConfigurationException(        "getAssociatedStylesheets failed", se);    }    catch (IOException ioe)    {      throw new TransformerConfigurationException(        "getAssociatedStylesheets failed", ioe);    }    return handler.getAssociatedStylesheet();  }  /**   * Create a new Transformer object that performs a copy   * of the source to the result.   *   * @param source An object that holds a URI, input stream, etc.   *   * @return A Transformer object that may be used to perform a transformation   * in a single thread, never null.   *   * @throws TransformerConfigurationException May throw this during   *            the parse when it is constructing the   *            Templates object and fails.   */  public TemplatesHandler newTemplatesHandler()          throws TransformerConfigurationException  {    return new StylesheetHandler(this);  }  /**   * Look up the value of a feature.   *   * <p>The feature name is any fully-qualified URI.  It is   * possible for an TransformerFactory to recognize a feature name but   * to be unable to return its value; this is especially true   * in the case of an adapter for a SAX1 Parser, which has   * no way of knowing whether the underlying parser is   * validating, for example.</p>   *   * @param name The feature name, which is a fully-qualified URI.   * @return The current state of the feature (true or false).   */  public boolean getFeature(String name)  {    // Try first with identity comparison, which     // will be faster.    if ((DOMResult.FEATURE == name) || (DOMSource.FEATURE == name)            || (SAXResult.FEATURE == name) || (SAXSource.FEATURE == name)            || (StreamResult.FEATURE == name)            || (StreamSource.FEATURE == name)            || (SAXTransformerFactory.FEATURE == name)            || (SAXTransformerFactory.FEATURE_XMLFILTER == name))      return true;    else if ((DOMResult.FEATURE.equals(name))             || (DOMSource.FEATURE.equals(name))             || (SAXResult.FEATURE.equals(name))             || (SAXSource.FEATURE.equals(name))             || (StreamResult.FEATURE.equals(name))             || (StreamSource.FEATURE.equals(name))             || (SAXTransformerFactory.FEATURE.equals(name))             || (SAXTransformerFactory.FEATURE_XMLFILTER.equals(name)))      return true;    else      return false;  }    public static boolean m_optimize = true;    /** Flag set by FEATURE_SOURCE_LOCATION.   * This feature specifies whether the transformation phase should   * keep track of line and column numbers for the input source   * document. Note that this works only when that   * information is available from the source -- in other words, if you   * pass in a DOM, there's little we can do for you.   *    * The default is false. Setting it true may significantly   * increase storage cost per node.    *    * %REVIEW% SAX2DTM is explicitly reaching up to retrieve this global field.   * We should instead have an architected pathway for passing hints of this   * sort down from TransformerFactory to Transformer to DTMManager to DTM.   * */  public static boolean m_source_location = false;    /**   * Allows the user to set specific attributes on the underlying   * implementation.   *   * @param name The name of the attribute.   * @param value The value of the attribute; Boolean or String="true"|"false"   *   * @throws IllegalArgumentException thrown if the underlying   * implementation doesn't recognize the attribute.   */  public void setAttribute(String name, Object value)          throws IllegalArgumentException  {    if (name.equals(FEATURE_INCREMENTAL))    {      if(value instanceof Boolean)      {        // Accept a Boolean object..        org.apache.xml.dtm.DTMManager.setIncremental(((Boolean)value).booleanValue());      }      else if(value instanceof String)      {        // .. or a String object        org.apache.xml.dtm.DTMManager.setIncremental((new Boolean((String)value)).booleanValue());      }      else      {        // Give a more meaningful error message        throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_BAD_VALUE, new Object[]{name, value})); //name + " bad value " + value);      }	}    else if (name.equals(FEATURE_OPTIMIZE))    {      if(value instanceof Boolean)      {        // Accept a Boolean object..        m_optimize = ((Boolean)value).booleanValue();      }      else if(value instanceof String)      {        // .. or a String object        m_optimize = (new Boolean((String)value)).booleanValue();      }      else      {        // Give a more meaningful error message        throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_BAD_VALUE, new Object[]{name, value})); //name + " bad value " + value);      }    }        // Custom Xalan feature: annotate DTM with SAX source locator fields.    // This gets used during SAX2DTM instantiation.     //    // %REVIEW% Should the name of this field really be in XalanProperties?    // %REVIEW% I hate that it's a global static, but didn't want to change APIs yet.    else if(name.equals(FEATURE_SOURCE_LOCATION))    {      if(value instanceof Boolean)      {        // Accept a Boolean object..        m_source_location = ((Boolean)value).booleanValue();      }      else if(value instanceof String)      {        // .. or a String object        m_source_location = (new Boolean((String)value)).booleanValue();      }      else      {        // Give a more meaningful error message        throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_BAD_VALUE, new Object[]{name, value})); //name + " bad value " + value);      }    }        else    {      throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_NOT_SUPPORTED, new Object[]{name})); //name + "not supported");    }  }  /**   * Allows the user to retrieve specific attributes on the underlying   * implementation.   *   * @param name The name of the attribute.   * @return value The value of the attribute.   *   * @throws IllegalArgumentException thrown if the underlying   * implementation doesn't recognize the attribute.   */  public Object getAttribute(String name) throws IllegalArgumentException  {    if (name.equals(FEATURE_INCREMENTAL))    {      return new Boolean(org.apache.xml.dtm.DTMManager.getIncremental());                }    else if (name.equals(FEATURE_OPTIMIZE))    {      return new Boolean(m_optimize);    }    else if (name.equals(FEATURE_SOURCE_LOCATION))    {      return new Boolean(m_source_location);    }    else      throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_ATTRIB_VALUE_NOT_RECOGNIZED, new Object[]{name})); //name + " attribute not recognized");  }  /**   * Create an XMLFilter that uses the given source as the   * transformation instructions.   *   * @param src The source of the transformation instructions.   *   * @return An XMLFilter object, or null if this feature is not supported.   *   * @throws TransformerConfigurationException   */  public XMLFilter newXMLFilter(Source src)          throws TransformerConfigurationException  {    Templates templates = newTemplates(src);    if( templates==null ) return null;        return newXMLFilter(templates);  }  /**   * Create an XMLFilter that uses the given source as the   * transformation instructions.   *   * @param src The source of the transformation instructions.   *   * @param templates non-null reference to Templates object.   *   * @return An XMLFilter object, or null if this feature is not supported.   *   * @throws TransformerConfigurationException   */  public XMLFilter newXMLFilter(Templates templates)          throws TransformerConfigurationException  {    try {      return new TrAXFilter(templates);    } catch( TransformerConfigurationException ex ) {      if( m_errorListener != null) {        try {          m_errorListener.fatalError( ex );          return null;        } catch( TransformerException ex1 ) {          new TransformerConfigurationException(ex1);        }      }      throw ex;    }  }  /**   * Get a TransformerHandler object that can process SAX   * ContentHandler events into a Result, based on the transformation   * instructions specified by the argument.   *   * @param src The source of the transformation instructions.   *   * @return TransformerHandler ready to transform SAX events.   *   * @throws TransformerConfigurationException   */  public TransformerHandler newTransformerHandler(Source src)          throws TransformerConfigurationException  {    Templates templates = newTemplates(src);    if( templates==null ) return null;        return newTransformerHandler(templates);  }  /**   * Get a TransformerHandler object that can process SAX   * ContentHandler events into a Result, based on the Templates argument.   *   * @param templates The source of the transformation instructions.   *   * @return TransformerHandler ready to transform SAX events.   * @throws TransformerConfigurationException   */  public TransformerHandler newTransformerHandler(Templates templates)          throws TransformerConfigurationException  {    try {      TransformerImpl transformer =        (TransformerImpl) templates.newTransformer();      transformer.setURIResolver(m_uriResolver);      TransformerHandler th =        (TransformerHandler) transformer.getInputContentHandler(true);      return th;    } catch( TransformerConfigurationException ex ) {      if( m_errorListener != null ) {

⌨️ 快捷键说明

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