javagenerator.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,601 行 · 第 1/5 页
JAVA
2,601 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * Free SoftwareFoundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.xsl;import com.caucho.java.JavaCompiler;import com.caucho.java.JavaWriter;import com.caucho.loader.DynamicClassLoader;import com.caucho.log.Log;import com.caucho.server.util.CauchoSystem;import com.caucho.util.CharBuffer;import com.caucho.util.IntArray;import com.caucho.util.IntMap;import com.caucho.vfs.Depend;import com.caucho.vfs.Path;import com.caucho.vfs.WriteStream;import com.caucho.xml.QAbstractNode;import com.caucho.xml.QAttr;import com.caucho.xml.QElement;import com.caucho.xml.QName;import com.caucho.xml.XmlChar;import com.caucho.xpath.Expr;import com.caucho.xpath.NamespaceContext;import com.caucho.xpath.expr.NumericExpr;import com.caucho.xpath.pattern.*;import com.caucho.xsl.fun.KeyFun;import com.caucho.xsl.java.*;import org.w3c.dom.*;import java.io.IOException;import java.text.DecimalFormatSymbols;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Locale;import java.util.logging.Logger;/** * Generates code for a Java based stylesheet. * * <pre> * package work.xsl; * public class foo extends JavaStylesheet { * } * </pre> */public class JavaGenerator extends Generator { private static final Logger log = Log.open(JavaGenerator.class); private static HashMap<QName,Class> _tagMap; private static HashMap<QName,Class> _topTagMap; private static int _count; Path _path; WriteStream _s; JavaWriter _out; ArrayList<AbstractPattern> _matchPatterns = new ArrayList<AbstractPattern>(); IntMap _matchMap = new IntMap(); ArrayList<AbstractPattern> _selectPatterns = new ArrayList<AbstractPattern>(); IntMap _selectMap = new IntMap(); ArrayList<Expr> _exprs = new ArrayList<Expr>(); IntMap _exprMap = new IntMap(); ArrayList<Sort[]> _sorts = new ArrayList<Sort[]>(); ArrayList<NamespaceContext> _namespaces = new ArrayList<NamespaceContext>(); ArrayList<XslNumberFormat> _formats = new ArrayList<XslNumberFormat>(); ArrayList<String> _functions = new ArrayList<String>(); ArrayList<Template> _templateList = new ArrayList<Template>(); ArrayList<String> _stylesheets = new ArrayList<String>(); int _templateCount = 0; // integer counting unique identifier int _unique; HashMap<String,String> _macros = new HashMap<String,String>(); ArrayList<Object> _fragments = new ArrayList<Object>(); ArrayList<String> _strings = new ArrayList<String>(); IntMap _stringMap = new IntMap(); IntArray _envDepth = new IntArray(); ArrayList<String> _modes = new ArrayList<String>(); private XslNode _xslNode; private boolean _isLineBegin; private int _depth; private int _callDepth; // integer counting the depth of nested selects private int _selectDepth; private int _selectLoopDepth; private int _flagCount; private String _className; private String _pkg; private String _currentPos; private ClassLoader _parentLoader; private JavaCompiler _compiler; private boolean _disableEscaping; private boolean _printLocation = true; private String _oldFilename = null; private int _oldLine = -1; private boolean _hasHeader; /** * Creates a new XSL generator for Java. * * @param xslGenerator the owning factory. * @param className the name of the generated class * @param encoding the generated output encoding. */ JavaGenerator(AbstractStylesheetFactory xslGenerator, String className, String encoding) throws IOException { super(xslGenerator); _parentLoader = xslGenerator.getClassLoader(); ArrayList pathDepends = new ArrayList(); _compiler = JavaCompiler.create(_parentLoader); _compiler.setClassDir(_workPath); if (encoding == null) { } else if (encoding.equalsIgnoreCase("UTF-16")) { // utf-16 isn't supported by some javac encoding = "UTF-8"; _compiler.setEncoding(encoding); } else { _compiler.setEncoding(encoding); } int p = className.lastIndexOf('.'); if (p >= 0) { _pkg = className.substring(0, p); className = className.substring(p + 1); } else { _pkg = "_xsl"; className = className; } _className = className; init((_pkg + "." + className).replace('.', '/') + ".java"); String fileName = (_pkg + "." + className).replace('.', '/') + ".java"; _path = _workPath.lookup(fileName); _path.getParent().mkdirs(); _s = _path.openWrite(); if (encoding != null) _s.setEncoding(encoding); if (_s.getEncoding() == null || _s.getEncoding().equals("ISO-8859-1")) _s.setEncoding("JAVA"); _out = new JavaWriter(_s); _out.setLineMap(_lineMap); _matchPatterns = new ArrayList<AbstractPattern>(); _selectPatterns = new ArrayList<AbstractPattern>(); _modes = new ArrayList<String>(); _modes.add(""); } protected JavaWriter getOut() { return _out; } public int getSelectDepth() { return _selectDepth; } public void setSelectDepth(int depth) { _selectDepth = depth; } public int pushSelectDepth() { return ++_selectDepth; } public int popSelectDepth() { return _selectDepth--; } public int getSelectLoopDepth() { return _selectLoopDepth; } public int pushSelectLoopDepth() { return ++_selectLoopDepth; } public int popSelectLoopDepth() { return _selectLoopDepth--; } public void setSelectLoopDepth(int depth) { _selectLoopDepth = depth; } public int generateId() { return _unique++; } public void clearUnique() { _unique = 0; } /** * Prints the generated header. */ protected void printHeader() throws IOException { if (_hasHeader) return; _hasHeader = true; println("/*"); println(" * Generated by " + com.caucho.Version.FULL_VERSION); println(" */"); println(); println("package " + _pkg + ";"); println(); println("import java.io.*;"); println("import java.util.*;"); println("import org.w3c.dom.*;"); println("import org.xml.sax.*;"); println("import com.caucho.util.*;"); println("import com.caucho.xml.*;"); println("import com.caucho.xpath.*;"); println("import com.caucho.xpath.expr.*;"); println("import com.caucho.xpath.pattern.*;"); println("import com.caucho.xsl.*;"); try { Class.forName("javax.servlet.Servlet"); println("import javax.servlet.*;"); println("import javax.servlet.jsp.*;"); println("import javax.servlet.http.*;"); } catch (Throwable e) { } for (int i = 0; i < _imports.size(); i++) println("import " + _imports.get(i) + ";"); println(); println("public class " + _className + " extends JavaStylesheet {"); pushDepth(); println("private StylesheetEnv stylesheets[];"); } protected void generateChild(Node child) throws Exception { XslNode node = createChild(child); if (node != null) node.generate(_out); } protected XslNode createChild(XslNode parent, Node childNode) throws Exception { XslNode xslNode = _xslNode; _xslNode = parent; XslNode child = createChild(childNode); _xslNode = xslNode; return child; } protected XslNode createChild(Node child) throws Exception { XslNode xslNode = null; if (child instanceof QElement) { QElement elt = (QElement) child; Class cl = _tagMap.get(elt.getQName()); if (cl != null) { xslNode = (XslNode) cl.newInstance(); xslNode.setGenerator(this); xslNode.setParent(_xslNode); xslNode.setStartLocation(((QAbstractNode) child).getBaseURI(), ((QAbstractNode) child).getFilename(), ((QAbstractNode) child).getLine()); QAttr attr = (QAttr) elt.getFirstAttribute(); for (; attr != null; attr = (QAttr) attr.getNextSibling()) { xslNode.addAttribute(attr.getQName(), attr.getNodeValue()); } xslNode.endAttributes(); XslNode oldNode = _xslNode; _xslNode = xslNode; Node node = elt.getFirstChild(); for (; node != null; node = node.getNextSibling()) { XslNode xslChild = createChild(node); if (xslChild != null) xslNode.addChild(xslChild); } xslNode.endElement(); _xslNode = oldNode; } /* else if (elt.getNodeName().equals("jsp:decl") || elt.getNodeName().equals("jsp:declaration") || elt.getNodeName().startsWith("jsp:directive")) { } */ else if (child.getNodeName().startsWith("xsl:") && ! XSLNS.equals(child.getNamespaceURI())) { throw error(child, L.l("<{0}> has an xsl: prefix, but is not in the {1} namespace. XSL requires an xmlns:xsl=\"{1}\" namespace attribute.", child.getNodeName(), XSLNS)); } else if (! XSLNS.equals(child.getNamespaceURI()) && ! XTPNS.equals(child.getNamespaceURI())) { xslNode = new XslElementNode(elt.getQName()); xslNode.setGenerator(this); xslNode.setParent(_xslNode); xslNode.setStartLocation(((QAbstractNode) child).getBaseURI(), ((QAbstractNode) child).getFilename(), ((QAbstractNode) child).getLine()); QAttr attr = (QAttr) elt.getFirstAttribute(); for (; attr != null; attr = (QAttr) attr.getNextSibling()) xslNode.addAttribute(attr.getQName(), attr.getNodeValue()); xslNode.endAttributes(); XslNode oldNode = _xslNode; _xslNode = xslNode; Node node = elt.getFirstChild(); for (; node != null; node = node.getNextSibling()) { XslNode xslChild = createChild(node); xslNode.addChild(xslChild); } xslNode.endElement(); _xslNode = oldNode; } else { throw error(child, L.l("<{0}> is an unknown XSL tag.", child.getNodeName())); /* XslWrapperNode wrapNode = new XslWrapperNode(); wrapNode.setNode(child); xslNode = wrapNode; xslNode.setGenerator(this); */ } } else if (child instanceof Text) { xslNode = new TextNode(((Text) child).getData()); xslNode.setGenerator(this); xslNode.setParent(_xslNode); } else if (child instanceof Comment) { } else if (child instanceof ProcessingInstruction) { } else throw new UnsupportedOperationException(String.valueOf(child)); if (xslNode != null) { xslNode.setStartLocation(((QAbstractNode) child).getBaseURI(), ((QAbstractNode) child).getFilename(), ((QAbstractNode) child).getLine()); } return xslNode; } /** * Generates code for a template * * @param absNode the XSL node for the emplace * @param name the template name * @param pattern the pattern string * @param mode the template's mode * @param priority the template's priority */ protected void printTemplate(Element absNode, String name, String pattern, String mode, double priority) throws Exception { throw new RuntimeException(); /* QElement node = (QElement) absNode; if (name != null && ! name.equals("")) addMacro(name, node); if (! pattern.equals("")) { String fun = createTemplatePattern(name, pattern, mode, priority); print("// '" + pattern.replace('\n', ' ') + "'"); if (mode != null && mode != "") { if (! _modes.contains(mode)) _modes.add(mode); println(" mode '" + mode + "'"); } else println(); printString("// " + node.getFilename() + ":" + node.getLine()); println(); println("private void " + fun + "(XslWriter out, Node inputNode, Env env)"); println(" throws Exception"); println("{"); pushDepth(); println("Object _xsl_tmp;"); println("Node node = inputNode;"); println("int _xsl_top = env.getTop();"); if (_isRawText) println("boolean oldEscaping = out.disableEscaping(true);"); else println("boolean oldEscaping = out.disableEscaping(false);"); String filename = node.getBaseURI(); if (filename != null) { int pos = _stylesheets.indexOf(filename); if (pos < 0) { pos = _stylesheets.size(); _stylesheets.add(filename); } println("env.setStylesheetEnv(stylesheets[" + pos + "]);"); } _selectDepth = 0; _unique = 0; if (node.getLocalName().equals("template") || node.getLocalName().equals("xsl:template")) generateChildren(node); else generateChild((QAbstractNode) node); if (! _isCacheable) println("out.setNotCacheable();"); println("out.disableEscaping(oldEscaping);"); println("env.popToTop(_xsl_top);"); popDepth(); println("}"); println(); } */ }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?