KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > nls > NLSHint


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.nls;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Properties JavaDoc;
16 import java.util.SortedMap JavaDoc;
17 import java.util.TreeMap JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22
23 import org.eclipse.core.filebuffers.FileBuffers;
24 import org.eclipse.core.filebuffers.ITextFileBuffer;
25 import org.eclipse.core.filebuffers.ITextFileBufferManager;
26 import org.eclipse.core.filebuffers.LocationKind;
27
28 import org.eclipse.jface.text.BadLocationException;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.Region;
31
32 import org.eclipse.osgi.util.NLS;
33
34 import org.eclipse.jdt.core.ICompilationUnit;
35 import org.eclipse.jdt.core.IJavaElement;
36 import org.eclipse.jdt.core.IJavaProject;
37 import org.eclipse.jdt.core.IPackageFragment;
38 import org.eclipse.jdt.core.JavaModelException;
39 import org.eclipse.jdt.core.Signature;
40 import org.eclipse.jdt.core.compiler.InvalidInputException;
41 import org.eclipse.jdt.core.dom.ASTVisitor;
42 import org.eclipse.jdt.core.dom.CompilationUnit;
43 import org.eclipse.jdt.core.dom.ITypeBinding;
44 import org.eclipse.jdt.core.dom.QualifiedName;
45 import org.eclipse.jdt.core.dom.SimpleName;
46
47 import org.eclipse.jdt.internal.ui.JavaPlugin;
48 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
49
50 /**
51  * calculates hints for the nls-refactoring out of a compilation unit.
52  * - package fragments of the accessor class and the resource bundle
53  * - accessor class name, resource bundle name
54  */

