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 XslApplyTemplates extends XslNode { 43 private String _select; 44 private String _mode; 45 46 private ArrayList <XslSort> _sorts = new ArrayList <XslSort>(); 47 48 51 public String getTagName() 52 { 53 return "xsl:apply-templates"; 54 } 55 56 59 public void addAttribute(QName name, String value) 60 throws XslParseException 61 { 62 if (name.getName().equals("select")) { 63 _select = value; 64 } 65 else if (name.getName().equals("mode")) { 66 _mode = value; 67 } 68 else 69 super.addAttribute(name, value); 70 } 71 72 75 public void endAttributes() 76 throws XslParseException 77 { 78 } 79 80 83 public void addChild(XslNode node) 84 throws XslParseException 85 { 86 if (node instanceof XslSort) { 87 _sorts.add((XslSort) node); 88 } 89 else 90 super.addChild(node); 91 } 92 93 98 public void generate(JavaWriter out) 99 throws Exception 100 { 101 AbstractPattern selectPattern = null; 102 103 if (_select != null) { 104 selectPattern = parseSelect(_select); 105 } 106 107 Sort []sort = null; 108 109 if (_sorts.size() > 0) { 110 sort = new Sort[_sorts.size()]; 111 112 for (int i = 0; i < _sorts.size(); i++) 113 sort[i] = _sorts.get(i).generateSort(); 114 } 115 116 117 if (sort != null && selectPattern == null) { 118 selectPattern = parseSelect("*"); 120 } 121 122 pushCall(out); 123 124 generateChildren(out); 125 126 printApplyTemplates(out, selectPattern, _mode, sort); 127 128 popCall(out); 129 } 130 131 138 private void printApplyTemplates(JavaWriter out, 139 AbstractPattern select, 140 String mode, 141 Sort []sort) 142 throws Exception 143 { 144 int min = 0; 145 int max = Integer.MAX_VALUE; 146 147 String applyName = "applyNode" + _gen.getModeName(mode); 148 String env = "_xsl_arg" + _gen.getCallDepth(); 149 150 if (select == null && sort == null) { 151 out.println("for (Node _xsl_node = node.getFirstChild();"); 152 out.println(" _xsl_node != null;"); 153 out.println(" _xsl_node = _xsl_node.getNextSibling()) {"); 154 out.println(" " + env + ".setSelect(node, null);"); 155 out.println(" " + env + ".setCurrentNode(_xsl_node);"); 156 out.println(" " + applyName + "(out, _xsl_node, " + env + ", " + 157 min + ", " + max + ");"); 158 out.println("}"); 159 } 160 else if (sort == null) { 161 int oldSelectDepth = _gen.getSelectDepth(); 162 163 String name = printSelectBegin(out, select, false, null); 164 165 out.println(env + ".setSelect(node, _select_patterns[" + 166 _gen.addSelect(select) + "]);"); 167 out.println(env + ".setCurrentNode(" + name + ");"); 168 169 out.println(applyName + "(out, " + name + ", " + env + ", " + 170 min + ", " + max + ");"); 171 172 for (; _gen.getSelectDepth() > oldSelectDepth; _gen.popSelectDepth()) { 173 out.popDepth(); 174 out.println("}"); 175 } 176 } 177 else { 178 int sortIndex = _gen.addSort(sort); 179 180 out.println("{"); 181 out.pushDepth(); 182 out.println("ArrayList _xsl_list = xslSort(node, env" + 183 ", _select_patterns[" + _gen.addSelect(select) + "]" + 184 ", _xsl_sorts[" + sortIndex + "]);"); 185 out.println(env + ".setContextSize(_xsl_list.size());"); 186 out.println("for (int _xsl_i = 0; _xsl_i < _xsl_list.size(); _xsl_i++) {"); 187 out.println(" " + env + ".setContextPosition(_xsl_i + 1);"); 188 out.println(" " + applyName + "(out, (Node) _xsl_list.get(_xsl_i)" + 189 ", " + env + ", " + min + ", " + max + ");"); 190 out.println("}"); 191 out.popDepth(); 192 out.println("}"); 193 } 194 } 195 196 protected void popScope(JavaWriter out) 197 throws Exception 198 { 199 } 200 } 201 | Popular Tags |