1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.el;
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: bayard $
25 **/
26
27 public class StringLiteral
28 extends Literal
29 {
30 //-------------------------------------
31 /**
32 *
33 * Constructor
34 **/
35 StringLiteral (Object 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 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 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 getValueFromToken (String pToken)
67 {
68 StringBuffer buf = new StringBuffer ();
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 toStringToken (String 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 buf = new StringBuffer ();
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 toIdentifierToken (String 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 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 getExpressionString ()
178 {
179 return toStringToken ((String) getValue ());
180 }
181
182 //-------------------------------------
183 }