From dd891c84c5fa2535e4fdfe6bae6ccf9ac5e9193d Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 2 Nov 2013 22:21:50 +0530 Subject: modified: license/Liberate.java. rewrote it. now the class can baptize + update parts of the copyright block. --- license/Liberate.java | 106 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 29 deletions(-) diff --git a/license/Liberate.java b/license/Liberate.java index 22ab291..7571afe 100644 --- a/license/Liberate.java +++ b/license/Liberate.java @@ -1,6 +1,5 @@ /*+ - * Copyright (C) 2012-2013 rsiddharth - * Contact me : rsiddharth@ninthfloor.org + * Copyright (C) 2012, 2013 rsiddharth * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,36 +23,72 @@ import scruf.io.*; public class Liberate { private String gpl; - - public Liberate() { - gpl = new ReadFile(new File("./scruf/license/gpl")).getContent(); - } - + + // Used in iterate() private Pattern pattern; private Matcher matcher; - private PrintWriter writer; + + // Used in update() + private String oldString; + private String newString; - public void baptize(String directory, String regex) { - File dir = new File(directory).getAbsoluteFile(); - File[] dirContents = dir.listFiles(); - String fileContent; - pattern = pattern.compile(regex); - for(File file:dirContents) { - if(file.isFile()) { - matcher = pattern.matcher(file.getName()); - if(matcher.find()) { - fileContent = new ReadFile(file).getContent(); - gpl(file,fileContent); - } - } - else { - // is a Directory. - baptize(file.getPath(),regex); - } + // TYPES: + public static int BAPTIZE = 1, UPDATE = 2; + + // For Baptize + public Liberate(String dir, String regex, int TYPE) { + gpl = new ReadFile(new File("./scruf/license/gpl")).getContent(); + iterate(dir, regex, TYPE); + } + + // For Update + public Liberate(String dir, String regex, int TYPE, + String oldString, String newString) { + this.oldString = oldString; + this.newString = newString; + iterate(dir, regex, TYPE); } + + public void iterate(String directory, String regex, int TYPE) { + File dir = new File(directory).getAbsoluteFile(); + File[] dirContents = dir.listFiles(); + String fileContent; + pattern = pattern.compile(regex); + for(File file:dirContents) { + if(file.isFile()) { + matcher = pattern.matcher(file.getName()); + if(matcher.find()) { + fileContent = new ReadFile(file).getContent(); + if(TYPE == BAPTIZE) + baptize(file,fileContent); + else if(TYPE == UPDATE) + update(file, fileContent); + } + } + else { + // is a Directory. + iterate(file.getPath(), regex, TYPE); + } + } } + + public void update(File file, + String fileContent) { + Matcher matcher = Pattern.compile(oldString).matcher(fileContent); + if(matcher.find()) { + System.out.println("Updating "+file+"..."); + StringBuffer sbuffer = new StringBuffer(); + matcher.appendReplacement(sbuffer, newString); + matcher.appendTail(sbuffer); + // write updated content to file. + new WriteFile(file, sbuffer.toString()).write(); + }else { + System.out.println("ol' string not found in " + file); + } + } - private void gpl(File file, String fileContent) { + private void baptize(File file, + String fileContent) { Matcher matcher = Pattern.compile("^/\\*\\+").matcher(fileContent); if(!(matcher.find())) { System.out.println("Baptizing "+file+"..."); @@ -66,8 +101,21 @@ public class Liberate { } public static void main(String[] args) { - Liberate libre = new Liberate(); - libre.baptize("./scruf/",".+\\.java$"); + /* Liberate copyrightUpdate = new Liberate("./scruf/", + ".+\\.java$", + Liberate.UPDATE, + "Copyright 2012 rsiddharth", + "Copyright 2012, 2013 rsiddharth "); + Liberate emailDelete = new Liberate("./scruf/", + ".+\\.java$", + Liberate.UPDATE, + " \\* Email: \n \\*", + " *");*/ + + Liberate baptize = new Liberate("./scruf/", + ".+\\.java$", + Liberate.BAPTIZE + ); } -} \ No newline at end of file +} -- cgit v1.2.3