|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Common Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/cpl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.releng.tools; |
| 12 |
|
| 13 |
import java.io.File; |
| 14 |
import java.io.FileNotFoundException; |
| 15 |
import java.io.FileOutputStream; |
| 16 |
import java.io.IOException; |
| 17 |
import java.lang.reflect.InvocationTargetException; |
| 18 |
import java.util.ArrayList; |
| 19 |
import java.util.Calendar; |
| 20 |
import java.util.GregorianCalendar; |
| 21 |
import java.util.HashMap; |
| 22 |
import java.util.Iterator; |
| 23 |
import java.util.List; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.Set; |
| 26 |
|
| 27 |
import org.eclipse.core.resources.IFile; |
| 28 |
import org.eclipse.core.resources.IResource; |
| 29 |
import org.eclipse.core.resources.IResourceVisitor; |
| 30 |
import org.eclipse.core.runtime.CoreException; |
| 31 |
import org.eclipse.core.runtime.IAdaptable; |
| 32 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 33 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 34 |
import org.eclipse.core.runtime.Platform; |
| 35 |
import org.eclipse.core.runtime.SubProgressMonitor; |
| 36 |
import org.eclipse.jface.action.IAction; |
| 37 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
| 38 |
import org.eclipse.jface.viewers.ISelection; |
| 39 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 40 |
import org.eclipse.releng.tools.preferences.RelEngCopyrightConstants; |
| 41 |
import org.eclipse.team.core.TeamException; |
| 42 |
import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile; |
| 43 |
import org.eclipse.team.internal.ccvs.core.ICVSRemoteResource; |
| 44 |
import org.eclipse.team.internal.ccvs.core.ILogEntry; |
| 45 |
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; |
| 46 |
import org.eclipse.ui.IActionDelegate; |
| 47 |
import org.eclipse.ui.IObjectActionDelegate; |
| 48 |
import org.eclipse.ui.IWorkbenchPart; |
| 49 |
import org.eclipse.ui.PlatformUI; |
| 50 |
|
| 51 |
public class AdvancedFixCopyrightAction implements IObjectActionDelegate { |
| 52 |
|
| 53 |
public class MyInnerClass implements IResourceVisitor { |
| 54 |
public IProgressMonitor monitor; |
| 55 |
public boolean visit(IResource resource) throws CoreException { |
| 56 |
if (resource.getType() == IResource.FILE) { |
| 57 |
processFile((IFile) resource, monitor); |
| 58 |
} |
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
private String propertiesCopyright; |
| 64 |
private String javaCopyright; |
| 65 |
private String newLine = System.getProperty("line.separator"); //$NON-NLS-1$ |
| 66 |
private Map log = new HashMap(); |
| 67 |
|
| 68 |
// The current selection |
| 69 |
protected IStructuredSelection selection; |
| 70 |
|
| 71 |
private static final int currentYear = new GregorianCalendar().get(Calendar.YEAR); |
| 72 |
|
| 73 |
/** |
| 74 |
* Constructor for Action1. |
| 75 |
*/ |
| 76 |
public AdvancedFixCopyrightAction() { |
| 77 |
super(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns the selected resources. |
| 82 |
* |
| 83 |
* @return the selected resources |
| 84 |
*/ |
| 85 |
protected IResource[] getSelectedResources() { |
| 86 |
ArrayList resources = null; |
| 87 |
if (!selection.isEmpty()) { |
| 88 |
resources = new ArrayList(); |
| 89 |
Iterator elements = selection.iterator(); |
| 90 |
while (elements.hasNext()) { |
| 91 |
Object next = elements.next(); |
| 92 |
if (next instanceof IResource) { |
| 93 |
resources.add(next); |
| 94 |
continue; |
| 95 |
} |
| 96 |
if (next instanceof IAdaptable) { |
| 97 |
IAdaptable a = (IAdaptable) next; |
| 98 |
Object adapter = a.getAdapter(IResource.class); |
| 99 |
if (adapter instanceof IResource) { |
| 100 |
resources.add(adapter); |
| 101 |
continue; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
if (resources != null && !resources.isEmpty()) { |
| 107 |
IResource[] result = new IResource[resources.size()]; |
| 108 |
resources.toArray(result); |
| 109 |
return result; |
| 110 |
} |
| 111 |
return new IResource[0]; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) |
| 116 |
*/ |
| 117 |
public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @see IActionDelegate#run(IAction) |
| 122 |
*/ |
| 123 |
public void run(IAction action) { |
| 124 |
|
| 125 |
log = new HashMap(); |
| 126 |
try { |
| 127 |
PlatformUI.getWorkbench().getProgressService().run(true, /* fork */ |
| 128 |
true, /* cancellable */ |
| 129 |
new IRunnableWithProgress() { |
| 130 |
|
| 131 |
public void run(IProgressMonitor monitor) |
| 132 |
throws InvocationTargetException, InterruptedException { |
| 133 |
try { |
| 134 |
long start = System.currentTimeMillis(); |
| 135 |
monitor.beginTask("Fixing copyrights...", //$NON-NLS-1$ |
| 136 |
IProgressMonitor.UNKNOWN); |
| 137 |
|
| 138 |
System.out.println("Start Fixing Copyrights"); //$NON-NLS-1$ |
| 139 |
IResource[] results = getSelectedResources(); |
| 140 |
System.out.println("Resources selected: " //$NON-NLS-1$ |
| 141 |
+ results.length); |
| 142 |
for (int i = 0; i < results.length; i++) { |
| 143 |
IResource resource = results[i]; |
| 144 |
System.out.println(resource.getName()); |
| 145 |
try { |
| 146 |
MyInnerClass myInnerClass = new MyInnerClass(); |
| 147 |
myInnerClass.monitor = monitor; |
| 148 |
resource.accept(myInnerClass); |
| 149 |
} catch (CoreException e1) { |
| 150 |
e1.printStackTrace(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
writeLogs(); |
| 155 |
displayLogs(); |
| 156 |
System.out.println("Done Fixing Copyrights"); //$NON-NLS-1$ |
| 157 |
long end = System.currentTimeMillis(); |
| 158 |
System.out.println("Total time: "+(end-start)+"ms"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 159 |
|
| 160 |
} finally { |
| 161 |
monitor.done(); |
| 162 |
} |
| 163 |
} |
| 164 |
}); |
| 165 |
} catch (InvocationTargetException e) { |
| 166 |
// TODO Auto-generated catch block |
| 167 |
e.printStackTrace(); |
| 168 |
} catch (InterruptedException e) { |
| 169 |
// TODO Auto-generated catch block |
| 170 |
e.printStackTrace(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Lookup and return the year in which the argument file was revised. Return -1 if |
| 176 |
* the revision year cannot be found. |
| 177 |
*/ |
| 178 |
private int getCVSModificationYear(IFile file, IProgressMonitor monitor) { |
| 179 |
try { |
| 180 |
monitor.beginTask("Fetching logs from CVS", 100); //$NON-NLS-1$ |
| 181 |
|
| 182 |
try { |
| 183 |
ICVSRemoteResource cvsFile = CVSWorkspaceRoot.getRemoteResourceFor(file); |
| 184 |
if (cvsFile != null) { |
| 185 |
// get the log entry for the revision loaded in the workspace |
| 186 |
ILogEntry entry = ((ICVSRemoteFile)cvsFile) |
| 187 |
.getLogEntry(new SubProgressMonitor(monitor, 100)); |
| 188 |
return entry.getDate().getYear() + 1900; |
| 189 |
} |
| 190 |
} catch (TeamException e) { |
| 191 |
// do nothing |
| 192 |
} |
| 193 |
} finally { |
| 194 |
monitor.done(); |
| 195 |
} |
| 196 |
|
| 197 |
return -1; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* |
| 202 |
*/ |
| 203 |
private void writeLogs() { |
| 204 |
|
| 205 |
FileOutputStream aStream; |
| 206 |
try { |
| 207 |
File aFile = new File(Platform.getLocation().toFile(), |
| 208 |
"copyrightLog.txt"); //$NON-NLS-1$ |
| 209 |
aStream = new FileOutputStream(aFile); |
| 210 |
Set aSet = log.entrySet(); |
| 211 |
Iterator errorIterator = aSet.iterator(); |
| 212 |
while (errorIterator.hasNext()) { |
| 213 |
Map.Entry anEntry = (Map.Entry) errorIterator.next(); |
| 214 |
String errorDescription = (String) anEntry.getKey(); |
| 215 |
aStream.write(errorDescription.getBytes()); |
| 216 |
aStream.write(newLine.getBytes()); |
| 217 |
List fileList = (List) anEntry.getValue(); |
| 218 |
Iterator listIterator = fileList.iterator(); |
| 219 |
while (listIterator.hasNext()) { |
| 220 |
String fileName = (String) listIterator.next(); |
| 221 |
aStream.write(" ".getBytes()); //$NON-NLS-1$ |
| 222 |
aStream.write(fileName.getBytes()); |
| 223 |
aStream.write(newLine.getBytes()); |
| 224 |
} |
| 225 |
} |
| 226 |
aStream.close(); |
| 227 |
} catch (FileNotFoundException e) { |
| 228 |
e.printStackTrace(); |
| 229 |
} catch (IOException e) { |
| 230 |
e.printStackTrace(); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
private void displayLogs() { |
| 235 |
|
| 236 |
Set aSet = log.entrySet(); |
| 237 |
Iterator errorIterator = aSet.iterator(); |
| 238 |
while (errorIterator.hasNext()) { |
| 239 |
Map.Entry anEntry = (Map.Entry) errorIterator.next(); |
| 240 |
String errorDescription = (String) anEntry.getKey(); |
| 241 |
System.out.println(errorDescription); |
| 242 |
List fileList = (List) anEntry.getValue(); |
| 243 |
Iterator listIterator = fileList.iterator(); |
| 244 |
while (listIterator.hasNext()) { |
| 245 |
String fileName = (String) listIterator.next(); |
| 246 |
System.out.println(" " + fileName); //$NON-NLS-1$ |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* @param file |
| 253 |
*/ |
| 254 |
private void processFile(IFile file, IProgressMonitor monitor) { |
| 255 |
SourceFile aSourceFile; |
| 256 |
|
| 257 |
String extension = file.getFileExtension(); |
| 258 |
if (extension == null) |
| 259 |
return; |
| 260 |
monitor.subTask(file.getFullPath().toOSString()); |
| 261 |
int fileType = AdvancedCopyrightComment.UNKNOWN_COMMENT; |
| 262 |
extension = extension.toLowerCase(); |
| 263 |
if (extension.equals("java")) { //$NON-NLS-1$ |
| 264 |
fileType = AdvancedCopyrightComment.JAVA_COMMENT; |
| 265 |
aSourceFile = new JavaFile(file); |
| 266 |
} else if (extension.equals("properties")) { //$NON-NLS-1$ |
| 267 |
// if stop processing if ignoring properties files |
| 268 |
if (RelEngPlugin.getDefault().getPreferenceStore().getBoolean(RelEngCopyrightConstants.IGNORE_PROPERTIES_KEY)) { |
| 269 |
return; |
| 270 |
} |
| 271 |
fileType = AdvancedCopyrightComment.PROPERTIES_COMMENT; |
| 272 |
aSourceFile = new PropertiesFile(file); |
| 273 |
} else |
| 274 |
return; |
| 275 |
|
| 276 |
if (aSourceFile.hasMultipleCopyrights()) { |
| 277 |
warn(file, null, "Multiple copyrights found. File UNCHANGED."); //$NON-NLS-1$//$NON-NLS-2$ |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
BlockComment copyrightComment = aSourceFile.firstCopyrightComment(); |
| 282 |
AdvancedCopyrightComment ibmCopyright = AdvancedCopyrightComment.parse(copyrightComment, fileType); |
| 283 |
if (ibmCopyright == null) { |
| 284 |
// if replacing all existing comments, use default copyright comment |
| 285 |
if (RelEngPlugin.getDefault().getPreferenceStore().getBoolean(RelEngCopyrightConstants.REPLACE_ALL_EXISTING_KEY)) { |
| 286 |
warn(file, copyrightComment, "Could not interpret copyright comment, using default comment"); //$NON-NLS-1$ |
| 287 |
ibmCopyright = AdvancedCopyrightComment.defaultComment(fileType); |
| 288 |
} else { |
| 289 |
warn(file, copyrightComment, "Could not interpret copyright comment"); //$NON-NLS-1$ |
| 290 |
return; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// figure out if the comment should be updated by comparing the date range |
| 295 |
// in the comment to the last modification time provided by CVS |
| 296 |
|
| 297 |
int revised = ibmCopyright.getRevisionYear(); |
| 298 |
int lastMod = revised; |
| 299 |
if (lastMod < currentYear) |
| 300 |
lastMod = getCVSModificationYear(file, new NullProgressMonitor()); |
| 301 |
|
| 302 |
// only exit if existing copyright comment already contains the year |
| 303 |
// of last modification |
| 304 |
if (lastMod <= revised && (copyrightComment != null)) |
| 305 |
return; |
| 306 |
|
| 307 |
// either replace old copyright or put the new one at the top of the file |
| 308 |
ibmCopyright.setRevisionYear(lastMod); |
| 309 |
if (copyrightComment == null) |
| 310 |
aSourceFile.insert(ibmCopyright.getCopyrightComment()); |
| 311 |
else { |
| 312 |
if (!copyrightComment.atTop()) |
| 313 |
warn(file, copyrightComment, "Old copyright not at start of file, new copyright replaces old in same location"); //$NON-NLS-1$ |
| 314 |
aSourceFile.replace(copyrightComment, ibmCopyright.getCopyrightComment()); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
private void warn(IFile file, BlockComment firstBlockComment, |
| 319 |
String errorDescription) { |
| 320 |
List aList = (List) log.get(errorDescription); |
| 321 |
if (aList == null) { |
| 322 |
aList = new ArrayList(); |
| 323 |
log.put(errorDescription, aList); |
| 324 |
} |
| 325 |
aList.add(file.getName()); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* @see IActionDelegate#selectionChanged(IAction, ISelection) |
| 330 |
*/ |
| 331 |
public void selectionChanged(IAction action, ISelection selection) { |
| 332 |
if (selection instanceof IStructuredSelection) { |
| 333 |
this.selection = (IStructuredSelection) selection; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
} |