📄 stylesheet.java
字号:
if (f != null) { try { from = (Pattern) xpath.compile(f); } catch (ClassCastException e) { String msg = "invalid pattern: " + f; throw new TransformerConfigurationException(msg); } } ret = new NodeNumberNode(level, count, from, format, lang, letterValue, gs, gz2); } ret.children = parse(children); return ret; } /** * copy-of */ final TemplateNode parseCopyOf(Node node) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String s = getRequiredAttribute(attrs, "select", node); Expr select = (Expr) xpath.compile(s); Node children = node.getFirstChild(); CopyOfNode ret = new CopyOfNode(select); ret.children = parse(children); return ret; } /** * message */ final TemplateNode parseMessage(Node node) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String t = getAttribute(attrs, "terminate"); boolean terminate = "yes".equals(t); Node children = node.getFirstChild(); MessageNode ret = new MessageNode(terminate); ret.children = parse(children); return ret; } /** * Parse template-level elements. */ final TemplateNode parse(Node node) throws TransformerConfigurationException { TemplateNode first = null; TemplateNode previous = null; while (node != null) { Node next = node.getNextSibling(); TemplateNode tnode = doParse(node); if (tnode != null) { if (first == null) first = tnode; if (previous != null) previous.next = tnode; previous = tnode; } node = next; } return first; } private final TemplateNode doParse(Node node) throws TransformerConfigurationException { // Hack to associate the document function with its declaring node current = node; 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); else if ("call-template".equals(name)) return parseCallTemplate(node); else if ("value-of".equals(name)) return parseValueOf(node); else if ("for-each".equals(name)) return parseForEach(node); else if ("if".equals(name)) return parseIf(node); else if ("choose".equals(name)) { Node children = node.getFirstChild(); ChooseNode ret = new ChooseNode(); ret.children = parse(children); return ret; } else if ("when".equals(name)) return parseWhen(node); else if ("otherwise".equals(name)) { Node children = node.getFirstChild(); OtherwiseNode ret = new OtherwiseNode(); ret.children = parse(children); return ret; } else if ("element".equals(name)) return parseElement(node); else if ("attribute".equals(name)) return parseAttribute(node); else if ("text".equals(name)) return parseText(node); else if ("copy".equals(name)) return parseCopy(node); else if ("processing-instruction".equals(name)) return parseProcessingInstruction(node); else if ("comment".equals(name)) { Node children = node.getFirstChild(); CommentNode ret = new CommentNode(); ret.children = parse(children); return ret; } else if ("number".equals(name)) return parseNumber(node); else if ("param".equals(name) || "variable".equals(name)) { int type = "variable".equals(name) ? Bindings.VARIABLE : Bindings.PARAM; NamedNodeMap attrs = node.getAttributes(); Node children = node.getFirstChild(); TemplateNode content = parse(children); QName paramName = getQName(getRequiredAttribute(attrs, "name", node)); String select = getAttribute(attrs, "select"); ParameterNode ret; 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); ret = new ParameterNode(paramName, expr, type); } else { ret = new ParameterNode(paramName, null, type); ret.children = content; } return ret; } else if ("copy-of".equals(name)) return parseCopyOf(node); else if ("message".equals(name)) return parseMessage(node); else if ("apply-imports".equals(name)) { Node children = node.getFirstChild(); ApplyImportsNode ret = new ApplyImportsNode(); ret.children = parse(children); return ret; } else { // xsl:fallback // Pass over any other XSLT nodes return null; } } String prefix = node.getPrefix(); if (extensionElementPrefixes.contains(prefix)) { // Check for xsl:fallback for (Node ctx = node.getFirstChild(); ctx != null; ctx = ctx.getNextSibling()) { String ctxUri = ctx.getNamespaceURI(); if (XSL_NS.equals(ctxUri) && "fallback".equals(ctx.getLocalName())) { ctx = ctx.getFirstChild(); return (ctx == null) ? null : parse(ctx); } } // Otherwise pass over extension element return null; } switch (node.getNodeType()) { case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: // Determine whether to strip whitespace Text text = (Text) node; if (!isPreserved(text, false)) { // Strip text.getParentNode().removeChild(text); return null; } break; case Node.COMMENT_NODE: // Ignore comments return null; 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 Node children = node.getFirstChild(); 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); TemplateNode newChild = new AttributeNode(n, ns, attr); newChild.children = grandchild; newChild.next = child; child = newChild; } String ename = node.getNodeName(); TemplateNode n = parseAttributeValueTemplate(ename, node); //TemplateNode ns = (namespaceUri == null) ? null : // parseAttributeValueTemplate(namespaceUri, node); TemplateNode ns = null; ElementNode ret = new ElementNode(n, ns, useAttributeSets, node); ret.children = child; return ret; } // Otherwise fall through break; } } catch (XPathExpressionException e) { DOMSourceLocator l = new DOMSourceLocator(node); throw new TransformerConfigurationException(e.getMessage(), l, e); } Node children = node.getFirstChild(); LiteralNode ret = new LiteralNode(node); ret.children = parse(children); return ret; } 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()); QName name = getQName(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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -