Examples of MethodGenerator


Examples of com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator

  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = new InstructionList();
  final XSLTC xsltc = classGen.getParser().getXSLTC();

  // private boolean Translet.stripSpace(int type) - cannot be static
  final MethodGenerator stripSpace =
      new MethodGenerator(ACC_PUBLIC | ACC_FINAL ,
      com.sun.org.apache.bcel.internal.generic.Type.BOOLEAN,
      new com.sun.org.apache.bcel.internal.generic.Type[] {
          Util.getJCRefType(DOM_INTF_SIG),
          com.sun.org.apache.bcel.internal.generic.Type.INT,
          com.sun.org.apache.bcel.internal.generic.Type.INT
      },
      new String[] { "dom","node","type" },
      "stripSpace",classGen.getClassName(),il,cpg);

  classGen.addInterface("com/sun/org/apache/xalan/internal/xsltc/StripFilter");

  final int paramDom = stripSpace.getLocalIndex("dom");
  final int paramCurrent = stripSpace.getLocalIndex("node");
  final int paramType = stripSpace.getLocalIndex("type");

  BranchHandle strip[] = new BranchHandle[rules.size()];
  BranchHandle preserve[] = new BranchHandle[rules.size()];
  int sCount = 0;
  int pCount = 0;

  // Traverse all strip/preserve rules
  for (int i = 0; i<rules.size(); i++) {
      // Get the next rule in the prioritised list
      WhitespaceRule rule = (WhitespaceRule)rules.elementAt(i);

      // Returns the namespace for a node in the DOM
      final int gns = cpg.addInterfaceMethodref(DOM_INTF,
                  "getNamespaceName",
                  "(I)Ljava/lang/String;");

      final int strcmp = cpg.addMethodref("java/lang/String",
            "compareTo",
            "(Ljava/lang/String;)I");

      // Handle elements="ns:*" type rule
      if (rule.getStrength() == RULE_NAMESPACE) {
    il.append(new ALOAD(paramDom));
    il.append(new ILOAD(paramCurrent));
    il.append(new INVOKEINTERFACE(gns,2));
    il.append(new PUSH(cpg, rule.getNamespace()));
    il.append(new INVOKEVIRTUAL(strcmp));
    il.append(ICONST_0);

    if (rule.getAction() == STRIP_SPACE) {
        strip[sCount++] = il.append(new IF_ICMPEQ(null));
    }
    else {
        preserve[pCount++] = il.append(new IF_ICMPEQ(null));
    }
      }
      // Handle elements="ns:el" type rule
      else if (rule.getStrength() == RULE_ELEMENT) {
    // Create the QName for the element
    final Parser parser = classGen.getParser();
    QName qname;
    if (rule.getNamespace() != Constants.EMPTYSTRING )
        qname = parser.getQName(rule.getNamespace(), null,
              rule.getElement());
    else
        qname = parser.getQName(rule.getElement());

    // Register the element.
    final int elementType = xsltc.registerElement(qname);
    il.append(new ILOAD(paramType));
    il.append(new PUSH(cpg, elementType));

    // Compare current node type with wanted element type
    if (rule.getAction() == STRIP_SPACE)
        strip[sCount++] = il.append(new IF_ICMPEQ(null));
    else
        preserve[pCount++] = il.append(new IF_ICMPEQ(null));
      }
  }

  if (defaultAction == STRIP_SPACE) {
      compileStripSpace(strip, sCount, il);
      compilePreserveSpace(preserve, pCount, il);
  }
  else {
      compilePreserveSpace(preserve, pCount, il);
      compileStripSpace(strip, sCount, il);
  }

  stripSpace.stripAttributes(true);
  stripSpace.setMaxLocals();
  stripSpace.setMaxStack();
  stripSpace.removeNOPs();

  classGen.addMethod(stripSpace.getMethod());
    }
View Full Code Here

Examples of com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator

  final ConstantPoolGen cpg = classGen.getConstantPool();
  final InstructionList il = new InstructionList();
  final XSLTC xsltc = classGen.getParser().getXSLTC();

  // private boolean Translet.stripSpace(int type) - cannot be static
  final MethodGenerator stripSpace =
      new MethodGenerator(ACC_PUBLIC | ACC_FINAL ,
      com.sun.org.apache.bcel.internal.generic.Type.BOOLEAN,
      new com.sun.org.apache.bcel.internal.generic.Type[] {
          Util.getJCRefType(DOM_INTF_SIG),
          com.sun.org.apache.bcel.internal.generic.Type.INT,
          com.sun.org.apache.bcel.internal.generic.Type.INT
      },
      new String[] { "dom","node","type" },
      "stripSpace",classGen.getClassName(),il,cpg);

  classGen.addInterface("com/sun/org/apache/xalan/internal/xsltc/StripFilter");

  if (defaultAction == STRIP_SPACE)
      il.append(ICONST_1);
  else
      il.append(ICONST_0);
  il.append(IRETURN);

  stripSpace.stripAttributes(true);
  stripSpace.setMaxLocals();
  stripSpace.setMaxStack();
  stripSpace.removeNOPs();

  classGen.addMethod(stripSpace.getMethod());
    }
