1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.compiler; 21 22 import java.util.Dictionary ; 23 import java.util.Vector ; 24 25 import com.sun.org.apache.bcel.internal.generic.GOTO_W; 26 import com.sun.org.apache.bcel.internal.generic.InstructionHandle; 27 import com.sun.org.apache.bcel.internal.generic.InstructionList; 28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator; 29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator; 30 31 47 final class TestSeq { 48 49 52 private int _kernelType; 53 54 58 private Vector _patterns = null; 59 60 63 private Mode _mode = null; 64 65 68 private Template _default = null; 69 70 73 private InstructionList _instructionList; 74 75 78 private InstructionHandle _start = null; 79 80 83 public TestSeq(Vector patterns, Mode mode) { 84 this(patterns, -2, mode); 85 } 86 87 public TestSeq(Vector patterns, int kernelType, Mode mode) { 88 _patterns = patterns; 89 _kernelType = kernelType; 90 _mode = mode; 91 } 92 93 98 public String toString() { 99 final int count = _patterns.size(); 100 final StringBuffer result = new StringBuffer (); 101 102 for (int i = 0; i < count; i++) { 103 final LocationPathPattern pattern = 104 (LocationPathPattern) _patterns.elementAt(i); 105 106 if (i == 0) { 107 result.append("Testseq for kernel " + _kernelType) 108 .append('\n'); 109 } 110 result.append(" pattern " + i + ": ") 111 .append(pattern.toString()) 112 .append('\n'); 113 } 114 return result.toString(); 115 } 116 117 120 public InstructionList getInstructionList() { 121 return _instructionList; 122 } 123 124 129 public double getPriority() { 130 final Template template = (_patterns.size() == 0) ? _default 131 : ((Pattern) _patterns.elementAt(0)).getTemplate(); 132 return template.getPriority(); 133 } 134 135 139 public int getPosition() { 140 final Template template = (_patterns.size() == 0) ? _default 141 : ((Pattern) _patterns.elementAt(0)).getTemplate(); 142 return template.getPosition(); 143 } 144 145 150 public void reduce() { 151 final Vector newPatterns = new Vector (); 152 153 final int count = _patterns.size(); 154 for (int i = 0; i < count; i++) { 155 final LocationPathPattern pattern = 156 (LocationPathPattern)_patterns.elementAt(i); 157 158 pattern.reduceKernelPattern(); 160 161 if (pattern.isWildcard()) { 163 _default = pattern.getTemplate(); 164 break; } 166 else { 167 newPatterns.addElement(pattern); 168 } 169 } 170 _patterns = newPatterns; 171 } 172 173 178 public void findTemplates(Dictionary templates) { 179 if (_default != null) { 180 templates.put(_default, this); 181 } 182 for (int i = 0; i < _patterns.size(); i++) { 183 final LocationPathPattern pattern = 184 (LocationPathPattern)_patterns.elementAt(i); 185 templates.put(pattern.getTemplate(), this); 186 } 187 } 188 189 195 private InstructionHandle getTemplateHandle(Template template) { 196 return (InstructionHandle)_mode.getTemplateInstructionHandle(template); 197 } 198 199 202 private LocationPathPattern getPattern(int n) { 203 return (LocationPathPattern)_patterns.elementAt(n); 204 } 205 206 212 public InstructionHandle compile(ClassGenerator classGen, 213 MethodGenerator methodGen, 214 InstructionHandle continuation) 215 { 216 if (_start != null) { 218 return _start; 219 } 220 221 final int count = _patterns.size(); 223 if (count == 0) { 224 return (_start = getTemplateHandle(_default)); 225 } 226 227 InstructionHandle fail = (_default == null) ? continuation 229 : getTemplateHandle(_default); 230 231 for (int n = count - 1; n >= 0; n--) { 233 final LocationPathPattern pattern = getPattern(n); 234 final Template template = pattern.getTemplate(); 235 final InstructionList il = new InstructionList(); 236 237 il.append(methodGen.loadCurrentNode()); 239 240 InstructionList ilist = _mode.getInstructionList(pattern); 242 if (ilist == null) { 243 ilist = pattern.compile(classGen, methodGen); 244 _mode.addInstructionList(pattern, ilist); 245 } 246 247 InstructionList copyOfilist = ilist.copy(); 249 250 FlowList trueList = pattern.getTrueList(); 251 if (trueList != null) { 252 trueList = trueList.copyAndRedirect(ilist, copyOfilist); 253 } 254 FlowList falseList = pattern.getFalseList(); 255 if (falseList != null) { 256 falseList = falseList.copyAndRedirect(ilist, copyOfilist); 257 } 258 259 il.append(copyOfilist); 260 261 final InstructionHandle gtmpl = getTemplateHandle(template); 263 final InstructionHandle success = il.append(new GOTO_W(gtmpl)); 264 265 if (trueList != null) { 266 trueList.backPatch(success); 267 } 268 if (falseList != null) { 269 falseList.backPatch(fail); 270 } 271 272 fail = il.getStart(); 274 275 if (_instructionList != null) { 277 il.append(_instructionList); 278 } 279 280 _instructionList = il; 282 } 283 return (_start = fail); 284 } 285 } 286 | Popular Tags |