transformerimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 640 行 · 第 1/2 页

JAVA
640
字号
/* * 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.LineMap;import com.caucho.util.L10N;import com.caucho.vfs.Encoding;import com.caucho.vfs.IOExceptionWrapper;import com.caucho.vfs.Path;import com.caucho.vfs.Vfs;import com.caucho.vfs.WriteStream;import com.caucho.xml.*;import com.caucho.xpath.XPathFun;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.xml.sax.ContentHandler;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.ext.LexicalHandler;import javax.xml.transform.ErrorListener;import javax.xml.transform.OutputKeys;import javax.xml.transform.Result;import javax.xml.transform.Source;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.stream.StreamResult;import javax.xml.transform.stream.StreamSource;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.ArrayList;import java.util.HashMap;import java.util.Properties;public class TransformerImpl extends javax.xml.transform.Transformer {  protected static L10N L = new L10N(TransformerImpl.class);    public final static String LINE_MAP = "caucho.line-map";  public final static String CACHE_DEPENDS = "caucho.cache.depends";  public final static String GENERATE_LOCATION = "caucho.generate.location";  protected StylesheetImpl _stylesheet;  protected HashMap<String,Object> _properties = new HashMap<String,Object>();  protected HashMap<String,Object> _parameters;    private URIResolver _uriResolver;  private ErrorListener _errorListener;  private Properties _output;    protected LineMap _lineMap;  protected ArrayList<Path> _cacheDepends = new ArrayList<Path>();  protected TransformerImpl(StylesheetImpl stylesheet)  {    _stylesheet = stylesheet;    _uriResolver = stylesheet.getURIResolver();  }  /**   * 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 boolean getFeature(String name)  {    if (name.equals(DOMResult.FEATURE))      return true;    else if (name.equals(DOMSource.FEATURE))      return true;    else if (name.equals(StreamSource.FEATURE))      return true;    else if (name.equals(StreamResult.FEATURE))      return true;    else if (name.equals(SAXSource.FEATURE))      return true;    else if (name.equals(SAXResult.FEATURE))      return true;    else      return false;  }  public void setFeature(String name, boolean enable)  {    if (name.equals(GENERATE_LOCATION))      _stylesheet.setGenerateLocation(enable);  }  public StylesheetImpl getStylesheet()  {    return _stylesheet;  }  public Object getProperty(String name)  {    Object property = _properties.get(name);    if (property != null)      return property;        if (name.equals(CACHE_DEPENDS))      return _cacheDepends;    else if (name.equals(LINE_MAP))      return _lineMap;    else      return _stylesheet.getProperty(name);  }  public void setProperty(String name, Object value)  {    _properties.put(name, value);  }  /**   * Sets a parameter that XPath expressions in the stylesheet can   * use as $name.   *   * @param name the name of the XPath variable.   * @param value the value for the variable.   */  public void setParameter(String name, Object value)  {    if (_parameters == null)      _parameters = new HashMap<String,Object>();        _parameters.put(name, value);  }  /**   * Returns a copy of the xsl:output properties.   *   * @return a copy of the properties.   */  public Properties getOutputProperties()  {    if (_output == null)      _output = (Properties) _stylesheet.getOutputProperties().clone();        return (Properties) _output.clone();  }  /**   * Sets the output properties.   *   * @param properties the new output properties.   */  public void setOutputProperties(Properties properties)  {    _output = properties;  }  /**   * Sets a single xsl:output property.   *   * @param name the name of the property.   * @param value the value of the property.   */  public void setOutputProperty(String name, String value)  {    if (_output == null)      _output = (Properties) _stylesheet.getOutputProperties().clone();    _output.put(name, value);  }  /**   * Returns the value of a single named xsl:output property.   *   * @param name the name of the property.   */  public String getOutputProperty(String name)  {    if (_output == null)      _output = (Properties) _stylesheet.getOutputProperties().clone();    return (String) _output.get(name);  }  /**   * Returns the named stylesheet parameter.   *   * @param name the name of the parameter.   *   * @ return the value of the named parameter.   */  public Object getParameter(String name)  {    if (_parameters == null)      return null;    else      return _parameters.get(name);  }  /**   * Clears all the external stylesheet parameters.   */  public void clearParameters()  {    if (_parameters != null)      _parameters.clear();        if (_cacheDepends != null)      _cacheDepends.clear();  }  /**   * Adds a new custom function.   *   * @param name the name of the function.   * @param fun the new function.   */  public void addFunction(String name, XPathFun fun)  {    _stylesheet.addFunction(name, fun);  }  /**   * Transforms the source into the result.   *   * @param source descriptor specifying the input source.   * @param result descriptor specifying the output result.   */  public void transform(Source source, Result result)    throws TransformerException  {    try {      Node node = parseDocument(source);            if (result instanceof StreamResult) {        StreamResult stream = (StreamResult) result;        if (stream.getOutputStream() != null)          transform(node, stream.getOutputStream(), null, result.getSystemId());        else if (stream.getWriter() != null) {          Writer writer = stream.getWriter();          WriteStream os = Vfs.openWrite(writer);          if (writer instanceof OutputStreamWriter) {	    String javaEncoding = ((OutputStreamWriter) writer).getEncoding();	    String mimeEncoding = Encoding.getMimeName(javaEncoding);            transform(node, os, mimeEncoding, result.getSystemId());	  }          else            transform(node, os, null, result.getSystemId());          os.flush();          os.free();        }        else {          WriteStream os = Vfs.lookup(result.getSystemId()).openWrite();                      try {            transform(node, os, null, result.getSystemId());          } finally {            os.close();          }

⌨️ 快捷键说明

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