stylesheet.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,729 行 · 第 1/4 页

JAVA
1,729
字号
  {    current = source;    // Tokenize    int len = value.length();    int off = 0;    List tokens = new ArrayList(); // text tokens    List types = new ArrayList(); // literal or expression    int depth = 0;    for (int i = 0; i < len; i++)      {        char c = value.charAt(i);        if (c == '{')          {            if (i < (len - 1) && value.charAt(i + 1) == '{')              {                tokens.add(value.substring(off, i + 1));                types.add(Boolean.FALSE);                i++;                off = i + 1;                continue;              }            if (depth == 0)              {                if (i - off > 0)                  {                    tokens.add(value.substring(off, i));                    types.add(Boolean.FALSE);                  }                off = i + 1;              }            depth++;          }        else if (c == '}')          {            if (i < (len - 1) && value.charAt(i + 1) == '}')              {                tokens.add(value.substring(off, i + 1));                types.add(Boolean.FALSE);                i++;                off = i + 1;                continue;              }            if (depth == 1)              {                if (i - off > 0)                  {                    tokens.add(value.substring(off, i));                    types.add(Boolean.TRUE);                  }                else                  {                    String msg = "attribute value template " +                      "must contain expression: " + value;                    DOMSourceLocator l = new DOMSourceLocator(source);                    throw new TransformerConfigurationException(msg, l);                  }                off = i + 1;              }            depth--;          }      }    if (depth > 0)      {        String msg = "invalid attribute value template: " + value;        throw new TransformerConfigurationException(msg);      }    if (len - off > 0)      {        // Trailing text        tokens.add(value.substring(off));        types.add(Boolean.FALSE);      }        // Construct template node tree    TemplateNode ret = null;    Document doc = source.getOwnerDocument();    len = tokens.size();    for (int i = len - 1; i >= 0; i--)      {        String token = (String) tokens.get(i);        Boolean type = (Boolean) types.get(i);        if (type == Boolean.TRUE)          {            // Expression text            Expr select = (Expr) xpath.compile(token);            ret = new ValueOfNode(null, ret, select, false);          }        else          {            // Verbatim text            ret = new LiteralNode(null, ret, doc.createTextNode(token));          }      }    return ret;  }  boolean isPreserved(Text text)    throws TransformerConfigurationException  {    // Check characters in text    String value = text.getData();    if (value != null)      {        int len = value.length();        for (int i = 0; i < len; i++)          {            char c = value.charAt(i);            if (c != 0x20 && c != 0x09 && c != 0x0a && c != 0x0d)              {                return true;              }          }      }    // Check parent node    Node ctx = text.getParentNode();    if (!preserveSpace.isEmpty())      {        for (Iterator i = preserveSpace.iterator(); i.hasNext(); )          {            NameTest preserveTest = (NameTest) i.next();            if (preserveTest.matches(ctx, 1, 1))              {                boolean override = false;                if (!stripSpace.isEmpty())                  {                    for (Iterator j = stripSpace.iterator(); j.hasNext(); )                      {                        NameTest stripTest = (NameTest) j.next();                        if (stripTest.matches(ctx, 1, 1))                          {                            override = true;                            break;                          }                      }                  }                if (!override)                  {                    return true;                  }              }          }      }    // Check whether any ancestor specified xml:space    while (ctx != null)      {        if (ctx.getNodeType() == Node.ELEMENT_NODE)          {            Element element = (Element) ctx;            String xmlSpace = element.getAttribute("xml:space");            if ("default".equals(xmlSpace))              {                break;              }            else if ("preserve".equals(xmlSpace))              {                return true;              }            else if (xmlSpace.length() > 0)              {                String msg = "Illegal value for xml:space: " + xmlSpace;                throw new TransformerConfigurationException(msg);              }            else if ("text".equals(ctx.getLocalName()) &&                     XSL_NS.equals(ctx.getNamespaceURI()))              {                // xsl:text implies xml:space='preserve'                return true;              }          }        ctx = ctx.getParentNode();      }    return false;  }  public XPathFunction resolveFunction(QName name, int arity)  {    String uri = name.getNamespaceURI();    if (XSL_NS.equals(uri) || uri == null || uri.length() == 0)      {        String localName = name.getLocalPart();        if ("document".equals(localName) && (arity == 1 || arity == 2))          {            if (current == null)              {                throw new RuntimeException("current is null");              }            return new DocumentFunction(getRootStylesheet(), current);          }        else if ("key".equals(localName) && (arity == 2))          {            return new KeyFunction(getRootStylesheet());          }        else if ("format-number".equals(localName) &&                 (arity == 2 || arity == 3))          {            return new FormatNumberFunction(getRootStylesheet());          }        else if ("current".equals(localName) && (arity == 0))          {            return new CurrentFunction(getRootStylesheet());          }        else if ("unparsed-entity-uri".equals(localName) && (arity == 1))          {            return new UnparsedEntityUriFunction();          }        else if ("generate-id".equals(localName) &&                 (arity == 1 || arity == 0))          {            return new GenerateIdFunction();          }        else if ("system-property".equals(localName) && (arity == 1))          {            return new SystemPropertyFunction();          }        else if ("element-available".equals(localName) && (arity == 1))          {            return new ElementAvailableFunction(this);          }        else if ("function-available".equals(localName) && (arity == 1))          {            return new FunctionAvailableFunction(this);          }      }    return null;  }    // -- Parsing --  /**   * apply-templates   */  final TemplateNode parseApplyTemplates(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String m = getAttribute(attrs, "mode");    QName mode = (m == null) ? null : getQName(m);    String s = getAttribute(attrs, "select");    if (s == null)      {        s = "child::node()";      }    List sortKeys = parseSortKeys(children);    List withParams = parseWithParams(children);    Expr select = (Expr) xpath.compile(s);    return new ApplyTemplatesNode(null, parse(next),                                  select, mode,                                  sortKeys, withParams, false);  }  /**   * call-template   */  final TemplateNode parseCallTemplate(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String n = getRequiredAttribute(attrs, "name", node);    QName name = getQName(n);    List withParams = parseWithParams(children);    return new CallTemplateNode(null, parse(next), name,                                withParams);  }    /**   * value-of   */  final TemplateNode parseValueOf(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String s = getRequiredAttribute(attrs, "select", node);    String doe = getAttribute(attrs, "disable-output-escaping");    boolean d = "yes".equals(doe);    Expr select = (Expr) xpath.compile(s);    return new ValueOfNode(null, parse(next), select, d);  }    /**   * for-each   */  final TemplateNode parseForEach(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String s = getRequiredAttribute(attrs, "select", node);    List sortKeys = parseSortKeys(children);    Expr select = (Expr) xpath.compile(s);    return new ForEachNode(parse(children), parse(next), select, sortKeys);  }    /**   * if   */  final TemplateNode parseIf(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String t = getRequiredAttribute(attrs, "test", node);    Expr test = (Expr) xpath.compile(t);    return new IfNode(parse(children), parse(next), test);  }    /**   * when   */  final TemplateNode parseWhen(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String t = getRequiredAttribute(attrs, "test", node);    Expr test = (Expr) xpath.compile(t);    return new WhenNode(parse(children), parse(next), test);  }    /**   * element   */  final TemplateNode parseElement(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String name = getRequiredAttribute(attrs, "name", node);    String namespace = getAttribute(attrs, "namespace");    String uas = getAttribute(attrs, "use-attribute-sets");    TemplateNode n = parseAttributeValueTemplate(name, node);    TemplateNode ns = (namespace == null) ? null :      parseAttributeValueTemplate(namespace, node);    return new ElementNode(parse(children), parse(next), n, ns, uas, node);  }  /**   * attribute   */  final TemplateNode parseAttribute(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String name = getRequiredAttribute(attrs, "name", node);    String namespace = getAttribute(attrs, "namespace");    TemplateNode n = parseAttributeValueTemplate(name, node);    TemplateNode ns = (namespace == null) ? null :      parseAttributeValueTemplate(namespace, node);    return new AttributeNode(parse(children), parse(next), n, ns, node);  }    /**   * text   */  final TemplateNode parseText(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String doe = getAttribute(attrs, "disable-output-escaping");    boolean d = "yes".equals(doe);    return new TextNode(parse(children), parse(next), d);  }    /**   * copy   */  final TemplateNode parseCopy(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String uas = getAttribute(attrs, "use-attribute-sets");    return new CopyNode(parse(children), parse(next), uas);  }    /**   * processing-instruction   */  final TemplateNode parseProcessingInstruction(Node node, Node children,                                                Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String name = getRequiredAttribute(attrs, "name", node);    return new ProcessingInstructionNode(parse(children),                                         parse(next), name);  }    /**   * number   */  final TemplateNode parseNumber(Node node, Node children, Node next)    throws TransformerConfigurationException, XPathExpressionException  {    NamedNodeMap attrs = node.getAttributes();    String v = getAttribute(attrs, "value");    String ff = getAttribute(attrs, "format");    if (ff == null)      {        ff = "1";      }    TemplateNode format = parseAttributeValueTemplate(ff, node);    String lang = getAttribute(attrs, "lang");    String lv = getAttribute(attrs, "letter-value");    int letterValue = "traditional".equals(lv) ?      AbstractNumberNode.TRADITIONAL :      AbstractNumberNode.ALPHABETIC;    String gs = getAttribute(attrs, "grouping-separator");    String gz = getAttribute(attrs, "grouping-size");    int gz2 = (gz != null && gz.length() > 0) ?      Integer.parseInt(gz) : 1;    if (v != null && v.length() > 0)      {        Expr value = (Expr) xpath.compile(v);        return new NumberNode(parse(children), parse(next),                              value, format, lang,                              letterValue, gs, gz2);      }    else      {        String l = getAttribute(attrs, "level");        int level =          "multiple".equals(l) ? NodeNumberNode.MULTIPLE :                      "any".equals(l) ? NodeNumberNode.ANY :                      NodeNumberNode.SINGLE;        String c = getAttribute(attrs, "count");        String f = getAttribute(attrs, "from");        Pattern count = null;        Pattern from = null;        if (c != null)          {            try              {                count = (Pattern) xpath.compile(c);              }            catch (ClassCastException e)              {                String msg = "invalid pattern: " + c;                throw new TransformerConfigurationException(msg);

⌨️ 快捷键说明

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