generator.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,537 行 · 第 1/5 页
JAVA
2,537 行
* * @param path Path to the include files. * * @return XML tree describing the XSL. */ Document readXsl(ReadStream file) throws IOException, XslParseException { try { addDepend(file.getPath()); if (_isStyleScript) { XslParser parser = new XslParser(); return parser.parse(file); } else return new Xml().parseDocument(file); } catch (org.xml.sax.SAXException e) { throw new XslParseException(e); } finally { file.close(); } } /** * Start the top-level stylesheet generation. */ private void generateStylesheet(Element elt, boolean isTop) throws Exception { QElement element = (QElement) elt; _isCauchoXsl = ! element.getAttribute("xsl-caucho").equals(""); String systemId = element.getBaseURI(); Path oldContext = _context; if (systemId != null) _context = _context.lookup(systemId); XslNode stylesheet = createChild(element); addNamespace(element); if (isTop) printHeader(); stylesheet.generateDeclaration(getOut()); stylesheet.generate(getOut()); _context = oldContext; /* _version = element.getAttribute("version"); if (_version.equals("")) _version = "1.0"; // generateAttributeSets(element); excludeNamespaces(element); String xslSpace = element.getAttribute("xsl-space"); ArrayList<XslNode> children = new ArrayList<XslNode>(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (! (child instanceof Element)) continue; int code = -1; String name = getXslLocal(child); if (name != null) code = _tags.get(name); else if ((name = getXtpLocal(child)) != null) code = _xtpTags.get(name); else { String childName = child.getNodeName(); if (childName.startsWith("jsp:directive.") || childName.equals("jsp:declaration") || childName.equals("jsp:scriptlet")) addGlobalAction((Element) child); continue; } NamespaceContext oldNamespace = addNamespace((Element) child); // generateTopLevelNode(code, (Element) child); XslNode node = createChild(child); children.add(node); _namespace = oldNamespace; } for (int i = 0; i < children.size(); i++) { XslNode node = children.get(i); node.generate(getOut()); } */ } abstract protected JavaWriter getOut(); abstract protected XslNode createChild(Node child) throws Exception; abstract protected XslNode createChild(XslNode parent, Node child) throws Exception; private void addGlobalAction(Element elt) { _globalActions.add(elt); } /** * Converts the exclude-result-prefixes into the "excludedNamespaces" * hashMap for later use. */ private void excludeNamespaces(Element element) throws Exception { if (! (element instanceof QElement)) return; QElement elt = (QElement) element; String excludeNamespace; excludeNamespace = element.getAttribute("exclude-result-prefixes"); if (! excludeNamespace.equals("")) { for (String prefix : excludeNamespace.split("[,\\s]+")) { String ns = elt.getNamespace(prefix); if (ns == null) throw error(elt, L.l("`{0}' must be a namespace prefix", prefix)); _excludedNamespaces.put(ns, ""); } } } public void addExcludedNamespace(String ns) { _excludedNamespaces.put(ns, ""); } public void addInit(XslNode node) { _inits.add(node); } public void addGlobalParameter(String param) { _globalParameters.add(param); } /** * Adds a file dependency for cacheable references to the output of * the stylesheet. */ private void addCacheDepends(String attr) { if (attr.equals("")) return; int i = 0; int ch = 0; int len = attr.length(); for (; i < len && XmlChar.isWhitespace((ch = attr.charAt(i))) || ch == ','; i++) { } CharBuffer cb = new CharBuffer(); while (i < len) { cb.clear(); for (; i < len && ! XmlChar.isWhitespace((ch = attr.charAt(i))) && ch != ','; i++) { cb.append((char) ch); } _cacheDepends.add(cb.toString()); for (; i < len && XmlChar.isWhitespace((ch = attr.charAt(i))) || ch == ','; i++) { } } } private void generateCacheDepends(String attr) throws Exception { if (attr.equals("")) return; int i = 0; int ch = 0; int len = attr.length(); for (; i < len && XmlChar.isWhitespace((ch = attr.charAt(i))) || ch == ','; i++) { } CharBuffer cb = new CharBuffer(); while (i < len) { cb.clear(); for (; i < len && ! XmlChar.isWhitespace((ch = attr.charAt(i))) && ch != ','; i++) { cb.append((char) ch); } printCacheDepends(cb.toString()); for (; i < len && XmlChar.isWhitespace((ch = attr.charAt(i))) || ch == ','; i++) { } } } /** * Generate code for xsl:template */ void generateTemplate(Element element) throws Exception { String name = element.getAttribute("name"); String patternString = element.getAttribute("match"); String mode = element.getAttribute("mode"); String priority = element.getAttribute("priority"); double dPriority = 0.0/0.0; if (! name.equals("")) _macros.put(name, name); if (name.equals("") && patternString.equals("")) throw error("xsl:template expects a `name' or a `match' attribute."); if (! priority.equals("")) { try { dPriority = Double.valueOf(priority).doubleValue(); } catch (Exception e) { throw error("xsl:template expects `priority' must be a double."); } } boolean oldCacheable = _isCacheable; boolean oldDefaultCacheable = _defaultCacheable; AbstractPattern oldNodeListContext = _nodeListContext; if (! patternString.equals("")) _nodeListContext = parseMatch(patternString); _isCacheable = true; printTemplate(element, name, patternString, mode, dPriority); _nodeListContext = oldNodeListContext; _isCacheable = oldCacheable; _defaultCacheable = oldDefaultCacheable; } public XslNode generateImport(String href) throws Exception { Path path = lookupPath(href); if (_files.get(path.getPath()) != null) return null; Document xsl = readFile(href, path); if (xsl == null) throw new FileNotFoundException(href); QElement top = (QElement) xsl.getDocumentElement(); if (top == null || ! "stylesheet".equals(getXslLocal(top)) && ! "transform".equals(getXslLocal(top))) { throw error("imported stylesheet `" + href + "' missing xsl:stylesheet."); } int oldMinImportance = _minImportance; Path oldContext = _context; Path virtualPath = _context.getParent().lookup(href); _context = virtualPath; _minImportance = _importance; boolean oldTop = _isTop; boolean oldRaw = _isRawText; _isTop = false; _isRawText = false; String systemId = top.getBaseURI(); if (systemId != null) _context = _context.lookup(systemId); XslStylesheet stylesheet = (XslStylesheet) createChild(top); _isRawText = oldRaw; _isTop = oldTop; _minImportance = oldMinImportance; _context = oldContext; incrementImportance(); return stylesheet; } /** * Generates code for xsl:include. The included file has the same * importance as the containing file. */ void generateInclude(Element element) throws Exception { String href = element.getAttribute("href"); if (href.equals("")) throw error("xsl:include expects `href' attribute."); if (element.getFirstChild() != null) throw error("xsl:include must be empty"); } public void generateInclude(XslNode parent, String href) throws Exception { Path path = lookupPath(href); if (_files.get(path.getPath()) != null) return; Document xsl = readFile(href, path); Element top = (Element) xsl.getDocumentElement(); if (top == null || ! "stylesheet".equals(getXslLocal(top)) && ! "transform".equals(getXslLocal(top))) { throw error("imported stylesheet `" + href + "' missing xsl:stylesheet."); } Path oldContext = _context; _context = path; for (Node node = top.getFirstChild(); node != null; node = node.getNextSibling()) { XslNode child = createChild(parent, node); if (child != null) parent.addChild(child); } // generateStylesheet(top, false); _context = oldContext; } /** * Returns the actual path for the relative href. */ private Path lookupPath(String href) { return _context.getParent().lookup(href); } private Document readFile(String href, Path virtualPath) throws Exception { Document xsl = (Document) _files.get(virtualPath.getPath()); if (xsl != null) throw new IllegalStateException(L.l("'{0}' is a duplicated path", virtualPath.getPath())); ReadStream rs; try { rs = _xslGenerator.openPath(href, _context.getURL()); } catch (Exception e) { throw new XslParseException(e); } Path path = rs.getPath(); xsl = readXsl(rs); Element subtop = xsl.getDocumentElement(); if (subtop == null) throw error(L.l("xsl:import file {0} is empty", path.getFullPath())); Path oldContext = _context; _context = virtualPath; _files.put(virtualPath.getPath(), xsl); _context = oldContext; return xsl; } void generateKey(Element element) throws Exception { String name = element.getAttribute("name"); if (name.equals("")) throw error("xsl:key expects `name' attribute."); String match = element.getAttribute("match"); if (match.equals("")) throw error("xsl:key expects `match' attribute."); String use = element.getAttribute("use"); if (use.equals("")) throw error("xsl:key expects `use' attribute."); if (element.getFirstChild() != null) throw error("xsl:key must be empty"); _keyFun.add(name, parseMatch(match), parseExpr(use)); } public void addKey(String name, AbstractPattern match, Expr use) { _keyFun.add(name, match, use); } void generateLocale(Element element) throws Exception { String name = element.getAttribute("name"); if (name.equals("")) name = "*"; DecimalFormatSymbols format = new DecimalFormatSymbols(); String value = element.getAttribute("decimal-separator"); if (value.length() > 0) format.setDecimalSeparator(value.charAt(0)); value = element.getAttribute("grouping-separator"); if (value.length() > 0) format.setGroupingSeparator(value.charAt(0)); value = element.getAttribute("infinity"); if (! value.equals("")) format.setInfinity(value); value = element.getAttribute("minus-sign"); if (value.length() > 0) format.setMinusSign(value.charAt(0)); value = element.getAttribute("NaN"); if (! value.equals("")) format.setNaN(value); value = element.getAttribute("percent"); if (value.length() > 0) format.setPercent(value.charAt(0)); value = element.getAttribute("per-mille"); if (value.length() > 0) format.setPerMill(value.charAt(0)); value = element.getAttribute("zero-digit"); if (value.length() > 0) format.setZeroDigit(value.charAt(0)); value = element.getAttribute("digit"); if (value.length() > 0) format.setDigit(value.charAt(0)); value = element.getAttribute("pattern-separator"); if (value.length() > 0) format.setPatternSeparator(value.charAt(0)); _formatNumberFun.addLocale(name, format); } void generateNamespaceAlias(Element element) throws Exception { if (! (element instanceof QElement)) return; QElement elt = (QElement) element; String stylesheetPrefix; String resultPrefix; stylesheetPrefix = (String) element.getAttribute("stylesheet-prefix"); resultPrefix = (String) element.getAttribute("result-prefix"); if (stylesheetPrefix.equals("")) throw error(element, "xsl:namespace-alias needs `stylesheet-prefix'"); if (resultPrefix.equals("")) throw error(element, "xsl:namespace-alias needs `result-prefix'"); } public void addNamespaceAlias(String stylesheetPrefix, String resultPrefix) { /* String stylesheetNs = getNamespace(stylesheetPrefix); if (stylesheetPrefix.equals("#default")) stylesheetNs = ""; else if (stylesheetNs.equals("")) throw error("`" + stylesheetPrefix + "' is not a valid namespace prefix"); String resultNs = getNamespace(resultPrefix);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?