KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.*;
21 import javax.servlet.http.*;
22 import javax.servlet.jsp.*;
23 import javax.servlet.jsp.tagext.*;
24
25 /**
26  * JSP Tag <b>group</b>, used to output the value for a single
27  * parenthesized group within a single match.
28  * <p>
29  * The parenthesized group <b>number</b> attribute must be set.
30  * <p>
31  * Must be nested inside a <b>match</b> tag.
32  * <p>
33  * JSP Tag Lib Descriptor
34  * <p><pre>
35  * &lt;name&gt;group&lt;/name&gt;
36  * &lt;tagclass&gt;org.apache.taglibs.regexp.GroupTag&lt;/tagclass&gt;
37  * &lt;bodycontent&gt;empty&lt;/bodycontent&gt;
38  * &lt;info&gt;Get the value of a single parenthesized group within a single match.&lt;/info&gt;
39  * &lt;attribute&gt;
40  * &lt;name&gt;number&lt;/name&gt;
41  * &lt;required&gt;true&lt;/required&gt;
42  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
43  * &lt;/attribute&gt;
44  * </pre>
45  *
46  * @see MatchTag
47  *
48  * @author Glenn Nielsen
49  */

50
51 public class GroupTag extends TagSupport
52 {
53     private int number = 0;
54
55     /**
56      * Method called at end of Tag to output group value
57      *
58      * @return EVAL_PAGE
59      */

60     public final int doEndTag() throws JspException
61     {
62     MatchTag mt = null;
63     try {
64         mt = (MatchTag)this.findAncestorWithClass(this,
65         Class.forName("org.apache.taglibs.regexp.MatchTag"));
66     } catch(Exception JavaDoc e) {
67     }
68     if( mt == null )
69         throw new JspException("regexp group tag could not find a parent match tag.");
70
71     String JavaDoc value = mt.getGroup(number);
72
73     try {
74         pageContext.getOut().write(value);
75     } catch(Exception JavaDoc e) {
76         throw new JspException("IO Error: " + e.getMessage());
77     }
78
79     return EVAL_PAGE;
80     }
81
82     /*
83      * Set the required attribute <b>number</b> to the
84      * parenthesized group number to get from match.
85      *
86      * @param int parenthesized match group number
87      */

88     public final void setNumber(int number)
89     {
90     this.number = number;
91     }
92
93 }
94
Popular Tags