KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import org.apache.oro.text.*;
22 import org.apache.oro.text.perl.*;
23 import org.apache.oro.text.regex.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26 import javax.servlet.jsp.*;
27 import javax.servlet.jsp.tagext.*;
28
29 /**
30  * JSP Tag <b>split</b>, used to implement a perl style
31  * split on the text.
32  * <p>
33  * The body of the tag iterates through each string generated
34  * from the split. The normal &lt;jsp:getProperty/&gt; can be
35  * used to get the value of each string split out using the
36  * id of the <b>split</b> tag script variable.
37  * <p>
38  * The attribute <b>text</b> must be set to the id of a
39  * <b>text</b> tag.
40  * <p>
41  * By default, the split is done on whitespace. Set the
42  * optional attribute <b>regexp</b> to the id of a <b>regexp</b>
43  * tag script variable id to perform the split using a custom
44  * regular expression.
45  * <p>
46  * You can set the optional tag attribute <b>limit</b> to the
47  * number of items you want to limit the split to.
48  * <p>
49  * JSP Tag Lib Descriptor
50  * <p><pre>
51  * &lt;name&gt;split&lt;/name&gt;
52  * &lt;tagclass&gt;org.apache.taglibs.regexp.SplitTag&lt;/tagclass&gt;
53  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
54  * &lt;info&gt;Implements a perl style split on the text.&lt;/info&gt;
55  * &lt;attribute&gt;
56  * &lt;name&gt;id&lt;/name&gt;
57  * &lt;required&gt;true&lt;/required&gt;
58  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
59  * &lt;/attribute&gt;
60  * &lt;attribute&gt;
61  * &lt;name&gt;text&lt;/name&gt;
62  * &lt;required&gt;true&lt;/required&gt;
63  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
64  * &lt;/attribute&gt;
65  * &lt;attribute&gt;
66  * &lt;name&gt;regexp&lt;/name&gt;
67  * &lt;required&gt;false&lt;/required&gt;
68  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
69  * &lt;/attribute&gt;
70  * &lt;attribute&gt;
71  * &lt;name&gt;limit&lt;/name&gt;
72  * &lt;required&gt;false&lt;/required&gt;
73  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
74  * &lt;/attribute&gt;
75  * </pre>
76  *
77  * @see RegexpTag
78  * @see TextTag
79  * @see RegexpData
80  * @see TextData
81  *
82  * @author Glenn Nielsen
83  */

84
85 public class SplitTag extends BodyTagSupport
86 {
87     private String JavaDoc regexpid = null;
88     private String JavaDoc textid = null;
89     private int limit = -1;
90     private Iterator JavaDoc values = null;
91     private String JavaDoc value = null;
92
93     /**
94      * Setup to split the text based on supplied attributes
95      *
96      * @return EVAL_BODY_TAG if there is a string from the split, BODY_SKIP if no strings resulted from the split
97      */

98     public final int doStartTag() throws JspException
99     {
100     TextData td = (TextData)pageContext.getAttribute(textid,PageContext.PAGE_SCOPE);
101     if( td == null )
102         throw new JspException(
103         "regexp tag split could not find text tag with id: " +
104         textid);
105
106     Perl5Util perl = new Perl5Util(RegexpData.getPatternCache());
107     String JavaDoc spliton = "/\\s+/m";
108     if( regexpid != null ) {
109             RegexpData rd = (RegexpData)pageContext.getAttribute(regexpid,PageContext.PAGE_SCOPE);
110             if( rd == null )
111                 throw new JspException(
112                     "regexp tag split could not find regexp tag with id: " +
113                     regexpid);
114         spliton = rd.getRegexp();
115     }
116     ArrayList JavaDoc split = new ArrayList JavaDoc();
117     if( limit > -1 ) {
118         perl.split(split,spliton,td.getText(),limit);
119     } else {
120         perl.split(split,spliton,td.getText());
121     }
122         values = split.iterator();
123     if( !values.hasNext() ) {
124         return SKIP_BODY;
125     }
126
127     value = (String JavaDoc)values.next();
128     pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
129
130     return EVAL_BODY_TAG;
131     }
132
133     /**
134      * Method called at end of each iteration of the split tag
135      *
136      * @return EVAL_BODY_TAG if there is another string split, or BODY_SKIP if there are no more strings
137      */

138     public final int doAfterBody() throws JspException
139     {
140     if( !values.hasNext() ) {
141         return SKIP_BODY;
142     }
143     value = (String JavaDoc)values.next();
144     return EVAL_BODY_TAG;
145     }
146
147     /**
148      * Method called at end of split Tag
149      *
150      * @return EVAL_PAGE
151      */

152     public final int doEndTag() throws JspException
153     {
154         if( id != null && id.length() > 0 )
155             pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
156     try {
157         if(bodyContent != null)
158         bodyContent.writeOut(bodyContent.getEnclosingWriter());
159     } catch(java.io.IOException JavaDoc e) {
160         throw new JspException("IO Error: " + e.getMessage());
161     }
162     return EVAL_PAGE;
163     }
164
165     /*
166      * Set the optional attribute <b>regexp</b>
167      *
168      * @param String name of regexp script variable
169      */

170     public final void setRegexp(String JavaDoc str)
171     {
172     regexpid = str;
173     }
174
175     /*
176      * Set the required attribute <b>text</b>
177      *
178      * @param String name of text script variable
179      */

180     public final void setText(String JavaDoc str)
181     {
182         textid = str;
183     }
184
185     /*
186      * Set the optional attribute <b>limit</b> to the
187      * number of strings to split out of text.
188      *
189      * @param int number of strings to limit split to
190      */

191     public final void setLimit(int limit)
192     {
193     this.limit = limit;
194     }
195
196     /**
197      * String split out from text can be obtained by the JSP page
198      * using &lt;jsp:getProperty name=<i>"id"</i> property="split"/&gt;
199      *
200      * @return String - string that was split out from text
201      */

202     public final String JavaDoc getSplit()
203     {
204     return value;
205     }
206
207 }
208
Popular Tags