1 16 17 package org.apache.taglibs.xtags.xpath; 18 19 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.io.IOException ; 26 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Stack ; 30 import java.util.ArrayList ; 31 32 import javax.servlet.ServletContext ; 33 import javax.servlet.jsp.JspException ; 34 import javax.servlet.jsp.JspWriter ; 35 import javax.servlet.jsp.PageContext ; 36 import javax.servlet.jsp.tagext.BodyTagSupport ; 37 38 import org.dom4j.DocumentException; 39 import org.dom4j.InvalidXPathException; 40 import org.dom4j.Node; 41 import org.dom4j.XPath; 42 import org.dom4j.DocumentFactory; 43 import org.dom4j.DocumentHelper; 44 import org.dom4j.rule.Action; 45 import org.dom4j.rule.Rule; 46 import org.dom4j.rule.Stylesheet; 47 48 import org.apache.taglibs.xtags.util.JspVariableContext; 49 50 55 public class StylesheetTag extends AbstractBodyTag { 56 57 58 private Stylesheet stylesheet = new Stylesheet(); 59 60 61 private String mode; 62 63 64 private Stylesheet previousStylesheet; 65 66 67 68 private Action valueOfAction; 69 70 private Stack templateResultLists = new Stack (); 71 private Stack resultIterators = new Stack (); 72 73 private List openResultList; 74 75 private int currentState; 76 private TemplateExecution currentMatch; 77 private StringBuffer avtOutput = new StringBuffer (256); 78 private StringBuffer actionOutput = new StringBuffer (2048); 79 80 static final int INITIALISE_STYLESHEET = 1; 81 static final int RUN_TEMPLATES = 2; 82 83 protected Object context; 84 85 public StylesheetTag() { 86 valueOfAction = new StylesheetValueOfAction(this); 88 stylesheet.setValueOfAction( valueOfAction ); 89 } 90 91 public Stylesheet getStylesheet() { 92 return stylesheet; 93 } 94 95 public void addTemplate( Rule rule ) { 96 stylesheet.addRule( rule ); 97 } 98 99 int getCurrentState() { 100 return this.currentState; 101 } 102 103 104 public void setPageContext(PageContext pageContext) { 107 super.setPageContext(pageContext); 108 } 109 110 public int doStartTag() throws JspException { 111 previousStylesheet = TagHelper.getStylesheet( pageContext ); 112 TagHelper.setStylesheet( pageContext, stylesheet ); 113 stylesheet.clear(); 114 templateResultLists.clear(); 115 actionOutput.delete(0, actionOutput.length() ); 116 117 currentState = INITIALISE_STYLESHEET; 118 119 return EVAL_BODY_TAG; 120 } 121 122 public int doAfterBody() throws JspException { 123 try { 124 if (currentState == INITIALISE_STYLESHEET) { 125 runStylesheet(); 127 128 currentState = RUN_TEMPLATES; 129 } else { 130 addOutputToResultList(); 133 bodyContent.clear(); 135 } 136 137 if (this.openResultList != null) { 138 this.templateResultLists.push(this.openResultList); 140 this.resultIterators.push( this.openResultList.iterator() ); 141 this.openResultList = null; 142 } 143 144 return processResults(); 145 } catch (Exception e) { 146 handleException(e); 147 } 148 return SKIP_BODY; 149 } 150 151 public int doEndTag() throws JspException { 152 TagHelper.setStylesheet( pageContext, previousStylesheet ); 154 reset(); 155 return EVAL_PAGE; 156 } 157 158 public void runStylesheet() throws Exception { 159 stylesheet.setModeName( getMode() ); 160 stylesheet.run( getInputNodes() ); 161 } 162 163 169 int processResults() throws IOException { 170 while (! this.resultIterators.isEmpty()) { 171 Iterator resultIter = (Iterator )this.resultIterators.peek(); 172 while (resultIter.hasNext()) { 173 Object o = resultIter.next(); 174 if (o instanceof String ) { 175 bodyContent.getEnclosingWriter().write( (String )o ); 176 } else{ 177 currentMatch = (TemplateExecution)o; 178 return EVAL_BODY_TAG; 179 } 180 } 181 this.resultIterators.pop(); 182 this.templateResultLists.pop(); 183 } 184 return SKIP_BODY; 185 } 186 187 196 TemplateExecution getTemplateExecution(String match) { 197 if (currentMatch.getMatch().equals(match)) { 198 return currentMatch; 199 } else { 200 return null; 201 } 202 } 203 204 public void release() { 205 super.release(); 206 reset(); 207 } 208 209 void reset() { 210 stylesheet.clear(); 211 openResultList = null; 212 currentMatch = null; 213 templateResultLists.clear(); 214 resultIterators.clear(); 215 actionOutput.delete(0, actionOutput.length() ); 216 avtOutput.delete(0, actionOutput.length() ); 217 } 218 221 224 public String getMode() { 225 return mode; 226 } 227 228 231 public void setMode(String mode) { 232 this.mode = mode; 233 } 234 235 237 void addOutput(String text){ 238 actionOutput.append(text); 239 } 240 241 void addOutputToResultList() { 242 if (actionOutput.length() > 0) { 243 getOpenResultList().add( actionOutput.toString() ); 244 actionOutput.delete(0, actionOutput.length() ); 245 } 246 } 247 248 253 void addTemplateExecution(TemplateExecution te) { 254 addOutputToResultList(); 257 getOpenResultList().add(te); 258 } 259 260 List getOpenResultList() { 261 if (this.openResultList == null) { 262 this.openResultList = new ArrayList (256); 263 } 264 return this.openResultList; 265 } 266 267 String processAVTs(String text) throws IOException , InvalidXPathException { 268 int marker = 0; 269 int leftBracket; 270 int rightBracket; 271 XPath xpath; 272 Object context = getInputNodes(); 273 274 avtOutput.delete(0, avtOutput.length() ); 275 276 while ( (leftBracket = text.indexOf('{', marker)) > 0) { 277 avtOutput.append(text.substring(marker,leftBracket)); 279 rightBracket = text.indexOf('}', leftBracket); 280 if (rightBracket < 0) { 281 marker = leftBracket; break; 284 } 285 xpath = createXPath( text.substring(leftBracket+1, rightBracket) ); 286 if (xpath == null) { 287 throw new InvalidXPathException( text.substring(leftBracket+1, rightBracket) ); 288 } 289 avtOutput.append( xpath.valueOf( context ) ); 290 marker = rightBracket+1; 291 } 292 if (marker < text.length()) { 293 avtOutput.append( text.substring(marker) ); 294 } 295 296 return avtOutput.toString(); 297 } 298 299 300 301 302 304 public Object getInputNodes() { 305 if ( context == null ) { 306 return TagHelper.getInputNodes( pageContext, this, true ); 307 } 308 return context; 309 } 310 311 312 protected XPath createXPath(String xpathExpression) { 313 XPath xpath = getDocumentFactory().createXPath( xpathExpression ); 314 xpath.setVariableContext( JspVariableContext.getInstance( pageContext ) ); 315 return xpath; 316 } 317 318 319 protected DocumentFactory getDocumentFactory() { 320 return DocumentFactory.getInstance(); 321 } 322 323 public void setContext(Object context) { 324 this.context = context; 325 } 326 } 327 | Popular Tags |