1 11 package org.eclipse.jdt.internal.corext.refactoring.nls; 12 13 import com.ibm.icu.text.Collator; 14 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.eclipse.text.edits.TextEdit; 23 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 import org.eclipse.core.runtime.IStatus; 28 29 import org.eclipse.core.resources.IFile; 30 import org.eclipse.core.resources.IProject; 31 import org.eclipse.core.resources.IResource; 32 import org.eclipse.core.resources.ResourcesPlugin; 33 34 import org.eclipse.ltk.core.refactoring.Change; 35 36 import org.eclipse.jdt.core.ICompilationUnit; 37 import org.eclipse.jdt.core.IJavaElement; 38 import org.eclipse.jdt.core.IJavaProject; 39 import org.eclipse.jdt.core.IPackageFragment; 40 import org.eclipse.jdt.core.IPackageFragmentRoot; 41 import org.eclipse.jdt.core.JavaCore; 42 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 43 import org.eclipse.jdt.core.formatter.CodeFormatter; 44 45 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 46 import org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange; 47 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil; 48 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 49 50 import org.eclipse.jdt.ui.CodeGeneration; 51 52 import org.eclipse.jdt.internal.ui.JavaPlugin; 53 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 54 import org.eclipse.jdt.internal.ui.preferences.MembersOrderPreferenceCache; 55 56 public class AccessorClassCreator { 57 58 private final ICompilationUnit fCu; 59 private final String fAccessorClassName; 60 private final IPath fAccessorPath; 61 private final IPath fResourceBundlePath; 62 private final IPackageFragment fAccessorPackage; 63 private final boolean fIsEclipseNLS; 64 private final NLSSubstitution[] fNLSSubstitutions; 65 private final String fSubstitutionPattern; 66 67 private AccessorClassCreator(ICompilationUnit cu, String accessorClassname, IPath accessorPath, IPackageFragment accessorPackage, IPath resourceBundlePath, boolean isEclipseNLS, NLSSubstitution[] nlsSubstitutions, String substitutionPattern) { 68 fCu= cu; 69 fAccessorClassName= accessorClassname; 70 fAccessorPath= accessorPath; 71 fAccessorPackage= accessorPackage; 72 fResourceBundlePath= resourceBundlePath; 73 fIsEclipseNLS= isEclipseNLS; 74 fNLSSubstitutions= nlsSubstitutions; 75 fSubstitutionPattern= substitutionPattern; 76 } 77 78 public static Change create(ICompilationUnit cu, String accessorClassname, IPath accessorPath, IPackageFragment accessorPackage, IPath resourceBundlePath, boolean isEclipseNLS, NLSSubstitution[] nlsSubstitutions, String substitutionPattern, IProgressMonitor pm) throws CoreException { 79 AccessorClassCreator accessorClass= new AccessorClassCreator(cu, accessorClassname, accessorPath, accessorPackage, resourceBundlePath, isEclipseNLS, nlsSubstitutions, substitutionPattern); 80 81 return new CreateTextFileChange(accessorPath, accessorClass.createAccessorCUSource(pm), null, "java"); } 83 84 private String createAccessorCUSource(IProgressMonitor pm) throws CoreException { 85 IProject project= getFileHandle(fAccessorPath).getProject(); 86 String lineDelimiter= StubUtility.getLineDelimiterPreference(project); 87 return CodeFormatterUtil.format(CodeFormatter.K_COMPILATION_UNIT, getUnformattedSource(pm), 0, null, lineDelimiter, fCu.getJavaProject()); 88 } 89 90 private static IFile getFileHandle(IPath filePath) { 91 if (filePath == null) 92 return null; 93 return ResourcesPlugin.getWorkspace().getRoot().getFile(filePath); 94 } 95 96 private String getUnformattedSource(IProgressMonitor pm) throws CoreException { 97 ICompilationUnit newCu= null; 98 try { 99 newCu= fAccessorPackage.getCompilationUnit(fAccessorPath.lastSegment()).getWorkingCopy(null); 100 101 String typeComment= null, fileComment= null; 102 final IJavaProject project= newCu.getJavaProject(); 103 final String lineDelim= StubUtility.getLineDelimiterUsed(project); 104 if (StubUtility.doAddComments(project)) { 105 typeComment= CodeGeneration.getTypeComment(newCu, fAccessorClassName, lineDelim); 106 fileComment= CodeGeneration.getFileComment(newCu, lineDelim); 107 } 108 String classContent= createClass(lineDelim); 109 String cuContent= CodeGeneration.getCompilationUnitContent(newCu, fileComment, typeComment, classContent, lineDelim); 110 if (cuContent == null) { 111 StringBuffer buf= new StringBuffer (); 112 if (fileComment != null) { 113 buf.append(fileComment).append(lineDelim); 114 } 115 if (!fAccessorPackage.isDefaultPackage()) { 116 buf.append("package ").append(fAccessorPackage.getElementName()).append(';'); } 118 buf.append(lineDelim).append(lineDelim); 119 if (typeComment != null) { 120 buf.append(typeComment).append(lineDelim); 121 } 122 buf.append(classContent); 123 cuContent= buf.toString(); 124 } 125 126 newCu.getBuffer().setContents(cuContent); 127 addImportsToAccessorCu(newCu, pm); 128 return newCu.getSource(); 129 } finally { 130 if (newCu != null) { 131 newCu.discardWorkingCopy(); 132 } 133 } 134 } 135 136 private void addImportsToAccessorCu(ICompilationUnit newCu, IProgressMonitor pm) throws CoreException { 137 ImportRewrite is= StubUtility.createImportRewrite(newCu, true); 138 if (fIsEclipseNLS) { 139 is.addImport("org.eclipse.osgi.util.NLS"); } else { 141 is.addImport("java.util.MissingResourceException"); is.addImport("java.util.ResourceBundle"); } 144 TextEdit edit= is.rewriteImports(pm); 145 JavaModelUtil.applyEdit(newCu, edit, false, null); 146 } 147 148 private String createClass(String lineDelim) throws CoreException { 149 if (fIsEclipseNLS) { 150 MembersOrderPreferenceCache sortOrder= JavaPlugin.getDefault().getMemberOrderPreferenceCache(); 151 int constructorIdx= sortOrder.getCategoryIndex(MembersOrderPreferenceCache.CONSTRUCTORS_INDEX); 152 int fieldIdx= sortOrder.getCategoryIndex(MembersOrderPreferenceCache.STATIC_FIELDS_INDEX); 153 int initIdx= sortOrder.getCategoryIndex(MembersOrderPreferenceCache.STATIC_INIT_INDEX); 154 155 String constructor= createConstructor(lineDelim) + lineDelim; 156 String initializer= createStaticInitializer(lineDelim) + lineDelim; 157 String fields= createStaticFields(lineDelim) + lineDelim; 158 159 StringBuffer result= new StringBuffer (); 160 result.append("public class ").append(fAccessorClassName).append(" extends NLS {"); result.append("private static final String ").append(NLSRefactoring.BUNDLE_NAME).append(" = \"").append(getResourceBundleName()).append("\"; "); result.append(NLSElement.createTagText(1)).append(lineDelim); 163 164 if (constructorIdx < fieldIdx) { 165 if (fieldIdx < initIdx) { 166 result.append(constructor); 167 result.append(fields); 168 result.append(initializer); 169 } else { 170 result.append(constructor); 171 result.append(initializer); 172 result.append(fields); 173 } 174 } else { 175 if (constructorIdx < initIdx) { 176 result.append(fields); 177 result.append(constructor); 178 result.append(initializer); 179 } else { 180 result.append(fields); 181 result.append(initializer); 182 result.append(constructor); 183 } 184 } 185 186 result.append('}').append(lineDelim); 187 188 return result.toString(); 189 } else { 190 MembersOrderPreferenceCache sortOrder= JavaPlugin.getDefault().getMemberOrderPreferenceCache(); 191 int constructorIdx= sortOrder.getCategoryIndex(MembersOrderPreferenceCache.CONSTRUCTORS_INDEX); 192 int methodIdx= sortOrder.getCategoryIndex(MembersOrderPreferenceCache.METHOD_INDEX); 193 194 String constructor= lineDelim + createConstructor(lineDelim); 195 String method= lineDelim + createGetStringMethod(lineDelim); 196 197 StringBuffer result= new StringBuffer (); 198 result.append("public class ").append(fAccessorClassName).append(" {"); result.append("private static final String ").append(NLSRefactoring.BUNDLE_NAME); result.append(" = \"").append(getResourceBundleName()).append("\"; ").append(NLSElement.createTagText(1)).append(lineDelim); 202 result.append(lineDelim).append("private static final ResourceBundle ").append(getResourceBundleConstantName()); result.append("= ResourceBundle.getBundle(").append(NLSRefactoring.BUNDLE_NAME).append(");").append(lineDelim); 205 if (constructorIdx < methodIdx) { 206 result.append(constructor); 207 result.append(method); 208 } else { 209 result.append(constructor); 210 result.append(method); 211 } 212 213 result.append(lineDelim).append('}').append(lineDelim); 214 215 return result.toString(); 216 } 217 } 218 219 private String getResourceBundleConstantName() { 220 return "RESOURCE_BUNDLE"; } 222 223 private String createStaticFields(String lineDelim) { 224 HashSet added= new HashSet (); 225 List subs= new ArrayList (); 226 for (int i= 0; i < fNLSSubstitutions.length; i++) { 227 NLSSubstitution substitution= fNLSSubstitutions[i]; 228 int newState= substitution.getState(); 229 if ((substitution.hasStateChanged() || substitution.isAccessorRename())&& newState == NLSSubstitution.EXTERNALIZED) { 230 if (added.add(substitution.getKey())) 231 subs.add(substitution); 232 } 233 } 234 Collections.sort(subs, new Comparator () { 235 private Collator fCollator= Collator.getInstance(); 236 public int compare(Object o1, Object o2) { 237 NLSSubstitution s0= (NLSSubstitution)o1; 238 NLSSubstitution s1= (NLSSubstitution)o2; 239 return fCollator.compare(s0.getKey(), s1.getKey()); 240 } 241 }); 242 StringBuffer buf= new StringBuffer (); 243 for (Iterator iter= subs.iterator(); iter.hasNext();) { 244 NLSSubstitution element= (NLSSubstitution)iter.next(); 245 appendStaticField(buf, element); 246 } 247 return buf.toString(); 248 } 249 250 private void appendStaticField(StringBuffer buf, NLSSubstitution substitution) { 251 buf.append("public static String "); buf.append(substitution.getKey()); 253 buf.append(';'); 254 } 255 256 private String createGetStringMethod(String lineDelim) throws CoreException { 257 StringBuffer result= new StringBuffer (); 258 259 result.append("public static String "); int i= fSubstitutionPattern.indexOf(NLSRefactoring.KEY); 261 if (i != -1) { 262 result.append(fSubstitutionPattern.substring(0, i)); 263 result.append("String key"); result.append(fSubstitutionPattern.substring(i + NLSRefactoring.KEY.length())); 265 } else { 266 result.append("getString(String key)"); } 269 result.append('{').append(lineDelim); 270 271 result.append("try {").append(lineDelim) .append("return ") .append(getResourceBundleConstantName()).append(".getString(key);").append(lineDelim) .append("} catch (MissingResourceException e) {").append(lineDelim) .append("return '!' + key + '!';").append(lineDelim) .append("}"); 278 result.append(lineDelim).append('}'); 279 return result.toString(); 280 } 281 282 private String createStaticInitializer(String lineDelim) throws CoreException { 283 return "static {" + lineDelim 285 + "// initialize resource bundle" + lineDelim 287 + "NLS.initializeMessages(BUNDLE_NAME, " + fAccessorClassName + ".class);" + lineDelim 289 + "}"; } 291 292 private String createConstructor(String lineDelim) { 293 return "private " + fAccessorClassName + "(){" + lineDelim + '}'; 295 } 296 297 314 315 private String getPropertyFileName() { 316 return fResourceBundlePath.lastSegment(); 317 } 318 319 private String getPropertyFileNameWithoutExtension() { 320 String fileName= getPropertyFileName(); 321 return fileName.substring(0, fileName.indexOf(NLSRefactoring.PROPERTY_FILE_EXT)); 322 } 323 324 private String getResourceBundleName() throws CoreException { 325 IResource res= ResourcesPlugin.getWorkspace().getRoot().findMember(fResourceBundlePath.removeLastSegments(1)); 326 if (res != null && res.exists()) { 327 IJavaElement el= JavaCore.create(res); 328 if (el instanceof IPackageFragment) { 329 IPackageFragment p= (IPackageFragment) el; 330 return p.getElementName() + '.' + getPropertyFileNameWithoutExtension(); 331 } else 332 if ((el instanceof IPackageFragmentRoot) || (el instanceof IJavaProject)) { 333 return getPropertyFileNameWithoutExtension(); 334 } 335 } 336 throw new CoreException(new StatusInfo(IStatus.ERROR, "Resourcebundle not specified")); } 338 } 339 | Popular Tags |