Beispiel #1
0
 public void display(int indent) {
   indent(indent);
   Util.println("ForEach");
   indent(indent + IndentIncrement);
   Util.println("select " + _select.toString());
   displayContents(indent + IndentIncrement);
 }
Beispiel #2
0
  /**
   * Set the class name for the generated translet. This class name is overridden if multiple
   * stylesheets are compiled in one go using the compile(Vector urls) method.
   *
   * @param className The name to assign to the translet class
   */
  public void setClassName(String className) {
    final String base = Util.baseName(className);
    final String noext = Util.noExtName(base);
    String name = Util.toJavaName(noext);

    if (_packageName == null) _className = name;
    else _className = _packageName + '.' + name;
  }
  /**
   * Compile a buildKeys() method into the output class. Note that keys for the input document are
   * created in topLevel(), not in this method. However, we still need this method to create keys
   * for documents loaded via the XPath document() function.
   */
  private String compileBuildKeys(ClassGenerator classGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();

    final com.sun.org.apache.bcel.internal.generic.Type[] argTypes = {
      Util.getJCRefType(DOM_INTF_SIG),
      Util.getJCRefType(NODE_ITERATOR_SIG),
      Util.getJCRefType(TRANSLET_OUTPUT_SIG),
      com.sun.org.apache.bcel.internal.generic.Type.INT
    };

    final String[] argNames = {DOCUMENT_PNAME, ITERATOR_PNAME, TRANSLET_OUTPUT_PNAME, "current"};

    final InstructionList il = new InstructionList();

    final MethodGenerator buildKeys =
        new MethodGenerator(
            ACC_PUBLIC,
            com.sun.org.apache.bcel.internal.generic.Type.VOID,
            argTypes,
            argNames,
            "buildKeys",
            _className,
            il,
            classGen.getConstantPool());

    buildKeys.addException("com.sun.org.apache.xalan.internal.xsltc.TransletException");

    final Enumeration elements = elements();
    while (elements.hasMoreElements()) {
      // xsl:key
      final Object element = elements.nextElement();
      if (element instanceof Key) {
        final Key key = (Key) element;
        key.translate(classGen, buildKeys);
        _keys.put(key.getName(), key);
      }
    }

    il.append(RETURN);

    // Compute max locals + stack and add method to class
    buildKeys.stripAttributes(true);
    buildKeys.setMaxLocals();
    buildKeys.setMaxStack();
    buildKeys.removeNOPs();

    classGen.addMethod(buildKeys.getMethod());

    return ("(" + DOM_INTF_SIG + NODE_ITERATOR_SIG + TRANSLET_OUTPUT_SIG + "I)V");
  }
  public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();

    if (!_isLiteral) {
      // if the ncname is an AVT, then the ncname has to be checked at runtime if it is a valid
      // ncname
      LocalVariableGen nameValue =
          methodGen.addLocalVariable2("nameValue", Util.getJCRefType(STRING_SIG), null);

      // store the name into a variable first so _name.translate only needs to be called once
      _name.translate(classGen, methodGen);
      nameValue.setStart(il.append(new ASTORE(nameValue.getIndex())));
      il.append(new ALOAD(nameValue.getIndex()));

      // call checkNCName if the name is an AVT
      final int check =
          cpg.addMethodref(BASIS_LIBRARY_CLASS, "checkNCName", "(" + STRING_SIG + ")V");
      il.append(new INVOKESTATIC(check));

      // Save the current handler base on the stack
      il.append(methodGen.loadHandler());
      il.append(DUP); // first arg to "attributes" call

      // load name value again
      nameValue.setEnd(il.append(new ALOAD(nameValue.getIndex())));
    } else {
      // Save the current handler base on the stack
      il.append(methodGen.loadHandler());
      il.append(DUP); // first arg to "attributes" call

      // Push attribute name
      _name.translate(classGen, methodGen); // 2nd arg
    }

    il.append(classGen.loadTranslet());
    il.append(
        new GETFIELD(
            cpg.addFieldref(TRANSLET_CLASS, "stringValueHandler", STRING_VALUE_HANDLER_SIG)));
    il.append(DUP);
    il.append(methodGen.storeHandler());

    // translate contents with substituted handler
    translateContents(classGen, methodGen);

    // get String out of the handler
    il.append(
        new INVOKEVIRTUAL(
            cpg.addMethodref(STRING_VALUE_HANDLER, "getValueOfPI", "()" + STRING_SIG)));
    // call "processingInstruction"
    final int processingInstruction =
        cpg.addInterfaceMethodref(
            TRANSLET_OUTPUT_INTERFACE,
            "processingInstruction",
            "(" + STRING_SIG + STRING_SIG + ")V");
    il.append(new INVOKEINTERFACE(processingInstruction, 3));
    // Restore old handler base from stack
    il.append(methodGen.storeHandler());
  }
  public void parseContents(Parser parser) {
    final String name = getAttribute("name");

    if (name.length() > 0) {
      _isLiteral = Util.isLiteral(name);
      if (_isLiteral) {
        if (!XML11Char.isXML11ValidNCName(name)) {
          ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_NCNAME_ERR, name, this);
          parser.reportError(Constants.ERROR, err);
        }
      }
      _name = AttributeValue.create(this, name, parser);
    } else reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "name");

    if (name.equals("xml")) {
      reportError(this, parser, ErrorMsg.ILLEGAL_PI_ERR, "xml");
    }
    parseChildren(parser);
  }
  /**
   * Compile a topLevel() method into the output class. This method is called from transform() to
   * handle all non-template top-level elements. Returns the signature of the topLevel() method.
   *
   * <p>Global variables/params and keys are first sorted to resolve dependencies between them. The
   * XSLT 1.0 spec does not allow a key to depend on a variable. However, for compatibility with
   * Xalan interpretive, that type of dependency is allowed. Note also that the buildKeys() method
   * is still generated as it is used by the LoadDocument class, but it no longer called from
   * transform().
   */
  private String compileTopLevel(ClassGenerator classGen) {

    final ConstantPoolGen cpg = classGen.getConstantPool();

    final com.sun.org.apache.bcel.internal.generic.Type[] argTypes = {
      Util.getJCRefType(DOM_INTF_SIG),
      Util.getJCRefType(NODE_ITERATOR_SIG),
      Util.getJCRefType(TRANSLET_OUTPUT_SIG)
    };

    final String[] argNames = {DOCUMENT_PNAME, ITERATOR_PNAME, TRANSLET_OUTPUT_PNAME};

    final InstructionList il = new InstructionList();

    final MethodGenerator toplevel =
        new MethodGenerator(
            ACC_PUBLIC,
            com.sun.org.apache.bcel.internal.generic.Type.VOID,
            argTypes,
            argNames,
            "topLevel",
            _className,
            il,
            classGen.getConstantPool());

    toplevel.addException("com.sun.org.apache.xalan.internal.xsltc.TransletException");

    // Define and initialize 'current' variable with the root node
    final LocalVariableGen current =
        toplevel.addLocalVariable(
            "current", com.sun.org.apache.bcel.internal.generic.Type.INT, null, null);

    final int setFilter =
        cpg.addInterfaceMethodref(
            DOM_INTF, "setFilter", "(Lcom/sun/org/apache/xalan/internal/xsltc/StripFilter;)V");

    final int gitr = cpg.addInterfaceMethodref(DOM_INTF, "getIterator", "()" + NODE_ITERATOR_SIG);
    il.append(toplevel.loadDOM());
    il.append(new INVOKEINTERFACE(gitr, 1));
    il.append(toplevel.nextNode());
    current.setStart(il.append(new ISTORE(current.getIndex())));

    // Create a new list containing variables/params + keys
    Vector varDepElements = new Vector(_globals);
    Enumeration elements = elements();
    while (elements.hasMoreElements()) {
      final Object element = elements.nextElement();
      if (element instanceof Key) {
        varDepElements.add(element);
      }
    }

    // Determine a partial order for the variables/params and keys
    varDepElements = resolveDependencies(varDepElements);

    // Translate vars/params and keys in the right order
    final int count = varDepElements.size();
    for (int i = 0; i < count; i++) {
      final TopLevelElement tle = (TopLevelElement) varDepElements.elementAt(i);
      tle.translate(classGen, toplevel);
      if (tle instanceof Key) {
        final Key key = (Key) tle;
        _keys.put(key.getName(), key);
      }
    }

    // Compile code for other top-level elements
    Vector whitespaceRules = new Vector();
    elements = elements();
    while (elements.hasMoreElements()) {
      final Object element = elements.nextElement();
      // xsl:decimal-format
      if (element instanceof DecimalFormatting) {
        ((DecimalFormatting) element).translate(classGen, toplevel);
      }
      // xsl:strip/preserve-space
      else if (element instanceof Whitespace) {
        whitespaceRules.addAll(((Whitespace) element).getRules());
      }
    }

    // Translate all whitespace strip/preserve rules
    if (whitespaceRules.size() > 0) {
      Whitespace.translateRules(whitespaceRules, classGen);
    }

    if (classGen.containsMethod(STRIP_SPACE, STRIP_SPACE_PARAMS) != null) {
      il.append(toplevel.loadDOM());
      il.append(classGen.loadTranslet());
      il.append(new INVOKEINTERFACE(setFilter, 2));
    }

    il.append(RETURN);

    // Compute max locals + stack and add method to class
    classGen.addMethod(toplevel);

    return ("(" + DOM_INTF_SIG + NODE_ITERATOR_SIG + TRANSLET_OUTPUT_SIG + ")V");
  }
 /** Add a static field */
 private void addStaticField(ClassGenerator classGen, String type, String name) {
   final FieldGen fgen =
       new FieldGen(
           ACC_PROTECTED | ACC_STATIC, Util.getJCRefType(type), name, classGen.getConstantPool());
   classGen.addField(fgen.getField());
 }
 private void addDOMField(ClassGenerator classGen) {
   final FieldGen fgen =
       new FieldGen(
           ACC_PUBLIC, Util.getJCRefType(DOM_INTF_SIG), DOM_FIELD, classGen.getConstantPool());
   classGen.addField(fgen.getField());
 }
 public void display(int indent) {
   indent(indent);
   Util.println("Stylesheet");
   displayContents(indent + IndentIncrement);
 }
