1 11 package org.eclipse.debug.internal.ui; 12 13 import java.io.File ; 14 import java.io.FileNotFoundException ; 15 import java.io.FileReader ; 16 import java.io.IOException ; 17 import java.util.regex.Pattern ; 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 38 public class ResourceExtender extends PropertyTester { 39 40 private static final String PROPERTY_MATCHES_PATTERN = "matchesPattern"; 42 private static final String PROJECT_NATURE = "projectNature"; 44 private static final String PROPERTY_MATCHES_CONTENT_TYPE = "matchesContentType"; 46 49 public boolean test(Object receiver, String method, Object [] args, Object 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 file= path.toFile(); 57 if (file.exists()) { 58 try { 59 FileReader reader= new FileReader (file); 60 IContentType contentType= Platform.getContentTypeManager().getContentType((String )expectedValue); 61 IContentDescription description= contentType.getDescriptionFor(reader, IContentDescription.ALL); 62 reader.close(); 63 if (description != null) { 64 return matchesContentType(description.getContentType(), (String )expectedValue); 65 } 66 } catch (FileNotFoundException e) { 67 return false; 68 } catch (IOException e) { 69 return false; 70 } 71 } 72 } 73 } 74 } else { 75 if (PROPERTY_MATCHES_PATTERN.equals(method)) { 76 String fileName = resource.getName(); 77 String expected = (String ) expectedValue; 78 expected = expected.replaceAll("\\.", "\\\\."); expected = expected.replaceAll("\\*", "\\.\\*"); Pattern 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 ) expectedValue); 87 } catch (CoreException e) { 88 return false; 89 } 90 } else if (PROPERTY_MATCHES_CONTENT_TYPE.equals(method)) { 91 return matchesContentType(resource, (String ) expectedValue); 92 } 93 } 94 return false; 95 } 96 97 105 private boolean matchesContentType(IContentType type, String 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 123 private boolean matchesContentType(IResource resource, String 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 |