KickJava   Java API By Example, From Geeks To Geeks.

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


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>match</b>, used to implement a perl style
30  * match on the text.
31  * <p>
32  * The body of the tag iterates through each match found.
33  * The normal &lt;jsp:getProperty/&gt; can be used to get
34  * the match results using the id of the <b>match</b> tag
35  * script variable.
36  * <p>
37  * The attribute <b>text</b> must be set to the id of a
38  * <b>text</b> tag, and the <b>regexp</b> attribute must
39  * be set to the id of a <b>regexp</b> tag.
40  * <p>
41  * JSP Tag Lib Descriptor
42  * <p><pre>
43  * &lt;name&gt;match&lt;/name&gt;
44  * &lt;tagclass&gt;org.apache.taglibs.regexp.MatchTag&lt;/tagclass&gt;
45  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
46  * &lt;info&gt;Uses the regexp to find matches in text, loops for each match found.&lt;/info&gt;
47  * &lt;attribute&gt;
48  * &lt;name&gt;id&lt;/name&gt;
49  * &lt;required&gt;true&lt;/required&gt;
50  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
51  * &lt;/attribute&gt;
52  * &lt;attribute&gt;
53  * &lt;name&gt;text&lt;/name&gt;
54  * &lt;required&gt;true&lt;/required&gt;
55  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
56  * &lt;/attribute&gt;
57  * &lt;attribute&gt;
58  * &lt;name&gt;regexp&lt;/name&gt;
59  * &lt;required&gt;true&lt;/required&gt;
60  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
61  * &lt;/attribute&gt;
62  * </pre>
63  *
64  * @see RegexpTag
65  * @see TextTag
66  * @see RegexpData
67  * @see TextData
68  *
69  * @author Glenn Nielsen
70  */

71
72 public class MatchTag extends BodyTagSupport
73 {
74     private String JavaDoc regexpid = null;
75     private String JavaDoc textid = null;
76     private Perl5Util perl = new Perl5Util(RegexpData.getPatternCache());
77     private PatternMatcherInput pmi = null;
78     private RegexpData rd = null;
79
80     /**
81      * Setup to loop through matches found in the text using regexp
82      *
83      * @return EVAL_BODY_TAG if a match is found, BODY_SKIP if no matches are found
84      */

85     public final int doStartTag() throws JspException
86     {
87     TextData td = (TextData)pageContext.getAttribute(textid,PageContext.PAGE_SCOPE);
88     if( td == null )
89         throw new JspException(
90         "regexp tag match could not find text tag with id: " +
91         textid);
92     pmi = new PatternMatcherInput(td.getText());
93
94         rd = (RegexpData)pageContext.getAttribute(regexpid,PageContext.PAGE_SCOPE);
95         if( rd == null )
96             throw new JspException(
97                 "regexp tag match could not find regexp tag with id: " +
98                 regexpid);
99     if( !perl.match(rd.getRegexp(),pmi) )
100         return SKIP_BODY;
101
102     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
103
104     return EVAL_BODY_TAG;
105     }
106
107     /**
108      * Method called at end of each iteration of the match tag
109      *
110      * @return EVAL_BODY_TAG if there is another string match, or BODY_SKIP if there are no more strings
111      */

112     public final int doAfterBody() throws JspException
113     {
114     if( !perl.match(rd.getRegexp(),pmi) )
115         return SKIP_BODY;
116     return EVAL_BODY_TAG;
117     }
118
119     /**
120      * Method called at end of match Tag
121      *
122      * @return EVAL_PAGE
123      */

124     public final int doEndTag() throws JspException
125     {
126         if( id != null && id.length() > 0 )
127             pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
128     try {
129         if(bodyContent != null)
130         bodyContent.writeOut(bodyContent.getEnclosingWriter());
131     } catch(java.io.IOException JavaDoc e) {
132         throw new JspException("IO Error: " + e.getMessage());
133     }
134     return EVAL_PAGE;
135     }
136
137     /*
138      * Set the required attribute <b>regexp</b>
139      *
140      * @param String name of regexp script variable
141      */

142     public final void setRegexp(String JavaDoc str)
143     {
144     regexpid = str;
145     }
146
147     /*
148      * Set the required attribute <b>text</b>
149      *
150      * @param String name of text script variable
151      */

152     public final void setText(String JavaDoc str)
153     {
154         textid = str;
155     }
156
157     /**
158      * Returns the entire string that was matched on
159      * using &lt;jsp:getProperty name=<i>"id"</i> property="match"/&gt;
160      *
161      * @return String - entire text of match
162      */

163     public final String JavaDoc getMatch()
164     {
165         return perl.toString();
166     }
167
168     /**
169      * Returns the string preceding the current match
170      * using &lt;jsp:getProperty name=<i>"id"</i> property="preMatch"/&gt;
171      *
172      * @return String - text preceding match
173      */

174     public final String JavaDoc getPreMatch()
175     {
176     return perl.preMatch();
177     }
178
179     /**
180      * Returns the string after the current match
181      * using &lt;jsp:getProperty name=<i>"id"</i> property="postMatch"/&gt;
182      *
183      * @return String - text after the match
184      */

185     public final String JavaDoc getPostMatch()
186     {
187         return perl.postMatch();
188     }
189
190     /**
191      * Returns the string for the group parenthesized number
192      *
193      * @return String - for group parenthesized number
194      */

195     public final String JavaDoc getGroup(int i)
196     {
197     String JavaDoc group = perl.group(i);
198     if( group == null )
199         return "";
200     return group;
201     }
202
203 }
204
Popular Tags