transformerhandlerimpl.java

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

JAVA
1,106
字号
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation.  All rights  * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment:   *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written  *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    nor may "Apache" appear in their name, without prior written *    permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xalan.transformer;import java.io.IOException;import org.xml.sax.XMLReader;import org.xml.sax.XMLFilter;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.Attributes;import org.xml.sax.EntityResolver;import org.xml.sax.DTDHandler;import org.xml.sax.ContentHandler;import org.xml.sax.ErrorHandler;import org.xml.sax.ext.DeclHandler;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.ext.LexicalHandler;import org.apache.xpath.objects.XString;// import org.xml.sax.ext.DeclHandler;import javax.xml.transform.sax.TransformerHandler;import javax.xml.transform.Transformer;import javax.xml.transform.Result;import org.apache.xpath.XPathContext;import org.apache.xml.dtm.DTM;import org.apache.xml.dtm.DTMManager;import org.apache.xml.dtm.ref.IncrementalSAXSource;import org.apache.xml.dtm.ref.IncrementalSAXSource_Filter;import org.apache.xml.dtm.ref.sax2dtm.SAX2DTM;import org.apache.xalan.res.XSLTErrorResources;import org.apache.xalan.res.XSLMessages;/** * A TransformerHandler * listens for SAX ContentHandler parse events and transforms * them to a Result. */public class TransformerHandlerImpl        implements EntityResolver, DTDHandler, ContentHandler, ErrorHandler,                   LexicalHandler, TransformerHandler, DeclHandler{    private boolean m_insideParse = false;  ////////////////////////////////////////////////////////////////////  // Constructors.  ////////////////////////////////////////////////////////////////////  /**   * Construct a TransformerHandlerImpl.   *   * @param transformer Non-null reference to the Xalan transformer impl.   * @param doFragment True if the result should be a document fragement.   * @param baseSystemID  The system ID to use as the base for relative URLs.   */  public TransformerHandlerImpl(TransformerImpl transformer,                                boolean doFragment, String baseSystemID)  {    super();    m_transformer = transformer;    m_baseSystemID = baseSystemID;    XPathContext xctxt = transformer.getXPathContext();    DTM dtm = xctxt.getDTM(null, true, transformer, true, true);        m_dtm = dtm;    dtm.setDocumentBaseURI(baseSystemID);    m_contentHandler = dtm.getContentHandler();    m_dtdHandler = dtm.getDTDHandler();    m_entityResolver = dtm.getEntityResolver();    m_errorHandler = dtm.getErrorHandler();    m_lexicalHandler = dtm.getLexicalHandler();  }    /**    * Do what needs to be done to shut down the CoRoutine management.   */  protected void clearCoRoutine()  {    clearCoRoutine(null);  }    /**    * Do what needs to be done to shut down the CoRoutine management.   */  protected void clearCoRoutine(SAXException ex)  {    if(null != ex)      m_transformer.setExceptionThrown(ex);        if(m_dtm instanceof SAX2DTM)    {      if(DEBUG)        System.err.println("In clearCoRoutine...");      try      {        SAX2DTM sax2dtm = ((SAX2DTM)m_dtm);                  if(null != m_contentHandler            && m_contentHandler instanceof IncrementalSAXSource_Filter)        {          IncrementalSAXSource_Filter sp =            (IncrementalSAXSource_Filter)m_contentHandler;          // This should now be all that's needed.          sp.deliverMoreNodes(false);        }                sax2dtm.clearCoRoutine(true);        m_contentHandler = null;        m_dtdHandler = null;        m_entityResolver = null;        m_errorHandler = null;        m_lexicalHandler = null;      }      catch(Throwable throwable)      {        throwable.printStackTrace();      }            if(DEBUG)        System.err.println("...exiting clearCoRoutine");    }  }    ////////////////////////////////////////////////////////////////////  // Implementation of javax.xml.transform.sax.TransformerHandler.  ////////////////////////////////////////////////////////////////////  /**   * Enables the user of the TransformerHandler to set the   * to set the Result for the transformation.   *   * @param result A Result instance, should not be null.   *   * @throws IllegalArgumentException if result is invalid for some reason.   */  public void setResult(Result result) throws IllegalArgumentException  {    if (null == result)      throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_RESULT_NULL, null)); //"result should not be null");    try    {      ContentHandler handler =        m_transformer.createResultContentHandler(result);      m_transformer.setContentHandler(handler);    }    catch (javax.xml.transform.TransformerException te)    {      throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_RESULT_COULD_NOT_BE_SET, null)); //"result could not be set");    }    m_result = result;  }  /**   * Set the base ID (URI or system ID) from where relative   * URLs will be resolved.   * @param systemID Base URI for the source tree.   */  public void setSystemId(String systemID)  {    m_baseSystemID = systemID;    m_dtm.setDocumentBaseURI(systemID);  }  /**   * Get the base ID (URI or system ID) from where relative   * URLs will be resolved.   * @return The systemID that was set with {@link #setSystemId}.   */  public String getSystemId()  {    return m_baseSystemID;  }  /**   * Get the Transformer associated with this handler, which   * is needed in order to set parameters and output properties.   *   * @return The Transformer associated with this handler   */  public Transformer getTransformer()  {    return m_transformer;  }  ////////////////////////////////////////////////////////////////////  // Implementation of org.xml.sax.EntityResolver.  ////////////////////////////////////////////////////////////////////  /**   * Filter an external entity resolution.   *   * @param publicId The entity's public identifier, or null.   * @param systemId The entity's system identifier.   * @return A new InputSource or null for the default.   *   * @throws IOException   * @throws SAXException The client may throw   *            an exception during processing.   * @throws java.io.IOException The client may throw an   *            I/O-related exception while obtaining the   *            new InputSource.   * @see org.xml.sax.EntityResolver#resolveEntity   */  public InputSource resolveEntity(String publicId, String systemId)          throws SAXException, IOException  {    if (m_entityResolver != null)    {      return m_entityResolver.resolveEntity(publicId, systemId);    }    else    {      return null;    }  }  ////////////////////////////////////////////////////////////////////  // Implementation of org.xml.sax.DTDHandler.  ////////////////////////////////////////////////////////////////////  /**   * Filter a notation declaration event.   *   * @param name The notation name.   * @param publicId The notation's public identifier, or null.   * @param systemId The notation's system identifier, or null.   * @throws SAXException The client may throw   *            an exception during processing.   * @see org.xml.sax.DTDHandler#notationDecl   */  public void notationDecl(String name, String publicId, String systemId)          throws SAXException  {    if (m_dtdHandler != null)    {      m_dtdHandler.notationDecl(name, publicId, systemId);    }  }  /**   * Filter an unparsed entity declaration event.   *   * @param name The entity name.   * @param publicId The entity's public identifier, or null.   * @param systemId The entity's system identifier, or null.   * @param notationName The name of the associated notation.   * @throws SAXException The client may throw   *            an exception during processing.   * @see org.xml.sax.DTDHandler#unparsedEntityDecl   */  public void unparsedEntityDecl(          String name, String publicId, String systemId, String notationName)            throws SAXException  {    if (m_dtdHandler != null)    {      m_dtdHandler.unparsedEntityDecl(name, publicId, systemId, notationName);    }  }  ////////////////////////////////////////////////////////////////////  // Implementation of org.xml.sax.ContentHandler.  ////////////////////////////////////////////////////////////////////  /**   * Filter a new document locator event.   *   * @param locator The document locator.   * @see org.xml.sax.ContentHandler#setDocumentLocator   */  public void setDocumentLocator(Locator locator)  {    if (DEBUG)      System.out.println("TransformerHandlerImpl#setDocumentLocator: "                         + locator.getSystemId());    this.m_locator = locator;        if(null == m_baseSystemID)    {      setSystemId(locator.getSystemId());    }    if (m_contentHandler != null)    {      m_contentHandler.setDocumentLocator(locator);    }  }  /**   * Filter a start document event.   *   * @throws SAXException The client may throw   *            an exception during processing.   * @see org.xml.sax.ContentHandler#startDocument   */

⌨️ 快捷键说明

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