Package org.apache.bcel.generic

Examples of org.apache.bcel.generic.InstructionList


     */
    private InstructionList compileDefaultRecursion(ClassGenerator classGen,
                MethodGenerator methodGen,
                InstructionHandle next) {
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = new InstructionList();
  final String applyTemplatesSig = classGen.getApplyTemplatesSig();
  final int git = cpg.addInterfaceMethodref(DOM_INTF,
              GET_CHILDREN,
              GET_CHILDREN_SIG);
  final int applyTemplates = cpg.addMethodref(getClassName(),
                functionName(),
                applyTemplatesSig);
  il.append(classGen.loadTranslet());
  il.append(methodGen.loadDOM());
 
  il.append(methodGen.loadDOM());
  il.append(new ILOAD(_currentIndex));
  il.append(new INVOKEINTERFACE(git, 2));
  il.append(methodGen.loadHandler());
  il.append(new INVOKEVIRTUAL(applyTemplates));
  il.append(new GOTO_W(next));
  return il;
    }
View Full Code Here


     */
    private InstructionList compileDefaultText(ClassGenerator classGen,
                 MethodGenerator methodGen,
                 InstructionHandle next) {
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = new InstructionList();

  final int chars = cpg.addInterfaceMethodref(DOM_INTF,
                CHARACTERS,
                CHARACTERS_SIG);
  il.append(methodGen.loadDOM());
  il.append(new ILOAD(_currentIndex));
  il.append(methodGen.loadHandler());
  il.append(new INVOKEINTERFACE(chars, 3));
  il.append(new GOTO_W(next));
  return il;
    }
View Full Code Here

  final Vector namespaces = xsltc.getNamespaceIndex();
  final Vector names = xsltc.getNamesIndex();
  final int namespaceCount = namespaces.size() + 1;
  final int namesCount = names.size();

  final InstructionList il = new InstructionList();
  final int[] types = new int[namespaceCount];
  final InstructionHandle[] targets = new InstructionHandle[types.length];

  if (namespaceCount > 0) {
      boolean compiled = false;

      // Initialize targets for namespace() switch statement
      for (int i = 0; i < namespaceCount; i++) {
    targets[i] = defaultTarget;
    types[i] = i;
      }

      // Add test sequences for known namespace types
      for (int i = DOM.NTYPES; i < (DOM.NTYPES+namesCount); i++) {
    if ((isNamespace[i]) && (isAttribute[i] == attrFlag)) {
        String name = (String)names.elementAt(i-DOM.NTYPES);
        String namespace = name.substring(0,name.lastIndexOf(':'));
        final int type = xsltc.registerNamespace(namespace);
       
        if ((i < _testSeq.length) &&
      (_testSeq[i] != null)) {
      targets[type] =
          (_testSeq[i]).compile(classGen,
                   methodGen,
                   defaultTarget);
      compiled = true;
        }
    }
      }

      // Return "null" if no test sequences were compiled
      if (!compiled) return(null);
   
      // Append first code in applyTemplates() - get type of current node
      final int getNS = cpg.addInterfaceMethodref(DOM_INTF,
              "getNamespaceType",
              "(I)I");
      il.append(methodGen.loadDOM());
      il.append(new ILOAD(_currentIndex));
      il.append(new INVOKEINTERFACE(getNS, 2));
      il.append(new SWITCH(types, targets, defaultTarget));
      return(il);
  }
  else {
      return(null);
  }
