/* * ChangeCVSRoot.java * * Created on 19. listopad 2003, 16:03 */ package utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * This class serves for correcting (unifying) CVS/Root contents in given CVS Working dir * @author delphym * @version $revision$ */ public class ChangeCVSRoot { /** * curent searched and replacing substrings (usr, server, repo) */ CvsRootInfo cri; /** If true it will print debug messages */ static boolean debug; /** Creates a new instance of ChangeCVSRoot */ public ChangeCVSRoot() { } /** * Creates a new instance of ChangeCVSRoot * @param debug If you pass boolean true then if you've got extra printed info * about proceded files */ public ChangeCVSRoot(boolean debug) { ChangeCVSRoot.debug = debug; } /** * This metod search recursively in given CVS Working for CVS/Root files * If it finds it then calls private modifyRoot() method which will parse the CVS/Root file * and replace desired parts of its content (server, user, repository) * @param folder Folder where to start search for CVS/Root file */ public void replaceRoots(File folder) { File [] flist = folder.listFiles(); if(flist == null) return; P.rintln(folder.toString()); File cvsRoot = new File(folder, "CVS/Root"); if (cvsRoot.exists()) modifyRoot(cvsRoot, cri); for (int i = 0; i < flist.length; i++) { if(flist[i].isDirectory()) { replaceRoots(flist[i]); } } } /** * It parses given CVS/Root file and extract and replace (if conditions are met) * its parts (user, server, repo) by given new values stored in cri parameter * @param cvsRoot just the CVS/Root file * @param cri it holds values of old & new substring: *
user
*
server
and *
repository
* which are going to be replaced in CVS/Root file(s) */ private void modifyRoot(File cvsRoot, ChangeCVSRoot.CvsRootInfo cri) { BufferedReader in = null; BufferedWriter out = null; try { in = new BufferedReader(new FileReader(cvsRoot)); String line = in.readLine(); in.close(); in = null; out = new BufferedWriter(new FileWriter(cvsRoot)); if(line==null) return; StringBuffer ll = new StringBuffer(line.trim()); P.rintln("This is content of .CVS/Root file BEFORE " + ll.toString()); //pserver:dan@localhost:2406/usr/local/CVSrepo //If we want to replace "repository" part if(cri.newRepo!=null) { if(cri.oldRepo!=null) { int index = ll.indexOf(cri.oldRepo); if(index > -1) { ll = ll.replace(index, ll.length(), cri.newRepo); } } else { int index = ll.indexOf("@"); index = ll.indexOf(":", index) +1; index = getLenghtOfOldPort(ll, index) + index; ll = ll.replace(index, ll.length(), cri.newRepo); } } //If we want replace "port" part if(cri.newPort!=null){ if(cri.oldPort!=null){ int index = ll.indexOf(":" + cri.oldPort); if(index > -1) { ll = ll.replace(++index, index + cri.oldPort.length(), cri.newPort); } } else { int index = ll.indexOf("@"); index = ll.indexOf(":", index); ll = ll.replace(++index, getLenghtOfOldPort(ll, index) + index, cri.newPort); } } //If we want to replace "user" part if(cri.newUser!=null) { if(cri.oldUser!=null) { int index = ll.indexOf(":" + cri.oldUser + "@"); int endindex = index + cri.oldUser.length(); if(index > -1) { ll = ll.replace(index+1, endindex+1, cri.newUser); } } else { int endindex = ll.indexOf("@"); int index = ll.indexOf(":", 1); ll = ll.replace(index+1, endindex, cri.newUser); } } //If we want to replace "server" part if(cri.newServer!=null) { if(cri.oldServer!=null) { int index = ll.indexOf("@" + cri.oldServer+":"); if(index > -1) { index++; int endindex = index + cri.oldServer.length(); ll = ll.replace(index, endindex, cri.newServer); } } else { int index = ll.indexOf("@"); int endindex = ll.indexOf(":", index); ll = ll.replace(index+1, endindex, cri.newServer); } } P.rintln("This is content of .CVS/Root file AFTER " + ll.toString()); out.write(ll.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if(in!=null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if(out!=null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } private int getLenghtOfOldPort(StringBuffer ll, int index) { char ch; int whereStart = index; do { ch = ll.charAt(whereStart++); P.rint(ch); if(whereStart >= ll.length()) break; }while (Character.isDigit(ch)); P.rintln("\nLength of founded 'old' port is: " + (whereStart -1 - index)); return (whereStart -1 - index); } /** * This subclass holds information about searched substrings (user, server, repository, port) and * their new versions. */ static class CvsRootInfo { String oldServer, oldUser, oldRepo; String newServer, newUser, newRepo; private String oldPort, newPort; /** * Setter for property newRepo. * @param newRepo New value of property newRepo. */ public void setNewRepo(java.lang.String newRepo) { this.newRepo = newRepo; } /** * Setter for property newServer. * @param newServer New value of property newServer. */ public void setNewServer(java.lang.String newServer) { this.newServer = newServer; } /** * Setter for property newUser. * @param newUser New value of property newUser. */ public void setNewUser(java.lang.String newUser) { this.newUser = newUser; } /** * Setter for property oldRepo. * @param oldRepo New value of property oldRepo. */ public void setOldRepo(java.lang.String oldRepo) { this.oldRepo = oldRepo; } /** * Setter for property oldServer. * @param oldServer New value of property oldServer. */ public void setOldServer(java.lang.String oldServer) { this.oldServer = oldServer; } /** * Setter for property oldUser. * @param oldUser New value of property oldUser. */ public void setOldUser(java.lang.String oldUser) { this.oldUser = oldUser; } /** * Setter for property newPort. * @param newPort New value of property newPort. */ public void setNewPort(int newPort) { if(newPort == 2401) { this.newPort = ""; } else { this.newPort = Integer.toString(newPort); } } /** * Setter for property oldPort. * @param oldPort New value of property oldPort. */ public void setOldPort(int oldPort) { if(oldPort == 2401) { this.oldPort = null; } else{ this.oldPort = Integer.toString(oldPort); } } } /** * @param args the command line arguments */ public static void main(String[] args) { ChangeCVSRoot.CvsRootInfo cri = new ChangeCVSRoot.CvsRootInfo(); ChangeCVSRoot root = new ChangeCVSRoot(true); cri.oldServer = "fortecvs"; cri.oldUser = "anoncvs"; cri.oldRepo = "/nbextra"; cri.setNewPort(2401); cri.newServer = "fortecvs.sfbay"; cri.newUser = "dmladek"; cri.newRepo = "/nbextra"; // cri.setOldPort(2403); root.cri = cri; root.replaceRoots(new File("/usr/local/home/delphym/cvs/nbextra.tmp")); } } /** This class with rint() and rintln() methods * is a wraper for java.io.PrintStream.println(). * It prints only if boolean debug is set to true */ class P { /** like System.out.print(String s) method * @param print This is the String which should be printed using * java.io.PrinterStream.print() method */ static public void rint(String print) { if(ChangeCVSRoot.debug) System.out.print(print); } /** like P.rintln(String s) method * @param print This is the String which should be printed using * java.io.PrinterStream.println() method */ static public void rintln(String print) { if(ChangeCVSRoot.debug) System.out.println(print); } /** like System.out.print(char c) method * @param print This is the String which should be printed using * java.io.PrinterStream.print() method */ static public void rint(char print) { if(ChangeCVSRoot.debug) System.out.print(print); } /** like P.rintln(char c) method * @param print This is the String which should be printed using * java.io.PrinterStream.println() method */ static public void rintln(char print) { if(ChangeCVSRoot.debug) System.out.println(print); } }