1 11 12 package org.eclipse.core.internal.propertytester; 13 14 import org.eclipse.core.expressions.PropertyTester; 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.QualifiedName; 18 19 24 public class ResourcePropertyTester extends PropertyTester { 25 26 30 protected static final String EXTENSION = "extension"; 32 36 protected static final String NAME = "name"; 38 42 protected static final String PATH = "path"; 44 52 protected static final String PERSISTENT_PROPERTY = "persistentProperty"; 54 58 protected static final String PROJECT_NATURE = "projectNature"; 60 68 protected static final String PROJECT_PERSISTENT_PROPERTY = "projectPersistentProperty"; 70 78 protected static final String PROJECT_SESSION_PROPERTY = "projectSessionProperty"; 80 84 protected static final String READ_ONLY = "readOnly"; 86 94 protected static final String SESSION_PROPERTY = "sessionProperty"; 96 102 public boolean test(Object receiver, String method, Object [] args, Object expectedValue) { 103 if (!(receiver instanceof IResource)) 104 return false; 105 IResource res = (IResource) receiver; 106 if (method.equals(NAME)) { 107 return new StringMatcher(toString(expectedValue)).match(res.getName()); 108 } else if (method.equals(PATH)) { 109 return new StringMatcher(toString(expectedValue)).match(res.getFullPath().toString()); 110 } else if (method.equals(EXTENSION)) { 111 return new StringMatcher(toString(expectedValue)).match(res.getFileExtension()); 112 } else if (method.equals(READ_ONLY)) { 113 ResourceAttributes attr = res.getResourceAttributes(); 114 return (attr != null && attr.isReadOnly()) == toBoolean(expectedValue); 115 } else if (method.equals(PROJECT_NATURE)) { 116 try { 117 IProject proj = res.getProject(); 118 return proj != null && proj.isAccessible() && proj.hasNature(toString(expectedValue)); 119 } catch (CoreException e) { 120 return false; 121 } 122 } else if (method.equals(PERSISTENT_PROPERTY)) { 123 return testProperty(res, true, args, expectedValue); 124 } else if (method.equals(PROJECT_PERSISTENT_PROPERTY)) { 125 return testProperty(res.getProject(), true, args, expectedValue); 126 } else if (method.equals(SESSION_PROPERTY)) { 127 return testProperty(res, false, args, expectedValue); 128 } else if (method.equals(PROJECT_SESSION_PROPERTY)) { 129 return testProperty(res.getProject(), false, args, expectedValue); 130 } 131 return false; 132 } 133 134 156 protected boolean testProperty(IResource resource, boolean persistentFlag, Object [] args, Object expectedValue) { 157 if (resource == null) 159 return false; 160 String propertyName; 161 String expectedVal; 162 if (args.length == 0) { 163 propertyName = toString(expectedValue); 164 expectedVal = null; 165 } else if (args.length == 1) { 166 propertyName = toString(args[0]); 167 expectedVal = null; 168 } else { 169 propertyName = toString(args[0]); 170 expectedVal = toString(args[1]); 171 } 172 try { 173 QualifiedName key = toQualifedName(propertyName); 174 Object actualVal = persistentFlag ? resource.getPersistentProperty(key) : resource.getSessionProperty(key); 175 if (actualVal == null) 176 return false; 177 return expectedVal == null || expectedVal.equals(actualVal.toString()); 178 } catch (CoreException e) { 179 } 181 return false; 182 } 183 184 192 protected boolean toBoolean(Object expectedValue) { 193 if (expectedValue instanceof Boolean ) { 194 return ((Boolean ) expectedValue).booleanValue(); 195 } 196 return true; 197 } 198 199 205 protected QualifiedName toQualifedName(String name) { 206 QualifiedName key; 207 int dot = name.lastIndexOf('.'); 208 if (dot != -1) { 209 key = new QualifiedName(name.substring(0, dot), name.substring(dot + 1)); 210 } else { 211 key = new QualifiedName(null, name); 212 } 213 return key; 214 } 215 216 225 protected String toString(Object expectedValue) { 226 return expectedValue == null ? "" : expectedValue.toString(); } 228 } 229 | Popular Tags |