00001 package edu.ksu.cis.bandera.util;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 import java.io.*;
00039 import java.util.*;
00040
00041 public class Copyright {
00042
00043 private static String year = "1998-2001";
00044 private static Hashtable authors = null;
00045 private static Hashtable modif = null;
00046 private static String version = "0.4.0";
00047 private static CustomFileFilter filter = new CustomFileFilter();
00048 private static boolean recurseDir = false;
00049 private static boolean keepAuthor = true;
00050 private static String lineSeparator = System.getProperty("line.separator");
00051
00052
00053
00054
00055 public static boolean analyzeFile(File fn)
00056 {
00057 String prolog = "/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *";
00058 String epilog = " * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */";
00059 boolean error = false;
00060 int state = 0;
00061 String copymsg = null;
00062 StringBuffer program = new StringBuffer("");
00063 LineNumberReader f;
00064
00065 try
00066 {
00067 f = new LineNumberReader(new FileReader(fn));
00068 } catch (Exception e)
00069 {
00070 System.out.println("Failure to read file '"+fn.getAbsolutePath()+"'");
00071 return false;
00072 }
00073
00074 System.out.print(fn+" -> ");
00075 String s;
00076 do
00077 {
00078 try {
00079 s = f.readLine();
00080 switch (state)
00081 {
00082 case 0: if (s.indexOf(prolog)!=-1) state = 1;
00083 else program.append(s+lineSeparator);
00084 break;
00085 case 1: if (s.indexOf(epilog)!=-1) state = 2; else
00086 if (s.indexOf("Copyright (C)")!=-1 && copymsg != null) copymsg = s;
00087 break;
00088 case 2: if (s != null) program.append(s+lineSeparator);
00089 }
00090 } catch (Exception e) { break; }
00091 } while (s != null);
00092
00093 try { f.close(); } catch (Exception ex) {}
00094
00095 if (keepAuthor && copymsg != null)
00096 {
00097 copymsg = get(copymsg)+lineSeparator;
00098 } else
00099 {
00100 String author = "", email = "", pad = " * Copyright (C) "+year;
00101 StringBuffer buf = new StringBuffer(padString(" * Version: "+version,69)+"*"+lineSeparator);
00102 for (Enumeration e = authors.keys(); e.hasMoreElements(); )
00103 {
00104 author = (String) e.nextElement();
00105 email = (String) authors.get(author);
00106 buf.append(padString(pad+" "+author+" ("+email+")",69)+"*"+lineSeparator);
00107 pad = padString(" *",pad.length());
00108 }
00109 if (!modif.isEmpty())
00110 {
00111 buf.append(padString(" * All rights reserved.",69)+"*"+lineSeparator);
00112 buf.append(padString(" *",69)+"*"+lineSeparator);
00113
00114 pad = padString(" * Modified by :",pad.length());
00115 for (Enumeration e = modif.keys(); e.hasMoreElements(); )
00116 {
00117 author = (String) e.nextElement();
00118 email = (String) authors.get(author);
00119 buf.append(padString(pad+" "+author+" ("+email+")",69)+"*"+lineSeparator);
00120 pad = padString(" *",pad.length());
00121 }
00122 buf.append(padString(" * Modifications are copyrighted into the respective person.",69)+"*"+lineSeparator);
00123 }
00124 }
00125
00126 try
00127 {
00128 PrintStream out = new PrintStream(new FileOutputStream(fn));
00129 out.print(copymsg+program.toString());
00130 out.close();
00131 } catch (IOException ex)
00132 {
00133 System.out.println("Cannot write to file '" + fn + "'");
00134 }
00135 return true;
00136 }
00137 private static void dirLister(File f)
00138 {
00139 File[] files = f.listFiles(filter);
00140 for (int i=0; i < files.length; i++)
00141 {
00142 if (files[i].isDirectory())
00143 {
00144 if (recurseDir) dirLister(files[i]);
00145 } else
00146 {
00147 analyzeFile(files[i]);
00148
00149 }
00150 }
00151 }
00152 private static void error(String s)
00153 {
00154 System.out.println(s);
00155 System.exit(1);
00156 }
00157 public static String get()
00158 {
00159 return get("1998-2001","SAnToS Laboratories","santos@cis.ksu.edu");
00160 }
00161 public static String get(String s)
00162 {
00163 StringBuffer buf = new StringBuffer();
00164
00165 buf.append("/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"+lineSeparator);
00166 buf.append(" * Bandera, a Java(TM) analysis and transformation toolkit *"+lineSeparator);
00167 buf.append(s);
00168 buf.append(" * All rights reserved. *"+lineSeparator);
00169 buf.append(" * *"+lineSeparator);
00170 buf.append(" * This work was done as a project in the SAnToS Laboratory, *"+lineSeparator);
00171 buf.append(" * Department of Computing and Information Sciences, Kansas State *"+lineSeparator);
00172 buf.append(" * University, USA (http://www.cis.ksu.edu/santos). *"+lineSeparator);
00173 buf.append(" * It is understood that any modification not identified as such is *"+lineSeparator);
00174 buf.append(" * not covered by the preceding statement. *"+lineSeparator);
00175 buf.append(" * *"+lineSeparator);
00176 buf.append(" * This work is free software; you can redistribute it and/or *"+lineSeparator);
00177 buf.append(" * modify it under the terms of the GNU Library General Public *"+lineSeparator);
00178 buf.append(" * License as published by the Free Software Foundation; either *"+lineSeparator);
00179 buf.append(" * version 2 of the License, or (at your option) any later version. *"+lineSeparator);
00180 buf.append(" * *"+lineSeparator);
00181 buf.append(" * This work is distributed in the hope that it will be useful, *"+lineSeparator);
00182 buf.append(" * but WITHOUT ANY WARRANTY; without even the implied warranty of *"+lineSeparator);
00183 buf.append(" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *"+lineSeparator);
00184 buf.append(" * Library General Public License for more details. *"+lineSeparator);
00185 buf.append(" * *"+lineSeparator);
00186 buf.append(" * You should have received a copy of the GNU Library General Public *"+lineSeparator);
00187 buf.append(" * License along with this toolkit; if not, write to the *"+lineSeparator);
00188 buf.append(" * Free Software Foundation, Inc., 59 Temple Place - Suite 330, *"+lineSeparator);
00189 buf.append(" * Boston, MA 02111-1307, USA. *"+lineSeparator);
00190 buf.append(" * *"+lineSeparator);
00191 buf.append(" * Java is a trademark of Sun Microsystems, Inc. *"+lineSeparator);
00192 buf.append(" * *"+lineSeparator);
00193 buf.append(" * To submit a bug report, send a comment, or get the latest news on *"+lineSeparator);
00194 buf.append(" * this project and other SAnToS projects, please visit the web-site *"+lineSeparator);
00195 buf.append(" * http://www.cis.ksu.edu/santos *"+lineSeparator);
00196 buf.append(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */"+lineSeparator);
00197 return buf.toString();
00198 }
00199 public static String get(String year, String name, String email)
00200 {
00201 StringBuffer copyrite = new StringBuffer(" * Copyright (C) "+year+" "+name+" ("+email+")");
00202
00203 while (copyrite.length()<69) { copyrite.append(" "); }
00204 copyrite.append("*"+lineSeparator);
00205
00206 return get(copyrite.toString());
00207 }
00208
00209
00210
00211 private static void help()
00212 {
00213 System.out.println("Usage: java Copyright [options] filename1 [filename2...]");
00214 System.out.println("Switches:");
00215 System.out.println(" -a To specify the authors");
00216 System.out.println(" -m To specify the modifiers of the source");
00217 System.out.println(" -p To specify the path");
00218 System.out.println(" -s To search for subdirectories as well");
00219 System.out.println(" -v To specify the version");
00220 System.out.println(" -y To specify the copyright year");
00221 System.out.println("Example: java Copyright -s \"*.java\"");
00222 System.out.println();
00223 System.out.println("Warning: You have to surround the wildcards in quotes if you use it in UNIX");
00224 System.exit(1);
00225 }
00226
00227
00228
00229
00230 public static void main(String[] args)
00231 {
00232 if (args.length < 1) help();
00233 int start = 0;
00234 LinkedList l = new LinkedList();
00235 String path = ".";
00236
00237 for (int i=0; i<args.length; i++)
00238 {
00239 if (args[i].startsWith("-s")) recurseDir = true;
00240 else if (args[i].startsWith("-a"))
00241 {
00242 try {
00243 authors = parseEmail(args[i].substring(2));
00244 keepAuthor = false;
00245 } catch (Exception e)
00246 {
00247 error("Error when parsing author name!");
00248 }
00249 } else if (args[i].startsWith("-y"))
00250 {
00251 try {
00252 year = args[i].substring(2);
00253 if (year == null || year.equals("")) throw new Exception();
00254 } catch (Exception e)
00255 {
00256 error("Error when parsing the year!");
00257 }
00258 } else if (args[i].startsWith("-m"))
00259 {
00260 try {
00261 modif = parseEmail(args[i].substring(2));
00262 } catch (Exception e)
00263 {
00264 error("Error when parsing modifier name!");
00265 }
00266 } else if (args[i].startsWith("-v"))
00267 {
00268 try {
00269 version = args[i].substring(2);
00270 if (version == null || version.equals("")) throw new Exception();
00271 } catch (Exception e)
00272 {
00273 error("Error when parsing version name!");
00274 }
00275 } else if (args[i].startsWith("-p"))
00276 {
00277 path = args[i].substring(2).trim();
00278 if (path == null || path.equals("")) path = ".";
00279 } else
00280 {
00281 l.addLast(args[i].trim());
00282 }
00283 }
00284 if (authors == null)
00285 {
00286 authors = new Hashtable();
00287 authors.put("SAnToS Laboratories","santos@cis.ksu.edu");
00288 }
00289 if (modif == null) modif = new Hashtable();
00290 if (l.isEmpty()) error("No file names specified");
00291
00292 filter.setFileList(l);
00293 System.out.println("Analyzing:");
00294
00295 }
00296 private static String padString(String s, int n)
00297 {
00298 StringBuffer buf = new StringBuffer(s);
00299 while (buf.length()<n) { buf.append(" "); }
00300 return buf.toString();
00301 }
00302 private static Hashtable parseEmail(String s)
00303 {
00304 Hashtable tbl = new Hashtable();
00305 StringTokenizer tok = new StringTokenizer(s, ";");
00306 while (tok.hasMoreTokens())
00307 {
00308 StringTokenizer etok = new StringTokenizer(tok.nextToken(), ",");
00309 String author = etok.nextToken().trim();
00310 String email = etok.nextToken().trim();
00311 if (author == null || email == null) throw new RuntimeException();
00312 tbl.put(author.replace('_',' '), email);
00313 }
00314 return tbl;
00315 }
00316 }