KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > regexp > SubstituteTag


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

16
17 package org.apache.taglibs.regexp;
18
19 import java.util.*;
20 import org.apache.oro.text.*;
21 import org.apache.oro.text.perl.*;
22 import org.apache.oro.text.regex.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25 import javax.servlet.jsp.*;
26 import javax.servlet.jsp.tagext.*;
27
28 /**
29  * JSP Tag <b>substitute</b>, used to perform global
30  * substitution of the provided text.
31  * <p>
32  * Outputs the new text with substitutions.
33  * <p>
34  * The attribute <b>regexp</b> must be set to the id of a <b>regexp</b>
35  * tag and the <b>text</b> attribute must be set to the id of a
36  * <b>text</b> tag.
37  * <p>
38  * JSP Tag Lib Descriptor
39  * <p><pre>
40  * &lt;name&gt;substitute&lt;/name&gt;
41  * &lt;tagclass&gt;org.apache.taglibs.regexp.SubstituteTag&lt;/tagclass&gt;
42  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
43  * &lt;info&gt;Uses the regexp to perform substitutions on the text.&lt;/info&gt;
44  * &lt;attribute&gt;
45  * &lt;name&gt;regexp&lt;/name&gt;
46  * &lt;required&gt;true&lt;/required&gt;
47  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
48  * &lt;/attribute&gt;
49  * &lt;attribute&gt;
50  * &lt;name&gt;text&lt;/name&gt;
51  * &lt;required&gt;true&lt;/required&gt;
52  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
53  * &lt;/attribute&gt;
54  * </pre>
55  *
56  * @see RegexpTag
57  * @see TextTag
58  * @see RegexpData
59  * @see TextData
60  *
61  * @author Glenn Nielsen
62  */

63
64 public class SubstituteTag extends TagSupport
65 {
66     private String JavaDoc regexpid = null;
67     private String JavaDoc textid = null;
68
69     /**
70      * Uses the regexp to perform substitutions on the text,
71      * then outputs the text.
72      *
73      * @return EVAL_BODY
74      */

75     public final int doEndTag() throws JspException
76     {
77     TextData td = (TextData)pageContext.getAttribute(textid,PageContext.PAGE_SCOPE);
78     if( td == null )
79         throw new JspException(
80         "regexp tag substitute could not find text tag with id: " +
81         textid);
82         RegexpData rd = (RegexpData)pageContext.getAttribute(regexpid,PageContext.PAGE_SCOPE);
83         if( rd == null )
84             throw new JspException(
85                 "regexp tag substitute could not find regexp tag with id: " +
86                 regexpid);
87
88     Perl5Util perl = new Perl5Util(rd.getPatternCache());
89     String JavaDoc out = perl.substitute(rd.getRegexp(),td.getText());
90
91     try {
92         pageContext.getOut().write(out);
93     } catch(Exception JavaDoc e) {
94         throw new JspException("IO Error: " + e.getMessage());
95     }
96
97     return EVAL_PAGE;
98     }
99
100     /*
101      * Set the required attribute <b>regexp</b>
102      *
103      * @param String name of regexp script variable
104      */

105     public final void setRegexp(String JavaDoc str)
106     {
107     regexpid = str;
108     }
109
110     /*
111      * Set the required attribute <b>text</b>
112      *
113      * @param String name of text script variable
114      */

115     public final void setText(String JavaDoc str)
116     {
117         textid = str;
118     }
119
120 }
121
Popular Tags