View Full Code Here

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

  final InstructionList mainIL = new InstructionList();
  final MethodGenerator methodGen =
      new MethodGenerator(ACC_PUBLIC | ACC_FINAL,
        org.apache.bcel.generic.Type.VOID,
        argTypes, argNames, functionName(),
        getClassName(), mainIL,
        classGen.getConstantPool());
  methodGen.addException("org.apache.xalan.xsltc.TransletException");

  // Create a local variable to hold the current node
  final LocalVariableGen current;
  current = methodGen.addLocalVariable2("current",
                org.apache.bcel.generic.Type.INT,
                mainIL.getEnd());
  _currentIndex = current.getIndex();

  // Create the "body" instruction list that will eventually hold the
  // code for the entire method (other ILs will be appended).
  final InstructionList body = new InstructionList();
  body.append(NOP);

  // Create an instruction list that contains the default next-node
  // iteration
  final InstructionList ilLoop = new InstructionList();
  ilLoop.append(methodGen.loadIterator());
  ilLoop.append(methodGen.nextNode());
  ilLoop.append(DUP);
  ilLoop.append(new ISTORE(_currentIndex));

  // The body of this code can get very large - large than can be handled
  // by a single IFNE(body.getStart()) instruction - need workaround:
        final BranchHandle ifeq = ilLoop.append(new IFEQ(null));
  final BranchHandle loop = ilLoop.append(new GOTO_W(null));
  ifeq.setTarget(ilLoop.append(RETURN));   // applyTemplates() ends here!
  final InstructionHandle ihLoop = ilLoop.getStart();

  // Compile default handling of elements (traverse children)
  InstructionList ilRecurse =
      compileDefaultRecursion(classGen, methodGen, ihLoop);
  InstructionHandle ihRecurse = ilRecurse.getStart();

  // Compile default handling of text/attribute nodes (output text)
  InstructionList ilText =
      compileDefaultText(classGen, methodGen, ihLoop);
  InstructionHandle ihText = ilText.getStart();

  // Distinguish attribute/element/namespace tests for further processing
  final int[] types = new int[DOM.NTYPES + names.size()];
  for (int i = 0; i < types.length; i++) {
      types[i] = i;
  }

  // Initialize isAttribute[] and isNamespace[] arrays
  final boolean[] isAttribute = new boolean[types.length];
  final boolean[] isNamespace = new boolean[types.length];
  for (int i = 0; i < names.size(); i++) {
      final String name = (String)names.elementAt(i);
      isAttribute[i + DOM.NTYPES] = isAttributeName(name);
      isNamespace[i + DOM.NTYPES] = isNamespaceName(name);
  }

  // Compile all templates - regardless of pattern type
  compileTemplates(classGen, methodGen, ihLoop);

  // Handle template with explicit "*" pattern
  final TestSeq elemTest = _testSeq[DOM.ELEMENT];
  InstructionHandle ihElem = ihRecurse;
  if (elemTest != null)
      ihElem = elemTest.compile(classGen, methodGen, ihRecurse);

  // Handle template with explicit "@*" pattern
  final TestSeq attrTest = _testSeq[DOM.ATTRIBUTE];
  InstructionHandle ihAttr = ihText;
  if (attrTest != null)
      ihAttr = attrTest.compile(classGen, methodGen, ihAttr);

  // Do tests for id() and key() patterns first
  InstructionList ilKey = null;
  if (_idxTestSeq != null) {
      loop.setTarget(_idxTestSeq.compile(classGen, methodGen, body.getStart()));
      ilKey = _idxTestSeq.getInstructionList();
  }
  else {
      loop.setTarget(body.getStart());
  }

  // If there is a match on node() we need to replace ihElem
  // and ihText if the priority of node() is higher
  if (_childNodeTestSeq != null) {
      // Compare priorities of node() and "*"
      double nodePrio = _childNodeTestSeq.getPriority();
      int    nodePos  = _childNodeTestSeq.getPosition();
      double elemPrio = (0 - Double.MAX_VALUE);
      int    elemPos  = Integer.MIN_VALUE;

      if (elemTest != null) {
    elemPrio = elemTest.getPriority();
    elemPos  = elemTest.getPosition();
      }
      if (elemPrio == Double.NaN || elemPrio < nodePrio ||
    (elemPrio == nodePrio && elemPos < nodePos))
      {
    ihElem = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
      }

      // Compare priorities of node() and text()
      final TestSeq textTest = _testSeq[DOM.TEXT];
      double textPrio = (0 - Double.MAX_VALUE);
      int    textPos  = Integer.MIN_VALUE;

      if (textTest != null) {
    textPrio = textTest.getPriority();
    textPos  = textTest.getPosition();
      }
      if (textPrio == Double.NaN || textPrio < nodePrio ||
          (textPrio == nodePrio && textPos < nodePos))
      {
    ihText = _childNodeTestSeq.compile(classGen, methodGen, ihLoop);
    _testSeq[DOM.TEXT] = _childNodeTestSeq;
      }
  }

  // Handle templates with "ns:*" pattern
  InstructionHandle elemNamespaceHandle = ihElem;
  InstructionList nsElem = compileNamespaces(classGen, methodGen,
               isNamespace, isAttribute,
               false, ihElem);
  if (nsElem != null) elemNamespaceHandle = nsElem.getStart();

  // Handle templates with "ns:@*" pattern
  InstructionHandle attrNamespaceHandle = ihAttr;
  InstructionList nsAttr = compileNamespaces(classGen, methodGen,
               isNamespace, isAttribute,
               true, ihAttr);
  if (nsAttr != null) attrNamespaceHandle = nsAttr.getStart();

  // Handle templates with "ns:elem" or "ns:@attr" pattern
  final InstructionHandle[] targets = new InstructionHandle[types.length];
  for (int i = DOM.NTYPES; i < targets.length; i++) {
      final TestSeq testSeq = _testSeq[i];
View Full Code Here

  throw new TypeCheckError(this);
    }

    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = methodGen.getInstructionList();

  // Save current node and current iterator on the stack
  il.append(methodGen.loadCurrentNode());
  il.append(methodGen.loadIterator());
   
  // Collect sort objects associated with this instruction
  final Vector sortObjects = new Vector();
  Enumeration children = elements();
  while (children.hasMoreElements()) {
      final Object child = children.nextElement();
      if (child instanceof Sort) {
    sortObjects.addElement(child);
      }
  }

  if ((_type != null) && (_type instanceof ResultTreeType)) {
      // Store existing DOM on stack - must be restored when loop is done
      il.append(methodGen.loadDOM());

      // <xsl:sort> cannot be applied to a result tree - issue warning
      if (sortObjects.size() > 0) {
    ErrorMsg msg = new ErrorMsg(ErrorMsg.RESULT_TREE_SORT_ERR,this);
    getParser().reportError(WARNING, msg);
      }

      // Put the result tree on the stack (DOM)
      _select.translate(classGen, methodGen);
      // Get an iterator for the whole DOM - excluding the root node
      _type.translateTo(classGen, methodGen, Type.NodeSet);
      // Store the result tree as the default DOM
      il.append(SWAP);
      il.append(methodGen.storeDOM());
  }
  else {
      // Compile node iterator
      if (sortObjects.size() > 0) {
    Sort.translateSortIterator(classGen, methodGen,
             _select, sortObjects);
      }
      else {
    _select.translate(classGen, methodGen);
      }

      if (_type instanceof ReferenceType == false) {
    _select.startResetIterator(classGen, methodGen);
      }
  }


  // Overwrite current iterator
  il.append(methodGen.storeIterator());

  // Give local variables (if any) default values before starting loop
  initializeVariables(classGen, methodGen);

  final BranchHandle nextNode = il.append(new GOTO(null));
  final InstructionHandle loop = il.append(NOP);

  translateContents(classGen, methodGen);
       
  nextNode.setTarget(il.append(methodGen.loadIterator()));
  il.append(methodGen.nextNode());
  il.append(DUP);
  il.append(methodGen.storeCurrentNode());
  il.append(new IFNE(loop));

  // Restore current DOM (if result tree was used instead for this loop)
  if ((_type != null) && (_type instanceof ResultTreeType)) {
      il.append(methodGen.storeDOM());     
  }

  // Restore current node and current iterator from the stack
  il.append(methodGen.storeIterator());
  il.append(methodGen.storeCurrentNode());
    }
