stylesheet.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,729 行 · 第 1/4 页
JAVA
1,729 行
} } if (f != null) { try { from = (Pattern) xpath.compile(f); } catch (ClassCastException e) { String msg = "invalid pattern: " + f; throw new TransformerConfigurationException(msg); } } return new NodeNumberNode(parse(children), parse(next), level, count, from, format, lang, letterValue, gs, gz2); } } /** * copy-of */ final TemplateNode parseCopyOf(Node node, Node children, Node next) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String s = getRequiredAttribute(attrs, "select", node); Expr select = (Expr) xpath.compile(s); return new CopyOfNode(parse(children), parse(next), select); } /** * message */ final TemplateNode parseMessage(Node node, Node children, Node next) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String t = getAttribute(attrs, "terminate"); boolean terminate = "yes".equals(t); return new MessageNode(parse(children), parse(next), terminate); } /** * Parse template-level elements. */ final TemplateNode parse(Node node) throws TransformerConfigurationException { if (node == null) { return null; } // Hack to associate the document function with its declaring node current = node; Node children = node.getFirstChild(); Node next = node.getNextSibling(); try { String namespaceUri = node.getNamespaceURI(); if (Stylesheet.XSL_NS.equals(namespaceUri) && Node.ELEMENT_NODE == node.getNodeType()) { String name = node.getLocalName(); if ("apply-templates".equals(name)) { return parseApplyTemplates(node, children, next); } else if ("call-template".equals(name)) { return parseCallTemplate(node, children, next); } else if ("value-of".equals(name)) { return parseValueOf(node, children, next); } else if ("for-each".equals(name)) { return parseForEach(node, children, next); } else if ("if".equals(name)) { return parseIf(node, children, next); } else if ("choose".equals(name)) { return new ChooseNode(parse(children), parse(next)); } else if ("when".equals(name)) { return parseWhen(node, children, next); } else if ("otherwise".equals(name)) { return new OtherwiseNode(parse(children), parse(next)); } else if ("element".equals(name)) { return parseElement(node, children, next); } else if ("attribute".equals(name)) { return parseAttribute(node, children, next); } else if ("text".equals(name)) { return parseText(node, children, next); } else if ("copy".equals(name)) { return parseCopy(node, children, next); } else if ("processing-instruction".equals(name)) { return parseProcessingInstruction(node, children, next); } else if ("comment".equals(name)) { return new CommentNode(parse(children), parse(next)); } else if ("number".equals(name)) { return parseNumber(node, children, next); } else if ("param".equals(name) || "variable".equals(name)) { boolean global = "variable".equals(name); NamedNodeMap attrs = node.getAttributes(); TemplateNode content = parse(children); String paramName = getRequiredAttribute(attrs, "name", node); String select = getAttribute(attrs, "select"); if (select != null) { if (content != null) { String msg = "parameter '" + paramName + "' has both select and content"; DOMSourceLocator l = new DOMSourceLocator(node); throw new TransformerConfigurationException(msg, l); } Expr expr = (Expr) xpath.compile(select); return new ParameterNode(null, parse(next), paramName, expr, global); } else { return new ParameterNode(content, parse(next), paramName, null, global); } } else if ("copy-of".equals(name)) { return parseCopyOf(node, children, next); } else if ("message".equals(name)) { return parseMessage(node, children, next); } else if ("apply-imports".equals(name)) { return new ApplyImportsNode(parse(children), parse(next)); } else { // xsl:fallback // Pass over any other XSLT nodes return parse(next); } } String prefix = node.getPrefix(); if (extensionElementPrefixes.contains(prefix)) { // Pass over extension elements return parse(next); } switch (node.getNodeType()) { case Node.TEXT_NODE: // Determine whether to strip whitespace Text text = (Text) node; if (!isPreserved(text)) { // Strip text.getParentNode().removeChild(text); return parse(next); } break; case Node.COMMENT_NODE: // Ignore comments return parse(next); case Node.ELEMENT_NODE: // Check for attribute value templates and use-attribute-sets NamedNodeMap attrs = node.getAttributes(); boolean convert = false; String useAttributeSets = null; int len = attrs.getLength(); for (int i = 0; i < len; i++) { Node attr = attrs.item(i); String value = attr.getNodeValue(); if (Stylesheet.XSL_NS.equals(attr.getNamespaceURI()) && "use-attribute-sets".equals(attr.getLocalName())) { useAttributeSets = value; convert = true; break; } int start = value.indexOf('{'); int end = value.indexOf('}'); if (start != -1 || end != -1) { convert = true; break; } } if (convert) { // Create an element-producing template node instead // with appropriate attribute-producing child template nodes TemplateNode child = parse(children); for (int i = 0; i < len; i++) { Node attr = attrs.item(i); String ans = attr.getNamespaceURI(); String aname = attr.getNodeName(); if (Stylesheet.XSL_NS.equals(ans) && "use-attribute-sets".equals(attr.getLocalName())) { continue; } String value = attr.getNodeValue(); TemplateNode grandchild = parseAttributeValueTemplate(value, node); TemplateNode n = parseAttributeValueTemplate(aname, node); TemplateNode ns = (ans == null) ? null : parseAttributeValueTemplate(ans, node); child = new AttributeNode(grandchild, child, n, ns, attr); } String ename = node.getNodeName(); TemplateNode n = parseAttributeValueTemplate(ename, node); TemplateNode ns = (namespaceUri == null) ? null : parseAttributeValueTemplate(namespaceUri, node); return new ElementNode(child, parse(next), n, ns, useAttributeSets, node); } // Otherwise fall through break; } } catch (XPathExpressionException e) { DOMSourceLocator l = new DOMSourceLocator(node); throw new TransformerConfigurationException(e.getMessage(), l, e); } return new LiteralNode(parse(children), parse(next), node); } final List parseSortKeys(Node node) throws TransformerConfigurationException, XPathExpressionException { List ret = new LinkedList(); while (node != null) { String namespaceUri = node.getNamespaceURI(); if (Stylesheet.XSL_NS.equals(namespaceUri) && Node.ELEMENT_NODE == node.getNodeType() && "sort".equals(node.getLocalName())) { NamedNodeMap attrs = node.getAttributes(); String s = getAttribute(attrs, "select"); if (s == null) { s = "."; } Expr select = (Expr) xpath.compile(s); String l = getAttribute(attrs, "lang"); TemplateNode lang = (l == null) ? null : parseAttributeValueTemplate(l, node); String dt = getAttribute(attrs, "data-type"); TemplateNode dataType = (dt == null) ? null : parseAttributeValueTemplate(dt, node); String o = getAttribute(attrs, "order"); TemplateNode order = (o == null) ? null : parseAttributeValueTemplate(o, node); String co = getAttribute(attrs, "case-order"); TemplateNode caseOrder = (co == null) ? null : parseAttributeValueTemplate(co, node); ret.add(new SortKey(select, lang, dataType, order, caseOrder)); } node = node.getNextSibling(); } return ret.isEmpty() ? null : ret; } final List parseWithParams(Node node) throws TransformerConfigurationException, XPathExpressionException { List ret = new LinkedList(); while (node != null) { String namespaceUri = node.getNamespaceURI(); if (Stylesheet.XSL_NS.equals(namespaceUri) && Node.ELEMENT_NODE == node.getNodeType() && "with-param".equals(node.getLocalName())) { NamedNodeMap attrs = node.getAttributes(); TemplateNode content = parse(node.getFirstChild()); String name = getRequiredAttribute(attrs, "name", node); String select = getAttribute(attrs, "select"); if (select != null) { if (content != null) { String msg = "parameter '" + name + "' has both select and content"; DOMSourceLocator l = new DOMSourceLocator(node); throw new TransformerConfigurationException(msg, l); } Expr expr = (Expr) xpath.compile(select); ret.add(new WithParam(name, expr)); } else { ret.add(new WithParam(name, content)); } } node = node.getNextSibling(); } return ret.isEmpty() ? null : ret; } /** * Created element nodes have a copy of the namespace nodes in the * stylesheet, except the XSLT namespace, extension namespaces, and * exclude-result-prefixes. */ final void addNamespaceNodes(Node source, Node target, Document doc, Collection elementExcludeResultPrefixes) { NamedNodeMap attrs = source.getAttributes(); if (attrs != null) { int len = attrs.getLength(); for (int i = 0; i < len; i++) { Node attr = attrs.item(i); String uri = attr.getNamespaceURI(); if (uri == XMLConstants.XMLNS_ATTRIBUTE_NS_URI) { String prefix = attr.getLocalName(); if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { prefix = "#default"; } String ns = attr.getNodeValue(); // Should the namespace be excluded? if (XSL_NS.equals(ns) || extensionElementPrefixes.contains(prefix) || elementExcludeResultPrefixes.contains(prefix) || excludeResultPrefixes.contains(prefix)) { continue; } // Is the namespace already defined on the target? if (prefix == "#default") { prefix = null; } if (target.lookupNamespaceURI(prefix) != null) { continue; } attr = attr.cloneNode(true); attr = doc.adoptNode(attr); target.getAttributes().setNamedItemNS(attr); } } } Node parent = source.getParentNode(); if (parent != null) { addNamespaceNodes(parent, target, doc, elementExcludeResultPrefixes); } } static final String getAttribute(NamedNodeMap attrs, String name) { Node attr = attrs.getNamedItem(name); if (attr == null) { return null; } String ret = attr.getNodeValue(); if (ret.length() == 0) { return null; } return ret; } static final String getRequiredAttribute(NamedNodeMap attrs, String name, Node source) throws TransformerConfigurationException { String value = getAttribute(attrs, name); if (value == null || value.length() == 0) { String msg = name + " attribute is required on " + source.getNodeName(); DOMSourceLocator l = new DOMSourceLocator(source); throw new TransformerConfigurationException(msg, l); } return value; } // Handle user data changes when nodes are cloned etc public void handle(short op, String key, Object data, Node src, Node dst) { dst.setUserData(key, data, this); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?