View Full Code Here

Examples of com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator

  argNames[5] = "lang";
  argNames[6] = "case_order";


        InstructionList il = new InstructionList();
        final MethodGenerator constructor =
            new MethodGenerator(ACC_PUBLIC,
                                com.sun.org.apache.bcel.internal.generic.Type.VOID,
                                argTypes, argNames, "<init>",
                                className, il, cpg);

        // Push all parameters onto the stack and called super.<init>()
        il.append(ALOAD_0);
        il.append(ALOAD_1);
        il.append(ALOAD_2);
        il.append(new ALOAD(3));
        il.append(new ALOAD(4));
        il.append(new ALOAD(5));
  il.append(new ALOAD(6));
  il.append(new ALOAD(7));
        il.append(new INVOKESPECIAL(cpg.addMethodref(NODE_SORT_FACTORY,
            "<init>",
            "(" + DOM_INTF_SIG
                + STRING_SIG
                + TRANSLET_INTF_SIG
                + "[" + STRING_SIG
    + "[" + STRING_SIG
    + "[" + STRING_SIG
                + "[" + STRING_SIG + ")V")));
        il.append(RETURN);

        // Override the definition of makeNodeSortRecord()
        il = new InstructionList();
        final MethodGenerator makeNodeSortRecord =
            new MethodGenerator(ACC_PUBLIC,
                Util.getJCRefType(NODE_SORT_RECORD_SIG),
                new com.sun.org.apache.bcel.internal.generic.Type[] {
                    com.sun.org.apache.bcel.internal.generic.Type.INT,
                    com.sun.org.apache.bcel.internal.generic.Type.INT },
                new String[] { "node", "last" }, "makeNodeSortRecord",
                className, il, cpg);

        il.append(ALOAD_0);
        il.append(ILOAD_1);
        il.append(ILOAD_2);
        il.append(new INVOKESPECIAL(cpg.addMethodref(NODE_SORT_FACTORY,
            "makeNodeSortRecord", "(II)" + NODE_SORT_RECORD_SIG)));
        il.append(DUP);
        il.append(new CHECKCAST(cpg.addClass(sortRecordClass)));

        // Initialize closure in record class
        final int ndups = dups.size();
        for (int i = 0; i < ndups; i++) {
            final VariableRefBase varRef = (VariableRefBase) dups.get(i);
            final VariableBase var = varRef.getVariable();
            final Type varType = var.getType();

            il.append(DUP);

            // Get field from factory class
            il.append(ALOAD_0);
            il.append(new GETFIELD(
                cpg.addFieldref(className,
                    var.getEscapedName(), varType.toSignature())));

            // Put field in record class
            il.append(new PUTFIELD(
                cpg.addFieldref(sortRecordClass,
                    var.getEscapedName(), varType.toSignature())));
        }
        il.append(POP);
        il.append(ARETURN);

        constructor.setMaxLocals();
        constructor.setMaxStack();
        sortRecordFactory.addMethod(constructor);
        makeNodeSortRecord.setMaxLocals();
        makeNodeSortRecord.setMaxStack();
        sortRecordFactory.addMethod(makeNodeSortRecord);
        xsltc.dumpClass(sortRecordFactory.getJavaClass());

        return className;
    }
View Full Code Here

Examples of com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator

                                    null, cpg.getConstantPool()));
                dups.add(varRef);
            }
        }

        MethodGenerator init = compileInit(sortObjects, sortRecord,
                                         cpg, className);
        MethodGenerator extract = compileExtract(sortObjects, sortRecord,
                                        cpg, className);
        sortRecord.addMethod(init);
        sortRecord.addMethod(extract);

        xsltc.dumpClass(sortRecord.getJavaClass());
View Full Code Here

Examples of com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator

                                           NodeSortRecordGenerator sortRecord,
                                           ConstantPoolGen cpg,
                                           String className)
    {
        final InstructionList il = new InstructionList();
        final MethodGenerator init =
            new MethodGenerator(ACC_PUBLIC,
                                com.sun.org.apache.bcel.internal.generic.Type.VOID,
                                null, null, "<init>", className,
                                il, cpg);

        // Call the constructor in the NodeSortRecord superclass
View Full Code Here

Examples of com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator

            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");
    }