View Full Code Here

  // Do nothing if other <xsl:output> element has higher precedence
  if (_disabled) return;

  ConstantPoolGen cpg = classGen.getConstantPool();
  InstructionList il = methodGen.getInstructionList();

  int field = 0;
        il.append(classGen.loadTranslet());

  // Only update _version field if set and different from default
  if ((_version != null) && (!_version.equals(XML_VERSION))) {
      field = cpg.addFieldref(TRANSLET_CLASS, "_version", STRING_SIG);
      il.append(DUP);
      il.append(new PUSH(cpg, _version));
      il.append(new PUTFIELD(field));
  }

  // Only update _method field if "method" attribute used
  if (_method != null) {
      field = cpg.addFieldref(TRANSLET_CLASS, "_method", STRING_SIG);
      il.append(DUP);
      il.append(new PUSH(cpg, _method));
      il.append(new PUTFIELD(field));
  }

  // Only update if _encoding field is "encoding" attribute used
  if (_encoding != null) {
      field = cpg.addFieldref(TRANSLET_CLASS, "_encoding", STRING_SIG);
      il.append(DUP);
      il.append(new PUSH(cpg, _encoding));
      il.append(new PUTFIELD(field));
  }

  // Only update if "omit-xml-declaration" used and set to 'yes'
  if (_omitHeader) {
      field = cpg.addFieldref(TRANSLET_CLASS, "_omitHeader", "Z");
      il.append(DUP);
      il.append(new PUSH(cpg, _omitHeader));
      il.append(new PUTFIELD(field));
  }

  // Add 'standalone' decaration to output - use text as is
  if (_standalone != null) {
      field = cpg.addFieldref(TRANSLET_CLASS, "_standalone", STRING_SIG);
      il.append(DUP);
      il.append(new PUSH(cpg, _standalone));
      il.append(new PUTFIELD(field));
  }

  // Set system/public doctype only if both are set
  field = cpg.addFieldref(TRANSLET_CLASS,"_doctypeSystem",STRING_SIG);
  il.append(DUP);
  il.append(new PUSH(cpg, _doctypeSystem));
  il.append(new PUTFIELD(field));
  field = cpg.addFieldref(TRANSLET_CLASS,"_doctypePublic",STRING_SIG);
  il.append(DUP);
  il.append(new PUSH(cpg, _doctypePublic));
  il.append(new PUTFIELD(field));
 
  // Add 'medye-type' decaration to output - if used
  if (_mediaType != null) {
      field = cpg.addFieldref(TRANSLET_CLASS, "_mediaType", STRING_SIG);
      il.append(DUP);
      il.append(new PUSH(cpg, _mediaType));
      il.append(new PUTFIELD(field));
  }

  // Compile code to set output indentation on/off
  if (_indent) {
      field = cpg.addFieldref(TRANSLET_CLASS, "_indent", "Z");
      il.append(DUP);
      il.append(new PUSH(cpg, _indent));
      il.append(new PUTFIELD(field));
  }

  // Forward to the translet any elements that should be output as CDATA
  if (_cdata != null) {
      int index = cpg.addMethodref(TRANSLET_CLASS,
           "addCdataElement",
           "(Ljava/lang/String;)V");

      StringTokenizer tokens = new StringTokenizer(_cdata);
      while (tokens.hasMoreTokens()) {
    il.append(DUP);
    il.append(new PUSH(cpg, tokens.nextToken()));
    il.append(new INVOKEVIRTUAL(index));
      }
  }
  il.append(POP); // Cleanup - pop last translet reference off stack
    }
