/*** * demo.java ***/ import java.io.*; import java.util.*; import java.util.Date.*; import java.net.*; import java.sql.Time; //Required when the TIME wrapper method in SQL //excel import java.io.File; import java.util.Date; import jxl.*; import jxl.write.*; import ie.tomallen.utils.*; /*** * demo, uses ie.tomallen.utils. * @author Tom Allen * @version 0.2 ***/ public class demo { /** * Static main, as demo class the execution class * * @param args Command Line argument */ public static void main(String[] args) { try { if (args.length == 0 ) { print_help(); System.exit(0); } String str = args[0].toUpperCase(); if (str.equals("R") || str.equals("REPLACE")) { replace(args); return; } if (str.equals("I") || str.equals("INFO")) { info(args); return; } if (str.equals("D") || str.equals("DIFF")) { diff(args); return; } if (str.equals("W")) //simple word count { word_count(args); return; } if (str.equals("S") || str.equals("SEARCH")) { search(args); return; } print_help(); } catch(TException e) { error_exit("main: "+e.getMessage()); } } static void info(String[] args) { if (args.length > 1) { PrintStream out = setOutput(args[1]); if ( out != System.out ) System.out.println("Output infomation to file: "+args[1]); Properties prop = System.getProperties(); out.println("Properties\n=========="); prop.list(out); out.println("\n\nAll Availables\n=============="); Locale c[] = Locale.getAvailableLocales(); for(int i=0; i "); return; } if (args[1].equals("web")) { System.out.println("Change from local to web"); strStart.add("file:///D|/web/2000ad_big/"); strEnd.add("http://www.fortunecity.com/tatooine/sputnik/53/"); strStart.add("file:///D|/web/2000ad_small/"); strEnd.add("http://homepage.eircom.net/~okku/"); } if (args[1].equals("local")) { System.out.println("Change from web to local"); strStart.add("http://www.fortunecity.com/tatooine/sputnik/53/"); strEnd.add("file:///D|/web/2000ad_big/"); strStart.add("http://homepage.eircom.net/~okku/"); strEnd.add("file:///D|/web/2000ad_small/"); } try { ReplaceStrings webChange = new ReplaceStrings(strStart,strEnd,".htm", true); webChange.setOutputFile("log.txt"); webChange.changeDirectory(args[2]); } catch(TException e) { throw new TException("replace: "+e.getMessage()); } } static void diff(String[] args) throws TException { System.out.println("Diff"); try { String filename1; if ( args.length < 2 ) { System.out.print("Enter first filename/path: "); filename1 = Utils.getInputString(); } else filename1 = args[1]; String filename2; if ( args.length < 3 ) { System.out.print("Enter second filename/path: "); filename2 = Utils.getInputString(); } else filename2 = args[2]; String filename3; if ( args.length < 4 ) { System.out.print("Enter output filename/path: "); filename3 = Utils.getInputString(); } else filename3 = args[3]; FileHandler fp1 = new FileHandler(filename1); FileHandler fp2 = new FileHandler(filename2); outstream = setOutput(filename3); Diff d = new Diff(fp1.readFile(),fp2.readFile()); outstream.println("Diff constructor finished"); Diff.change script = d.diff_2(); if (script == null) outstream.println("No differences"); else outstream.println("\n"+d.print_script(script)); } catch(TException te) { throw new TException("diff: "+te.getMessage()); } } static void word_count(String[] args) throws TException { System.out.println("Word Count"); try { String str; if ( args.length < 2 ) { System.out.print("Enter word to count: "); str = Utils.getInputString(); } else str = args[1]; String filename; if ( args.length < 3 ) { System.out.print("Enter file to count in: "); filename = getInputString(); } else filename = args[2]; FileHandler fp = new FileHandler(filename); String filestr = fp.readFile(); int count = count_str(filestr,str); System.out.println("String: "+str+" occured "+count+" times!"); } catch(TException te) { throw new TException("word_count: "+te.getMessage()); } } static void search(String[] args) throws TException { System.out.println("String Search"); String file_dir =null,str = null,filename3 = null; try { if ( args.length < 2 ) { System.out.print("Enter file/directory to seach in:"); file_dir = Utils.getInputString(); } else file_dir = args[1]; if ( args.length < 3 ) { System.out.print("Enter String to search for: "); str = Utils.getInputString(); } else str = args[2]; if ( args.length < 4 ) { System.out.print("Enter output filename/path: "); filename3 = Utils.getInputString(); } else filename3 = args[3]; outstream = setOutput(filename3); DirectoryHandler dir = new DirectoryHandler(file_dir,true); if ( dir.isValid() ) { Vector list = dir.returnVector(); if (outstream != System.out ) { outstream.print("Files to search: "); for(int i=0; i 524288 ) //if greater than 1/2 a megabyte { outstream.println(fp_search.getName()+" is over half a megabye, so do not search"); continue; } String filestr = fp_search.readFile(); int count = count_str(filestr,str); if (count > 0) { outstream.println("String:"+str+" occured "+count+" times in file:"+fp_search.getName()); find_lines(filestr,str); } } } else { FileHandler fp = null; fp = new FileHandler(file_dir); String filestr = fp.readFile(); int count = count_str(filestr,str); outstream.println("String: "+str+" occured "+count+" times in file:"+fp.getName()); if (count > 0) find_lines(filestr,str); } } catch(TException te) { throw new TException("search: "+te.getMessage()); } } static int count_str(String srcStr,String searchStr) { int count = -1, pos = -1; do { count++; pos++; pos = srcStr.indexOf(searchStr,pos); } while(pos != -1); return(count); } static void find_lines(String srcStr,String searchStr) { StringTokenizer tokens = new StringTokenizer(srcStr, "\n"); int num = tokens.countTokens(); outstream.print("\tSearch string occurs on lines: "); for(int i=0; i