KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > SampleBodyTag


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.sample.servlet;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.servlet.jsp.JspTagException JavaDoc;
25 import javax.servlet.jsp.JspWriter JavaDoc;
26 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
27
28 /**
29  * Sample tag that interacts with its body. The tag acts as a filter for its
30  * body. "Target" and "Replacement" Strings are defined by the tag's attributes
31  * and each "occurrence" of the target is replaced by the "replacement".
32  *
33  * @version $Id: SampleBodyTag.java,v 1.3 2004/02/29 16:36:45 vmassol Exp $
34  */

35 public class SampleBodyTag extends BodyTagSupport JavaDoc
36 {
37     /**
38      * The substring to be replaced in the body.
39      */

40     private String JavaDoc target;
41
42     /**
43      * The substring that will replace the target in the body.
44      */

45     private String JavaDoc replacement;
46
47     /**
48      * Sets the substring to be replaced in the body.
49      *
50      * @param theTarget the substring to be replaced in the body
51      */

52     public void setTarget(String JavaDoc theTarget)
53     {
54         this.target = theTarget;
55     }
56
57     /**
58      * Sets the substring that will replace the target in the body.
59      *
60      * @param theReplacement the replacement string
61      */

62     public void setReplacement(String JavaDoc theReplacement)
63     {
64         this.replacement = theReplacement;
65     }
66
67     /**
68      * @see BodyTagSupport#doAfterBody()
69      */

70     public int doAfterBody() throws JspTagException JavaDoc
71     {
72         String JavaDoc contentString = this.bodyContent.getString();
73         StringBuffer JavaDoc contentBuffer = new StringBuffer JavaDoc(contentString);
74
75         int beginIndex = -1;
76         int targetLength = this.target.length();
77
78         // while instances of target still exist
79
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         // write out the changed body
89
JspWriter JavaDoc pageWriter = this.bodyContent.getEnclosingWriter();
90
91         try
92         {
93             pageWriter.write(contentString);
94         }
95         catch (IOException JavaDoc e)
96         {
97             throw new JspTagException JavaDoc(e.getMessage());
98         }
99
100         return SKIP_BODY;
101     }
102
103     /**
104      * @see BodyTagSupport#release()
105      */

106     public void release()
107     {
108         this.target = null;
109         this.replacement = null;
110     }
111 }
112
Popular Tags