View Full Code Here

Examples of org.apache.xalan.xsltc.compiler.util.MethodGenerator

  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];
      // Jump straight to namespace tests ?
      if (isNamespace[i]) {
    if (isAttribute[i])
        targets[i] = attrNamespaceHandle;
    else
        targets[i] = elemNamespaceHandle;
      }
      // Test first, then jump to namespace tests
      else if (testSeq != null) {
    if (isAttribute[i])
        targets[i] = testSeq.compile(classGen, methodGen,
             attrNamespaceHandle);
    else
        targets[i] = testSeq.compile(classGen, methodGen,
             elemNamespaceHandle);
      }
      else {
    targets[i] = ihLoop;
      }
  }


  // Handle pattern with match on root node - default: traverse children
  targets[DOM.ROOT] = _rootPattern != null
      ? getTemplateInstructionHandle(_rootPattern.getTemplate())
      : ihRecurse;
 
  // Handle any pattern with match on text nodes - default: output text
  targets[DOM.TEXT] = _testSeq[DOM.TEXT] != null
      ? _testSeq[DOM.TEXT].compile(classGen, methodGen, ihText)
      : ihText;

  // This DOM-type is not in use - default: process next node
  targets[DOM.NAMESPACE] = ihLoop;

  // Match unknown element in DOM - default: check for namespace match
  targets[DOM.ELEMENT] = elemNamespaceHandle;

  // Match unknown attribute in DOM - default: check for namespace match
  targets[DOM.ATTRIBUTE] = attrNamespaceHandle;

  // Match on processing instruction - default: process next node
  InstructionHandle ihPI = ihLoop;
  if (_childNodeTestSeq != null) ihPI = ihElem;
  if (_testSeq[DOM.PROCESSING_INSTRUCTION] != null)
      targets[DOM.PROCESSING_INSTRUCTION] =
    _testSeq[DOM.PROCESSING_INSTRUCTION].
    compile(classGen, methodGen, ihPI);
  else
      targets[DOM.PROCESSING_INSTRUCTION] = ihPI;
 
  // Match on comments - default: process next node
  InstructionHandle ihComment = ihLoop;
  if (_childNodeTestSeq != null) ihComment = ihElem;
  targets[DOM.COMMENT] = _testSeq[DOM.COMMENT] != null
      ? _testSeq[DOM.COMMENT].compile(classGen, methodGen, ihComment)
      : ihComment;

  // Now compile test sequences for various match patterns:
  for (int i = DOM.NTYPES; i < targets.length; i++) {
      final TestSeq testSeq = _testSeq[i];
      // Jump straight to namespace tests ?
      if ((testSeq == null) || (isNamespace[i])) {
    if (isAttribute[i])
        targets[i] = attrNamespaceHandle;
    else
        targets[i] = elemNamespaceHandle;
      }
      // Match on node type
      else {
    if (isAttribute[i])
        targets[i] = testSeq.compile(classGen, methodGen,
             attrNamespaceHandle);
    else
        targets[i] = testSeq.compile(classGen, methodGen,
             elemNamespaceHandle);
      }
  }

  if (ilKey != null) body.insert(ilKey);

  // Append first code in applyTemplates() - get type of current node
  final int getType = cpg.addInterfaceMethodref(DOM_INTF,
                  "getType", "(I)I");
  body.append(methodGen.loadDOM());
  body.append(new ILOAD(_currentIndex));
  body.append(new INVOKEINTERFACE(getType, 2));

  // Append switch() statement - main dispatch loop in applyTemplates()
  InstructionHandle disp = body.append(new SWITCH(types, targets, ihLoop));

  // Append all the "case:" statements
  appendTestSequences(body);
  // Append the actual template code
  appendTemplateCode(body);

  // Append NS:* node tests (if any)
  if (nsElem != null) body.append(nsElem);
  // Append NS:@* node tests (if any)
  if (nsAttr != null) body.append(nsAttr);

  // Append default action for element and root nodes
  body.append(ilRecurse);
  // Append default action for text and attribute nodes
  body.append(ilText);

  // putting together constituent instruction lists
  mainIL.append(new GOTO_W(ihLoop));
  mainIL.append(body);
  // fall through to ilLoop
  mainIL.append(ilLoop);

  peepHoleOptimization(methodGen);
  methodGen.stripAttributes(true);
 
  methodGen.setMaxLocals();
  methodGen.setMaxStack();
  methodGen.removeNOPs();
  classGen.addMethod(methodGen.getMethod());

  // Compile method(s) for <xsl:apply-imports/> for this mode
  if (_importLevels != null) {
      Enumeration levels = _importLevels.keys();
      while (levels.hasMoreElements()) {
View Full Code Here

Examples of org.apache.xalan.xsltc.compiler.util.MethodGenerator

  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()+'_'+max,
        getClassName(), mainIL,
        classGen.getConstantPool());
  methodGen.addException("org.apache.xalan.xsltc.TransletException");

  // Create the 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;
  }

  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
  compileTemplateCalls(classGen, methodGen, ihLoop, min, max);

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

  // Handle template with explicit "@*" pattern
  final TestSeq attrTest = _testSeq[DOM.ATTRIBUTE];
  InstructionHandle ihAttr = ihLoop;
  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
  InstructionList nsAttr = compileNamespaces(classGen, methodGen,
               isNamespace, isAttribute,
               true, ihAttr);
  InstructionHandle attrNamespaceHandle = 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];
      // Jump straight to namespace tests ?
      if (isNamespace[i]) {
    if (isAttribute[i])
        targets[i] = attrNamespaceHandle;
    else
        targets[i] = elemNamespaceHandle;
      }
      // Test first, then jump to namespace tests
      else if (testSeq != null) {
    if (isAttribute[i])
        targets[i] = testSeq.compile(classGen, methodGen,
             attrNamespaceHandle);
    else
        targets[i] = testSeq.compile(classGen, methodGen,
             elemNamespaceHandle);
      }
      else {
    targets[i] = ihLoop;
      }
  }

  // Handle pattern with match on root node - default: traverse children
  targets[DOM.ROOT] = _rootPattern != null
      ? getTemplateInstructionHandle(_rootPattern.getTemplate())
      : ihRecurse;
 
  // Handle any pattern with match on text nodes - default: loop
  targets[DOM.TEXT] = _testSeq[DOM.TEXT] != null
      ? _testSeq[DOM.TEXT].compile(classGen, methodGen, ihText)
      : ihText;

  // This DOM-type is not in use - default: process next node
  targets[DOM.NAMESPACE] = ihLoop;

  // Match unknown element in DOM - default: check for namespace match
  targets[DOM.ELEMENT] = elemNamespaceHandle;

  // Match unknown attribute in DOM - default: check for namespace match
  targets[DOM.ATTRIBUTE] = attrNamespaceHandle;

  // Match on processing instruction - default: loop
  InstructionHandle ihPI = ihLoop;
  if (_childNodeTestSeq != null) ihPI = ihElem;
  if (_testSeq[DOM.PROCESSING_INSTRUCTION] != null) {
      targets[DOM.PROCESSING_INSTRUCTION] =
    _testSeq[DOM.PROCESSING_INSTRUCTION].
    compile(classGen, methodGen, ihPI);
  }
  else {
      targets[DOM.PROCESSING_INSTRUCTION] = ihPI;
  }
 
  // Match on comments - default: process next node
  InstructionHandle ihComment = ihLoop;
  if (_childNodeTestSeq != null) ihComment = ihElem;
  targets[DOM.COMMENT] = _testSeq[DOM.COMMENT] != null
      ? _testSeq[DOM.COMMENT].compile(classGen, methodGen, ihComment)
      : ihComment;

  // Now compile test sequences for various match patterns:
  for (int i = DOM.NTYPES; i < targets.length; i++) {
      final TestSeq testSeq = _testSeq[i];
      // Jump straight to namespace tests ?
      if ((testSeq == null) || (isNamespace[i])) {
    if (isAttribute[i])
        targets[i] = attrNamespaceHandle;
    else
        targets[i] = elemNamespaceHandle;
      }
      // Match on node type
      else {
    if (isAttribute[i])
        targets[i] = testSeq.compile(classGen, methodGen,
             attrNamespaceHandle);
    else
        targets[i] = testSeq.compile(classGen, methodGen,
             elemNamespaceHandle);
      }
  }

  if (ilKey != null) body.insert(ilKey);

  // Append first code in applyTemplates() - get type of current node
  final int getType = cpg.addInterfaceMethodref(DOM_INTF,
                  "getType", "(I)I");
  body.append(methodGen.loadDOM());
  body.append(new ILOAD(_currentIndex));
  body.append(new INVOKEINTERFACE(getType, 2));

  // Append switch() statement - main dispatch loop in applyTemplates()
  InstructionHandle disp = body.append(new SWITCH(types,targets,ihLoop));

  // Append all the "case:" statements
  appendTestSequences(body);
  // Append the actual template code
  appendTemplateCode(body);

  // Append NS:* node tests (if any)
  if (nsElem != null) body.append(nsElem);
  // Append NS:@* node tests (if any)
  if (nsAttr != null) body.append(nsAttr);

  // Append default action for element and root nodes
  body.append(ilRecurse);
  // Append default action for text and attribute nodes
  body.append(ilText);

  // putting together constituent instruction lists
  mainIL.append(new GOTO_W(ihLoop));
  mainIL.append(body);
  // fall through to ilLoop
  mainIL.append(ilLoop);

  peepHoleOptimization(methodGen);
  methodGen.stripAttributes(true);
 
  methodGen.setMaxLocals();
  methodGen.setMaxStack();
  methodGen.removeNOPs();
  classGen.addMethod(methodGen.getMethod());

  // Restore original (complete) set of templates for this transformation
  _templates = oldTemplates;
    }
