javagenerator.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,601 行 · 第 1/5 页

JAVA
2,601
字号
  public void addMacro(String name, String functionName)  {    _macros.put(name, functionName);  }  /*  public void addMacro(String name)  {    addMacro(name, "_xsl_macro_" + toJavaIdentifier(name);  }  */  public boolean hasMacro(String name)  {    return _macros.keySet().contains(name);  }  /**   * Generates the pattern for a matching pattern   *   * @param name the mangled name of the function   * @param match the XPath match pattern   * @param mode the template mode   * @param priority the template priority   * @param node the source XML node from the XSL file   *   * @return the name of the function   */  public String createTemplatePattern(String name, AbstractPattern match,				      String mode, double priority)    throws Exception  {    String tagName;    if (name != null)      tagName = getName(name);    else      tagName = getName(match.toString());    String function = "_xsl_template_" + tagName;    _functions.add(function);    if (match != null) {      Template template = addPattern(match,                                     mode, priority, function,                                     _functions.size());      _templateList.add(template);    }    else      _templateList.add(null);    return function;  }    protected void startDisableEscaping()    throws IOException  {    if (! _isRawText)      println("out.disableEscaping(true);");  }   protected void endDisableEscaping()    throws IOException  {    if (! _isRawText)      println("out.disableEscaping(false);");  }  /**   * Creates Java code to print plain text.   */  protected void writeText(String text)    throws Exception  {    if (text == null || text.length() == 0)      return;    int index = _stringMap.get(text);    if (index < 0) {      index = _strings.size();      _stringMap.put(text, index);      _strings.add(text);    }    printLocation(_systemId, _filename, _line);    println("out.write(_xsl_string" + index + ", 0, " + text.length() + ");");  }  protected void printElement(Node node)    throws Exception  {    QElement elt = (QElement) node;    String name = node.getNodeName();    if (name.equals("jsp:decl") || name.equals("jsp:declaration")) {      println("if (out.isFlagFirst(" + _flagCount++ + ")) {");      pushDepth();    }        String prefix = elt.getPrefix();    String local = elt.getLocalName();    String namespace = elt.getNamespaceURI();    String []postPrefix = (String []) _namespaceAliases.get(namespace);    if (postPrefix != null) {      prefix = postPrefix[0];      namespace = postPrefix[1];      if (prefix == null || prefix.equals(""))        name = local;      else        name = prefix + ":" + local;    }    if (_excludedNamespaces.get(namespace) != null)      namespace = null;    printLocation(_systemId, _filename, _line);    if (namespace == null || namespace.equals("")) {      print("out.pushElement(");      print(name == null ? "null" : ("\"" + name + "\""));      println(");");    } else {      print("out.pushElement(");      print(namespace == null ? "null" : ("\"" + namespace + "\""));      print(prefix == null ? ", null" : (", \"" + prefix + "\""));      print(local == null ? ", null" : (", \"" + local + "\""));      print(name == null ? ", null" : (", \"" + name + "\""));      println(");");    }        printUseAttributeSet((QElement) node, false);        NamedNodeMap list = node.getAttributes();    for (int i = 0; i < list.getLength(); i++) {      QAbstractNode attr = (QAbstractNode) list.item(i);      printAttribute(attr, elt);    }        generateChildren(node);        println("out.popElement();");    if (node.getNodeName().equals("jsp:decl") ||        node.getNodeName().equals("jsp:declaration")) {      popDepth();      println("}");    }  }  /**   * Prints a command to set the current file and line into the   * generated document.   *   * @param filename the source filename   * @param line the source line number.   */  public void printLocation(String systemId, String filename, int line)    throws Exception  {    if (_printLocation && filename != null && ! _isSpecial) {      print("out.setLocation(");      if (systemId != null) {	print("\"");	printString(systemId);	print("\"");      }      else	print("null");      print(", \"");      printString(filename);      println("\", " + line + ");");      _oldFilename = filename;      _oldLine = line;    }  }  /**   * Prints code for an element's attributes.   */  private void printAttribute(QAbstractNode attr, QElement elt)    throws Exception  {    if (attr.getNodeName().equals("xsl:use-attribute-sets")) {    }    else if (XSLNS.equals(elt.getNamespace(attr.getPrefix()))) {    }    else if (XTPNS.equals(elt.getNamespace(attr.getPrefix()))) {    }    else {      QAbstractNode qnode = (QAbstractNode) attr;      String prefix = qnode.getPrefix();      String local = qnode.getLocalName();      String namespace = qnode.getNamespaceURI();      String value = attr.getNodeValue();      String []postSuffix = (String []) _namespaceAliases.get(namespace);      if (postSuffix != null) {	prefix = postSuffix[0];	namespace = postSuffix[1];      }      else if (value.equals(XSLNS) && prefix.equals("xmlns"))        return;      else if (value.equals(XTPNS) && prefix.equals("xmlns"))        return;      if (_excludedNamespaces.get(namespace) != null)	namespace = null;            if ("".equals(prefix) && ("".equals(namespace) || namespace == null)) {        String var = generateStringVar(value, elt);        println("out.setAttribute(\"" + local + "\", " + var + ");");      }      else {        print("out.pushAttribute(");        print(prefix == null ? "null" : ("\"" + prefix + "\""));        print(local == null ? ", null" : (", \"" + local + "\""));        print(namespace == null ? ", null" : (", \"" + namespace + "\""));        println(");");        generateString(value, ',', elt);        println("out.popAttribute();");      }    }  }  protected void pushCall()    throws IOException  {    println("{");    pushDepth();    _callDepth++;    println("Env _xsl_arg" + _callDepth + " = XPath.createCall(env);");  }  public int pushCallDepth()  {    return ++_callDepth;  }  public int popCallDepth()  {    return _callDepth--;  }  public int getCallDepth()  {    return _callDepth;  }  protected void popCall()    throws IOException  {    //println("_xsl_arg" + callDepth + ".free();");    _callDepth--;    popDepth();    println("}");  }  /**   * Prints code for xsl:apply-templates   *   * @param select the select pattern   * @param mode the template mode   * @param sort the sort expressions   */  protected void printApplyTemplates(AbstractPattern select,                                     String mode,                                     Sort []sort)    throws Exception  {    int min = 0;    int max = Integer.MAX_VALUE;    String applyName = "applyNode" + getModeName(mode);    String env = "_xsl_arg" + _callDepth;    if (select == null && sort == null) {      println("for (Node _xsl_node = node.getFirstChild();");      println("     _xsl_node != null;");      println("     _xsl_node = _xsl_node.getNextSibling()) {");      println("  " + env + ".setSelect(node, null);");      println("  " + env + ".setCurrentNode(_xsl_node);");      println("  " + applyName + "(out, _xsl_node, " + env + ", " +              min + ", " + max + ");");      println("}");    }    else if (sort == null) {      int oldSelectDepth = _selectDepth;      println(env + ".setSelect(node, _select_patterns[" +              addSelect(select) + "]);");      String name = printSelectBegin(select, false, null);      println(env + ".setCurrentNode(" + name + ");");      println(applyName + "(out, " + name + ", " + env + ", " +               min + ", " + max + ");");      for (; _selectDepth > oldSelectDepth; _selectDepth--) {        popDepth();        println("}");      }    }    else {      println("{");      pushDepth();      println("ArrayList _xsl_list = xslSort(node, env" +	      ", _select_patterns[" + addSelect(select) + "]" +	      ", _xsl_sorts[" + _sorts.size() + "]);");      println(env + ".setContextSize(_xsl_list.size());");      println("for (int _xsl_i = 0; _xsl_i < _xsl_list.size(); _xsl_i++) {");      println("  " + env + ".setContextPosition(_xsl_i + 1);");      println("  " + applyName + "(out, (Node) _xsl_list.get(_xsl_i)" + 	      ", " + env + ", " + min + ", " + max + ");");      println("}");      popDepth();      println("}");      _sorts.add(sort);    }  }  public int addSort(Sort []sort)  {    int index = _sorts.size();        _sorts.add(sort);    return index;  }  /**   * Prints code to implement xsl:apply-imports   *   * @param mode the mode of the imported files   * @param min the min importance   * @param max the max importance   */  protected void printApplyImports(String mode, int min, int max)    throws Exception  {  }  protected void printCallTemplate(String name, String mode)    throws Exception  {    println(getMacroName(name) + "(out, node, _xsl_arg" +            _callDepth + ");");  }  public String getMacroName(String name)  {    return _macros.get(name);    //return "_xsl_macro_" + toJavaIdentifier(name);  }  /**   * Prints the value for a parameter.   */  protected void printParam(String name, String value, Element elt)    throws Exception  {    print("_xsl_arg" + _callDepth + ".addVar(\"" + name + "\", ");    generateString(value, '+', elt);    println(");");  }    protected void printParam(String name, Object value)    throws Exception  {    if (value instanceof Expr) {      print("_exprs[" + addExpr((Expr) value) + "]");      println(".addVar(_xsl_arg" + _callDepth + ", \"" + name + "\", " +              "node, env);");    }    else {      print("_xsl_arg" + _callDepth + ".addVar(\"");      print(name);      print("\", ");      printVariableValue(value);      println(");");    }  }  /**   * Prints code to add the value of an expression as a parameter.   */  protected void printParamVariable(String name, Expr value)    throws Exception  {    print("_exprs[" + addExpr(value) + "]");    println(".addParam(env, \"" + name + "\", " +            "node, env);");  }  protected void printParamVariable(String name, Element value)    throws Exception  {    if (value.getFirstChild() != null) {      println("_xsl_tmp = env.getVar(\"" + name + "\");");       println("if (_xsl_tmp == null)");      print("  _xsl_tmp = ");      printVariableValue(value);      println(";");      println("env.addVar(\"" + name + "\", _xsl_tmp);");    }  }  protected void printVariable(String name, Object value)    throws Exception  {    if (value instanceof Expr) {      print("_exprs[" + addExpr((Expr) value) + "]");      println(".addVar(env, \"" + name + "\", node, env);");    }    else {      print("env.addVar(\"");      print(name);      print("\", ");      printVariableValue(value);      println(");");    }  }  protected void printAssign(String name, Object value)    throws Exception  {    if (value instanceof Expr) {      print("_exprs[" + addExpr((Expr) value) + "]");      println(".setVar(\"" + name + "\", node, env, node);");    }    else {      print("env.setVar(\"");      print(name);      print("\", ");      printVariableValue(value);      println(");");    }  }  private void printVariableValue(Object value)    throws Exception  {    if (value instanceof Expr) {      print("_exprs[" + addExpr((Expr) value) + "].evalObject(node, env)");    }    else if (value instanceof Node) {      print("_xsl_fragment" + _fragments.size() + "(out, node, env)");      _fragments.add(value);    }    else      throw new RuntimeException();  }  protected void printPopScope(int count)    throws Exception  {    if (count > 0)      println("env.popVars(" + count + ");");  }  protected void printCopyOf(String select, Element elt)    throws Exception  {    println("out.copyOf(_exprs[ " + addExpr(select) +            "].evalObject(node, env));");  }  protected void printSelectValue(String select, Element elt)    throws Exception  {    printStringExpr(select, elt);  }  protected void printForEach(Element element, String select)    throws Exception  {    println("{");    pushDepth();    AbstractPattern selectPattern = null;    try {      selectPattern = parseSelect(select);    } catch (Exception e) {    }        boolean hasExprEnv = ! allowJavaSelect(selectPattern);    int id = _unique++;        String sel = "_xsl_sel" + id;    String oldCxt = "_xsl_cxt" + id;    String oldCur = "_xsl_cur" + id;    String oldSel = "_xsl_old_sel" + id;    String oldEnv = "_xsl_env" + id;    println("com.caucho.xpath.pattern.AbstractPattern " + sel + ";");    print(sel + " = _select_patterns[");    print(createNodeSet(select, element));    println("];");    println("Node " + oldCxt + " = env.getContextNode();");    println("Node " + oldCur + " = env.getCurrentNode();");        if (! hasExprEnv) {      println("AbstractPattern " + oldSel + " = env.setSelect(node, " + sel + ");");    }            // String pos = "_xsl_pos" + unique++;    String iter = "_xsl_iter" + _unique++;    int oldSelectDepth = _selectDepth;        // println("int " + pos + " = 0;");    boolean hasEnv = false;        if (allowJavaSelect(selectPattern)) {      println("ExprEnvironment " + oldEnv + " = env.setExprEnv(null);");      

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?