Fisheye: Tag 1.2 refers to a dead (removed) revision in file `org.lams.toolbuilder/src/org/lams/toolbuilder/renameTool/Main.java'. Fisheye: No comparison available. Pass `N' to diff? Index: org.lams.toolbuilder/src/org/lams/toolbuilder/renameTool/RenameTool.java =================================================================== RCS file: /usr/local/cvsroot/org.lams.toolbuilder/src/org/lams/toolbuilder/renameTool/RenameTool.java,v diff -u --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ org.lams.toolbuilder/src/org/lams/toolbuilder/renameTool/RenameTool.java 4 Sep 2007 07:01:33 -0000 1.1 @@ -0,0 +1,288 @@ +package org.lams.toolbuilder.renameTool; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.lams.toolbuilder.util.LamsToolBuilderLog; + +/** + * + * @author Anthony Sukkar + * + */ +public class RenameTool { + + private String taskliststr; + + private Set txtType = new HashSet(); + + private List nameList = new ArrayList(); + + public RenameTool(String taskliststr, String source) + { + this.taskliststr = taskliststr; + txtType.add("clay"); + txtType.add("classpath"); + txtType.add("txt"); + txtType.add("cvsignore"); + txtType.add("html"); + txtType.add("htm"); + txtType.add("java"); + txtType.add("js"); + txtType.add("jsp"); + txtType.add("MF"); + txtType.add("bat"); + txtType.add("myd"); + txtType.add("mymetadata"); + txtType.add("project"); + txtType.add("properties"); + txtType.add("sh"); + txtType.add("sql"); + txtType.add("tag"); + txtType.add("log"); + txtType.add("tld"); + txtType.add("xml"); + } + + public boolean renameTool() throws Exception + { + File tasklist = new File(taskliststr); + + if (!tasklist.exists()) + { + LamsToolBuilderLog.logError(new Exception("Cannot find tasklist configuration file")); + throw new Exception("Cannot find tasklist configuration file"); + } + + File sourceDir = null; + try { + FileInputStream fis = new FileInputStream(tasklist); + BufferedReader br = new BufferedReader(new InputStreamReader(fis)); + + String line; + while ((line = br.readLine()) != null) { + + line = line.trim(); + + if (line.length() == 0 || (line.charAt(0) == '#')) + continue; + + String[] strArray = line.split("\\s"); + + String command = strArray[0]; + + if (command.equals("Source")) { + sourceDir = getFile(strArray); + } + + if (command.equals("Rename")) { + + if (strArray.length != 3) { + continue; + } else { + String[] pair = { strArray[1], strArray[2] }; + nameList.add(pair); + } + } + + } + + visitFile(sourceDir, "rename"); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + System.exit(0); + } catch (IOException e) { + e.printStackTrace(); + } + + + return true; + } + + /* + public static void main(String[] args) { + + txtType.add("clay"); + txtType.add("classpath"); + txtType.add("txt"); + txtType.add("cvsignore"); + txtType.add("html"); + txtType.add("htm"); + txtType.add("java"); + txtType.add("js"); + txtType.add("jsp"); + txtType.add("MF"); + txtType.add("bat"); + txtType.add("myd"); + txtType.add("mymetadata"); + txtType.add("project"); + txtType.add("properties"); + txtType.add("sh"); + txtType.add("sql"); + txtType.add("tag"); + txtType.add("log"); + txtType.add("tld"); + txtType.add("xml"); + + + File tasklist = new File("src/org/lams/toolbuilder/renameTool/tasklist.conf"); + + if (!tasklist.exists()) { + System.out.println("Can't find 'tasklist.conf'"); + } + + File sourceDir = null; + try { + FileInputStream fis = new FileInputStream(tasklist); + BufferedReader br = new BufferedReader(new InputStreamReader(fis)); + + String line; + while ((line = br.readLine()) != null) { + + line = line.trim(); + + if (line.length() == 0 || (line.charAt(0) == '#')) + continue; + + String[] strArray = line.split("\\s"); + + String command = strArray[0]; + + if (command.equals("Source")) { + sourceDir = getFile(strArray); + } + + if (command.equals("Rename")) { + + if (strArray.length != 3) { + continue; + } else { + String[] pair = { strArray[1], strArray[2] }; + nameList.add(pair); + } + } + + } + + visitFile(sourceDir, "rename"); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + System.exit(0); + } catch (IOException e) { + e.printStackTrace(); + } + } + */ + public void renameFile(File dir) { + for (String[] pair : nameList) { + String newFileName = updateFilename(dir.getName(), pair[0], pair[1]); + dir.renameTo(new File(dir.getParentFile(), newFileName)); + } + } + + private String updateFilename(String currentFileName, String regex, + String replacement) { + + String newFileName = currentFileName.replaceAll(regex, replacement); + + if (!currentFileName.equals(newFileName)) { + + String format = "Renamed %1$-40s ---> %2$-1s\n"; + + System.out.format(format, currentFileName, newFileName); + + } + return newFileName; + } + + public void replaceText(File file) { + + try { + FileInputStream fis = new FileInputStream(file); + BufferedReader br = new BufferedReader(new InputStreamReader(fis)); + + File tmpfile = File.createTempFile("rename", "tmp", file + .getParentFile()); + + BufferedWriter bw = new BufferedWriter(new FileWriter(tmpfile)); + + String line; + while ((line = br.readLine()) != null) { + + for (String[] pair : nameList) { + line = line.replaceAll(pair[0], pair[1]); + } + + bw.write(line); + bw.newLine(); + } + + bw.close(); + br.close(); + + String fileName = file.getName(); + file.delete(); + tmpfile.renameTo(new File(tmpfile.getParentFile(), fileName)); + + } catch (FileNotFoundException fnfe) { + fnfe.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public void visitFile(File file, String mode) { + + if (file.isDirectory() + && (file.getName().equals("CVS") || file.getName().equals( + "build"))) { + mode = "delete"; + } + + if (file.isDirectory()) { + String[] children = file.list(); + for (int i = 0; i < children.length; i++) { + visitFile(new File(file, children[i]), mode); + } + } + + if (mode.equals("rename")) { + + if (file.isFile()) { + + String[] a = file.getName().split("\\."); + + if ((a.length == 1) || txtType.contains(a[a.length-1])) { + replaceText(file); + } + } + renameFile(file); + } + + if (mode.equals("delete")) { + file.delete(); + System.out.println("Deleted file: " + file.getPath()); + } + } + + public File getFile(String[] array) { + // merge remaining array elements and restoring spaces. + String path = ""; + for (int i = 1; i < array.length; i++) { + path += array[i] + " "; + } + return new File(path.trim()); + } +} Index: org.lams.toolbuilder/src/org/lams/toolbuilder/wizards/LAMSNewToolWizard.java =================================================================== RCS file: /usr/local/cvsroot/org.lams.toolbuilder/src/org/lams/toolbuilder/wizards/LAMSNewToolWizard.java,v diff -u -r1.1 -r1.2 --- org.lams.toolbuilder/src/org/lams/toolbuilder/wizards/LAMSNewToolWizard.java 22 Aug 2007 14:07:19 -0000 1.1 +++ org.lams.toolbuilder/src/org/lams/toolbuilder/wizards/LAMSNewToolWizard.java 4 Sep 2007 07:01:33 -0000 1.2 @@ -2,13 +2,15 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; +import org.eclipse.osgi.util.NLS; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.INewWizard; import org.eclipse.ui.IWorkbench; import org.eclipse.core.runtime.*; import org.eclipse.jface.operation.*; import java.lang.reflect.InvocationTargetException; +import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.core.resources.*; @@ -22,6 +24,8 @@ import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jface.wizard.ProgressMonitorPart; +import org.eclipse.ui.actions.WorkspaceModifyOperation; +import org.lams.toolbuilder.renameTool.RenameTool; /** * This is a sample new wizard. Its role is to create a new file * resource in the provided container. If the container resource @@ -37,6 +41,9 @@ private LAMSNewToolWizardPage page; private ISelection selection; + // The handle to the new LAMS Tool Project to be created + private IProject projectHandle; + private static String toolName; private static String vendor; private static String compatibility; @@ -85,91 +92,114 @@ if (!page.useDefaults()) { projPath = page.getLocationPath(); - /*if (newPath.toFile().exists()) { + if (projPath.toFile().exists()) { // add the project key to the path if this folder exists - newPath = newPath.addTrailingSeparator().append(projectKey).addTrailingSeparator(); - System.out.println("Changed project location to: " + newPath); + projPath = projPath.addTrailingSeparator().append("Sample_Project").addTrailingSeparator(); + System.out.println("Changed project location to: " + projPath); } - */ + } - + IWorkspace workspace = ResourcesPlugin.getWorkspace(); final IProjectDescription description = workspace.newProjectDescription(project.getName()); - description.setLocation(projPath.append("lams_tool")); + description.setLocation(projPath); - // setup java project capability - IJavaProject jproject = JavaCore.create(project); - IPath output = project.getFolder("bin").getFullPath(); - - //ProgressMonitorPart monitor = new ProgressMonitorPart(); - - try{ - - monitor.beginTask("Create LAMS Tool Project", 2); - project.create(description, new SubProgressMonitor(monitor, 1000)); - monitor.done(); - } - catch (CoreException e) - { - - } - - - - return true; - - /* - projectKey = page.getProjectKey().trim(); - projectPackage = page.getProjectPackage().trim(); - PACKAGE_BASE = projectPackage.replaceAll("\\.", "\\/") + "/"; - //System.out.println("AZAZAZ: new PACKAGE_BASE = " + PACKAGE_BASE); - projectType = page.getProjectType(); - projectImpl = page.getProjectImpl(); - addTesting = page.isTesting(); - */ - - /* - final String toolName = page.getToolName(); - final String toolSignature = page.getToolSignature(); - final String vendor = page.getVendor(); - final String compatibility = page.getCompatibility(); - IRunnableWithProgress op = new IRunnableWithProgress() { - public void run(IProgressMonitor monitor) throws InvocationTargetException { - try { - doFinish(toolName, toolSignature, vendor, compatibility, monitor); - } catch (CoreException e) { - throw new InvocationTargetException(e); - } finally { - monitor.done(); - } + //Operation to create a new LAMS Tool Project + WorkspaceModifyOperation op = new WorkspaceModifyOperation() { + protected void execute(IProgressMonitor monitor) + throws CoreException { + monitor.beginTask("Create LAMS Tool project", 2000); + createLamsToolProject(project, description, + new SubProgressMonitor(monitor, 1000)); + monitor.done(); } }; + + // Run the project operations for creating the LAMS Tool Project try { getContainer().run(true, false, op); - } catch (InterruptedException e) { + } catch (InterruptedException e) + { + LamsToolBuilderLog.logInfo("Project creation cancelled by user"); return false; } catch (InvocationTargetException e) { - Throwable realException = e.getTargetException(); - MessageDialog.openError(getShell(), "Error", realException.getMessage()); + // ie.- one of the steps resulted in a core exception + Throwable t = e.getTargetException(); + LamsToolBuilderLog.logError(t); + if (t instanceof CoreException) { + if (((CoreException) t).getStatus().getCode() == + IResourceStatus.CASE_VARIANT_EXISTS) { + MessageDialog.openError(getShell(), + "New Project Error:", + NLS.bind("Case Variant Exists:", project.getName())); + } else { + ErrorDialog.openError(getShell(), + "New Project Error:", + null, // no special message + ((CoreException) t).getStatus()); + } + } else { + Throwable realException = e.getTargetException(); + MessageDialog.openError(getShell(), "Error", realException.getMessage()); + return false; + } return false; } + + this.projectHandle = project; return true; - */ } - private void createLamsProject (IProject projectHandle, IProjectDescription description,IProgressMonitor monitor) + private void createLamsToolProject (IProject projHandle, IProjectDescription description,IProgressMonitor monitor) throws CoreException, OperationCanceledException { - monitor.beginTask("Creating LAMS tool project", 10); - - projectHandle.create(description, monitor); - monitor.worked(2); + + monitor.beginTask("Creating LAMS tool project", 50); + + if (monitor.isCanceled()) { throw new OperationCanceledException(); } + + //############### test project to template from + String containerName = "lams_tool_sbmt"; + + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + IProject projTemplate = (IProject)root.findMember(new Path(containerName)); + if (!projTemplate.exists() || !(projTemplate instanceof IContainer)) + { + throwCoreException("Project \"" + containerName + "\" does not exist."); + } + + + monitor.worked(2); + + try{ + projTemplate.copy(description, IResource.FORCE | IResource.SHALLOW, new SubProgressMonitor(monitor, 50)); + } + catch (CoreException e) + { + LamsToolBuilderLog.logError(e); + } + projHandle.open(monitor); + monitor.worked(2); + + + projHandle.open(monitor); + monitor.done(); + + } + + + // Get method for the project created by this wizard + public IProject getProjectHandle() + { + return this.projectHandle; + } + /** * The worker method. It will find the container, create the * file if missing or just replace its contents, and open