KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > util > QualifiedNameFinder


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.jdt.internal.corext.refactoring.util;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16 import java.util.regex.Pattern JavaDoc;
17
18 import org.eclipse.text.edits.ReplaceEdit;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.NullProgressMonitor;
25
26 import org.eclipse.core.filebuffers.FileBuffers;
27
28 import org.eclipse.core.resources.IFile;
29 import org.eclipse.core.resources.IProject;
30 import org.eclipse.core.resources.IResource;
31
32 import org.eclipse.search.core.text.TextSearchEngine;
33 import org.eclipse.search.core.text.TextSearchMatchAccess;
34 import org.eclipse.search.core.text.TextSearchRequestor;
35 import org.eclipse.search.core.text.TextSearchScope;
36
37 import org.eclipse.ltk.core.refactoring.GroupCategory;
38 import org.eclipse.ltk.core.refactoring.GroupCategorySet;
39 import org.eclipse.ltk.core.refactoring.TextChange;
40
41 import org.eclipse.jdt.core.IJavaElement;
42 import org.eclipse.jdt.core.JavaCore;
43
44 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
45 import org.eclipse.jdt.internal.corext.refactoring.changes.TextChangeCompatibility;
46
47 import org.eclipse.jdt.internal.ui.util.PatternConstructor;
48
49 public class QualifiedNameFinder {
50
51     private static final GroupCategorySet QUALIFIED_NAMES= new GroupCategorySet(
52         new GroupCategory("org.eclipse.jdt.internal.corext.qualifiedNames", //$NON-NLS-1$
53
RefactoringCoreMessages.QualifiedNameFinder_qualifiedNames_name,
54             RefactoringCoreMessages.QualifiedNameFinder_qualifiedNames_description));
55     
56     private static class ResultCollector extends TextSearchRequestor {
57         
58         private String JavaDoc fNewValue;
59         private QualifiedNameSearchResult fResult;
60         
61         public ResultCollector(QualifiedNameSearchResult result, String JavaDoc newValue) {
62             fResult= result;
63             fNewValue= newValue;
64         }
65         
66         public boolean acceptFile(IFile file) throws CoreException {
67             IJavaElement element= JavaCore.create(file);
68             if ((element != null && element.exists()))
69                 return false;
70             
71             // Only touch text files (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=114153 ):
72
if (! FileBuffers.getTextFileBufferManager().isTextFileLocation(file.getFullPath(), false))
73                 return false;
74             
75             IPath path= file.getProjectRelativePath();
76             String JavaDoc segment= path.segment(0);
77             if (segment != null && (segment.startsWith(".refactorings") || segment.startsWith(".deprecations"))) //$NON-NLS-1$ //$NON-NLS-2$
78
return false;
79
80             return true;
81         }
82         
83         public boolean acceptPatternMatch(TextSearchMatchAccess matchAccess) throws CoreException {
84             int start= matchAccess.getMatchOffset();
85             int length= matchAccess.getMatchLength();
86             
87             // skip embedded FQNs (bug 130764):
88
if (start > 0) {
89                 char before= matchAccess.getFileContentChar(start - 1);
90                 if (before == '.' || Character.isJavaIdentifierPart(before))
91                     return true;
92             }
93             int fileContentLength= matchAccess.getFileContentLength();
94             int end= start + length;
95             if (end < fileContentLength) {
96                 char after= matchAccess.getFileContentChar(end);
97                 if (Character.isJavaIdentifierPart(after))
98                     return true;
99             }
100             
101             IFile file= matchAccess.getFile();
102             TextChange change= fResult.getChange(file);
103             TextChangeCompatibility.addTextEdit(
104                 change,
105                 RefactoringCoreMessages.QualifiedNameFinder_update_name,
106                 new ReplaceEdit(start, length, fNewValue), QUALIFIED_NAMES);
107             
108             return true;
109         }
110     }
111         
112     public QualifiedNameFinder() {
113     }
114     
115     public static void process(QualifiedNameSearchResult result, String JavaDoc pattern, String JavaDoc newValue, String JavaDoc filePatterns, IProject root, IProgressMonitor monitor) {
116         Assert.isNotNull(pattern);
117         Assert.isNotNull(newValue);
118         Assert.isNotNull(root);
119         
120         if (monitor == null)
121             monitor= new NullProgressMonitor();
122         
123         if (filePatterns == null || filePatterns.length() == 0) {
124             // Eat progress.
125
monitor.beginTask("", 1); //$NON-NLS-1$
126
monitor.worked(1);
127             return;
128         }
129
130         ResultCollector collector= new ResultCollector(result, newValue);
131         TextSearchEngine engine= TextSearchEngine.create();
132         Pattern JavaDoc searchPattern= PatternConstructor.createPattern(pattern, true, false);
133         
134         engine.search(createScope(filePatterns, root), collector, searchPattern, monitor);
135     }
136     
137     private static TextSearchScope createScope(String JavaDoc filePatterns, IProject root) {
138         HashSet JavaDoc res= new HashSet JavaDoc();
139         res.add(root);
140         addReferencingProjects(root, res);
141         IResource[] resArr= (IResource[]) res.toArray(new IResource[res.size()]);
142         Pattern JavaDoc filePattern= getFilePattern(filePatterns);
143         
144         return TextSearchScope.newSearchScope(resArr, filePattern, false);
145     }
146     
147     private static Pattern JavaDoc getFilePattern(String JavaDoc filePatterns) {
148         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(filePatterns, ","); //$NON-NLS-1$
149
String JavaDoc[] filePatternArray= new String JavaDoc[tokenizer.countTokens()];
150         int i= 0;
151         while (tokenizer.hasMoreTokens()) {
152             filePatternArray[i++]= tokenizer.nextToken().trim();
153         }
154         return PatternConstructor.createPattern(filePatternArray, true, false);
155     }
156     
157     private static void addReferencingProjects(IProject root, Set JavaDoc res) {
158         IProject[] projects= root.getReferencingProjects();
159         for (int i= 0; i < projects.length; i++) {
160             IProject project= projects[i];
161             if (res.add(project)) {
162                 addReferencingProjects(project, res);
163             }
164         }
165     }
166 }
167
Popular Tags