View Full Code Here

          cpg.addUtf8(var.getVariable()),
          cpg.addUtf8(var.getType().toSignature()),
          null, cpg.getConstantPool()));
  }

  final InstructionList il = new InstructionList();
  testGen = new TestGenerator(ACC_PUBLIC | ACC_FINAL,
            org.apache.bcel.generic.Type.BOOLEAN,
            new org.apache.bcel.generic.Type[] {
          org.apache.bcel.generic.Type.INT,
          org.apache.bcel.generic.Type.INT,
          org.apache.bcel.generic.Type.INT,
          org.apache.bcel.generic.Type.INT,
          Util.getJCRefType(TRANSLET_SIG),
          Util.getJCRefType(NODE_ITERATOR_SIG)
            },
            new String[] {
          "node",
          "position",
          "last",
          "current",
          "translet",
          "iterator"
            },
            "test", _className, il, cpg);
   
  // Store the dom in a local variable
  local = testGen.addLocalVariable("document",
           Util.getJCRefType(DOM_INTF_SIG),
           null, null);
  final String className = classGen.getClassName();
  il.append(filterGen.loadTranslet());
  il.append(new CHECKCAST(cpg.addClass(className)));
  il.append(new GETFIELD(cpg.addFieldref(className,
                 DOM_FIELD, DOM_INTF_SIG)));
  il.append(new ASTORE(local.getIndex()));

  // Store the dom index in the test generator
  testGen.setDomIndex(local.getIndex());

  _exp.translate(filterGen, testGen);
  il.append(IRETURN);
 
  testGen.stripAttributes(true);
  testGen.setMaxLocals();
  testGen.setMaxStack();
  testGen.removeNOPs();
