KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > propertytester > ResourcePropertyTester


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
20  * A property tester for various properties of resources.
21  *
22  * @since 3.2
23  */

24 public class ResourcePropertyTester extends PropertyTester {
25
26     /**
27      * A property indicating the file extension (value <code>"extension"</code>).
28      * "*" and "?" wild cards are supported.
29      */

30     protected static final String JavaDoc EXTENSION = "extension"; //$NON-NLS-1$
31

32     /**
33      * A property indicating the file name (value <code>"name"</code>). "*"
34      * and "?" wild cards are supported.
35      */

36     protected static final String JavaDoc NAME = "name"; //$NON-NLS-1$
37

38     /**
39      * A property indicating the file path (value <code>"path"</code>). "*"
40      * and "?" wild cards are supported.
41      */

42     protected static final String JavaDoc PATH = "path"; //$NON-NLS-1$
43

44     /**
45      * A property indicating a persistent property on the selected resource
46      * (value <code>"persistentProperty"</code>). If two arguments are given,
47      * this treats the first as the property name, and the second as the expected
48      * property value. If only one argument (or just the expected value) is
49      * given, this treats it as the property name, and simply tests for existence of
50      * the property on the resource.
51      */

52     protected static final String JavaDoc PERSISTENT_PROPERTY = "persistentProperty"; //$NON-NLS-1$
53

54     /**
55      * A property indicating the project nature (value
56      * <code>"projectNature"</code>).
57      */

58     protected static final String JavaDoc PROJECT_NATURE = "projectNature"; //$NON-NLS-1$
59

60     /**
61      * A property indicating a persistent property on the selected resource's
62      * project. (value <code>"projectPersistentProperty"</code>). If two
63      * arguments are given, this treats the first as the property name, and the
64      * second as the expected property value. If only one argument (or just the
65      * expected value) is given, this treats it as the property name, and simply
66      * tests for existence of the property on the resource.
67      */

68     protected static final String JavaDoc PROJECT_PERSISTENT_PROPERTY = "projectPersistentProperty"; //$NON-NLS-1$
69

70     /**
71      * A property indicating a session property on the selected resource's
72      * project. (value <code>"projectSessionProperty"</code>). If two
73      * arguments are given, this treats the first as the property name, and the
74      * second as the expected property value. If only one argument (or just the
75      * expected value) is given, this treats it as the property name, and simply
76      * tests for existence of the property on the resource.
77      */

78     protected static final String JavaDoc PROJECT_SESSION_PROPERTY = "projectSessionProperty"; //$NON-NLS-1$
79

80     /**
81      * A property indicating whether the file is read only (value
82      * <code>"readOnly"</code>).
83      */

84     protected static final String JavaDoc READ_ONLY = "readOnly"; //$NON-NLS-1$
85

86     /**
87      * A property indicating a session property on the selected resource (value
88      * <code>"sessionProperty"</code>). If two arguments are given, this
89      * treats the first as the property name, and the second as the expected
90      * property value. If only one argument (or just the expected value) is
91      * given, this treats it as the property name, and simply tests for existence of
92      * the property on the resource.
93      */

94     protected static final String JavaDoc SESSION_PROPERTY = "sessionProperty"; //$NON-NLS-1$
95

96     /*
97      * (non-Javadoc)
98      *
99      * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object,
100      * java.lang.String, java.lang.Object[], java.lang.Object)
101      */

102     public boolean test(Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] args, Object JavaDoc 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     /**
135      * Tests whether a session or persistent property on the resource or its
136      * project matches the given value.
137      *
138      * @param resource
139      * the resource to check
140      * @param persistentFlag
141      * <code>true</code> for a persistent property,
142      * <code>false</code> for a session property
143      * @param args
144      * additional arguments to evaluate the property.
145      * If of length 0, this treats the expectedValue as the property name
146      * and does a simple check for existence of the property.
147      * If of length 1, this treats the first argument as the property name
148      * and does a simple check for existence of the property.
149      * If of length 2, this treats the first argument as the property name,
150      * the second argument as the expected value, and checks for equality
151      * with the actual property value.
152      * @param expectedValue
153      * used only if args is of length 0 (see Javadoc for args parameter)
154      * @return whether there is a match
155      */

156     protected boolean testProperty(IResource resource, boolean persistentFlag, Object JavaDoc[] args, Object JavaDoc expectedValue) {
157         //the project of IWorkspaceRoot is null
158
if (resource == null)
159             return false;
160         String JavaDoc propertyName;
161         String JavaDoc 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 JavaDoc 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             //if the resource is not accessible, fall through and return false below
180
}
181         return false;
182     }
183
184     /**
185      * Converts the given expected value to a boolean.
186      *
187      * @param expectedValue
188      * the expected value (may be <code>null</code>).
189      * @return <code>false</code> if the expected value equals Boolean.FALSE,
190      * <code>true</code> otherwise
191      */

192     protected boolean toBoolean(Object JavaDoc expectedValue) {
193         if (expectedValue instanceof Boolean JavaDoc) {
194             return ((Boolean JavaDoc) expectedValue).booleanValue();
195         }
196         return true;
197     }
198
199     /**
200      * Converts the given name to a qualified name.
201      *
202      * @param name the name
203      * @return the qualified name
204      */

205     protected QualifiedName toQualifedName(String JavaDoc 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     /**
217      * Converts the given expected value to a <code>String</code>.
218      *
219      * @param expectedValue
220      * the expected value (may be <code>null</code>).
221      * @return the empty string if the expected value is <code>null</code>,
222      * otherwise the <code>toString()</code> representation of the
223      * expected value
224      */

225     protected String JavaDoc toString(Object JavaDoc expectedValue) {
226         return expectedValue == null ? "" : expectedValue.toString(); //$NON-NLS-1$
227
}
228 }
229
Popular Tags