📄 stylesheet.java
字号:
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); TemplateNode ret2 = new ValueOfNode(select, false); ret2.next = ret; ret = ret2; } else { // Verbatim text TemplateNode ret2 = new LiteralNode(doc.createTextNode(token)); ret2.next = ret; ret = ret2; } } return ret; } /** * Whitespace stripping. * @param text the text node * @param source true if a source node, false if a stylesheet text node * @see http://www.w3.org/TR/xslt#strip */ boolean isPreserved(Text text, boolean source) 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 (source) { // Source document text node boolean preserve = true; float psPriority = 0.0f, ssPriority = 0.0f; if (!stripSpace.isEmpty()) { // Conflict resolution StrippingInstruction ssi = null, psi = null; for (Iterator i = stripSpace.iterator(); i.hasNext(); ) { StrippingInstruction si = (StrippingInstruction) i.next(); if (si.element.matches(ctx, 1, 1)) { if (ssi != null) { if (si.precedence < ssi.precedence) continue; float p = si.getPriority(); if (p < ssPriority) continue; } ssi = si; } } for (Iterator i = preserveSpace.iterator(); i.hasNext(); ) { StrippingInstruction si = (StrippingInstruction) i.next(); if (si.element.matches(ctx, 1, 1)) { if (psi != null) { if (si.precedence < psi.precedence) continue; float p = si.getPriority(); if (p < psPriority) continue; } psi = si; } } if (ssi != null) { if (psi != null) { if (psi.precedence < ssi.precedence) preserve = false; else if (psPriority < ssPriority) preserve = false; } else preserve = false; } } if (preserve) return true; } else { // Stylesheet text node if (STYLESHEET_PRESERVE_TEXT.matches(ctx, 1, 1)) 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); } } 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(new NamespaceProxy(current)); else if ("function-available".equals(localName) && (arity == 1)) return new FunctionAvailableFunction(new NamespaceProxy(current)); } return null; } // -- Parsing -- /** * apply-templates */ final TemplateNode parseApplyTemplates(Node node) 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()"; Node children = node.getFirstChild(); List sortKeys = parseSortKeys(children); List withParams = parseWithParams(children); Expr select = (Expr) xpath.compile(s); return new ApplyTemplatesNode(select, mode, sortKeys, withParams, false); } /** * call-template */ final TemplateNode parseCallTemplate(Node node) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String n = getRequiredAttribute(attrs, "name", node); QName name = getQName(n); Node children = node.getFirstChild(); List withParams = parseWithParams(children); return new CallTemplateNode(name, withParams); } /** * value-of */ final TemplateNode parseValueOf(Node node) 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(select, d); } /** * for-each */ final TemplateNode parseForEach(Node node) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String s = getRequiredAttribute(attrs, "select", node); Node children = node.getFirstChild(); List sortKeys = parseSortKeys(children); Expr select = (Expr) xpath.compile(s); ForEachNode ret = new ForEachNode(select, sortKeys); ret.children = parse(children); return ret; } /** * if */ final TemplateNode parseIf(Node node) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String t = getRequiredAttribute(attrs, "test", node); Expr test = (Expr) xpath.compile(t); Node children = node.getFirstChild(); IfNode ret = new IfNode(test); ret.children = parse(children); return ret; } /** * when */ final TemplateNode parseWhen(Node node) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String t = getRequiredAttribute(attrs, "test", node); Expr test = (Expr) xpath.compile(t); Node children = node.getFirstChild(); WhenNode ret = new WhenNode(test); ret.children = parse(children); return ret; } /** * element */ final TemplateNode parseElement(Node node) 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); Node children = node.getFirstChild(); ElementNode ret = new ElementNode(n, ns, uas, node); ret.children = parse(children); return ret; } /** * attribute */ final TemplateNode parseAttribute(Node node) 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); Node children = node.getFirstChild(); AttributeNode ret = new AttributeNode(n, ns, node); ret.children = parse(children); return ret; } /** * text */ final TemplateNode parseText(Node node) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String doe = getAttribute(attrs, "disable-output-escaping"); boolean d = "yes".equals(doe); Node children = node.getFirstChild(); TextNode ret = new TextNode(d); ret.children = parse(children); return ret; } /** * copy */ final TemplateNode parseCopy(Node node) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String uas = getAttribute(attrs, "use-attribute-sets"); Node children = node.getFirstChild(); CopyNode ret = new CopyNode(uas); ret.children = parse(children); return ret; } /** * processing-instruction */ final TemplateNode parseProcessingInstruction(Node node) throws TransformerConfigurationException, XPathExpressionException { NamedNodeMap attrs = node.getAttributes(); String name = getRequiredAttribute(attrs, "name", node); Node children = node.getFirstChild(); ProcessingInstructionNode ret = new ProcessingInstructionNode(name); ret.children = parse(children); return ret; } /** * number */ final TemplateNode parseNumber(Node node) 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; Node children = node.getFirstChild(); TemplateNode ret; if (v != null && v.length() > 0) { Expr value = (Expr) xpath.compile(v); ret = new NumberNode(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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -