KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.utils.Policy;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.content.IContentDescription;
19 import org.eclipse.core.runtime.content.IContentType;
20
21 /**
22  * A property tester for various properties of files.
23  *
24  * @since 3.2
25  */

26 public class FilePropertyTester extends ResourcePropertyTester {
27
28     /**
29      * A property indicating that we are looking to verify that the file matches
30      * the content type matching the given identifier. The identifier is
31      * provided as the expected value.
32      */

33     private static final String JavaDoc CONTENT_TYPE_ID = "contentTypeId"; //$NON-NLS-1$
34

35     /*
36      * (non-Javadoc)
37      *
38      * @see org.eclipse.core.internal.resources.ResourcePropertyTester#test(java.lang.Object,
39      * java.lang.String, java.lang.Object[], java.lang.Object)
40      */

41     public boolean test(Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] args, Object JavaDoc expectedValue) {
42         if ((receiver instanceof IFile) && method.equals(CONTENT_TYPE_ID))
43             return testContentType((IFile) receiver, toString(expectedValue));
44         return false;
45     }
46
47     /**
48      * Tests whether the content type for <code>file</code> matches the
49      * <code>contentTypeId</code>. It is possible that this method call could
50      * cause the file to be read. It is also possible (through poor plug-in
51      * design) for this method to load plug-ins.
52      *
53      * @param file
54      * The file for which the content type should be determined; must
55      * not be <code>null</code>.
56      * @param contentTypeId
57      * The expected content type; must not be <code>null</code>.
58      * @return <code>true</code> iff the best matching content type has an
59      * identifier that matches <code>contentTypeId</code>;
60      * <code>false</code> otherwise.
61      */

62     private boolean testContentType(final IFile file, String JavaDoc contentTypeId) {
63         final String JavaDoc expectedValue = contentTypeId.trim();
64
65         String JavaDoc actualValue = null;
66         try {
67             IContentDescription contentDescription = file.getContentDescription();
68             if (contentDescription != null) {
69                 IContentType contentType = contentDescription.getContentType();
70                 actualValue = contentType.getId();
71             }
72         } catch (CoreException e) {
73             Policy.log(IStatus.ERROR, "Core exception while retrieving the content description", e);//$NON-NLS-1$
74
}
75         return expectedValue.equals(actualValue);
76     }
77
78 }
79
Popular Tags