KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 public class TranslateFunction extends SimpleNode {
33
34     /**
35      * Used to create an instance of the TranslateFunction class
36      *
37      *
38      * @param id
39      *
40      */

41     public TranslateFunction(int id) {
42         super(id);
43     }
44
45     /**
46      * Used to create an instance of the TranslateFunction class
47      *
48      *
49      * @param p
50      * @param id
51      *
52      */

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

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

112     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
113             throws EvaluationException {
114
115         String JavaDoc result;
116
117         try {
118             String JavaDoc arg1 =
119                 Convert.toString(jjtGetChild(0).evaluate(pageContext,
120                     icontext));
121             String JavaDoc arg2 =
122                 Convert.toString(jjtGetChild(1).evaluate(pageContext,
123                     icontext));
124             String JavaDoc arg3 =
125                 Convert.toString(jjtGetChild(2).evaluate(pageContext,
126                     icontext));
127
128             result = arg1;
129
130             char replacement;
131
132             for (int i = 0; i < arg2.length(); i++) {
133                 if (i < arg3.length()) {
134                     replacement = arg3.charAt(i);
135                 } else {
136                     replacement = 0;
137                 }
138
139                 result = result.replace(arg2.charAt(i), replacement);
140             }
141         } catch (ConversionException ce) {
142             throw new EvaluationException(this, ce.getMessage());
143         }
144
145         return result;
146     }
147 }
148
Popular Tags