Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [platform-releng-dev] Fix for the copyright tool


Hi,

I changed the tool to update the creation year to 2005. I can easily change it to put 2005 for each file. Right now the tool updates the revision year only if the range contains only one year. But otherwise, it is preserving the year of the last change from the CVS history.
I found the problem with the extra whitespace. The problem was coming an incomplete change related to the first fix for javadoc comment.

Here is a new patch to apply on HEAD.



Olivier
Index: plugin.xml
===================================================================
RCS file: /home/eclipse/org.eclipse.releng.tools/plugin.xml,v
retrieving revision 1.4
diff -u -r1.4 plugin.xml
--- plugin.xml	22 Feb 2005 19:47:37 -0000	1.4
+++ plugin.xml	23 Feb 2005 03:01:28 -0000
@@ -91,6 +91,30 @@
                id="org.eclipse.releng.tools.fixCopyrights">
          </action>
       </objectContribution>
+      <objectContribution
+      		adaptable="true"
+            objectClass="org.eclipse.core.resources.IFolder"
+            id="org.eclipse.releng.internal.tools.CopyrightContribution">
+         <action
+               label="Fix Copyrights..."
+               class="org.eclipse.releng.tools.FixCopyrightAction"
+               menubarPath="additions"
+               enablesFor="+"
+               id="org.eclipse.releng.tools.fixCopyrights">
+         </action>
+      </objectContribution>
+      <objectContribution
+      		adaptable="true"
+            objectClass="org.eclipse.core.resources.IFile"
+            id="org.eclipse.releng.internal.tools.CopyrightContribution">
+         <action
+               label="Fix Copyrights..."
+               class="org.eclipse.releng.tools.FixCopyrightAction"
+               menubarPath="additions"
+               enablesFor="+"
+               id="org.eclipse.releng.tools.fixCopyrights">
+         </action>
+      </objectContribution>
    </extension>
    
    	<!-- ********** Action Sets ************** -->
Index: src/org/eclipse/releng/tools/FixCopyrightAction.java
===================================================================
RCS file: /home/eclipse/org.eclipse.releng.tools/src/org/eclipse/releng/tools/FixCopyrightAction.java,v
retrieving revision 1.6
diff -u -r1.6 FixCopyrightAction.java
--- src/org/eclipse/releng/tools/FixCopyrightAction.java	22 Feb 2005 04:06:27 -0000	1.6
+++ src/org/eclipse/releng/tools/FixCopyrightAction.java	23 Feb 2005 03:01:28 -0000
@@ -57,33 +57,50 @@
 	 * 
 	 * @return the selected resources
 	 */
