1 20 package org.apache.cactus.sample.servlet; 21 22 import java.io.IOException ; 23 24 import javax.servlet.jsp.JspTagException ; 25 import javax.servlet.jsp.JspWriter ; 26 import javax.servlet.jsp.tagext.BodyTagSupport ; 27 28 35 public class SampleBodyTag extends BodyTagSupport 36 { 37 40 private String target; 41 42 45 private String replacement; 46 47 52 public void setTarget(String theTarget) 53 { 54 this.target = theTarget; 55 } 56 57 62 public void setReplacement(String theReplacement) 63 { 64 this.replacement = theReplacement; 65 } 66 67 70 public int doAfterBody() throws JspTagException 71 { 72 String contentString = this.bodyContent.getString(); 73 StringBuffer contentBuffer = new StringBuffer (contentString); 74 75 int beginIndex = -1; 76 int targetLength = this.target.length(); 77 78 while ((beginIndex = contentString.indexOf(this.target)) > -1) 80 { 81 int endIndex = beginIndex + targetLength; 82 83 contentBuffer.replace(beginIndex, endIndex, this.replacement); 84 85 contentString = contentBuffer.toString(); 86 } 87 88 JspWriter pageWriter = this.bodyContent.getEnclosingWriter(); 90 91 try 92 { 93 pageWriter.write(contentString); 94 } 95 catch (IOException e) 96 { 97 throw new JspTagException (e.getMessage()); 98 } 99 100 return SKIP_BODY; 101 } 102 103 106 public void release() 107 { 108 this.target = null; 109 this.replacement = null; 110 } 111 } 112 | Popular Tags |