Beispiel #10
0
  /**
   * Compile transform() into the output class. This method is used to initialize global variables
   * and global parameters. The current node is set to be the document's root node.
   */
  private void compileTransform(ClassGenerator classGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();

    /*
     * Define the the method transform with the following signature:
     * void transform(DOM, NodeIterator, HandlerBase)
     */
    final com.sun.org.apache.bcel.internal.generic.Type[] argTypes =
        new com.sun.org.apache.bcel.internal.generic.Type[3];
    argTypes[0] = Util.getJCRefType(DOM_INTF_SIG);
    argTypes[1] = Util.getJCRefType(NODE_ITERATOR_SIG);
    argTypes[2] = Util.getJCRefType(TRANSLET_OUTPUT_SIG);

    final String[] argNames = new String[3];
    argNames[0] = DOCUMENT_PNAME;
    argNames[1] = ITERATOR_PNAME;
    argNames[2] = TRANSLET_OUTPUT_PNAME;

    final InstructionList il = new InstructionList();
    final MethodGenerator transf =
        new MethodGenerator(
            ACC_PUBLIC,
            com.sun.org.apache.bcel.internal.generic.Type.VOID,
            argTypes,
            argNames,
            "transform",
            _className,
            il,
            classGen.getConstantPool());
    transf.addException("com.sun.org.apache.xalan.internal.xsltc.TransletException");

    // Define and initialize current with the root node
    final LocalVariableGen current =
        transf.addLocalVariable(
            "current", com.sun.org.apache.bcel.internal.generic.Type.INT, null, null);
    final String applyTemplatesSig = classGen.getApplyTemplatesSig();
    final int applyTemplates =
        cpg.addMethodref(getClassName(), "applyTemplates", applyTemplatesSig);
    final int domField = cpg.addFieldref(getClassName(), DOM_FIELD, DOM_INTF_SIG);

    // push translet for PUTFIELD
    il.append(classGen.loadTranslet());
    // prepare appropriate DOM implementation

    if (isMultiDocument()) {
      il.append(new NEW(cpg.addClass(MULTI_DOM_CLASS)));
      il.append(DUP);
    }

    il.append(classGen.loadTranslet());
    il.append(transf.loadDOM());
    il.append(
        new INVOKEVIRTUAL(
            cpg.addMethodref(
                TRANSLET_CLASS, "makeDOMAdapter", "(" + DOM_INTF_SIG + ")" + DOM_ADAPTER_SIG)));
    // DOMAdapter is on the stack

    if (isMultiDocument()) {
      final int init = cpg.addMethodref(MULTI_DOM_CLASS, "<init>", "(" + DOM_INTF_SIG + ")V");
      il.append(new INVOKESPECIAL(init));
      // MultiDOM is on the stack
    }

    // store to _dom variable
    il.append(new PUTFIELD(domField));

    // continue with globals initialization
    final int gitr = cpg.addInterfaceMethodref(DOM_INTF, "getIterator", "()" + NODE_ITERATOR_SIG);
    il.append(transf.loadDOM());
    il.append(new INVOKEINTERFACE(gitr, 1));
    il.append(transf.nextNode());
    current.setStart(il.append(new ISTORE(current.getIndex())));

    // Transfer the output settings to the output post-processor
    il.append(classGen.loadTranslet());
    il.append(transf.loadHandler());
    final int index =
        cpg.addMethodref(TRANSLET_CLASS, "transferOutputSettings", "(" + OUTPUT_HANDLER_SIG + ")V");
    il.append(new INVOKEVIRTUAL(index));

    /*
     * Compile buildKeys() method. Note that this method is not
     * invoked here as keys for the input document are now created
     * in topLevel(). However, this method is still needed by the
     * LoadDocument class.
     */
    final String keySig = compileBuildKeys(classGen);
    final int keyIdx = cpg.addMethodref(getClassName(), "buildKeys", keySig);

    // Look for top-level elements that need handling
    final Enumeration toplevel = elements();
    if (_globals.size() > 0 || toplevel.hasMoreElements()) {
      // Compile method for handling top-level elements
      final String topLevelSig = compileTopLevel(classGen);
      // Get a reference to that method
      final int topLevelIdx = cpg.addMethodref(getClassName(), "topLevel", topLevelSig);
      // Push all parameters on the stack and call topLevel()
      il.append(classGen.loadTranslet()); // The 'this' pointer
      il.append(classGen.loadTranslet());
      il.append(new GETFIELD(domField)); // The DOM reference
      il.append(transf.loadIterator());
      il.append(transf.loadHandler()); // The output handler
      il.append(new INVOKEVIRTUAL(topLevelIdx));
    }

    // start document
    il.append(transf.loadHandler());
    il.append(transf.startDocument());

    // push first arg for applyTemplates
    il.append(classGen.loadTranslet());
    // push translet for GETFIELD to get DOM arg
    il.append(classGen.loadTranslet());
    il.append(new GETFIELD(domField));
    // push remaining 2 args
    il.append(transf.loadIterator());
    il.append(transf.loadHandler());
    il.append(new INVOKEVIRTUAL(applyTemplates));
    // endDocument
    il.append(transf.loadHandler());
    il.append(transf.endDocument());

    il.append(RETURN);

    // Compute max locals + stack and add method to class
    classGen.addMethod(transf);
  }
