1 16 17 package org.apache.taglibs.xtags.xpath; 18 19 20 21 import java.io.IOException ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import javax.servlet.ServletContext ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspWriter ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.tagext.BodyTagSupport ; 30 31 import org.dom4j.Node; 32 import org.dom4j.XPath; 33 import org.dom4j.DocumentHelper; 34 import org.dom4j.rule.Action; 35 import org.dom4j.rule.Pattern; 36 import org.dom4j.rule.Rule; 37 import org.dom4j.rule.RuleManager; 38 39 44 public class TemplateTag extends AbstractBodyTag { 45 46 47 private String name; 48 49 50 private String mode; 51 52 53 private double priority; 54 55 56 private String jsp; 57 58 59 private Rule rule; 60 61 62 private Action action; 63 64 65 private boolean avt; 66 67 68 private Pattern pattern; 69 70 71 private String match; 72 73 private StylesheetTag stylesheetTag; 74 private Object oldContext = null; 75 76 public TemplateTag() { 78 } 79 80 public int doStartTag() throws JspException { 83 StylesheetTag tag = getStylesheetTag(); 84 if (tag.getCurrentState() == StylesheetTag.INITIALISE_STYLESHEET) { 85 Rule rule = getRule(); 86 if ( rule != null ) { 87 if ( tag != null ) { 88 tag.addTemplate( rule ); 89 } 90 } 91 return SKIP_BODY; 92 } else { 93 TemplateExecution te = tag.getTemplateExecution(match); 94 if ( te != null ) { 95 oldContext = TagHelper.getInputNodes( pageContext ); 96 TagHelper.setInputNodes( pageContext, te.getNode() ); 97 98 return EVAL_BODY_TAG; 99 } else { 100 return SKIP_BODY; 101 } 102 } 103 } 104 105 public int doAfterBody() throws JspException { 106 try { 107 String text = bodyContent.getString(); 108 if (isAvt() == true) { 109 text = getStylesheetTag().processAVTs(text); 110 } 111 getStylesheetTag().addOutput( text ); 112 bodyContent.clearBody(); 113 if (oldContext != null) { 114 TagHelper.setInputNodes( pageContext, oldContext ); 115 oldContext = null; 116 } 117 } catch (IOException e) { 118 handleException(e); 119 } 120 return SKIP_BODY; 121 } 122 123 124 public void release() { 125 super.release(); 126 name = null; 127 jsp = null; 128 match = null; 129 rule = null; 130 action = null; 131 pattern = null; 132 oldContext = null; 133 } 134 135 136 void preApplyTemplates() throws IOException { 137 String text = bodyContent.getString(); 140 if (isAvt() == true) { 141 text = getStylesheetTag().processAVTs(text); 142 } 143 getStylesheetTag().addOutput( text ); 144 bodyContent.clearBody(); 145 } 146 147 150 153 public boolean isAvt() { 154 return avt; 155 } 156 157 160 public void setAvt(boolean avt) { 161 this.avt = avt; 162 } 163 164 public void setMatch( String match ) { 165 this.match = match; 167 168 StylesheetTag tag = getStylesheetTag(); 169 if (tag.getCurrentState() == StylesheetTag.INITIALISE_STYLESHEET ) { 170 pattern = createPattern( match ); 171 } 172 } 173 174 177 public double getPriority() { 178 return priority; 179 } 180 181 184 public void setPriority(double priority) { 185 this.priority = priority; 186 } 187 188 191 public String getName() { 192 return name; 193 } 194 195 198 public void setName(String name) { 199 this.name = name; 200 } 201 202 205 public StylesheetTag getStylesheetTag() { 206 if ( stylesheetTag == null ) { 207 stylesheetTag = (StylesheetTag) findAncestorWithClass( 208 this, StylesheetTag.class 209 ); 210 } 211 return stylesheetTag; 212 } 213 214 215 218 public Action getAction() { 219 if ( action == null ) { 220 action = createAction(); 221 } 222 return action; 223 } 224 225 228 public void setAction(Action action) { 229 this.action = action; 230 } 231 232 235 public void setMode(String mode) { 236 this.mode = mode; 237 } 238 239 242 public String getJsp() { 243 return jsp; 244 } 245 246 249 public void setJsp(String jsp) { 250 this.jsp = jsp; 251 } 252 253 256 public Rule getRule() { 257 if ( rule == null ) { 258 rule = createRule(); 259 } 260 return rule; 261 } 262 263 266 public void setRule(Rule rule) { 267 this.rule = rule; 268 } 269 270 271 272 273 protected Rule createRule() { 276 Rule rule = new Rule( pattern, getAction() ); 277 rule.setMode( mode ); 278 return rule; 279 } 280 281 protected Pattern createPattern( String xpath ) { 282 return DocumentHelper.createPattern( xpath ); 283 } 284 285 protected Action createAction() { 288 if ( jsp != null ) { 289 return new JspAction( pageContext, jsp ); 290 } 291 else { 292 return new BodyAction( getStylesheetTag(), this.match ); 293 } 294 } 295 } 296 | Popular Tags |