📄 parser.java
字号:
} catch (ParserConfigurationException e) { ErrorMsg err = new ErrorMsg(ErrorMsg.SAX_PARSER_CONFIG_ERR); reportError(ERROR, err); } catch (SAXParseException e){ reportError(ERROR, new ErrorMsg(e.getMessage(),e.getLineNumber())); } catch (SAXException e) { reportError(ERROR, new ErrorMsg(e.getMessage())); } return null; } public SyntaxTreeNode getDocumentRoot() { return _root; } private String _PImedia = null; private String _PItitle = null; private String _PIcharset = null; /** * Set the parameters to use to locate the correct <?xml-stylesheet ...?> * processing instruction in the case where the input document is an * XML document with one or more references to a stylesheet. * @param media The media attribute to be matched. May be null, in which * case the prefered templates will be used (i.e. alternate = no). * @param title The value of the title attribute to match. May be null. * @param charset The value of the charset attribute to match. May be null. */ protected void setPIParameters(String media, String title, String charset) { _PImedia = media; _PItitle = title; _PIcharset = charset; } /** * Extracts the DOM for the stylesheet. In the case of an embedded * stylesheet, it extracts the DOM subtree corresponding to the * embedded stylesheet that has an 'id' attribute whose value is the * same as the value declared in the <?xml-stylesheet...?> processing * instruction (P.I.). In the xml-stylesheet P.I. the value is labeled * as the 'href' data of the P.I. The extracted DOM representing the * stylesheet is returned as an Element object. */ private SyntaxTreeNode getStylesheet(SyntaxTreeNode root) throws CompilerException { // Assume that this is a pure XSL stylesheet if there is not // <?xml-stylesheet ....?> processing instruction if (_target == null) { if (!_rootNamespaceDef) { ErrorMsg msg = new ErrorMsg(ErrorMsg.MISSING_XSLT_URI_ERR); throw new CompilerException(msg.toString()); } return(root); } // Find the xsl:stylesheet or xsl:transform with this reference if (_target.charAt(0) == '#') { SyntaxTreeNode element = findStylesheet(root, _target.substring(1)); if (element == null) { ErrorMsg msg = new ErrorMsg(ErrorMsg.MISSING_XSLT_TARGET_ERR, _target, root); throw new CompilerException(msg.toString()); } return(element); } else { return(loadExternalStylesheet(_target)); } } /** * Find a Stylesheet element with a specific ID attribute value. * This method is used to find a Stylesheet node that is referred * in a <?xml-stylesheet ... ?> processing instruction. */ private SyntaxTreeNode findStylesheet(SyntaxTreeNode root, String href) { if (root == null) return null; if (root instanceof Stylesheet) { String id = root.getAttribute("id"); if (id.equals(href)) return root; } Vector children = root.getContents(); if (children != null) { final int count = children.size(); for (int i = 0; i < count; i++) { SyntaxTreeNode child = (SyntaxTreeNode)children.elementAt(i); SyntaxTreeNode node = findStylesheet(child, href); if (node != null) return node; } } return null; } /** * For embedded stylesheets: Load an external file with stylesheet */ private SyntaxTreeNode loadExternalStylesheet(String location) throws CompilerException { InputSource source; // Check if the location is URL or a local file if ((new File(location)).exists()) source = new InputSource("file:"+location); else source = new InputSource(location); SyntaxTreeNode external = (SyntaxTreeNode)parse(source); return(external); } private void initAttrTable(String elementName, String[] attrs) { _instructionAttrs.put(getQName(XSLT_URI, XSL, elementName), attrs); } private void initInstructionAttrs() { initAttrTable("template", new String[] {"match", "name", "priority", "mode"}); initAttrTable("stylesheet", new String[] {"id", "version", "extension-element-prefixes", "exclude-result-prefixes"}); initAttrTable("transform", new String[] {"id", "version", "extension-element-prefixes", "exclude-result-prefixes"}); initAttrTable("text", new String[] {"disable-output-escaping"}); initAttrTable("if", new String[] {"test"}); initAttrTable("choose", new String[] {}); initAttrTable("when", new String[] {"test"}); initAttrTable("otherwise", new String[] {}); initAttrTable("for-each", new String[] {"select"}); initAttrTable("message", new String[] {"terminate"}); initAttrTable("number", new String[] {"level", "count", "from", "value", "format", "lang", "letter-value", "grouping-separator", "grouping-size"}); initAttrTable("comment", new String[] {}); initAttrTable("copy", new String[] {"use-attribute-sets"}); initAttrTable("copy-of", new String[] {"select"}); initAttrTable("param", new String[] {"name", "select"}); initAttrTable("with-param", new String[] {"name", "select"}); initAttrTable("variable", new String[] {"name", "select"}); initAttrTable("output", new String[] {"method", "version", "encoding", "omit-xml-declaration", "standalone", "doctype-public", "doctype-system", "cdata-section-elements", "indent", "media-type"}); initAttrTable("sort", new String[] {"select", "order", "case-order", "lang", "data-type"}); initAttrTable("key", new String[] {"name", "match", "use"}); initAttrTable("fallback", new String[] {}); initAttrTable("attribute", new String[] {"name", "namespace"}); initAttrTable("attribute-set", new String[] {"name", "use-attribute-sets"}); initAttrTable("value-of", new String[] {"select", "disable-output-escaping"}); initAttrTable("element", new String[] {"name", "namespace", "use-attribute-sets"}); initAttrTable("call-template", new String[] {"name"}); initAttrTable("apply-templates", new String[] {"select", "mode"}); initAttrTable("apply-imports", new String[] {}); initAttrTable("decimal-format", new String[] {"name", "decimal-separator", "grouping-separator", "infinity", "minus-sign", "NaN", "percent", "per-mille", "zero-digit", "digit", "pattern-separator"}); initAttrTable("import", new String[] {"href"}); initAttrTable("include", new String[] {"href"}); initAttrTable("strip-space", new String[] {"elements"}); initAttrTable("preserve-space", new String[] {"elements"}); initAttrTable("processing-instruction", new String[] {"name"}); initAttrTable("namespace-alias", new String[] {"stylesheet-prefix", "result-prefix"}); } /** * Initialize the _instructionClasses Hashtable, which maps XSL element * names to Java classes in this package. */ private void initStdClasses() { initStdClass("template", "Template"); initStdClass("stylesheet", "Stylesheet"); initStdClass("transform", "Stylesheet"); initStdClass("text", "Text"); initStdClass("if", "If"); initStdClass("choose", "Choose"); initStdClass("when", "When"); initStdClass("otherwise", "Otherwise"); initStdClass("for-each", "ForEach"); initStdClass("message", "Message"); initStdClass("number", "Number"); initStdClass("comment", "Comment"); initStdClass("copy", "Copy"); initStdClass("copy-of", "CopyOf"); initStdClass("param", "Param"); initStdClass("with-param", "WithParam"); initStdClass("variable", "Variable"); initStdClass("output", "Output"); initStdClass("sort", "Sort"); initStdClass("key", "Key"); initStdClass("fallback", "Fallback"); initStdClass("attribute", "XslAttribute"); initStdClass("attribute-set", "AttributeSet"); initStdClass("value-of", "ValueOf"); initStdClass("element", "XslElement"); initStdClass("call-template", "CallTemplate"); initStdClass("apply-templates", "ApplyTemplates"); initStdClass("apply-imports", "ApplyImports"); initStdClass("decimal-format", "DecimalFormatting"); initStdClass("import", "Import"); initStdClass("include", "Include"); initStdClass("strip-space", "Whitespace"); initStdClass("preserve-space", "Whitespace"); initStdClass("processing-instruction", "ProcessingInstruction"); initStdClass("namespace-alias", "NamespaceAlias"); } private void initStdClass(String elementName, String className) { _instructionClasses.put(getQName(XSLT_URI, XSL, elementName), COMPILER_PACKAGE + '.' + className); } public boolean elementSupported(String namespace, String localName) { return(_instructionClasses.get(getQName(namespace, XSL, localName)) != null); } public boolean functionSupported(String fname) { return(_symbolTable.lookupPrimop(fname) != null); } private void initExtClasses() { initExtClass("output", "TransletOutput"); initExtClass(REDIRECT_URI, "write", "TransletOutput"); } private void initExtClass(String elementName, String className) { _instructionClasses.put(getQName(TRANSLET_URI, TRANSLET, elementName), COMPILER_PACKAGE + '.' + className); } private void initExtClass(String namespace, String elementName, String className) { _instructionClasses.put(getQName(namespace, TRANSLET, elementName), COMPILER_PACKAGE + '.' + className); } /** * Add primops and base functions to the symbol table. */ private void initSymbolTable() { MethodType I_V = new MethodType(Type.Int, Type.Void); MethodType I_R = new MethodType(Type.Int, Type.Real); MethodType I_S = new MethodType(Type.Int, Type.String); MethodType I_D = new MethodType(Type.Int, Type.NodeSet); MethodType R_I = new MethodType(Type.Real, Type.Int); MethodType R_V = new MethodType(Type.Real, Type.Void); MethodType R_R = new MethodType(Type.Real, Type.Real); MethodType R_D = new MethodType(Type.Real, Type.NodeSet); MethodType R_O = new MethodType(Type.Real, Type.Reference); MethodType I_I = new MethodType(Type.Int, Type.Int); MethodType D_O = new MethodType(Type.NodeSet, Type.Reference); MethodType D_V = new MethodType(Type.NodeSet, Type.Void); MethodType D_S = new MethodType(Type.NodeSet, Type.String); MethodType D_D = new MethodType(Type.NodeSet, Type.NodeSet); MethodType A_V = new MethodType(Type.Node, Type.Void); MethodType S_V = new MethodType(Type.String, Type.Void); MethodType S_S = new MethodType(Type.String, Type.String); MethodType S_A = new MethodType(Type.String, Type.Node); MethodType S_D = new MethodType(Type.String, Type.NodeSet); MethodType S_O = new MethodType(Type.String, Type.Reference); MethodType B_O = new MethodType(Type.Boolean, Type.Reference); MethodType B_V = new MethodType(Type.Boolean, Type.Void); MethodType B_B = new MethodType(Type.Boolean, Type.Boolean); MethodType B_S = new MethodType(Type.Boolean, Type.String); MethodType D_X = new MethodType(Type.NodeSet, Type.Object); MethodType R_RR = new MethodType(Type.Real, Type.Real, Type.Real); MethodType I_II = new MethodType(Type.Int, Type.Int, Type.Int); MethodType B_RR = new MethodType(Type.Boolean, Type.Real, Type.Real); MethodType B_II = new MethodType(Type.Boolean, Type.Int, Type.Int); MethodType S_SS = new MethodType(Type.String, Type.String, Type.String); MethodType S_DS = new MethodType(Type.String, Type.Real, Type.String); MethodType S_SR = new MethodType(Type.String, Type.String, Type.Real); MethodType O_SO = new MethodType(Type.Reference, Type.String, Type.Reference); MethodType D_SS = new MethodType(Type.NodeSet, Type.String, Type.String); MethodType D_SD = new MethodType(Type.NodeSet, Type.String, Type.NodeSet); MethodType B_BB = new MethodType(Type.Boolean, Type.Boolean, Type.Boolean); MethodType B_SS = new MethodType(Type.Boolean, Type.String, Type.String); MethodType S_SD = new MethodType(Type.String, Type.String, Type.NodeSet); MethodType S_DSS = new MethodType(Type.String, Type.Real, Type.String, Type.String); MethodType S_SRR = new MethodType(Type.String, Type.String, Type.Real, Type.Real); MethodType S_SSS = new MethodType(Type.String, Type.String, Type.String, Type.String); /* * Standard functions: implemented but not in this table concat(). * When adding a new function make sure to uncomment * the corresponding line in <tt>FunctionAvailableCall</tt>. */ // The following functions are inlined _symbolTable.addPrimop("current", A_V); _symbolTable.addPrimop("last", I_V); _symbolTable.addPrimop("position", I_V); _symbolTable.addPrimop("true", B_V); _symbolTable.addPrimop("false", B_V); _symbolTable.addPrimop("not", B_B); _symbolTable.addPrimop("name", S_V); _symbolTable.addPrimop("name", S_A); _symbolTable.addPrimop("generate-id", S_V); _symbolTable.addPrimop("generate-id", S_A); _symbolTable.addPrimop("ceiling", R_R); _symbolTable.addPrimop("floor", R_R); _symbolTable.addPrimop("round", R_R); _symbolTable.addPrimop("contains", B_SS); _symbolTable.addPrimop("number", R_O); _symbolTable.addPrimop("number", R_V); _symbolTable.addPrimop("boolean", B_O); _symbolTable.addPrimop("string", S_O); _symbolTable.addPrimop("string", S_V); _symbolTable.addPrimop("translate", S_SSS); _symbolTable.addPrimop("string-length", I_V); _symbolTable.addPrimop("string-length", I_S); _symbolTable.addPrimop("starts-with", B_SS); _symbolTable.addPrimop("format-number", S_DS); _symbolTable.addPrimop("format-number", S_DSS); _symbolTable.addPrimop("unparsed-entity-uri", S_S); _symbolTable.addPrimop("key", D_SS); _symbolTable.addPrimop("key", D_SD); _symbolTable.addPrimop("id", D_S); _symbolTable.addPrimop("id", D_D); _symbolTable.addPrimop("namespace-uri", S_V); _symbolTable.addPrimop("function-available", B_S); _symbolTable.addPrimop("element-available", B_S); _symbolTable.addPrimop("document", D_S); _symbolTable.addPrimop("document", D_V); // The following functions are implemented in the basis library _symbolTable.addPrimop("count", I_D); _symbolTable.addPrimop("sum", R_D); _symbolTable.addPrimop("local-name", S_V); _symbolTable.addPrimop("local-name", S_D); _symbolTable.addPrimop("namespace-uri", S_V); _symbolTable.addPrimop("namespace-uri", S_D); _symbolTable.addPrimop("substring", S_SR); _symbolTable.addPrimop("substring", S_SRR); _symbolTable.addPrimop("substring-after", S_SS); _symbolTable.addPrimop("substring-before", S_SS); _symbolTable.addPrimop("normalize-space", S_V); _symbolTable.addPrimop("normalize-space", S_S); _symbolTable.addPrimop("system-property", S_S); // Extensions _symbolTable.addPrimop("nodeset", D_O); _symbolTable.addPrimop("objectType", S_O); _symbolTable.addPrimop("cast", O_SO); // Operators +, -, *, /, % defined on real types. _symbolTable.addPrimop("+", R_RR); _symbolTable.addPrimop("-", R_RR); _symbolTable.addPrimop("*", R_RR); _symbolTable.addPrimop("/", R_RR); _symbolTable.addPrimop("%", R_RR); // Operators +, -, * defined on integer types. // Operators / and % are not defined on integers (may cause exception) _symbolTable.addPrimop("+", I_II); _symbolTable.addPrimop("-", I_II); _symbolTable.addPrimop("*", I_II); // Operators <, <= >, >= defined on real types. _symbolTable.addPrimop("<", B_RR); _symbolTable.addPrimop("<=", B_RR); _symbolTable.addPrimop(">", B_RR); _symbolTable.addPrimop(">=", B_RR); // Operators <, <= >, >= defined on int types. _symbolTable.addPrimop("<", B_II); _symbolTable.addPrimop("<=", B_II); _symbolTable.addPrimop(">", B_II); _symbolTable.addPrimop(">=", B_II); // Operators <, <= >, >= defined on boolean types. _symbolTable.addPrimop("<", B_BB); _symbolTable.addPrimop("<=", B_BB); _symbolTable.addPrimop(">", B_BB); _symbolTable.addPrimop(">=", B_BB); // Operators 'and' and 'or'. _symbolTable.addPrimop("or", B_BB); _symbolTable.addPrimop("and", B_BB); // Unary minus. _symbolTable.addPrimop("u-", R_R); _symbolTable.addPrimop("u-", I_I); } public SymbolTable getSymbolTable() { return _symbolTable; } public Template getTemplate() { return _template; } public void setTemplate(Template template) { _template = template; } private int _templateIndex = 0; public int getTemplateIndex() { return(_templateIndex++); } /** * Creates a new node in the abstract syntax tree. This node can be * o) a supported XSLT 1.0 element * o) an unsupported XSLT element (post 1.0) * o) a supported XSLT extension * o) an unsupported XSLT extension * o) a literal result element (not an XSLT element and not an extension) * Unsupported elements do not directly generate an error. We have to wait * until we have received all child elements of an unsupported element to * see if any <xsl:fallback> elements exist. */ private boolean versionIsOne = true; public SyntaxTreeNode makeInstance(String uri, String prefix, String local, Attributes attributes) { SyntaxTreeNode node = null; QName qname = getQName(uri, prefix, local); String className = (String)_instructionClasses.get(qname); if (className != null) { try { final Class clazz = ObjectFactory.findProviderClass( className, ObjectFactory.findClassLoader(), true); node = (SyntaxTreeNode)clazz.newInstance(); node.setQName(qname); node.setParser(this); if (_locator != null) { node.setLineNumber(getLineNumber()); } if (node instanceof Stylesheet) { _xsltc.setStylesheet((Stylesheet)node); } checkForSuperfluousAttributes(node, attributes); } catch (ClassNotFoundException e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -