1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.el;
18
19
20
21
22
23
24
25
26
27 public class StringLiteral
28 extends Literal
29 {
30
31
32
33
34
35 StringLiteral (Object pValue)
36 {
37 super (pValue);
38 }
39
40
41
42
43
44
45
46 public static StringLiteral fromToken (String pToken)
47 {
48 return new StringLiteral (getValueFromToken (pToken));
49 }
50
51
52
53
54
55
56 public static StringLiteral fromLiteralValue (String pValue)
57 {
58 return new StringLiteral (pValue);
59 }
60
61
62
63
64
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
91
92
93 public static String toStringToken (String pValue)
94 {
95
96 if (pValue.indexOf ('\"') < 0 &&
97 pValue.indexOf ('\\') < 0) {
98 return "\"" + pValue + "\"";
99 }
100
101
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
129
130
131 public static String toIdentifierToken (String pValue)
132 {
133
134 if (isJavaIdentifier (pValue)) {
135 return pValue;
136 }
137
138
139 else {
140 return toStringToken (pValue);
141 }
142 }
143
144
145
146
147
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
172
173
174
175
176
177 public String getExpressionString ()
178 {
179 return toStringToken ((String) getValue ());
180 }
181
182
183 }