1 11 package org.eclipse.core.internal.expressions; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.List ; 16 17 import org.w3c.dom.Element ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IAdapterManager; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.Platform; 23 24 import org.eclipse.core.expressions.Expression; 25 import org.eclipse.core.expressions.ICountable; 26 import org.eclipse.core.expressions.IIterable; 27 28 public class Expressions { 29 30 31 public static final boolean TRACING; 32 static { 33 String value= Platform.getDebugOption("org.eclipse.core.expressions/tracePropertyResolving"); TRACING= value != null && value.equalsIgnoreCase("true"); } 36 37 private Expressions() { 38 } 40 41 public static boolean isInstanceOf(Object element, String type) { 42 if (element == null) 44 return false; 45 return isSubtype(element.getClass(), type); 46 } 47 48 private static boolean isSubtype(Class clazz, String type) { 49 if (clazz.getName().equals(type)) 50 return true; 51 Class superClass= clazz.getSuperclass(); 52 if (superClass != null && isSubtype(superClass, type)) 53 return true; 54 Class [] interfaces= clazz.getInterfaces(); 55 for (int i= 0; i < interfaces.length; i++) { 56 if (isSubtype(interfaces[i], type)) 57 return true; 58 } 59 return false; 60 } 61 62 public static void checkAttribute(String name, String value) throws CoreException { 63 if (value == null) { 64 throw new CoreException(new ExpressionStatus( 65 ExpressionStatus.MISSING_ATTRIBUTE, 66 Messages.format(ExpressionMessages.Expression_attribute_missing, name))); 67 } 68 } 69 70 public static void checkAttribute(String name, String value, String [] validValues) throws CoreException { 71 checkAttribute(name, value); 72 for (int i= 0; i < validValues.length; i++) { 73 if (value.equals(validValues[i])) 74 return; 75 } 76 throw new CoreException(new ExpressionStatus( 77 ExpressionStatus.WRONG_ATTRIBUTE_VALUE, 78 Messages.format(ExpressionMessages.Expression_attribute_invalid_value, value))); 79 } 80 81 public static void checkCollection(Object var, Expression expression) throws CoreException { 82 if (var instanceof Collection ) 83 return; 84 throw new CoreException(new ExpressionStatus( 85 ExpressionStatus.VARIABLE_IS_NOT_A_COLLECTION, 86 Messages.format(ExpressionMessages.Expression_variable_not_a_collection, expression.toString()))); 87 } 88 89 public static void checkList(Object var, Expression expression) throws CoreException { 90 if (var instanceof List ) 91 return; 92 throw new CoreException(new ExpressionStatus( 93 ExpressionStatus.VARIABLE_IS_NOT_A_LIST, 94 Messages.format(ExpressionMessages.Expression_variable_not_a_list, expression.toString()))); 95 } 96 97 108 public static IIterable getAsIIterable(Object var, Expression expression) throws CoreException { 109 if (var instanceof IIterable) { 110 return (IIterable)var; 111 } else { 112 IAdapterManager manager= Platform.getAdapterManager(); 113 IIterable result= (IIterable)manager.getAdapter(var, IIterable.class); 114 if (result != null) 115 return result; 116 117 if (manager.queryAdapter(var, IIterable.class.getName()) == IAdapterManager.NOT_LOADED) 118 return null; 119 120 throw new CoreException(new ExpressionStatus( 121 ExpressionStatus.VARIABLE_IS_NOT_A_COLLECTION, 122 Messages.format(ExpressionMessages.Expression_variable_not_iterable, expression.toString()))); 123 } 124 } 125 126 137 public static ICountable getAsICountable(Object var, Expression expression) throws CoreException { 138 if (var instanceof ICountable) { 139 return (ICountable)var; 140 } else { 141 IAdapterManager manager= Platform.getAdapterManager(); 142 ICountable result= (ICountable)manager.getAdapter(var, ICountable.class); 143 if (result != null) 144 return result; 145 146 if (manager.queryAdapter(var, ICountable.class.getName()) == IAdapterManager.NOT_LOADED) 147 return null; 148 149 throw new CoreException(new ExpressionStatus( 150 ExpressionStatus.VARIABLE_IS_NOT_A_COLLECTION, 151 Messages.format(ExpressionMessages.Expression_variable_not_countable, expression.toString()))); 152 } 153 } 154 155 public static boolean getOptionalBooleanAttribute(IConfigurationElement element, String attributeName) { 156 String value= element.getAttribute(attributeName); 157 if (value == null) 158 return false; 159 return Boolean.valueOf(value).booleanValue(); 160 } 161 162 public static boolean getOptionalBooleanAttribute(Element element, String attributeName) { 163 String value= element.getAttribute(attributeName); 164 if (value.length() == 0) 165 return false; 166 return Boolean.valueOf(value).booleanValue(); 167 } 168 169 171 public static final Object [] EMPTY_ARGS= new Object [0]; 172 173 public static Object [] getArguments(IConfigurationElement element, String attributeName) throws CoreException { 174 String args= element.getAttribute(attributeName); 175 if (args != null) { 176 return parseArguments(args); 177 } else { 178 return EMPTY_ARGS; 179 } 180 } 181 182 public static Object [] getArguments(Element element, String attributeName) throws CoreException { 183 String args= element.getAttribute(attributeName); 184 if (args.length() > 0) { 185 return parseArguments(args); 186 } else { 187 return EMPTY_ARGS; 188 } 189 } 190 191 public static Object [] parseArguments(String args) throws CoreException { 192 List result= new ArrayList (); 193 int start= 0; 194 int comma; 195 while ((comma= findNextComma(args, start)) != -1) { 196 result.add(convertArgument(args.substring(start, comma).trim())); 197 start= comma + 1; 198 } 199 result.add(convertArgument(args.substring(start).trim())); 200 return result.toArray(); 201 } 202 203 private static int findNextComma(String str, int start) throws CoreException { 204 boolean inString= false; 205 for (int i= start; i < str.length(); i++) { 206 char ch= str.charAt(i); 207 if (ch == ',' && ! inString) 208 return i; 209 if (ch == '\'') { 210 if (!inString) { 211 inString= true; 212 } else { 213 if (i + 1 < str.length() && str.charAt(i + 1) == '\'') { 214 i++; 215 } else { 216 inString= false; 217 } 218 } 219 } else if (ch == ',' && !inString) { 220 return i; 221 } 222 } 223 if (inString) 224 throw new CoreException(new ExpressionStatus( 225 ExpressionStatus.STRING_NOT_TERMINATED, 226 Messages.format(ExpressionMessages.Expression_string_not_terminated, str))); 227 228 return -1; 229 } 230 231 public static Object convertArgument(String arg) throws CoreException { 232 if (arg == null) { 233 return null; 234 } else if (arg.length() == 0) { 235 return arg; 236 } else if (arg.charAt(0) == '\'' && arg.charAt(arg.length() - 1) == '\'') { 237 return unEscapeString(arg.substring(1, arg.length() - 1)); 238 } else if ("true".equals(arg)) { return Boolean.TRUE; 240 } else if ("false".equals(arg)) { return Boolean.FALSE; 242 } else if (arg.indexOf('.') != -1) { 243 try { 244 return Float.valueOf(arg); 245 } catch (NumberFormatException e) { 246 return arg; 247 } 248 } else { 249 try { 250 return Integer.valueOf(arg); 251 } catch (NumberFormatException e) { 252 return arg; 253 } 254 } 255 } 256 257 public static String unEscapeString(String str) throws CoreException { 258 StringBuffer result= new StringBuffer (); 259 for (int i= 0; i < str.length(); i++) { 260 char ch= str.charAt(i); 261 if (ch == '\'') { 262 if (i == str.length() - 1 || str.charAt(i + 1) != '\'') 263 throw new CoreException(new ExpressionStatus( 264 ExpressionStatus.STRING_NOT_CORRECT_ESCAPED, 265 Messages.format(ExpressionMessages.Expression_string_not_correctly_escaped, str))); 266 result.append('\''); 267 i++; 268 } else { 269 result.append(ch); 270 } 271 } 272 return result.toString(); 273 } 274 } 275 | Popular Tags |