KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jstl > ExpressionString


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.jstl;
18
19 import java.util.Map JavaDoc;
20
21 /**
22  *
23  * <p>Represents an expression String consisting of a mixture of
24  * Strings and Expressions.
25  *
26  * @author Nathan Abramson - Art Technology Group
27  * @author Shawn Bayern
28  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: pierred $
29  **/

30
31 public class ExpressionString
32 {
33   //-------------------------------------
34
// Properties
35
//-------------------------------------
36
// property elements
37

38   Object JavaDoc [] mElements;
39   public Object JavaDoc [] getElements ()
40   { return mElements; }
41   public void setElements (Object JavaDoc [] pElements)
42   { mElements = pElements; }
43
44   //-------------------------------------
45
/**
46    *
47    * Constructor
48    **/

49   public ExpressionString (Object JavaDoc [] pElements)
50   {
51     mElements = pElements;
52   }
53
54   //-------------------------------------
55
/**
56    *
57    * Evaluates the expression string by evaluating each element,
58    * converting it to a String (using toString, or "" for null values)
59    * and concatenating the results into a single String.
60    **/

61   public String JavaDoc evaluate (Object JavaDoc pContext,
62               VariableResolver pResolver,
63               Map JavaDoc functions,
64               String JavaDoc defaultPrefix,
65               Logger pLogger)
66     throws ELException
67   {
68     StringBuffer JavaDoc buf = new StringBuffer JavaDoc ();
69     for (int i = 0; i < mElements.length; i++) {
70       Object JavaDoc elem = mElements [i];
71       if (elem instanceof String JavaDoc) {
72     buf.append ((String JavaDoc) elem);
73       }
74       else if (elem instanceof Expression) {
75     Object JavaDoc val =
76       ((Expression) elem).evaluate (pContext,
77                     pResolver,
78                     functions,
79                     defaultPrefix,
80                     pLogger);
81     if (val != null) {
82       buf.append (val.toString ());
83     }
84       }
85     }
86     return buf.toString ();
87   }
88
89   //-------------------------------------
90
/**
91    *
92    * Returns the expression in the expression language syntax
93    **/

94   public String JavaDoc getExpressionString ()
95   {
96     StringBuffer JavaDoc buf = new StringBuffer JavaDoc ();
97     for (int i = 0; i < mElements.length; i++) {
98       Object JavaDoc elem = mElements [i];
99       if (elem instanceof String JavaDoc) {
100     buf.append ((String JavaDoc) elem);
101       }
102       else if (elem instanceof Expression) {
103     buf.append ("${");
104     buf.append (((Expression) elem).getExpressionString ());
105     buf.append ("}");
106       }
107     }
108     return buf.toString ();
109   }
110
111   //-------------------------------------
112
}
113
Popular Tags