View Full Code Here

     * filter object and a reference to the predicate's closure.
     */
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {

  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = methodGen.getInstructionList();

  if (_nthPositionFilter || _nthDescendant) {
      _exp.translate(classGen, methodGen);
  }
  else if (isNodeValueTest() && (getParent() instanceof Step)) {
      _value.translate(classGen, methodGen);
      il.append(new CHECKCAST(cpg.addClass(STRING_CLASS)));
      il.append(new PUSH(cpg, ((EqualityExpr)_exp).getOp()));
  }
  else {
      translateFilter(classGen, methodGen);
  }
    }
View Full Code Here

     */
    public void translateFilter(ClassGenerator classGen,
        MethodGenerator methodGen)
    {
  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = methodGen.getInstructionList();

  // Compile auxiliary class for filter
  compileFilter(classGen, methodGen);
 
  // Create new instance of filter
  il.append(new NEW(cpg.addClass(_className)));
  il.append(DUP);
  il.append(new INVOKESPECIAL(cpg.addMethodref(_className,
                 "<init>", "()V")));

  // Initialize closure variables
  final int length = (_closureVars == null) ? 0 : _closureVars.size();

  for (int i = 0; i < length; i++) {
      VariableRefBase varRef = (VariableRefBase) _closureVars.get(i);
      VariableBase var = varRef.getVariable();
      Type varType = var.getType();

      il.append(DUP);

      // Find nearest closure implemented as an inner class
      Closure variableClosure = _parentClosure;
      while (variableClosure != null) {
    if (variableClosure.inInnerClass()) break;
    variableClosure = variableClosure.getParentClosure();
      }

      // Use getfield if in an inner class
      if (variableClosure != null) {
    il.append(ALOAD_0);
    il.append(new GETFIELD(
        cpg.addFieldref(variableClosure.getInnerClassName(),
      var.getVariable(), varType.toSignature())));
      }
      else {
    // Use a load of instruction if in translet class
    il.append(var.loadInstruction());
      }

      // Store variable in new closure
      il.append(new PUTFIELD(
        cpg.addFieldref(_className, var.getVariable(),
      varType.toSignature())));
  }
    }
View Full Code Here

  while (templates.hasMoreElements()) {
      final Template template = (Template)templates.nextElement();
      final int prec = template.getImportPrecedence();
      if ((prec >= min) && (prec < max)) {
    if (template.hasContents()) {
        InstructionList til = template.compile(classGen, methodGen);
        til.append(new GOTO_W(next));
        _templateILs.put(template, til);
        _templateIHs.put(template, til.getStart());
    }
    else {
        // empty template
        _templateIHs.put(template, next);
    }
View Full Code Here

TOP

Related Classes of org.apache.bcel.generic.InstructionList

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.