KickJava   Java API By Example, From Geeks To Geeks.

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


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>existsmatch</b>, used to determine if a regular
30  * expression match can be found in the text.
31  * <p>
32  * Includes the body of the tag if the a match was found.
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  * You can set the optional tag attribute <b>value</b> to <i>true</i> or
39  * <i>false</i>. The body of the tag is included if existsmatch matches
40  * the value.
41  * <p>
42  * JSP Tag Lib Descriptor
43  * <p><pre>
44  * &lt;name&gt;existsmatch&lt;/name&gt;
45  * &lt;tagclass&gt;org.apache.taglibs.regexp.ExistsMatchTag&lt;/tagclass&gt;
46  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
47  * &lt;info&gt;Includes the body of the tag if the regexp attribute exists.&lt;/info&gt;
48  * &lt;attribute&gt;
49  * &lt;name&gt;regexp&lt;/name&gt;
50  * &lt;required&gt;true&lt;/required&gt;
51  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
52  * &lt;/attribute&gt;
53  * &lt;attribute&gt;
54  * &lt;name&gt;text&lt;/name&gt;
55  * &lt;required&gt;true&lt;/required&gt;
56  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
57  * &lt;/attribute&gt;
58  * &lt;attribute&gt;
59  * &lt;name&gt;value&lt;/name&gt;
60  * &lt;required&gt;false&lt;/required&gt;
61  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
62  * &lt;/attribute&gt;
63  * </pre>
64  *
65  * @see RegexpTag
66  * @see TextTag
67  * @see RegexpData
68  * @see TextData
69  *
70  * @author Glenn Nielsen
71  */

72
73 public class ExistsMatchTag extends TagSupport
74 {
75     private String JavaDoc regexpid = null;
76     private String JavaDoc textid = null;
77     private boolean value = true;
78
79     /**
80      * Includes the body of the tag if the regexp finds a match in text.
81      *
82      * @return SKIP_BODY if existsmatch doesn't match value, EVAL_BODY_include if existsmatch matches value
83      */

84     public final int doStartTag() throws JspException
85     {
86     boolean result = false;
87
88     TextData td = (TextData)pageContext.getAttribute(textid,PageContext.PAGE_SCOPE);
89     if( td == null )
90         throw new JspException(
91         "regexp tag existsmatch could not find text tag with id: " +
92         textid);
93         RegexpData rd = (RegexpData)pageContext.getAttribute(regexpid,PageContext.PAGE_SCOPE);
94         if( rd == null )
95             throw new JspException(
96                 "regexp tag existsmatch could not find regexp tag with id: " +
97                 regexpid);
98
99     Perl5Util perl = new Perl5Util(rd.getPatternCache());
100     if( perl.match(rd.getRegexp(),td.getText()) )
101         result = true;
102
103     if( value == result )
104         return EVAL_BODY_INCLUDE;
105
106     return SKIP_BODY;
107     }
108
109     /*
110      * Set the required attribute <b>regexp</b>
111      *
112      * @param String name of regexp script variable
113      */

114     public final void setRegexp(String JavaDoc str)
115     {
116     regexpid = str;
117     }
118
119     /*
120      * Set the required attribute <b>text</b>
121      *
122      * @param String name of text script variable
123      */

124     public final void setText(String JavaDoc str)
125     {
126         textid = str;
127     }
128
129     /**
130      * Set the optional tag attribute <b>value</b> to true or false.
131      *
132      * @param boolean <b>true</b> or <b>false</b>
133      */

134     public final void setValue(boolean value)
135     {
136     this.value = value;
137     }
138
139 }
140
Popular Tags