1 55 56 package org.apache.commons.el; 57 58 65 66 public class StringLiteral 67 extends Literal 68 { 69 74 StringLiteral (Object pValue) 75 { 76 super (pValue); 77 } 78 79 85 public static StringLiteral fromToken (String pToken) 86 { 87 return new StringLiteral (getValueFromToken (pToken)); 88 } 89 90 95 public static StringLiteral fromLiteralValue (String pValue) 96 { 97 return new StringLiteral (pValue); 98 } 99 100 105 public static String getValueFromToken (String pToken) 106 { 107 StringBuffer buf = new StringBuffer (); 108 int len = pToken.length () - 1; 109 boolean escaping = false; 110 for (int i = 1; i < len; i++) { 111 char ch = pToken.charAt (i); 112 if (escaping) { 113 buf.append (ch); 114 escaping = false; 115 } 116 else if (ch == '\\') { 117 escaping = true; 118 } 119 else { 120 buf.append (ch); 121 } 122 } 123 return buf.toString (); 124 } 125 126 132 public static String toStringToken (String pValue) 133 { 134 if (pValue.indexOf ('\"') < 0 && 136 pValue.indexOf ('\\') < 0) { 137 return "\"" + pValue + "\""; 138 } 139 140 else { 142 StringBuffer buf = new StringBuffer (); 143 buf.append ('\"'); 144 int len = pValue.length (); 145 for (int i = 0; i < len; i++) { 146 char ch = pValue.charAt (i); 147 if (ch == '\\') { 148 buf.append ('\\'); 149 buf.append ('\\'); 150 } 151 else if (ch == '\"') { 152 buf.append ('\\'); 153 buf.append ('\"'); 154 } 155 else { 156 buf.append (ch); 157 } 158 } 159 buf.append ('\"'); 160 return buf.toString (); 161 } 162 } 163 164 170 public static String toIdentifierToken (String pValue) 171 { 172 if (isJavaIdentifier (pValue)) { 174 return pValue; 175 } 176 177 else { 179 return toStringToken (pValue); 180 } 181 } 182 183 188 static boolean isJavaIdentifier (String pValue) 189 { 190 int len = pValue.length (); 191 if (len == 0) { 192 return false; 193 } 194 else { 195 if (!Character.isJavaIdentifierStart (pValue.charAt (0))) { 196 return false; 197 } 198 else { 199 for (int i = 1; i < len; i++) { 200 if (!Character.isJavaIdentifierPart (pValue.charAt (i))) { 201 return false; 202 } 203 } 204 return true; 205 } 206 } 207 } 208 209 216 public String getExpressionString () 217 { 218 return toStringToken ((String ) getValue ()); 219 } 220 221 } 223 | Popular Tags |