aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <rsiddharth@ninthfloor.org>2013-11-02 22:21:50 +0530
committerrsiddharth <rsiddharth@ninthfloor.org>2013-11-02 22:21:50 +0530
commitdd891c84c5fa2535e4fdfe6bae6ccf9ac5e9193d (patch)
treec3881174e2a27f5f20e73a1b10df5371a6148866
parent3640483a3221a0eaffc7aa1eaf57dbe8758f24e3 (diff)
downloadscruf-dd891c84c5fa2535e4fdfe6bae6ccf9ac5e9193d.tar.gz
scruf-dd891c84c5fa2535e4fdfe6bae6ccf9ac5e9193d.tar.bz2
scruf-dd891c84c5fa2535e4fdfe6bae6ccf9ac5e9193d.tar.xz
scruf-dd891c84c5fa2535e4fdfe6bae6ccf9ac5e9193d.zip
modified: license/Liberate.java. rewrote it.
now the class can baptize + update parts of the copyright block.
-rw-r--r--license/Liberate.java106
1 files 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 <rsiddharth@ninthfloor.org>
*
* 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 <rsiddharth@ninthfloor.org>");
+ Liberate emailDelete = new Liberate("./scruf/",
+ ".+\\.java$",
+ Liberate.UPDATE,
+ " \\* Email: <rsiddharth@ninthfloor.org> \n \\*",
+ " *");*/
+
+ Liberate baptize = new Liberate("./scruf/",
+ ".+\\.java$",
+ Liberate.BAPTIZE
+ );
}
-} \ No newline at end of file
+}