KickJava   Java API By Example, From Geeks To Geeks.

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


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>regexp</b>, used to define a perl regular expression
30  * for later use as a script variable.
31  * <p>
32  * The body of the tag is used as the regular expression.
33  * <p>
34  * JSP Tag Lib Descriptor
35  * <p><pre>
36  * &lt;name&gt;regexp&lt;/name&gt;
37  * &lt;tagclass&gt;org.apache.taglibs.regexp.RegexpTag&lt;/tagclass&gt;
38  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
39  * &lt;info&gt;Create a script variable for a regular expression.&lt;/info&gt;
40  * &lt;attribute&gt;
41  * &lt;name&gt;id&lt;/name&gt;
42  * &lt;required&gt;true&lt;/required&gt;
43  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
44  * &lt;/attribute&gt;
45  * </pre>
46  *
47  * @see RegexpData
48  *
49  * @author Glenn Nielsen
50  */

51
52 public class RegexpTag extends BodyTagSupport
53 {
54     // The regular expression to use for this tag
55
private String JavaDoc regexptext = null;
56
57     /**
58      * Returns EVAL_BODY_TAG so the body of the tag can be evaluated.
59      *
60      * @return EVAL_BODY_TAG
61      */

62     public final int doStartTag() throws JspException
63     {
64     return EVAL_BODY_TAG;
65     }
66
67     /**
68      * Read the body of the regexp tag to obtain the regular expression.
69      *
70      * @return SKIP_BODY
71      */

72     public final int doAfterBody() throws JspException
73     {
74     // Use the body of the tag as regular expression
75
BodyContent body = getBodyContent();
76     regexptext = body.getString().trim();
77     // Clear the body since we only used it as input for the
78
// regular expression
79
body.clearBody();
80
81     // Make sure we have some text for the regexp
82
if( regexptext == null || regexptext.length() <= 0 )
83         throw new JspException(
84         "regexp tag regexp could not find a regular expresion in body of tag.");
85
86     // Add the regular expression
87
RegexpData rd = new RegexpData(regexptext);
88     PatternCacheLRU regexp = RegexpData.getPatternCache();
89     try {
90             regexp.addPattern(regexptext,
91                 Perl5Compiler.MULTILINE_MASK|Perl5Compiler.READ_ONLY_MASK);
92     } catch(MalformedPatternException e) {
93             throw new JspException("regexp tag regexp, malformed pattern \"" +
94                 regexptext + "\" " + e.getMessage());
95         }
96
97     pageContext.setAttribute(id,rd,PageContext.PAGE_SCOPE);
98     return SKIP_BODY;
99     }
100 }
101
Popular Tags