Beispiel #11
0
  /**
   * Compiles an XSL stylesheet passed in through an InputStream
   *
   * @param input An InputSource that will pass in the stylesheet contents
   * @param name The name of the translet class to generate - can be null
   * @return 'true' if the compilation was successful
   */
  public boolean compile(InputSource input, String name) {
    try {
      // Reset globals in case we're called by compile(Vector v);
      reset();

      // The systemId may not be set, so we'll have to check the URL
      String systemId = null;
      if (input != null) {
        systemId = input.getSystemId();
      }

      // Set the translet class name if not already set
      if (_className == null) {
        if (name != null) {
          setClassName(name);
        } else if (systemId != null && !systemId.equals("")) {
          setClassName(Util.baseName(systemId));
        }

        // Ensure we have a non-empty class name at this point
        if (_className == null || _className.length() == 0) {
          setClassName("GregorSamsa"); // default translet name
        }
      }

      // Get the root node of the abstract syntax tree
      SyntaxTreeNode element = null;
      if (_reader == null) {
        element = _parser.parse(input);
      } else {
        element = _parser.parse(_reader, input);
      }

      // Compile the translet - this is where the work is done!
      if ((!_parser.errorsFound()) && (element != null)) {
        // Create a Stylesheet element from the root node
        _stylesheet = _parser.makeStylesheet(element);
        _stylesheet.setSourceLoader(_loader);
        _stylesheet.setSystemId(systemId);
        _stylesheet.setParentStylesheet(null);
        _stylesheet.setTemplateInlining(_templateInlining);
        _parser.setCurrentStylesheet(_stylesheet);

        // Create AST under the Stylesheet element (parse & type-check)
        _parser.createAST(_stylesheet);
      }
      // Generate the bytecodes and output the translet class(es)
      if ((!_parser.errorsFound()) && (_stylesheet != null)) {
        _stylesheet.setCallsNodeset(_callsNodeset);
        _stylesheet.setMultiDocument(_multiDocument);
        _stylesheet.setHasIdCall(_hasIdCall);

        // Class synchronization is needed for BCEL
        synchronized (getClass()) {
          _stylesheet.translate();
        }
      }
    } catch (Exception e) {
      /*if (_debug)*/ e.printStackTrace();
      _parser.reportError(Constants.FATAL, new ErrorMsg(e));
    } catch (Error e) {
      if (_debug) e.printStackTrace();
      _parser.reportError(Constants.FATAL, new ErrorMsg(e));
    } finally {
      _reader = null; // reset this here to be sure it is not re-used
    }
    return !_parser.errorsFound();
  }