View Full Code Here

Examples of org.apache.xalan.xsltc.compiler.util.MethodGenerator

  argNames[5] = "lang";
  argNames[6] = "case_order";
 

  InstructionList il = new InstructionList();
  final MethodGenerator constructor =
      new MethodGenerator(ACC_PUBLIC,
        org.apache.bcel.generic.Type.VOID,
        argTypes, argNames, "<init>",
        className, il, cpg);

  // Push all parameters onto the stack and called super.<init>()
  il.append(ALOAD_0);
  il.append(ALOAD_1);
  il.append(ALOAD_2);
  il.append(new ALOAD(3));
  il.append(new ALOAD(4));
  il.append(new ALOAD(5));
  il.append(new ALOAD(6));
  il.append(new ALOAD(7));
  il.append(new INVOKESPECIAL(cpg.addMethodref(NODE_SORT_FACTORY,
      "<init>",
      "(" + DOM_INTF_SIG
    + STRING_SIG
    + TRANSLET_INTF_SIG
    + "[" + STRING_SIG
    + "[" + STRING_SIG
    + "[" + STRING_SIG
    + "[" + STRING_SIG + ")V")));
  il.append(RETURN);

  // Override the definition of makeNodeSortRecord()
  il = new InstructionList();
  final MethodGenerator makeNodeSortRecord =
      new MethodGenerator(ACC_PUBLIC,
    Util.getJCRefType(NODE_SORT_RECORD_SIG),
    new org.apache.bcel.generic.Type[] {
        org.apache.bcel.generic.Type.INT,
        org.apache.bcel.generic.Type.INT },
    new String[] { "node", "last" }, "makeNodeSortRecord",
    className, il, cpg);

  il.append(ALOAD_0);
  il.append(ILOAD_1);
  il.append(ILOAD_2);
  il.append(new INVOKESPECIAL(cpg.addMethodref(NODE_SORT_FACTORY,
      "makeNodeSortRecord", "(II)" + NODE_SORT_RECORD_SIG)));
  il.append(DUP);
  il.append(new CHECKCAST(cpg.addClass(sortRecordClass)));

  // Initialize closure in record class
  final int ndups = dups.size();
  for (int i = 0; i < ndups; i++) {
      final VariableRefBase varRef = (VariableRefBase) dups.get(i);
      final VariableBase var = varRef.getVariable();
      final Type varType = var.getType();
     
      il.append(DUP);

      // Get field from factory class
      il.append(ALOAD_0);
      il.append(new GETFIELD(
    cpg.addFieldref(className,
        var.getEscapedName(), varType.toSignature())));

      // Put field in record class
      il.append(new PUTFIELD(
    cpg.addFieldref(sortRecordClass,
        var.getEscapedName(), varType.toSignature())));
  }
  il.append(POP);
  il.append(ARETURN);

  constructor.setMaxLocals();
  constructor.setMaxStack();
  sortRecordFactory.addMethod(constructor);
  makeNodeSortRecord.setMaxLocals();
  makeNodeSortRecord.setMaxStack();
  sortRecordFactory.addMethod(makeNodeSortRecord);
  xsltc.dumpClass(sortRecordFactory.getJavaClass());

  return className;
    }
View Full Code Here

Examples of org.apache.xalan.xsltc.compiler.util.MethodGenerator

            null, cpg.getConstantPool()));
    dups.add(varRef);
      }
  }

  MethodGenerator init = compileInit(sortObjects, sortRecord,
             cpg, className);
  MethodGenerator extract = compileExtract(sortObjects, sortRecord,
                   cpg, className);
  sortRecord.addMethod(init);
  sortRecord.addMethod(extract);

  xsltc.dumpClass(sortRecord.getJavaClass());
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.