1 28 29 package com.caucho.xsl.java; 30 31 import com.caucho.java.JavaWriter; 32 import com.caucho.xml.QName; 33 import com.caucho.xpath.pattern.AbstractPattern; 34 import com.caucho.xsl.Sort; 35 import com.caucho.xsl.XslParseException; 36 37 import java.util.ArrayList ; 38 39 42 public class XslForEach extends XslNode { 43 private String _select; 44 45 private ArrayList <XslSort> _sorts = new ArrayList <XslSort>(); 46 47 50 public void addAttribute(QName name, String value) 51 throws XslParseException 52 { 53 if (name.getName().equals("select")) { 54 _select = value; 55 } 56 else 57 super.addAttribute(name, value); 58 } 59 60 63 public void endAttributes() 64 throws XslParseException 65 { 66 70 } 71 72 75 public void addChild(XslNode node) 76 throws XslParseException 77 { 78 if (node instanceof XslSort) { 79 _sorts.add((XslSort) node); 80 } 81 else 82 super.addChild(node); 83 } 84 85 90 public void generate(JavaWriter out) 91 throws Exception 92 { 93 if (_sorts.size() != 0) { 94 printForEachSort(out); 95 return; 96 } 97 98 out.println("{"); 99 out.pushDepth(); 100 101 AbstractPattern select = parseSelect(_select); 102 103 boolean hasExprEnv = ! allowJavaSelect(select); 104 105 int id = _gen.generateId(); 106 107 String sel = "_xsl_sel" + id; 108 String oldCxt = "_xsl_cxt" + id; 109 String oldCur = "_xsl_cur" + id; 110 String oldSel = "_xsl_old_sel" + id; 111 String oldEnv = "_xsl_env" + id; 112 String oldSize = "_xsl_old_size" + id; 113 114 out.println("com.caucho.xpath.pattern.AbstractPattern " + sel + ";"); 115 out.print(sel + " = _select_patterns["); 116 out.print(_gen.addSelect(select)); 117 out.println("];"); 118 out.println("Node " + oldCxt + " = env.getContextNode();"); 119 out.println("Node " + oldCur + " = env.getCurrentNode();"); 120 121 if (! hasExprEnv) { 122 out.println("AbstractPattern " + oldSel + " = env.setSelect(node, " + sel + ");"); 123 out.println("int " + oldSize + " = env.setContextSize(0);"); 124 } 125 126 127 String iter = "_xsl_iter" + _gen.generateId(); 129 130 int oldSelectDepth = _gen.getSelectDepth(); 131 132 134 boolean hasEnv = false; 135 136 if (allowJavaSelect(select)) { 137 out.println("ExprEnvironment " + oldEnv + " = env.setExprEnv(null);"); 138 139 String ptr = printSelectBegin(out, select, true, null); 140 141 _gen.pushLoop(); 142 out.println("Node " + _gen.getElement() + " = node;"); 143 out.println("node = " + ptr + ";"); 144 } 145 else { 146 out.print("NodeIterator " + iter + " = " + sel); 147 out.println(".select(node, " + getEnv() + ");"); 148 out.println("ExprEnvironment " + oldEnv + " = env.setExprEnv(" + iter + ");"); 149 out.println("while (" + iter + ".hasNext()) {"); 150 out.pushDepth(); 151 _gen.setSelectDepth(_gen.getSelectDepth() + 1); 152 153 _gen.pushLoop(); 154 155 out.println("Node " + _gen.getElement() + " = node;"); 156 out.println("node = " + iter + ".nextNode();"); 157 158 } 159 out.println("env.setCurrentNode(node);"); 160 161 163 166 AbstractPattern oldNodeListContext = _gen.getNodeListContext(); 167 _gen.setNodeListContext(parseMatch(_select)); 168 169 generateChildren(out); 170 171 _gen.setNodeListContext(oldNodeListContext); 172 173 175 out.println("node = " + _gen.getElement() + ";"); 176 out.println("env.setCurrentNode(" + oldCur + ");"); 177 178 int selectDepth = _gen.getSelectDepth(); 179 180 for (; selectDepth > oldSelectDepth; selectDepth--) { 181 out.popDepth(); 182 out.println("}"); 183 } 184 _gen.setSelectDepth(oldSelectDepth); 185 186 out.println("env.setExprEnv(" + oldEnv + ");"); 187 188 if (! hasExprEnv) { 189 out.println("env.setSelect(" + oldCxt + ", " + oldSel + ");"); 190 out.println("env.setContextSize(" + oldSize + ");"); 191 } 193 194 out.popDepth(); 195 out.println("}"); 196 197 _gen.popLoop(); 198 } 199 200 268 269 public void printForEachSort(JavaWriter out) 270 throws Exception 271 { 272 Sort []sort = new Sort[_sorts.size()]; 273 274 for (int i = 0; i < _sorts.size(); i++) 275 sort[i] = _sorts.get(i).generateSort(); 276 277 out.println("{"); 278 out.pushDepth(); 279 280 AbstractPattern select = parseSelect(_select); 281 282 boolean hasExprEnv = ! allowJavaSelect(select); 283 284 int id = _gen.generateId(); 285 286 String sel = "_xsl_sel" + id; 287 String oldCxt = "_xsl_cxt" + id; 288 String oldCur = "_xsl_cur" + id; 289 String oldSel = "_xsl_old_sel" + id; 290 String oldEnv = "_xsl_env" + id; 291 292 out.println("env.setCurrentNode(node);"); 293 String pos = "_xsl_pos" + _gen.generateId(); 294 String list = "_xsl_list" + _gen.generateId(); 295 296 int sortIndex = _gen.addSort(sort); 297 298 out.println("Node " + oldCxt + " = env.getContextNode();"); 299 out.println("Node " + oldCur + " = env.getCurrentNode();"); 300 301 out.println("ArrayList " + list + 302 " = xslSort(node, env" + 303 ", _select_patterns[" + _gen.addSelect(select) + "]" + 304 ", _xsl_sorts[" + sortIndex + "]);"); 305 out.println("env.setContextSize(" + list + ".size());"); 306 out.println("for (int " + pos + " = 1; " + pos + 307 " <= " + list + ".size(); " + pos + "++) {"); 308 _gen.pushLoop(); 309 out.pushDepth(); 310 out.println("Node " + _gen.getElement() + " = node;"); 311 out.println("node = (Node) " + list + ".get(" + pos + " - 1);"); 312 313 String oldPos = _gen.getCurrentPosition(); 314 _gen.setCurrentPosition(pos); 315 316 out.println("env.setPosition(" + pos + ");"); 317 318 AbstractPattern oldNodeListContext = _gen.getNodeListContext(); 319 _gen.setNodeListContext(parseMatch(_select)); 320 321 generateChildren(out); 322 323 _gen.setCurrentPosition(oldPos); 324 325 _gen.setNodeListContext(oldNodeListContext); 326 327 out.println("node = " + _gen.getElement() + ";"); 328 329 out.popDepth(); 330 out.println("}"); 331 _gen.popLoop(); 332 out.popDepth(); 333 out.println("}"); 334 } 335 } 336 | Popular Tags |