KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
20  *
21  * <p>An expression representing a String literal value.
22  *
23  * @author Nathan Abramson - Art Technology Group
24  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: pierred $
25  **/

26
27 public class StringLiteral
28   extends Literal
29 {
30   //-------------------------------------
31
/**
32    *
33    * Constructor
34    **/

35   StringLiteral (Object JavaDoc pValue)
36   {
37     super (pValue);
38   }
39
40   //-------------------------------------
41
/**
42    *
43    * Returns a StringLiteral parsed from the given token (enclosed by
44    * single or double quotes)
45    **/

46   public static StringLiteral fromToken (String JavaDoc pToken)
47   {
48     return new StringLiteral (getValueFromToken (pToken));
49   }
50
51   //-------------------------------------
52
/**
53    *
54    * Returns a StringLiteral with the given string value
55    **/

56   public static StringLiteral fromLiteralValue (String JavaDoc pValue)
57   {
58     return new StringLiteral (pValue);
59   }
60
61   //-------------------------------------
62
/**
63    *
64    * Parses the given token into the literal value
65    **/

66   public static String JavaDoc getValueFromToken (String JavaDoc pToken)
67   {
68     StringBuffer JavaDoc buf = new StringBuffer JavaDoc ();
69     int len = pToken.length () - 1;
70     boolean escaping = false;
71     for (int i = 1; i < len; i++) {
72       char ch = pToken.charAt (i);
73       if (escaping) {
74     buf.append (ch);
75     escaping = false;
76       }
77       else if (ch == '\\') {
78     escaping = true;
79       }
80       else {
81     buf.append (ch);
82       }
83     }
84     return buf.toString ();
85   }
86
87   //-------------------------------------
88
/**
89    *
90    * Converts the specified value to a String token, using " as the
91    * enclosing quotes and escaping any characters that need escaping.
92    **/

93   public static String JavaDoc toStringToken (String JavaDoc pValue)
94   {
95     // See if any escaping is needed
96
if (pValue.indexOf ('\"') < 0 &&
97     pValue.indexOf ('\\') < 0) {
98       return "\"" + pValue + "\"";
99     }
100
101     // Escaping is needed
102
else {
103       StringBuffer JavaDoc buf = new StringBuffer JavaDoc ();
104       buf.append ('\"');
105       int len = pValue.length ();
106       for (int i = 0; i < len; i++) {
107     char ch = pValue.charAt (i);
108     if (ch == '\\') {
109       buf.append ('\\');
110       buf.append ('\\');
111     }
112     else if (ch == '\"') {
113       buf.append ('\\');
114       buf.append ('\"');
115     }
116     else {
117       buf.append (ch);
118     }
119       }
120       buf.append ('\"');
121       return buf.toString ();
122     }
123   }
124
125   //-------------------------------------
126
/**
127    *
128    * Converts the specified value to an identifier token, escaping it
129    * as a string literal if necessary.
130    **/

131   public static String JavaDoc toIdentifierToken (String JavaDoc pValue)
132   {
133     // See if it's a valid java identifier
134
if (isJavaIdentifier (pValue)) {
135       return pValue;
136     }
137
138     // Return as a String literal
139
else {
140       return toStringToken (pValue);
141     }
142   }
143
144   //-------------------------------------
145
/**
146    *
147    * Returns true if the specified value is a legal java identifier
148    **/

149   static boolean isJavaIdentifier (String JavaDoc pValue)
150   {
151     int len = pValue.length ();
152     if (len == 0) {
153       return false;
154     }
155     else {
156       if (!Character.isJavaIdentifierStart (pValue.charAt (0))) {
157     return false;
158       }
159       else {
160     for (int i = 1; i < len; i++) {
161       if (!Character.isJavaIdentifierPart (pValue.charAt (i))) {
162         return false;
163       }
164     }
165     return true;
166       }
167     }
168   }
169
170   //-------------------------------------
171
// Expression methods
172
//-------------------------------------
173
/**
174    *
175    * Returns the expression in the expression language syntax
176    **/

177   public String JavaDoc getExpressionString ()
178   {
179     return toStringToken ((String JavaDoc) getValue ());
180   }
181
182   //-------------------------------------
183
}
184
Popular Tags