-	protected IResource[] getSelectedResources() {
+	protected IFile[] getSelectedResources() {
 		ArrayList resources = null;
 		if (!selection.isEmpty()) {
 			resources = new ArrayList();
 			Iterator elements = selection.iterator();
 			while (elements.hasNext()) {
 				Object next = elements.next();
-				if (next instanceof IResource) {
-					resources.add(next);
-					continue;
-				}
-				if (next instanceof IAdaptable) {
-					IAdaptable a = (IAdaptable) next;
-					Object adapter = a.getAdapter(IResource.class);
-					if (adapter instanceof IResource) {
-						resources.add(adapter);
-						continue;
-					}
+				IResource resource = (IResource) next;
+				switch(resource.getType()) {
+					case IResource.FILE :
+						resources.add((IFile) resource);
+						break;
+					case IResource.FOLDER :
+					case IResource.PROJECT :
+						addMembers((IContainer) resource, resources);
+						break;
 				}
 			}
 		}
 		if (resources != null && !resources.isEmpty()) {
-			IResource[] result = new IResource[resources.size()];
+			IFile[] result = new IFile[resources.size()];
 			resources.toArray(result);
 			return result;
 		}
-		return new IResource[0];
+		return new IFile[0];
+	}
+
+	private void addMembers(IContainer container, List list) {
+		try {
+			IResource[] resources = container.members();
+			for (int i = 0, max = resources.length; i < max; i++) {
+				IResource resource = resources[i];
+				switch(resource.getType()) {
+					case IResource.FOLDER :
+					case IResource.PROJECT :
+						addMembers((IContainer) resource, list);
+						break;
+					case IResource.FILE :
+						list.add((IFile) resource);
+				}
+			}
+		} catch (CoreException e) {
+			e.printStackTrace();
+		}
 	}
 
 	/**
@@ -96,29 +113,22 @@
 	 * @see IActionDelegate#run(IAction)
 	 */
 	public void run(IAction action) {
-
 		log = new HashMap();
 		try {
+			final IResource[] results = getSelectedResources();
 			PlatformUI.getWorkbench().getProgressService().run(true, /* fork */
 			true, /* cancellable */
 			new WorkspaceModifyOperation() {
 				protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
 					try {
-						monitor.beginTask("Fixing copyrights...", IProgressMonitor.UNKNOWN);
-
+						monitor.beginTask("Fixing copyrights...", results.length);
 						System.out.println("Start Fixing Copyrights");
-						IResource[] results = getSelectedResources();
 						System.out.println("Resources selected: " + results.length);
 						for (int i = 0; i < results.length; i++) {
 							IResource resource = results[i];
-							System.out.println(resource.getName());
-							try {
-								MyInnerClass myInnerClass = new MyInnerClass();
-								myInnerClass.monitor = monitor;
-								resource.accept(myInnerClass);
-							} catch (CoreException e1) {
-								e1.printStackTrace();
-							}
+							System.out.println("Resource selected: " + resource.getName());
+							processFile((IFile) resource, monitor);
+							monitor.worked(1);
 						}
 
 						writeLogs();
@@ -131,10 +141,8 @@
 				}
 			});
 		} catch (InvocationTargetException e) {
-			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} catch (InterruptedException e) {
-			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
 	}
@@ -267,9 +275,9 @@
 
 		// either replace old copyright or put the new one at the top of the file
 		ibmCopyright.setRevisionYear(lastMod);
-		if (copyrightComment == null)
+		if (copyrightComment == null) {
 			aSourceFile.insert(ibmCopyright.getCopyrightComment());
-		else {
+		} else {
 			if (!copyrightComment.atTop())
 				warn(file, copyrightComment, "Old copyright not at start of file, new copyright replaces old in same location"); //$NON-NLS-1$
 			aSourceFile.replace(copyrightComment, ibmCopyright.getCopyrightComment());
Index: src/org/eclipse/releng/tools/IBMCopyrightComment.java
===================================================================
RCS file: /home/eclipse/org.eclipse.releng.tools/src/org/eclipse/releng/tools/IBMCopyrightComment.java,v
retrieving revision 1.6
diff -u -r1.6 IBMCopyrightComment.java
--- src/org/eclipse/releng/tools/IBMCopyrightComment.java	21 Feb 2005 18:05:38 -0000	1.6
+++ src/org/eclipse/releng/tools/IBMCopyrightComment.java	23 Feb 2005 03:01:28 -0000
@@ -23,7 +23,7 @@
     public static final int JAVA_COMMENT = 1;
     public static final int PROPERTIES_COMMENT = 2;
 
-    private static final int DEFAULT_CREATION_YEAR = 2004;
+    private static final int DEFAULT_CREATION_YEAR = 2005;
 
     private int commentStyle = 0;
     private int creationYear = -1;
@@ -70,22 +70,23 @@
    	    String endStr = comma == -1 ? null : yearRange.substring(comma + 1);
 
    	    int startYear = -1;
-   	    if (startStr != null)
+   	    if (startStr != null) {
 	   	    try {
 	   	        startYear = Integer.parseInt(startStr.trim());
 	   	    } catch(NumberFormatException e) {
 	   	        // do nothing
 	   	    }
+   	    }
 
    	    int endYear = -1;
-   	    if (endStr != null)
+   	    if (endStr != null) {
    	        try {
    	            endYear = Integer.parseInt(endStr.trim());
    	        } catch(NumberFormatException e) {
    	            // do nothing
    	        }
+   	    }
 
-   	    
    	    String contribComment = body.substring(contrib);
    	    StringTokenizer tokens = new StringTokenizer(contribComment, "\r\n"); //$NON-NLS-1$
    	    tokens.nextToken();
@@ -96,9 +97,11 @@
    	        if (contributor.indexOf("***********************************") == -1 //$NON-NLS-1$
    	         && contributor.indexOf("###################################") == -1) { //$NON-NLS-1$
    	            int c = contributor.indexOf(linePrefix);
-   	            if (c != -1)
+   	            if (c == 0) {
+					// it has to be at the beginning of the line
    	                contributor = contributor.substring(c + linePrefix.length());
-   	            contributors.add(contributor.trim());
+   	            }
+   	            contributors.add(contributor);
    	        }
    	    }
 
@@ -116,12 +119,12 @@
 
     private static String getLinePrefix(int commentStyle) {
         switch(commentStyle) {
-        case JAVA_COMMENT:
-            return " * ";  //$NON-NLS-1$
-        case PROPERTIES_COMMENT:
-            return "# "; //$NON-NLS-1$
-        default:
-            return null;
+	        case JAVA_COMMENT:
+	            return " *";  //$NON-NLS-1$
+	        case PROPERTIES_COMMENT:
+	            return "#"; //$NON-NLS-1$
+	        default:
+	            return null;
         }
 	}
 
@@ -140,7 +143,6 @@
 			writeLegal(writer, linePrefix);
 			writeContributions(writer, linePrefix);
 		    writeCommentEnd(writer);
-
 			return out.toString();
 		} finally {
 		    writer.close();
@@ -159,27 +161,37 @@
 	}
 
 	private void writeLegal(PrintWriter writer, String linePrefix) {
-		writer.print(linePrefix + "Copyright (c) " + creationYear); //$NON-NLS-1$
+		writer.print(linePrefix + " Copyright (c) " + creationYear); //$NON-NLS-1$
 		if (revisionYear != -1 && revisionYear != creationYear)
 	        writer.print(", " + revisionYear); //$NON-NLS-1$
 		writer.println(" IBM Corporation and others."); //$NON-NLS-1$
 
-		writer.println(linePrefix + "All rights reserved. This program and the accompanying materials"); //$NON-NLS-1$
-		writer.println(linePrefix + "are made available under the terms of the Eclipse Public License v1.0"); //$NON-NLS-1$
-		writer.println(linePrefix + "which accompanies this distribution, and is available at"); //$NON-NLS-1$
-		writer.println(linePrefix + "http://www.eclipse.org/legal/epl-v10.html";); //$NON-NLS-1$
+		writer.println(linePrefix + " All rights reserved. This program and the accompanying materials"); //$NON-NLS-1$
+		writer.println(linePrefix + " are made available under the terms of the Eclipse Public License v1.0"); //$NON-NLS-1$
+		writer.println(linePrefix + " which accompanies this distribution, and is available at"); //$NON-NLS-1$
+		writer.println(linePrefix + " http://www.eclipse.org/legal/epl-v10.html";); //$NON-NLS-1$
 	}
 
 	private void writeContributions(PrintWriter writer, String linePrefix) {
 		writer.println(linePrefix);
-		writer.println(linePrefix + "Contributors:"); //$NON-NLS-1$
+		writer.println(linePrefix + " Contributors:"); //$NON-NLS-1$
 
 		if (contributors == null || contributors.size() <= 0)
-		    writer.println(linePrefix + "    IBM Corporation - initial API and implementation"); //$NON-NLS-1$
+		    writer.println(linePrefix + "     IBM Corporation - initial API and implementation"); //$NON-NLS-1$
 		else {
 			Iterator i = contributors.iterator();
-			while (i.hasNext())
-			    writer.println(linePrefix + "    " + (String)i.next());  //$NON-NLS-1$
+			while (i.hasNext()) {
+				String contributor = (String) i.next();
+				if (contributor.length() > 0) {
+					if (Character.isWhitespace(contributor.charAt(0))) {
+					    writer.println(linePrefix + contributor);  //$NON-NLS-1$
+					} else {
+					    writer.println(linePrefix + " " + contributor);  //$NON-NLS-1$
+					}
+				} else {
+				    writer.println(linePrefix);  //$NON-NLS-1$
+				}
+			}
 		}
 	}
 

Back to the top