abstractstylesheetfactory.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,258 行 · 第 1/3 页

JAVA
1,258
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.xsl;import com.caucho.java.JavaCompiler;import com.caucho.loader.DynamicClassLoader;import com.caucho.loader.EnvironmentLocal;import com.caucho.loader.SimpleLoader;import com.caucho.log.Log;import com.caucho.server.util.CauchoSystem;import com.caucho.util.Base64;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import com.caucho.util.LruCache;import com.caucho.vfs.Crc64Stream;import com.caucho.vfs.Path;import com.caucho.vfs.MergePath;import com.caucho.vfs.ReadStream;import com.caucho.vfs.Vfs;import com.caucho.vfs.WriteStream;import com.caucho.xml.*;import com.caucho.xpath.Expr;import com.caucho.xpath.XPath;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.DocumentType;import org.w3c.dom.Node;import org.xml.sax.ContentHandler;import org.xml.sax.InputSource;import org.xml.sax.XMLFilter;import org.xml.sax.XMLReader;import javax.xml.transform.ErrorListener;import javax.xml.transform.Source;import javax.xml.transform.Templates;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.transform.URIResolver;import javax.xml.transform.dom.DOMResult;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.sax.SAXResult;import javax.xml.transform.sax.SAXSource;import javax.xml.transform.sax.SAXTransformerFactory;import javax.xml.transform.sax.TemplatesHandler;import javax.xml.transform.sax.TransformerHandler;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.Reader;import java.lang.ref.SoftReference;import java.util.Iterator;import java.util.Random;import java.util.logging.Level;import java.util.logging.Logger;/** * Abstract factory for creating stylesheets. */abstract public class AbstractStylesheetFactory  extends SAXTransformerFactory {  static final Logger log = Log.open(AbstractStylesheetFactory.class);  static final L10N L = new L10N(AbstractStylesheetFactory.class);  private static    EnvironmentLocal<LruCache<String,SoftReference<StylesheetImpl>>> _stylesheetCache =    new EnvironmentLocal<LruCache<String,SoftReference<StylesheetImpl>>>();			    private URIResolver _uriResolver;  private ErrorListener _errorListener;  private String _systemId;  private Path _workPath;  private Path _stylePath;  private ClassLoader _loader;  private String _className;  private boolean _isAutoCompile = true;  private boolean _loadPrecompiledStylesheet = true;  protected AbstractStylesheetFactory()  {  }  /**   * Returns an implementation-specific attribute.   *   * @param name the attribute name   */  public Object getAttribute(String name)  {    return null;  }  /**   * Sets an implementation-specific attribute.   *   * @param name the attribute name   * @param value the attribute value   */  public void setAttribute(String name, Object value)  {  }  /**   * Returns an implementation-specific feature.   *   * @param name the feature name   */  public boolean getFeature(String name)  {    if (name.equals(SAXTransformerFactory.FEATURE) ||        name.equals(SAXTransformerFactory.FEATURE_XMLFILTER) ||        name.equals(DOMResult.FEATURE) ||        name.equals(DOMSource.FEATURE) ||        name.equals(SAXResult.FEATURE) ||        name.equals(SAXSource.FEATURE) ||        name.equals(StreamResult.FEATURE) ||        name.equals(StreamSource.FEATURE))      return true;    else      return false;  }  /**   * Sets an implementation-specific feature   *   * @param name the feature name   * @param value the feature value   */  public void setFeature(String name, boolean value)  {  }  /**   * Returns the URI to filename resolver.   */  public URIResolver getURIResolver()  {    return _uriResolver;  }  /**   * Sets the URI to filename resolver.   */  public void setURIResolver(URIResolver uriResolver)  {    _uriResolver = uriResolver;  }  /**   * Returns the error listener.   */  public ErrorListener getErrorListener()  {    return _errorListener;  }  /**   * Sets the error listener.   */  public void setErrorListener(ErrorListener errorListener)  {    _errorListener = errorListener;  }  public String getSystemId()  {    return _systemId;  }  public void setSystemId(String systemId)  {    _systemId = systemId;  }  /**   * Sets the search path for stylesheets.  Generally applications will use   * MergePath to create their search path.   *   * @param path path containing stylesheets.   */  public void setStylePath(Path path)  {    _stylePath = path;  }  /**   * Returns the stylesheet search path.   */  public Path getStylePath()  {    if (_stylePath != null)      return _stylePath;    else      return getSearchPath();  }  /**   * Sets the search path for stylesheets.  Generally applications will use   * MergePath to create their search path.   *   * @param path path containing stylesheets.   */  public void setSearchPath(Path path)  {    _stylePath = path;  }  /**   * Returns the stylesheet search path.   */  public Path getSearchPath()  {    if (_stylePath != null)      return _stylePath;    else      return Vfs.getPwd();  }  /**   * Sets the working directory.   */  public void setWorkPath(Path path)  {    _workPath = path;  }  /**   * Gets the working directory.   */  public Path getWorkPath()  {    if (_workPath != null)      return _workPath;    else      return CauchoSystem.getWorkPath();  }  public void setClassName(String className)  {    _className = className;  }  public String getClassName()  {    return _className;  }  /**   * Sets the classloader for the stylesheet.   *   * @param loader the new loader.   */  public void setClassLoader(ClassLoader loader)  {    _loader = loader;  }  /**   * Gets the classloader for the stylesheet.   */  public ClassLoader getClassLoader()  {    return _loader;  }  /**   * Returns true if precompiled stylesheets should be loaded.   */  public boolean getLoadPrecompiledStylesheet()  {    return _loadPrecompiledStylesheet;  }  /**   * Returns true if precompiled stylesheets should be loaded.   */  public void setLoadPrecompiledStylesheet(boolean preload)  {    _loadPrecompiledStylesheet = preload;  }  /**   * Returns true if the stylesheet should be automatically compiled.   */  public boolean isAutoCompile()  {    return _isAutoCompile;  }  /**   * Returns true if precompiled stylesheets should be loaded.   */  public void setAutoCompile(boolean autoCompile)  {    _isAutoCompile = autoCompile;  }  /**   * Returns the stylesheet source object associated with the given   * XML document.   *   * @param source the XML document which needs a stylesheet.   * @param media the media attribute for the stylesheet   * @param title the title attribute for the stylesheet   * @param charset the character encoding for the stylesheet result.   */  public Source getAssociatedStylesheet(Source source,                                        String media,                                        String title,                                        String charset)    throws TransformerConfigurationException  {    try {      XmlStylesheetReader reader = new XmlStylesheetReader();      parseSAX(source, reader);      String href = reader.getAssociatedStylesheet(media, title, charset);      if (href == null)        return null;      String base = source.getSystemId();          return getSource(href, base);    } catch (Exception e) {      throw new TransformerConfigurationException(e);    }  }    /**   * Opens a relative path.   */  private Source getSource(String href, String base)    throws Exception  {    Path subpath;    if (href == null)      href = "";    if (base == null)      base = "/";        if (_uriResolver != null) {      if (href.startsWith("/") || base.equals("/"))        subpath = getSearchPath().lookup(href);      else {        subpath = getSearchPath().lookup(base).getParent().lookup(href);      }            Source source = _uriResolver.resolve(href, base);      if (source != null) {        if (source.getSystemId() == null)          source.setSystemId(subpath.getURL());        return source;      }    }        if (href.startsWith("/") || base.equals("/"))      subpath = getSearchPath().lookup(href);    else {      if (base.startsWith("file:"))        base = base.substring(5);            subpath = getSearchPath().lookup(base).getParent().lookup(href);    }    return new StreamSource(subpath.getURL());  }  private void parseSAX(Source source, ContentHandler handler)    throws TransformerConfigurationException  {    try {      if (source instanceof SAXSource) {        SAXSource saxSource = (SAXSource) source;        XMLReader reader = saxSource.getXMLReader();        InputSource inputSource = saxSource.getInputSource();        reader.setContentHandler(handler);        reader.parse(inputSource);      }      else if (source instanceof StreamSource) {        XmlParser parser = new Xml();

⌨️ 快捷键说明

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