55 public class NLSHint {
56     
57     private String JavaDoc fAccessorName;
58     private IPackageFragment fAccessorPackage;
59     private String JavaDoc fResourceBundleName;
60     private IPackageFragment fResourceBundlePackage;
61     private NLSSubstitution[] fSubstitutions;
62
63     public NLSHint(ICompilationUnit cu, CompilationUnit astRoot) {
64         Assert.isNotNull(cu);
65         Assert.isNotNull(astRoot);
66         
67         IPackageFragment cuPackage= (IPackageFragment) cu.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
68
69         fAccessorName= NLSRefactoring.DEFAULT_ACCESSOR_CLASSNAME;
70         fAccessorPackage= cuPackage;
71         fResourceBundleName= NLSRefactoring.DEFAULT_PROPERTY_FILENAME + NLSRefactoring.PROPERTY_FILE_EXT;
72         fResourceBundlePackage= cuPackage;
73         
74         IJavaProject project= cu.getJavaProject();
75         NLSLine[] lines= createRawLines(cu);
76         
77         AccessorClassReference accessClassRef= findFirstAccessorReference(lines, astRoot);
78         
79         if (accessClassRef == null) {
80             // Look for Eclipse NLS approach
81
List JavaDoc eclipseNLSLines= new ArrayList JavaDoc();
82             accessClassRef= createEclipseNLSLines(getDocument(cu), astRoot, eclipseNLSLines);
83             if (!eclipseNLSLines.isEmpty()) {
84                 NLSLine[] rawLines= lines;
85                 int rawLinesLength= rawLines.length;
86                 int eclipseLinesLength= eclipseNLSLines.size();
87                 lines= new NLSLine[rawLinesLength + eclipseLinesLength];
88                 for (int i= 0; i < rawLinesLength; i++)
89                     lines[i]= rawLines[i];
90                 for (int i= 0; i < eclipseLinesLength; i++)
91                     lines[i+rawLinesLength]= (NLSLine)eclipseNLSLines.get(i);
92             }
93         }
94         
95         Properties JavaDoc props= null;
96         if (accessClassRef != null)
97             props= NLSHintHelper.getProperties(project, accessClassRef);
98         
99         if (props == null)
100             props= new Properties JavaDoc();
101         
102         fSubstitutions= createSubstitutions(lines, props, astRoot);
103         
104         if (accessClassRef != null) {
105             fAccessorName= accessClassRef.getName();
106             ITypeBinding accessorClassBinding= accessClassRef.getBinding();
107             
108             try {
109                 IPackageFragment accessorPack= NLSHintHelper.getPackageOfAccessorClass(project, accessorClassBinding);
110                 if (accessorPack != null) {
111                     fAccessorPackage= accessorPack;
112                 }
113                 
114                 String JavaDoc fullBundleName= accessClassRef.getResourceBundleName();
115                 if (fullBundleName != null) {
116                     fResourceBundleName= Signature.getSimpleName(fullBundleName) + NLSRefactoring.PROPERTY_FILE_EXT;
117                     String JavaDoc packName= Signature.getQualifier(fullBundleName);
118                     
119                     IPackageFragment pack= NLSHintHelper.getResourceBundlePackage(project, packName, fResourceBundleName);
120                     if (pack != null) {
121                         fResourceBundlePackage= pack;
122                     }
123                 }
124             } catch (JavaModelException e) {
125             }
126         }
127     }
128     
129     private AccessorClassReference createEclipseNLSLines(final IDocument document, CompilationUnit astRoot, List JavaDoc nlsLines) {
130         
131         final AccessorClassReference[] firstAccessor= new AccessorClassReference[1];
132         final SortedMap JavaDoc lineToNLSLine= new TreeMap JavaDoc();
133         
134         astRoot.accept(new ASTVisitor() {
135             
136             private ICompilationUnit fCache_CU;
137             private CompilationUnit fCache_AST;
138
139             public boolean visit(QualifiedName node) {
140                 ITypeBinding type= node.getQualifier().resolveTypeBinding();
141                 if (type != null) {
142                     ITypeBinding superType= type.getSuperclass();
143                     if (superType != null && NLS.class.getName().equals(superType.getQualifiedName())) {
144                         Integer JavaDoc line;
145                         try {
146                             line = new Integer JavaDoc(document.getLineOfOffset(node.getStartPosition()));
147                         } catch (BadLocationException e) {
148                             return true; // ignore and continue
149
}
150                         NLSLine nlsLine= (NLSLine)lineToNLSLine.get(line);
151                         if (nlsLine == null) {
152                             nlsLine= new NLSLine(line.intValue());
153                             lineToNLSLine.put(line, nlsLine);
154                         }
155                         SimpleName name= node.getName();
156                         NLSElement element= new NLSElement(node.getName().getIdentifier(), name.getStartPosition(),
157                                 name.getLength(), nlsLine.size() - 1, true);
158                         nlsLine.add(element);
159                         String JavaDoc bundleName;
160                         try {
161                             ICompilationUnit bundleCU= (ICompilationUnit)type.getJavaElement().getAncestor(IJavaElement.COMPILATION_UNIT);
162                             if (fCache_CU == null || !fCache_CU.equals(bundleCU) || fCache_AST == null) {
163                                 fCache_CU= bundleCU;
164                                 if (fCache_CU != null)
165                                     fCache_AST= JavaPlugin.getDefault().getASTProvider().getAST(fCache_CU, ASTProvider.WAIT_YES, null);
166                                 else
167                                     fCache_AST= null;
168                             }
169                             bundleName = NLSHintHelper.getResourceBundleName(fCache_AST);
170                         } catch (JavaModelException e) {
171                             return true; // ignore this accessor and continue
172
}
173                         element.setAccessorClassReference(new AccessorClassReference(type, bundleName, new Region(node.getStartPosition(), node.getLength())));
174                         
175                         if (firstAccessor[0] == null)
176                             firstAccessor[0]= element.getAccessorClassReference();
177                         
178                     }
179                 }
180                 return true;
181             }
182         });
183         
184         nlsLines.addAll(lineToNLSLine.values());
185         return firstAccessor[0];
186     }
187     
188     private IDocument getDocument(ICompilationUnit cu) {
189         IPath path= cu.getPath();
190         ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
191         try {
192             manager.connect(path, LocationKind.NORMALIZE, null);
193         } catch (CoreException e) {
194             return null;
195         }
196         
197         try {
198             ITextFileBuffer buffer= manager.getTextFileBuffer(path, LocationKind.NORMALIZE);
199             if (buffer != null)
200                 return buffer.getDocument();
201         } finally {
202             try {
203                 manager.disconnect(path, LocationKind.NORMALIZE, null);
204             } catch (CoreException e) {
205                 return null;
206             }
207         }
208         return null;
209     }
210
211     private NLSSubstitution[] createSubstitutions(NLSLine[] lines, Properties JavaDoc props, CompilationUnit astRoot) {
212         List JavaDoc result= new ArrayList JavaDoc();
213         
214         for (int i= 0; i < lines.length; i++) {
215             NLSElement[] elements= lines[i].getElements();
216             for (int j= 0; j < elements.length; j++) {
217                 NLSElement nlsElement= elements[j];
218                 if (nlsElement.hasTag()) {
219                     AccessorClassReference accessorClassReference= NLSHintHelper.getAccessorClassReference(astRoot, nlsElement);
220                     if (accessorClassReference == null) {
221                         // no accessor class => not translated
222
result.add(new NLSSubstitution(NLSSubstitution.IGNORED, stripQuotes(nlsElement.getValue()), nlsElement));
223                     } else {
224                         String JavaDoc key= stripQuotes(nlsElement.getValue());
225                         String JavaDoc value= props.getProperty(key);
226                         result.add(new NLSSubstitution(NLSSubstitution.EXTERNALIZED, key, value, nlsElement, accessorClassReference));
227                     }
228                 } else if (nlsElement.isEclipseNLS()) {
229                     String JavaDoc key= nlsElement.getValue();
230                     result.add(new NLSSubstitution(NLSSubstitution.EXTERNALIZED, key, props.getProperty(key), nlsElement, nlsElement.getAccessorClassReference()));
231                 } else {
232                     result.add(new NLSSubstitution(NLSSubstitution.INTERNALIZED, stripQuotes(nlsElement.getValue()), nlsElement));
233                 }
234             }
235         }
236         return (NLSSubstitution[]) result.toArray(new NLSSubstitution[result.size()]);
237     }
238     
239     private static AccessorClassReference findFirstAccessorReference(NLSLine[] lines, CompilationUnit astRoot) {
240         for (int i= 0; i < lines.length; i++) {
241             NLSElement[] elements= lines[i].getElements();
242             for (int j= 0; j < elements.length; j++) {
243                 NLSElement nlsElement= elements[j];
244                 if (nlsElement.hasTag()) {
245                     AccessorClassReference accessorClassReference= NLSHintHelper.getAccessorClassReference(astRoot, nlsElement);
246                     if (accessorClassReference != null) {
247                         return accessorClassReference;
248                     }
249                 }
250             }
251         }
252         
253         // try to find a access with missing //non-nls tag (bug 75155)
254
for (int i= 0; i < lines.length; i++) {
255             NLSElement[] elements= lines[i].getElements();
256             for (int j= 0; j < elements.length; j++) {
257                 NLSElement nlsElement= elements[j];
258                 if (!nlsElement.hasTag()) {
259                     AccessorClassReference accessorClassReference= NLSHintHelper.getAccessorClassReference(astRoot, nlsElement);
260                     if (accessorClassReference != null) {
261                         return accessorClassReference;
262                     }
263                 }
264             }
265         }
266         return null;
267     }
268
269     private static String JavaDoc stripQuotes(String JavaDoc str) {
270         return str.substring(1, str.length() - 1);
271     }
272
273     private static NLSLine[] createRawLines(ICompilationUnit cu) {
274         try {
275             return NLSScanner.scan(cu);
276         } catch (JavaModelException x) {
277             return new NLSLine[0];
278         } catch (InvalidInputException x) {
279             return new NLSLine[0];
280         }
281     }
282     
283
284     public String JavaDoc getAccessorClassName() {
285         return fAccessorName;
286     }
287     
288 // public boolean isEclipseNLS() {
289
// return fIsEclipseNLS;
290
// }
291

292     public IPackageFragment getAccessorClassPackage() {
293         return fAccessorPackage;
294     }
295
296     public String JavaDoc getResourceBundleName() {
297         return fResourceBundleName;
298     }
299
300     public IPackageFragment getResourceBundlePackage() {
301         return fResourceBundlePackage;
302     }
303
304     public NLSSubstitution[] getSubstitutions() {
305         return fSubstitutions;
306     }
307
308
309 }
310
Popular Tags