KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > expression > TokenizeFunction


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.standard.lang.jpath.expression;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 import javax.servlet.jsp.PageContext JavaDoc;
23
24 import org.apache.taglibs.standard.lang.jpath.adapter.CollectionAdapter;
25 import org.apache.taglibs.standard.lang.jpath.adapter.ConversionException;
26 import org.apache.taglibs.standard.lang.jpath.adapter.Convert;
27 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext;
28
29 /**
30  * The TokenizeFunction class
31  *
32  *
33  * @author <a HREF='mailto:scott.hasse@isthmusgroup.com'>Scott Hasse</a>
34  * @version
35  */

36 public class TokenizeFunction extends SimpleNode {
37
38     /**
39      * Used to create an instance of the TokenizeFunction class
40      *
41      *
42      * @param id
43      *
44      */

45     public TokenizeFunction(int id) {
46         super(id);
47     }
48
49     /**
50      * Used to create an instance of the TokenizeFunction class
51      *
52      *
53      * @param p
54      * @param id
55      *
56      */

57     public TokenizeFunction(Parser p, int id) {
58         super(p, id);
59     }
60
61     /**
62      * Provides a method to print a normalized version of the original
63      * expression. The normalized version has standardized spacing and
64      * parenthesis, and can be used to compare expressions formatted
65      * in different ways to see if they are actually the same expression.
66      *
67      *
68      * @return The normalized version of the original expression
69      *
70      */

71     public String JavaDoc toNormalizedString() {
72
73         boolean first = true;
74         String JavaDoc normalized;
75
76         normalized = "tokenize(";
77
78         if (children != null) {
79             for (int i = 0; i < children.length; ++i) {
80                 if (!first) {
81                     normalized = normalized + ",";
82                 }
83
84                 first = false;
85
86                 SimpleNode n = (SimpleNode) children[i];
87
88                 if (n != null) {
89                     normalized = normalized + n.toNormalizedString();
90                 }
91             }
92         }
93
94         normalized = normalized + ")";
95
96         return normalized;
97     }
98
99     /**
100      * This method evaluates this node of the expression and all child nodes.
101      * It returns the result of the
102      * evaluation as an <tt>Object</tt>. If any problems are encountered
103      * during the evaluation, an <tt>EvaluationException</tt> is thrown.
104      *
105      *
106      * @param pageContext the current JSP PageContext
107      *
108      * @param icontext the Iteration Context of the expression. If there is
109      * no interation context, this should be null.
110      *
111      * @return the result of the expression evaluation as an object
112      *
113      * @throws EvaluationException if a problem is encountered during the
114      * evaluation
115      */

116     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
117             throws EvaluationException {
118
119         Object JavaDoc result;
120
121         try {
122             String JavaDoc arg1 =
123                 Convert.toString(jjtGetChild(0).evaluate(pageContext,
124                     icontext));
125             String JavaDoc arg2 =
126                 Convert.toString(jjtGetChild(1).evaluate(pageContext,
127                     icontext));
128             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(arg1, arg2);
129             ArrayList JavaDoc a = new ArrayList JavaDoc();
130
131             while (st.hasMoreTokens()) {
132                 a.add(st.nextToken());
133             }
134
135             result = CollectionAdapter.adapt(a);
136         } catch (ConversionException ce) {
137             throw new EvaluationException(this, ce.getMessage());
138         }
139
140         return result;
141     }
142 }
143
Popular Tags