KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > ResourceExtender


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.debug.internal.ui;
12
13 import java.io.File JavaDoc;
14 import java.io.FileNotFoundException JavaDoc;
15 import java.io.FileReader JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.regex.Pattern JavaDoc;
18
19 import org.eclipse.core.expressions.PropertyTester;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IAdaptable;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.content.IContentDescription;
28 import org.eclipse.core.runtime.content.IContentType;
29 import org.eclipse.ui.IPathEditorInput;
30
31 /**
32  * ResourceExtender provides property testers for the XML expression language
33  * evaluation. We provide a copy in Debug so that launch shortcuts can add
34  * contextual launch enablement that does not require their plugins to be
35  * loaded. Copied from
36  * org.eclipse.jdt.internal.corext.refactoring.participants.xml.ResourceExtender
37  */

38 public class ResourceExtender extends PropertyTester {
39
40     private static final String JavaDoc PROPERTY_MATCHES_PATTERN = "matchesPattern"; //$NON-NLS-1$
41

42     private static final String JavaDoc PROJECT_NATURE = "projectNature"; //$NON-NLS-1$
43

44     private static final String JavaDoc PROPERTY_MATCHES_CONTENT_TYPE = "matchesContentType"; //$NON-NLS-1$
45

46     /* (non-Javadoc)
47      * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
48      */

49     public boolean test(Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] args, Object JavaDoc expectedValue) {
50         IResource resource = (IResource) ((IAdaptable) receiver).getAdapter(IResource.class);
51         if (resource == null) {
52             if (PROPERTY_MATCHES_CONTENT_TYPE.equals(method)) {
53                 IPathEditorInput editorInput = (IPathEditorInput) ((IAdaptable) receiver).getAdapter(IPathEditorInput.class);
54                 if (editorInput != null) {
55                     IPath path= editorInput.getPath();
56                     File JavaDoc file= path.toFile();
57                     if (file.exists()) {
58                         try {
59                             FileReader JavaDoc reader= new FileReader JavaDoc(file);
60                             IContentType contentType= Platform.getContentTypeManager().getContentType((String JavaDoc)expectedValue);
61                             IContentDescription description= contentType.getDescriptionFor(reader, IContentDescription.ALL);
62                             reader.close();
63                             if (description != null) {
64                                 return matchesContentType(description.getContentType(), (String JavaDoc)expectedValue);
65                             }
66                         } catch (FileNotFoundException JavaDoc e) {
67                             return false;
68                         } catch (IOException JavaDoc e) {
69                             return false;
70                         }
71                     }
72                 }
73             }
74         } else {
75             if (PROPERTY_MATCHES_PATTERN.equals(method)) {
76                 String JavaDoc fileName = resource.getName();
77                 String JavaDoc expected = (String JavaDoc) expectedValue;
78                 expected = expected.replaceAll("\\.", "\\\\."); //$NON-NLS-1$//$NON-NLS-2$
79
expected = expected.replaceAll("\\*", "\\.\\*"); //$NON-NLS-1$//$NON-NLS-2$
80
Pattern JavaDoc pattern = Pattern.compile(expected);
81                 boolean retVal = pattern.matcher(fileName).find();
82                 return retVal;
83             } else if (PROJECT_NATURE.equals(method)) {
84                 try {
85                     IProject proj = resource.getProject();
86                     return proj.isAccessible() && proj.hasNature((String JavaDoc) expectedValue);
87                 } catch (CoreException e) {
88                     return false;
89                 }
90             } else if (PROPERTY_MATCHES_CONTENT_TYPE.equals(method)) {
91                 return matchesContentType(resource, (String JavaDoc) expectedValue);
92             }
93         }
94         return false;
95     }
96     
97     /**
98      * Returns whether the given type or one of its base types matches the
99      * given content type identifier.
100      *
101      * @param type content type or <code>null</code>
102      * @param typeId content type identifier
103      * @return
104      */

105     private boolean matchesContentType(IContentType type, String JavaDoc typeId) {
106         while (type != null) {
107             if (typeId.equals(type.getId())) {
108                 return true;
109             }
110             type = type.getBaseType();
111         }
112         return false;
113     }
114
115     /**
116      * Returns whether or not the given file's content type matches the
117      * specified content type.
118      *
119      * Content types are looked up in the content type registry.
120      *
121      * @return whether or not the given resource has the given content type
122      */

123     private boolean matchesContentType(IResource resource, String JavaDoc contentType) {
124         if (resource == null || !(resource instanceof IFile) || !resource.exists()) {
125             return false;
126         }
127         IFile file = (IFile) resource;
128         IContentDescription description;
129         try {
130             description = file.getContentDescription();
131         } catch (CoreException e) {
132             return false;
133         }
134         if (description != null) {
135             return matchesContentType(description.getContentType(), contentType);
136         }
137         return false;
138     }
139
140 }
141
Popular Tags