outputproperties.java

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

JAVA
1,123
字号
/* * 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.templates;import java.io.BufferedInputStream;import java.io.InputStream;import java.io.IOException;import java.util.Vector;import java.util.Hashtable;import java.util.Properties;import java.util.Enumeration;import java.lang.Cloneable;import java.security.AccessController;import java.security.PrivilegedAction;import org.w3c.dom.Document;import org.apache.xml.utils.QName;import org.apache.xml.utils.FastStringBuffer;import org.apache.xml.utils.WrappedRuntimeException;import org.apache.xalan.serialize.Method;import org.apache.xalan.extensions.ExtensionHandler;import org.apache.xalan.res.XSLTErrorResources;import org.apache.xalan.res.XSLMessages;import javax.xml.transform.TransformerException;import javax.xml.transform.OutputKeys;/** * This class provides information from xsl:output elements. It is mainly * a wrapper for {@link java.util.Properties}, but can not extend that class * because it must be part of the {@link org.apache.xalan.templates.ElemTemplateElement} * heararchy. * <p>An OutputProperties list can contain another OutputProperties list as * its "defaults"; this second property list is searched if the property key * is not found in the original property list.</p> * @see <a href="http://www.w3.org/TR/xslt#dtd">XSLT DTD</a> * @see <a href="http://www.w3.org/TR/xslt#output">xsl:output in XSLT Specification</a> * @ */public class OutputProperties extends ElemTemplateElement        implements Cloneable{  /**   * Creates an empty OutputProperties with no default values.   */  public OutputProperties()  {    this(Method.XML);  }  /**   * Creates an empty OutputProperties with the specified defaults.   *   * @param   defaults   the defaults.   */  public OutputProperties(Properties defaults)  {    m_properties = new Properties(defaults);  }  /**   * Creates an empty OutputProperties with the defaults specified by   * a property file.  The method argument is used to construct a string of   * the form output_[method].properties (for instance, output_html.properties).   * The output_xml.properties file is always used as the base.   * <p>At the moment, anything other than 'text', 'xml', and 'html', will   * use the output_xml.properties file.</p>   *   * @param   method non-null reference to method name.   */  public OutputProperties(String method)  {    m_properties = new Properties(getDefaultMethodProperties(method));  }    static final String S_XSLT_PREFIX = "xslt.output.";  static final int S_XSLT_PREFIX_LEN = S_XSLT_PREFIX.length();  static final String S_XALAN_PREFIX = "org.apache.xslt.";  static final int S_XALAN_PREFIX_LEN = S_XALAN_PREFIX.length();    /** Built-in extensions namespace, reexpressed in {namespaceURI} syntax   * suitable for prepending to a localname to produce a "universal   * name".   */  static final String S_BUILTIN_EXTENSIONS_UNIVERSAL=        "{"+Constants.S_BUILTIN_EXTENSIONS_URL+"}";    /**   * The old built-in extension namespace   */  static final String S_BUILTIN_OLD_EXTENSIONS_UNIVERSAL=        "{"+Constants.S_BUILTIN_OLD_EXTENSIONS_URL+"}";    static final int S_BUILTIN_OLD_EXTENSIONS_UNIVERSAL_LEN = S_BUILTIN_OLD_EXTENSIONS_UNIVERSAL.length();    /**   * Fix up a string in an output properties file according to    * the rules of {@link #loadPropertiesFile}.   *    * @param s non-null reference to string that may need to be fixed up.   * @return A new string if fixup occured, otherwise the s argument.   */  static private String fixupPropertyString(String s, boolean doClipping)  {    int index;    if (doClipping && s.startsWith(S_XSLT_PREFIX))    {      s = s.substring(S_XSLT_PREFIX_LEN);    }    if (s.startsWith(S_XALAN_PREFIX))    {      s = S_BUILTIN_EXTENSIONS_UNIVERSAL + s.substring(S_XALAN_PREFIX_LEN);    }    if ((index = s.indexOf("\\u003a")) > 0)    {      String temp = s.substring(index+6);      s = s.substring(0, index) + ":" + temp;    }    return s;  }    /**   * Load the properties file from a resource stream.  If a    * key name such as "org.apache.xslt.xxx", fix up the start of    * string to be a curly namespace.  If a key name starts with    * "xslt.output.xxx", clip off "xslt.output.".  If a key name *or* a    * key value is discovered, check for \u003a in the text, and    * fix it up to be ":", since earlier versions of the JDK do not    * handle the escape sequence (at least in key names).   *    * @param resourceName non-null reference to resource name.   * @param defaults Default properties, which may be null.   */  static private Properties loadPropertiesFile(final String resourceName, Properties defaults)    throws IOException  {    // This static method should eventually be moved to a thread-specific class    // so that we can cache the ContextClassLoader and bottleneck all properties file    // loading throughout Xalan.    Properties props = new Properties(defaults);    InputStream is = null;    BufferedInputStream bis = null;    try {      try {	// Using doPrivileged to be able to read property file without opening        // up secured container permissions like J2EE container        is =(InputStream)AccessController.doPrivileged( new PrivilegedAction() {          public Object run() {            try {              java.lang.reflect.Method getCCL = Thread.class.getMethod(                  "getContextClassLoader", NO_CLASSES);              if (getCCL != null) {                ClassLoader contextClassLoader = (ClassLoader)                    getCCL.invoke(Thread.currentThread(), NO_OBJS);                return ( contextClassLoader.getResourceAsStream (                    "org/apache/xalan/templates/" + resourceName) );              }            }            catch ( Exception e ) { }            return null;                      }         });      }      catch (Exception e) {}      if ( is == null ) {        is = (InputStream)AccessController.doPrivileged( new PrivilegedAction(){	  public Object run() {            return OutputProperties.class.getResourceAsStream(resourceName);          }        });      }            bis = new BufferedInputStream(is);      props.load(bis);    }     catch (IOException ioe) {      if ( defaults == null ) {        throw ioe;      }      else {        throw new WrappedRuntimeException(XSLMessages.createMessage(XSLTErrorResources.ER_COULD_NOT_LOAD_RESOURCE, new Object[]{resourceName}), ioe); //"Could not load '"+resourceName+"' (check CLASSPATH), now using just the defaults ", ioe);      }    }    catch (SecurityException se) {      // Repeat IOException handling for sandbox/applet case -sc      if ( defaults == null ) {        throw se;      }      else {        throw new WrappedRuntimeException(XSLMessages.createMessage(XSLTErrorResources.ER_COULD_NOT_LOAD_RESOURCE, new Object[]{resourceName}), se); //"Could not load '"+resourceName+"' (check CLASSPATH, applet security), now using just the defaults ", se);      }    }     finally {      if ( bis != null ) {        bis.close();      }      if (is != null ) {        is.close();      }    }        // Note that we're working at the HashTable level here,     // and not at the Properties level!  This is important     // because we don't want to modify the default properties.    // NB: If fixupPropertyString ends up changing the property    // name or value, we need to remove the old key and re-add    // with the new key and value.  However, then our Enumeration    // could lose its place in the HashTable.  So, we first    // clone the HashTable and enumerate over that since the    // clone will not change.  When we migrate to Collections,    // this code should be revisited and cleaned up to use    // an Iterator which may (or may not) alleviate the need for    // the clone.  Many thanks to Padraig O'hIceadha    // <padraig@gradient.ie> for finding this problem.  Bugzilla 2000.    Enumeration keys = ((Properties) props.clone()).keys();    while(keys.hasMoreElements())    {      String key = (String)keys.nextElement();      // Now check if the given key was specified as a       // System property. If so, the system property       // overides the default value in the propery file.      String value = null;      try {        value = System.getProperty(key);      }      catch (SecurityException se) {        // No-op for sandbox/applet case, leave null -sc      }      if (value == null)              value = (String)props.get(key);                                   String newKey = fixupPropertyString(key, true);      String newValue = null;      try {        newValue = System.getProperty(newKey);      }      catch (SecurityException se) {        // No-op for sandbox/applet case, leave null -sc      }      if (newValue == null)        newValue = fixupPropertyString(value, false);      else        newValue = fixupPropertyString(newValue, false);             if(key != newKey || value != newValue)      {        props.remove(key);        props.put(newKey, newValue);      }          }        return props;  }  /**   * Creates an empty OutputProperties with the defaults specified by   * a property file.  The method argument is used to construct a string of   * the form output_[method].properties (for instance, output_html.properties).   * The output_xml.properties file is always used as the base.   * <p>At the moment, anything other than 'text', 'xml', and 'html', will   * use the output_xml.properties file.</p>   *   * @param   method non-null reference to method name.   *   * @return Properties object that holds the defaults for the given method.   */  static public Properties getDefaultMethodProperties(String method)  {    String fileName = null;    Properties defaultProperties = null;    // According to this article : Double-check locking does not work    // http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-toolbox.html    try    {      synchronized (m_synch_object)      {        if (null == m_xml_properties)  // double check        {          fileName = "output_xml.properties";          m_xml_properties = loadPropertiesFile(fileName, null);        }      }      if (method.equals(Method.XML))      {        defaultProperties = m_xml_properties;      }      else if (method.equals(Method.HTML))      {            if (null == m_html_properties)  // double check            {              fileName = "output_html.properties";              m_html_properties = loadPropertiesFile(fileName,                                                     m_xml_properties);            }        defaultProperties = m_html_properties;      }      else if (method.equals(Method.Text))      {            if (null == m_text_properties)  // double check            {              fileName = "output_text.properties";              m_text_properties = loadPropertiesFile(fileName,                                                     m_xml_properties);

⌨️ 快捷键说明

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