Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StringLiteral |
|
| 3.25;3.25 |
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 | 3 | super (pValue); |
38 | 3 | } |
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 | 3 | 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 | 0 | 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 | 3 | StringBuffer buf = new StringBuffer (); |
69 | 3 | int len = pToken.length () - 1; |
70 | 3 | boolean escaping = false; |
71 | 12 | for (int i = 1; i < len; i++) { |
72 | 9 | char ch = pToken.charAt (i); |
73 | 9 | if (escaping) { |
74 | 2 | buf.append (ch); |
75 | 2 | escaping = false; |
76 | } | |
77 | 7 | else if (ch == '\\') { |
78 | 2 | escaping = true; |
79 | } | |
80 | else { | |
81 | 5 | buf.append (ch); |
82 | } | |
83 | } | |
84 | 3 | 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 | 0 | if (pValue.indexOf ('\"') < 0 && |
97 | pValue.indexOf ('\\') < 0) { | |
98 | 0 | return "\"" + pValue + "\""; |
99 | } | |
100 | ||
101 | // Escaping is needed | |
102 | else { | |
103 | 0 | StringBuffer buf = new StringBuffer (); |
104 | 0 | buf.append ('\"'); |
105 | 0 | int len = pValue.length (); |
106 | 0 | for (int i = 0; i < len; i++) { |
107 | 0 | char ch = pValue.charAt (i); |
108 | 0 | if (ch == '\\') { |
109 | 0 | buf.append ('\\'); |
110 | 0 | buf.append ('\\'); |
111 | } | |
112 | 0 | else if (ch == '\"') { |
113 | 0 | buf.append ('\\'); |
114 | 0 | buf.append ('\"'); |
115 | } | |
116 | else { | |
117 | 0 | buf.append (ch); |
118 | } | |
119 | } | |
120 | 0 | buf.append ('\"'); |
121 | 0 | 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 | 0 | if (isJavaIdentifier (pValue)) { |
135 | 0 | return pValue; |
136 | } | |
137 | ||
138 | // Return as a String literal | |
139 | else { | |
140 | 0 | 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 | 0 | int len = pValue.length (); |
152 | 0 | if (len == 0) { |
153 | 0 | return false; |
154 | } | |
155 | else { | |
156 | 0 | if (!Character.isJavaIdentifierStart (pValue.charAt (0))) { |
157 | 0 | return false; |
158 | } | |
159 | else { | |
160 | 0 | for (int i = 1; i < len; i++) { |
161 | 0 | if (!Character.isJavaIdentifierPart (pValue.charAt (i))) { |
162 | 0 | return false; |
163 | } | |
164 | } | |
165 | 0 | 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 | 0 | return toStringToken ((String) getValue ()); |
180 | } | |
181 | ||
182 | //------------------------------------- | |
183 | } |