xslparser.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,584 行 · 第 1/3 页
JAVA
1,584 行
if ((ch = read()) == '>') { ProcessingInstruction pi; pi = xsl.createProcessingInstruction(name, text.toString()); text.clear(); return pi; } else addText('?'); } else { addText((char) ch); ch = read(); } } throw error(L.l("expected `{0}' at {1}", ">", badChar(-1))); } private int parseDecl(Node parent) throws IOException, XslParseException { int ch = read(); if (ch == '[') { if ((ch = read()) != 'C') { addText("<!["); return ch; } else if ((ch = read()) != 'D') { addText("<![C"); return ch; } else if ((ch = read()) != 'A') { addText("<![CD"); return ch; } else if ((ch = read()) != 'T') { addText("<![CDA"); return ch; } else if ((ch = read()) != 'A') { addText("<![CDAT"); return ch; } else if ((ch = read()) != '[') { addText("<![CDATA"); return ch; } else { ch = read(); while (ch > 0) { if (ch == ']') { ch = read(); while (ch == ']') { if ((ch = read()) == '>') return read(); else addText(']'); } addText(']'); } else { addText((char) ch); ch = read(); } } return ch; } } if (ch != '-') { addText("<!"); return ch; } if ((ch = read()) != '-') { addText("<!-"); return ch; } while (ch >= 0) { if ((ch = read()) == '-') { ch = read(); while (ch == '-') { if ((ch = read()) == '>') return read(); } } } throw error(L.l("expected `{0}' at {1}", "-->", badChar(-1))); } /** * Parses the shortcut for valueOf <{...}> */ private void parseValueOf(Node parent) throws IOException, XslParseException { int ch = read(); while (ch >= 0) { if (ch == '}') { ch = read(); if (ch == '>') { QElement elt; elt = (QElement) xsl.createElementNS(XSLNS, "xsl:value-of"); elt.setAttribute("select", text.toString()); elt.setLocation(is.getURL(), is.getUserPath(), line, 0); parent.appendChild(elt); text.clear(); return; } else addText('}'); } else { addText((char) ch); ch = read(); } } } /** * parses top-level templates: * * pattern << ... >> -- <xsl:template match='pattern'>...</xsl:template> * pattern <# ... #> -- <xsl:template match='pattern'> * <xtp:scriptlet>...</xtp:scriptlet> * </xsl:template> * pattern <#= ... #> -- <xsl:template match='pattern'> * <xtp:expression>...</xtp:expression> * </xsl:template> */ private void parseSpecial(Node parent, int ch) throws IOException, XslParseException { char tail = '#'; String element = "xtp:scriptlet"; text.clear(); String filename = is.getUserPath(); int line = this.line; while (ch >= 0) { if (ch == '<') { filename = is.getUserPath(); line = this.line; ch = read(); if (ch == '#') { tail = '#'; ch = read(); if (ch == '=') { ch = read(); element = "xtp:expression"; } break; } else if (ch == '<') { tail = '>'; break; } else if (ch == '\\') { addText((char) read()); ch = read(); } } else { addText((char) ch); ch = read(); } } while (text.length() > 0 && Character.isSpace(text.charAt(text.length() - 1))) { text.setLength(text.length() - 1); } QElement template = (QElement) xsl.createElementNS(XSLNS, "xsl:template"); parent.appendChild(template); String match = text.toString(); template.setAttribute("match", match); boolean isName = true; for (int i = 0; i < match.length(); i++) { if (! XmlChar.isNameChar(match.charAt(i))) { isName = false; break; } } if (isName && false) // XXX: problems template.setAttribute("name", match); if (defaultMode != null) template.setAttribute("mode", defaultMode); template.setLocation(filename, filename, line, 0); text.clear(); inTemplate = true; if (tail == '>') { if (rawText) template.setAttribute("xml:space", "preserve"); parseNode(template, ">>", false, read()); inTemplate = false; return; } QNode scriptlet = (QNode) xsl.createElementNS(XTPNS, element); scriptlet.setLocation(filename, filename, line, 0); while (ch >= 0) { if (ch == tail) { ch = read(); if (ch == '>') break; else addText(tail); } else { addText((char) ch); ch = read(); } } scriptlet.appendChild(xsl.createTextNode(text.toString())); template.appendChild(scriptlet); text.clear(); inTemplate = false; } private void parseBlock(Node parent, int ch) throws IOException, XslParseException { char tail = '#'; String element = "xtp:scriptlet"; for (; XmlChar.isWhitespace((char) ch); ch = read()) { } if (ch == ';') return; if (ch != '<') throw error(L.l("expected `{0}' at {1}", "<", badChar(ch))); String filename = is.getUserPath(); int line = this.line; ch = read(); if (ch == '#') { tail = '#'; ch = read(); if (ch == '=') { ch = read(); element = "xtp:expression"; } } else if (ch == '<') { tail = '>'; } else throw error(L.l("expected block at {1}", "block", badChar(ch))); if (tail == '>') { if (rawText) ((Element) parent).setAttribute("xml:space", "preserve"); parseNode(parent, ">>", false, read()); return; } QNode scriptlet = (QNode) xsl.createElementNS(XTPNS, element); scriptlet.setLocation(filename, filename, line, 0); while (ch >= 0) { if (ch == tail) { ch = read(); if (ch == '>') break; else addText(tail); } else { addText((char) ch); ch = read(); } } scriptlet.appendChild(xsl.createTextNode(text.toString())); parent.appendChild(scriptlet); text.clear(); } private void addText(char ch) { if (text.length() == 0) { if (ch == '\n') textLine = line - 1; else textLine = line; } text.append(ch); } private void addText(String s) { if (text.length() == 0) textLine = line; text.append(s); } private int skipWhitespace(int ch) throws IOException { for (; XmlChar.isWhitespace(ch); ch = read()) { } return ch; } private int readTag(int ch) throws IOException { tag.clear(); for (; XmlChar.isNameChar(ch); ch = read()) tag.append((char) ch); return ch; } /** * Scans an attribute value, storing the results in <code>tag</code>. * * @param ch the current read character. * @return the next read character after the value. */ private int readValue(int ch) throws IOException, XslParseException { tag.clear(); if (ch == '\'') { for (ch = read(); ch >= 0 && ch != '\''; ch = read()) { if (ch == '&') { ch = parseEntityReference(); tag.append(text); text.clear(); unread(ch); } else tag.append((char) ch); } if (ch != '\'') throw error(L.l("expected `{0}' at {1}", "'", badChar(ch))); return read(); } else if (ch == '"') { for (ch = read(); ch >= 0 && ch != '"'; ch = read()) { if (ch == '&') { ch = parseEntityReference(); tag.append(text); text.clear(); unread(ch); } else tag.append((char) ch); } if (ch != '\"') throw error(L.l("expected `{0}' at {1}", "\"", badChar(ch))); return read(); } else if (XmlChar.isNameChar(ch)) { for (; XmlChar.isNameChar(ch); ch = read()) tag.append((char) ch); return ch; } else throw error(L.l("expected attribute value at {0}", badChar(ch))); } /** * Add the current accumulated text to the parent as a text node. * * @param parent node to contain the text. */ private void addText(Node parent) { if (text.getLength() == 0) { } else { Text textNode = (Text) xsl.createTextNode(text.toString()); QAbstractNode node = (QAbstractNode) textNode; node.setLocation(is.getURL(), is.getUserPath(), textLine, 0); parent.appendChild(textNode); } text.clear(); } /** * Returns an error including the current filename and line in emacs style. * * @param message the error message. */ private XslParseException error(String message) { return new XslParseException(getFilename() + ":" + getLine() + ": " + message); } /** * Returns an error including the current filename and line in emacs style. * * @param message the error message. */ private XslParseException error(Exception e) { if (e.getMessage() != null) return new XslParseException(getFilename() + ":" + getLine() + ": " + e.getMessage()); else return new XslParseException(getFilename() + ":" + getLine() + ": " + e); } /** * Return the source filename. */ private String getFilename() { return is.getPath().getUserPath(); } /** * Return the source line. */ private int getLine() { return line; } /** * Returns a string for the error character. */ private String badChar(int ch) { if (ch < 0) return L.l("end of file"); else if (ch == '\n' || ch == '\r') return L.l("end of line"); else return "`" + (char) ch + "'"; } /** * Reads a character from the stream, keeping track of newlines. */ public int read() throws IOException { if (peek >= 0) { int ch = peek; peek = -1; return ch; } int ch = is.readChar(); if (ch == '\r') { if ((ch = is.readChar()) != '\n') { if (ch >= 0) { if (ch == '\r') peek = '\n'; else peek = ch; } } ch = '\n'; } if (ch == '\n') line++; return ch; } void unread(int ch) { peek = ch; } static { _xslCommands = new HashMap<String,String>(); _xslCommands.put("apply-templates", "select"); _xslCommands.put("call-template", "name"); _xslCommands.put("apply-imports", ""); _xslCommands.put("for-each", "select"); _xslCommands.put("value-of", "select"); _xslCommands.put("copy-of", "select"); _xslCommands.put("number", "value"); _xslCommands.put("choose", ""); _xslCommands.put("when", "test"); _xslCommands.put("otherwise", ""); _xslCommands.put("if", "test"); _xslCommands.put("text", ""); _xslCommands.put("copy", ""); _xslCommands.put("variable", "name"); _xslCommands.put("param", "name"); _xslCommands.put("with-param", "name"); _xslCommands.put("message", ""); _xslCommands.put("fallback", ""); _xslCommands.put("processing-instruction", "name"); _xslCommands.put("comment", ""); _xslCommands.put("element", "name"); _xslCommands.put("attribute", "name"); _xslCommands.put("import", "href"); _xslCommands.put("include", "href"); _xslCommands.put("strip-space", "elements"); _xslCommands.put("preserve-space", "elements"); _xslCommands.put("output", ""); _xslCommands.put("key", ""); _xslCommands.put("decimal-format", ""); _xslCommands.put("attribute-set", "name"); _xslCommands.put("variable", "name"); _xslCommands.put("param", "name"); _xslCommands.put("template", "match"); _xslCommands.put("namespace-alias", ""); // two args // xslt 2.0 _xslCommands.put("result-document", "href"); _xtpCommands = new HashMap<String,String>(); _xtpCommands.put("while", "test"); _xtpCommands.put("expression", "expr"); _xtpCommands.put("expr", "expr"); _xtpCommands.put("scriptlet", ""); _xtpCommands.put("declaration", ""); _xtpCommands.put("directive.page", ""); _xtpCommands.put("directive.cache", ""); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?