⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stylesheet.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* Stylesheet.java --    Copyright (C) 2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package gnu.xml.transform;import java.text.DecimalFormat;import java.text.DecimalFormatSymbols;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.LinkedHashSet;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import java.util.StringTokenizer;import java.util.TreeSet;import javax.xml.XMLConstants;import javax.xml.namespace.NamespaceContext;import javax.xml.namespace.QName;import javax.xml.transform.Source;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.xpath.XPathFunction;import javax.xml.xpath.XPathFunctionResolver;import javax.xml.xpath.XPathExpressionException;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.DOMException;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.Text;import org.w3c.dom.UserDataHandler;import gnu.xml.xpath.Expr;import gnu.xml.xpath.NameTest;import gnu.xml.xpath.NodeTypeTest;import gnu.xml.xpath.Pattern;import gnu.xml.xpath.Selector;import gnu.xml.xpath.Root;import gnu.xml.xpath.Test;import gnu.xml.xpath.XPathImpl;/** * An XSL stylesheet. * * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> */class Stylesheet  implements NamespaceContext, XPathFunctionResolver, UserDataHandler, Cloneable{  static final String XSL_NS = "http://www.w3.org/1999/XSL/Transform";    static final int OUTPUT_XML = 0;  static final int OUTPUT_HTML = 1;  static final int OUTPUT_TEXT = 2;  final TransformerFactoryImpl factory;  TransformerImpl transformer;  Stylesheet parent;  final XPathImpl xpath;  final String systemId;  final int precedence;  final boolean debug;  /**   * Version of XSLT.   */  String version;  Collection extensionElementPrefixes;  Collection excludeResultPrefixes;  /**   * Set of element names for which we should strip whitespace.   */  Set stripSpace;  /**   * Set of element names for which we should preserve whitespace.   */  Set preserveSpace;  /**   * Output options.   */  Node output;  int outputMethod;  String outputVersion;  String outputEncoding;  boolean outputOmitXmlDeclaration;  boolean outputStandalone;  String outputPublicId;  String outputSystemId;  Collection outputCdataSectionElements;  boolean outputIndent;  String outputMediaType;  /**   * Keys.   */  Collection keys;  /**   * Decimal formats.   */  Map decimalFormats;    /**   * Namespace aliases.   */  Map namespaceAliases;  /**   * Attribute-sets.   */  List attributeSets;  /**   * Variables.   */  List variables;  /**   * Variable and parameter bindings.   */  Bindings bindings;  /**   * Templates.   */  LinkedList templates;  TemplateNode builtInNodeTemplate;  TemplateNode builtInTextTemplate;  /**   * Holds the current node while parsing.   * Necessary to associate the document function with its declaring node,   * to resolve namespaces, and to maintain the current node for the   * current() function.   */  Node current;  /**   * Set by a terminating message.   */  transient boolean terminated;  /**   * Current template in force.   */  transient Template currentTemplate;  Stylesheet(TransformerFactoryImpl factory,             Stylesheet parent,             Document doc,             String systemId,             int precedence)    throws TransformerConfigurationException  {    this.factory = factory;    this.systemId = systemId;    this.precedence = precedence;    this.parent = parent;    extensionElementPrefixes = new HashSet();    excludeResultPrefixes = new HashSet();    stripSpace = new LinkedHashSet();    preserveSpace = new LinkedHashSet();    outputCdataSectionElements = new LinkedHashSet();    xpath = (XPathImpl) factory.xpathFactory.newXPath();    if (parent == null)      {        bindings = new Bindings(this);        attributeSets = new LinkedList();        variables = new LinkedList();        namespaceAliases = new LinkedHashMap();        templates = new LinkedList();        keys = new LinkedList();        decimalFormats = new LinkedHashMap();        initDefaultDecimalFormat();        xpath.setNamespaceContext(this);        xpath.setXPathFunctionResolver(this);      }    else      {        /* Test for import circularity */        for (Stylesheet ctx = this; ctx.parent != null; ctx = ctx.parent)          {            if (systemId != null && systemId.equals(ctx.parent.systemId))              {                String msg = "circularity importing " + systemId;                throw new TransformerConfigurationException(msg);              }          }        /* OK */        Stylesheet root = getRootStylesheet();        bindings = root.bindings;        attributeSets = root.attributeSets;        variables = root.variables;        namespaceAliases = root.namespaceAliases;        templates = root.templates;        keys = root.keys;        decimalFormats = root.decimalFormats;        xpath.setNamespaceContext(root);        xpath.setXPathFunctionResolver(root);      }    xpath.setXPathVariableResolver(bindings);    Test anyNode = new NodeTypeTest((short) 0);    List tests = Collections.singletonList(anyNode);    builtInNodeTemplate =      new ApplyTemplatesNode(new Selector(Selector.CHILD, tests),                             null, null, null, true);    builtInTextTemplate =      new ValueOfNode(new Selector(Selector.SELF, tests),                      false);        parse(doc.getDocumentElement(), true);    current = doc; // Alow namespace resolution during processing        debug = ("yes".equals(System.getProperty("xsl.debug")));    if (debug)      {        System.err.println("Stylesheet: " + doc.getDocumentURI());        for (Iterator i = templates.iterator(); i.hasNext(); )          {            Template t = (Template) i.next();            t.list(System.err);            System.err.println("--------------------");          }      }  }  Stylesheet getRootStylesheet()  {    Stylesheet stylesheet = this;    while (stylesheet.parent != null)      {        stylesheet = stylesheet.parent;      }    return stylesheet;  }    void initDefaultDecimalFormat()  {    DecimalFormat defaultDecimalFormat = new DecimalFormat();    DecimalFormatSymbols symbols = new DecimalFormatSymbols();    symbols.setDecimalSeparator('.');    symbols.setGroupingSeparator(',');    symbols.setPercent('%');    symbols.setPerMill('\u2030');    symbols.setZeroDigit('0');    symbols.setDigit('#');    symbols.setPatternSeparator(';');    symbols.setInfinity("Infinity");    symbols.setNaN("NaN");    symbols.setMinusSign('-');    defaultDecimalFormat.setDecimalFormatSymbols(symbols);    decimalFormats.put(null, defaultDecimalFormat);  }    // -- Cloneable --  public Object clone()  {    try      {        Stylesheet clone = (Stylesheet) super.clone();        clone.bindings = (Bindings) bindings.clone();        LinkedList templates2 = new LinkedList();        for (Iterator i = templates.iterator(); i.hasNext(); )          {            Template t = (Template) i.next();            templates2.add(t.clone(clone));          }        clone.templates = templates2;        LinkedList attributeSets2 = new LinkedList();        for (Iterator i = attributeSets.iterator(); i.hasNext(); )          {            AttributeSet as = (AttributeSet) i.next();            attributeSets2.add(as.clone(clone));          }        clone.attributeSets = attributeSets2;        LinkedList variables2 = new LinkedList();        for (Iterator i = variables.iterator(); i.hasNext(); )          {            ParameterNode var = (ParameterNode) i.next();            variables2.add(var.clone(clone));          }        clone.variables = variables2;        LinkedList keys2 = new LinkedList();        for (Iterator i = keys.iterator(); i.hasNext(); )          {            Key k = (Key) i.next();            keys2.add(k.clone(clone));          }        clone.keys = keys2;                return clone;      }    catch (CloneNotSupportedException e)      {        throw new Error(e.getMessage());      }  }  // -- Variable evaluation --  void initTopLevelVariables(Node context)    throws TransformerException  {    current = context;    // Sort the variables into order    // See XSLT 11.4: "If the template or expression specifying the value of    // a global variable x references a global variable y, then the value    // for y must be computed before the value of x."    List topLevel = new ArrayList(variables);    Collections.sort(topLevel);    for (Iterator i = topLevel.iterator(); i.hasNext(); )      {        ParameterNode var = (ParameterNode) i.next();        bindings.set(var.name,                     var.getValue(this, null, context, 1, 1),                     var.type);      }    current = null;  }  // -- NamespaceContext --  public String getNamespaceURI(String prefix)  {    return (current == null) ? null : current.lookupNamespaceURI(prefix);  }  public String getPrefix(String namespaceURI)  {    return (current == null) ? null : current.lookupPrefix(namespaceURI);  }  public Iterator getPrefixes(String namespaceURI)  {    // TODO    return Collections.singleton(getPrefix(namespaceURI)).iterator();  }  final QName getQName(String name)  {    String localName = name, uri = null, prefix = null;    int ci = name.indexOf(':');    if (ci != -1)      {        prefix = name.substring(0, ci);        localName = name.substring(ci + 1);        uri = getNamespaceURI(prefix);      }    return new QName(uri, localName, prefix);  }    // -- Template selection --    TemplateNode getTemplate(QName mode, Node context, boolean applyImports)    throws TransformerException  {    if (debug)      {        System.err.println("getTemplate: mode="+mode+" context="+context);      }    Set candidates = new TreeSet();    for (Iterator j = templates.iterator(); j.hasNext(); )      {        Template t = (Template) j.next();        boolean isMatch = t.matches(mode, context);        if (applyImports)          {            if (currentTemplate == null)              {                String msg = "current template may not be null " +                  "during apply-imports";                throw new TransformerException(msg);              }            if (!currentTemplate.imports(t))              {                isMatch = false;              }          }        //System.err.println("\t"+context+" "+t+"="+isMatch);        if (isMatch)          {            candidates.add(t);          }      }    //System.err.println("\tcandidates="+candidates);    if (candidates.isEmpty())      {        // Apply built-in template        // Current template is unchanged        if (debug)          {            System.err.println("\tbuiltInTemplate context="+context);          }        switch (context.getNodeType())          {          case Node.ELEMENT_NODE:          case Node.DOCUMENT_NODE:          case Node.DOCUMENT_FRAGMENT_NODE:          case Node.PROCESSING_INSTRUCTION_NODE:          case Node.COMMENT_NODE:            return builtInNodeTemplate;          case Node.TEXT_NODE:          case Node.ATTRIBUTE_NODE:            return builtInTextTemplate;          default:            return null;

⌨️ 快捷键说明

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