📄 whitespace.java
字号:
// Skip to next rule (there might not be any)... idx++; } } // The rules vector could be empty if first rule has strength RULE_ALL if (rules.size() == 0) { return defaultAction; } // Now work backwards and strip away all rules that have the same // action as the default rule (no reason the check them at the end). do { currentRule = (WhitespaceRule)rules.lastElement(); if (currentRule.getAction() == defaultAction) { rules.removeElementAt(rules.size() - 1); } else { break; } } while (rules.size() > 0); // Signal that whitespace detection predicate must be used. return defaultAction; } public static void compileStripSpace(BranchHandle strip[], int sCount, InstructionList il) { final InstructionHandle target = il.append(ICONST_1); il.append(IRETURN); for (int i = 0; i < sCount; i++) { strip[i].setTarget(target); } } public static void compilePreserveSpace(BranchHandle preserve[], int pCount, InstructionList il) { final InstructionHandle target = il.append(ICONST_0); il.append(IRETURN); for (int i = 0; i < pCount; i++) { preserve[i].setTarget(target); } } /* private static void compileDebug(ClassGenerator classGen, InstructionList il) { final ConstantPoolGen cpg = classGen.getConstantPool(); final int prt = cpg.addMethodref("java/lang/System/out", "println", "(Ljava/lang/String;)V"); il.append(DUP); il.append(new INVOKESTATIC(prt)); } */ /** * Compiles the predicate method */ private static void compilePredicate(Vector rules, int defaultAction, ClassGenerator classGen) { 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()); } /** * Compiles the predicate method */ private static void compileDefault(int defaultAction, ClassGenerator classGen) { 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()); } /** * Takes a vector of WhitespaceRule objects and generates a predicate * method. This method returns the translets default action for handling * whitespace text-nodes: * - USE_PREDICATE (run the method generated by this method) * - STRIP_SPACE (always strip whitespace text-nodes) * - PRESERVE_SPACE (always preserve whitespace text-nodes) */ public static int translateRules(Vector rules, ClassGenerator classGen) { // Get the core rules in prioritized order final int defaultAction = prioritizeRules(rules); // The rules vector may be empty after prioritising if (rules.size() == 0) { compileDefault(defaultAction,classGen); return defaultAction; } // Now - create a predicate method and sequence through rules... compilePredicate(rules, defaultAction, classGen); // Return with the translets required action ( return USE_PREDICATE; } /** * Sorts a range of rules with regard to PRIORITY only */ private static void quicksort(Vector rules, int p, int r) { while (p < r) { final int q = partition(rules, p, r); quicksort(rules, p, q); p = q + 1; } } /** * Used with quicksort method above */ private static int partition(Vector rules, int p, int r) { final WhitespaceRule x = (WhitespaceRule)rules.elementAt((p+r) >>> 1); int i = p - 1, j = r + 1; while (true) { while (x.compareTo((WhitespaceRule)rules.elementAt(--j)) < 0) { } while (x.compareTo((WhitespaceRule)rules.elementAt(++i)) > 0) { } if (i < j) { final WhitespaceRule tmp = (WhitespaceRule)rules.elementAt(i); rules.setElementAt(rules.elementAt(j), i); rules.setElementAt(tmp, j); } else { return j; } } } /** * Type-check contents/attributes - nothing to do... */ public Type typeCheck(SymbolTable stable) throws TypeCheckError { return Type.Void; // We don't return anything. } /** * This method should not produce any code */ public void translate(ClassGenerator classGen, MethodGenerator methodGen) { }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -