S8383: Java: OO Concepts & Java for COBOL Programmers.
SHARE Technical Conference
August, 2001


SHARE: It's not an acronym. It's what we do!
Example Programs

class IbfExtract { static final String COPYRIGHT = "Copyright 2000, JSR Systems. See: www.Jsrsys.com/copyright."; static final int MAXOUT = 40; JsrSysout sysout = new JsrSysout(); JsrUtil u = new JsrUtil(); String inputLine = " "; JsrLineIn input = new JsrLineIn(); JsrLineOut[] output = new JsrLineOut[MAXOUT]; String[] outName = new String[MAXOUT]; int line100 = 0; int recNum = 0; int i = 0; // output index int iMax = 0; String fileName = ""; public static void main(String argv[]) { IbfExtract IbfExtract = new IbfExtract(); IbfExtract.run(argv); } public void run(String parm[]) { sysout.display("IbfExtract--"+COPYRIGHT); sysout.display("IbfExtract--usage: IbfExtract "); if (parm.length < 1) { sysout.display("IbfExtract--requires one parameters"); return; } for (i = 0; i < MAXOUT; i++) {outName[i] = "";} sysout.display(u.getDateTime()+"--RP5196 Begin Extract"); input.setName(parm[0]); while (0 <= (recNum=input.getNext())) { inputLine = input.getLine(); fileName = inputLine.substring(0,10).trim(); if (fileName.equals("RP5226")) fileName += "."+inputLine.substring(17,21); for (i = 0; i < iMax && !fileName.equals(outName[i]); i++) {} //sysout.display("*"+fileName+i+"*"); if (i == iMax) { iMax++; output[i] = new JsrLineOut(); outName[i] = fileName; output[i].setName(fileName); sysout.display("*"+fileName+"["+i+"]"); } line100++; if (line100 == 100) { line100 = 0; sysout.speedometer("Copying record: "+recNum); } output[i].setLine(inputLine); } for (i = 0; i < iMax; i++) { output[i].close(); } sysout.display(u.getDateTime()+"--RP5196 End-- Extract"); } }
class IbfReport { static final String COPYRIGHT = "Copyright 2000, Imperial Business Forms & JSR Systems. See: www.Jsrsys.com/copyright."; static final int MAXREC = 50000; JsrSysout sysout = new JsrSysout(); JsrUtil u = new JsrUtil(); String[] inputLine = new String[MAXREC]; JsrLineIn input = new JsrLineIn(); JsrSortByKey sort = new JsrSortByKey(); JsrLineOut output = new JsrLineOut(); int line100 = 0; int recNum = 0; int i = 0; int iMax = 0; int[] keys; int testMax = 0; //setting to non-zero will process only first n records. int pageNum = 0; int lineCount = 99; int linesPerPage = 84; String tl1 = u.padRight("1",133); String tl2 = u.padRight("0",133); String tl2b = " "; String hdr1 = u.padRight(" ",133); String today = u.getDateTime(); String year4 = today.substring(0,4); String mm2 = today.substring(5,7); String[] mmName = {" JANUARY", " FEBRUARY", " MARCH", " APRIL", " MAY", " JUNE", " JULY", " AUGUST", "SEPTEMBER", " OCTOBER", " NOVEMBER", " DECEMBER"}; int intYear; int intMonth; String ccAll; String ccBrch; String ccHBGN; String ccCatType; String ccCostCode; String ccMonth; String ccPrtAcd; String division = " "; String costLabel = " "; String reportName = "IbfReport"; public static void main(String argv[]) { IbfReport IbfReport = new IbfReport(); IbfReport.run(argv); } public void run(String parm[]) { lineCount = 99; // repeat of same report starts on new page. pageNum = 0; // start each report with new page numbers 2000-12-17 setReportName(); // sets reportName for following displays, etc. setSortKeys(); // set sort keys array values... sysout.display(reportName+"--"+COPYRIGHT); sysout.display(reportName+"--usage: "+reportName+" <Control Card> <input> <output> "); if (parm.length < 3) { sysout.display(reportName+"--requires three parameters"); return; } input.setName(parm[0]); // set for control card input input.getNext(); // read first output "control card" output.setName(parm[2]); out133(input.getLine()); input.getNext(); // read control card. ccAll = u.padRight(input.getLine(),40); // this eliminates IndexOutOfRange errors in setCC! input.setName(parm[1]); // reset for datafile input setCC(); // sets Control Card (CC) fields if (ccBrch.equals("30")) division = "DALLAS DIVISION"; if (ccBrch.equals("37")) division = "HOUSTON DIVISION"; if (ccBrch.equals("72")) division = "MONTGOMERY DIVISION"; if (ccBrch.equals("73")) division = "MOBILE DIVISION"; if (ccBrch.equals("74")) division = "MERIDIAN DIVISION"; if (ccBrch.equals("75")) division = "LAKELAND DIVISION"; intYear = u.getInt(year4); intMonth = u.getInt(ccMonth); sysout.display("year="+intYear+" mm="+intMonth+" Control Card="+ccAll); if (intMonth < 1 || intMonth > 12) { sysout.display(reportName+"--Invalid month, terminating execution"); System.exit(4); } if (mm2.equals("12") && ccMonth.equals("01")) intYear++; sysout.display("year="+intYear+" mm="+intMonth); setTl2(); // sets tl2 title line setHdr1(); // sets hdr2 header i = 0; while (0 <= (recNum=input.getNext())) { inputLine[i] = input.getLine(); //sysout.display(inputLine[i]); iMax=i; i++; line100++; if (line100 == 100) { line100 = 0; sysout.speedometer("Loading record: "+recNum); }//sysout.display("Loading record: "+recNum+" iMax="+iMax); if (recNum == testMax) break; // set test EOF } iMax++; keys[0] = iMax; // set actual table length. sysout.display(u.getDateTime()+"--"+parm[1]+" Begin sort"); sort.sort(inputLine, keys); sysout.display(u.getDateTime()+"--"+parm[1]+" End-- sort"); for (i = 0; i < iMax; i++) { //sysout.display(inputLine[i]); if (lineCount > linesPerPage) newPage(); lineCount++; setDetail(); // method overridden } out133("0"+"Record Total = " + u.padLeft(iMax,10) ); output.close(); } String picNum(String num) { int wholeInt; wholeInt = u.getInt(num); if (wholeInt == Integer.MIN_VALUE) return " "+num.substring(0,5); else return u.padLeft(wholeInt,10,2); } void out133(String shortLine) { output.setLine(u.padRight(shortLine,133)); } void newPage() { pageNum++; out133(tl1); out133(tl2+u.padLeft(pageNum,5)+tl2b); out133(hdr1); out133(" "); lineCount = 4; } // it is expected the all the following methods will be overridden void setReportName() { } void setSortKeys() { keys = new int[] {0,1,1,0,}; // set default keys... //keys [0] = sort table length (actual), rest column,length,A/D triplets } void setCC() { } void setTl2() { } void setHdr1() { } void setDetail() { out133(" "); } }
class IbfRP5196 extends IbfReport { public static void main(String argv[]) { IbfRP5196 IbfRP5196 = new IbfRP5196(); IbfRP5196.run(argv); } void setReportName() { reportName = "IbfRP5196"; // this should be set to class name // used in displays } void setSortKeys() { keys = new int[] {0,95,1,0,24,31,0}; // descending on catalog, ascending on name //keys [0] = sort table length (actual), rest column,length,A/D triplets } void setCC() // this method defines location of Control Card fields. { ccBrch = ccAll.substring(0,2); ccHBGN = " "; ccCatType = ccAll.substring(2,5); ccCostCode = ccAll.substring(5,6); ccMonth = ccAll.substring(6,8); ccPrtAcd = ccAll.substring(8,9); } void setTl2() { tl2 = "0 "+u.padRight(division,40) +u.padRight("BERGEN BRUNSWIG CATALOG",24) +mmName[intMonth-1]+" " +u.padZero(intYear,2) +" RP5196 PAGE " ; } void setHdr1() { if (ccCostCode.equals("Y")) costLabel = "Cost"; hdr1 = " "+u.padRight("Item",8) +u.padRight("Description",32) +u.padRight("N D C",14) +u.padRight("Unit",5) +u.padLeft(costLabel,10)+" " +u.padLeft("List",10)+" CL " +u.padRight("Vendor",11) + "Product Code" ; } void setDetail() { out133(" "+ inputLine[i].substring(17,20)+"-"+ inputLine[i].substring(20,23)+" "+ inputLine[i].substring(23,54)+" "+ inputLine[i].substring(54,68)+" "+ inputLine[i].substring(68,70)+" "+ picNum(inputLine[i].substring(70,77))+" "+ picNum(inputLine[i].substring(77,84))+" "+ inputLine[i].substring(94,95)+" "+ inputLine[i].substring(84,94)+" "+ inputLine[i].substring(95,107) ); } }
class IbfRP5226 extends IbfReport { public static void main(String argv[]) { IbfRP5226 IbfRP5226 = new IbfRP5226(); IbfRP5226.run(argv); } void setReportName() { reportName = "IbfRP5226"; // this should be set to class name // used in displays //testMax = 100; // set to limit input for testing. } void setSortKeys() { keys = new int[] {0,102,1,0,28,31,0}; // descending on catalog, ascending on name //keys [0] = sort table length (actual), rest column,length,A/D triplets } void setCC() // this method defines location of Control Card fields. { ccBrch = ccAll.substring(0,2); ccHBGN = ccAll.substring(2,6); ccCatType = ccAll.substring(6,7); ccCostCode = "X"; ccMonth = ccAll.substring(7,9); ccPrtAcd = ccAll.substring(9,10); } void setTl2() { tl2 = "0 BG-"+ccHBGN+" " +u.padRight(division,26) +u.padRight("BERGEN BRUNSWIG GROUP CATALOG",30) +mmName[intMonth-1]+" " +u.padZero(intYear,2) +" PAGE" ; tl2b = " RP5226B"; } void setHdr1() { hdr1 = " "+u.padRight("Item",8) +u.padRight("Description",32) +u.padRight("N D C",14) +u.padRight("Unit",5) +u.padLeft("AWP",8)+" " +u.padLeft("Cost",10)+" Ind CL " +u.padRight("Vendor",10) + "Product Code" ; } void setDetail() { out133(" "+ inputLine[i].substring(21,24)+"-"+ inputLine[i].substring(24,27)+" "+ inputLine[i].substring(27,58)+" "+ inputLine[i].substring(58,72)+" "+ inputLine[i].substring(72,74)+ picNum(inputLine[i].substring(74,81))+" "+ picNum(inputLine[i].substring(81,88))+" "+ inputLine[i].substring(88,91)+" "+ inputLine[i].substring(101,102)+" "+ inputLine[i].substring(91,100)+" "+ inputLine[i].substring(102,114) ); } }
/****************************************************************************** // JsrLineIn.java: * to simplify input of *.txt type files. * Sample Use: * JsrLineIn file1 = new JsrLineIn(); * String[] strfile1[2]; * strfile1[0] = "C:\reqlook.dxx" * while (0 <= file1.read(strfile1[])) { process records from strfile1[1]; * } *------------------------------------------------------------ *2000-06-25 add following methods to make more "object" like. * illustrated by "sample" use. * JsrLineIn file1 = new JsrLineIn(); * String file1Name = "input.txt"; * String file1Line; * file1.setName(file1Name); // open new file (close old if one is open). * // this is a noOP if file1Name matches open file. * while (0 <== file1.getNext() // reads next record * { // returns rec# or -rec# for EOF file1Line = file1.getLine(); // process records here * } * also: int getCount(); returns current record counter * example: (at EOF rec# is negative) * int recordsRead = - file1.getCount(); */ import java.io.*; public class JsrLineIn { static final String COPYRIGHT = "Copyright 1998-2000, JSR Systems. See: www.Jsrsys.com/copyright."; int recNum = 0; // 0 if no open files (see eofNum)! int eofNum = 0; // - recNum of last file closed! String fileLine = ""; // last Line (record) read. String fileName = ""; // name of last open file. //1.0 DataInputStream inFileStream; //1.0 DataInput inFile; BufferedReader inFile; JsrSysout sysout = new JsrSysout(); JsrUtil u = new JsrUtil(); public JsrLineIn() // null constructor { } int open(String parmName) { if (recNum != 0) { close(); } // sysout.display("Open Input: " + parmName); // move to after FileNotFound: // open code here fileName = parmName; recNum = 0; //1.0 try {inFileStream = //1.0 new DataInputStream(new FileInputStream ( fileName)); //1.0 inFile = inFileStream; try {inFile = new BufferedReader(new InputStreamReader (new FileInputStream ( fileName)),1000000); } catch (FileNotFoundException eFNF) {sysout.display("FileNotFound: " + fileName); recNum=-1; eofNum=-1; // } if (recNum == 0) sysout.display("Open Input: " + parmName); return recNum; } int close() { sysout.display("Close Input: " + u.padLeft (recNum, 10) + " Records: " + u.padRight(fileName,44)); if (recNum >= 0) { //1.0 try {inFileStream.close(); // free up resource try {inFile.close(); // free up resource }catch ( IOException eIO ) {sysout.display("I/OError! " + fileName); } } eofNum = - recNum; recNum = 0; fileName = ""; //sysout.display("closeReturn: "+eofNum+": "+recNum); return eofNum; } // the following "object" methods were added 2000-06-35 int getCount() { if (recNum > 0) return recNum; // file currently open else return eofNum; // eof (- count) last file Read } void setName(String parmName) // oops, this will require re-compile of many programs. { if (!parmName.equals(fileName)) { open(parmName); } return; } int getNext() { if (fileName.equals("")) { sysout.display("JsrLineIn--must use setName([name of File])!!!"); sysout.display("JsrLineIn--before reading w/ getNext()!!!!!!!!"); return -1; } if (recNum >= 0) { try {fileLine = inFile.readLine(); } catch ( IOException eIO ) {sysout.display("I/OError! " + fileName); } if (fileLine == null) return close(); else recNum++; } // sysout.display ("recNum="+recNum+ "Rec="+ fileLine); return recNum; } String getLine() { return fileLine; } // the following method is kept for backward compatability! int read(String nameRecord[]) { // sysout.display("Read: " + nameRecord[0]); if (!nameRecord[0].equals(fileName)) { open(nameRecord[0]); } if (recNum >= 0) { try {nameRecord[1] = inFile.readLine(); } catch ( IOException eIO ) {sysout.display("I/OError! " + fileName); } if (nameRecord[1] == null) return close(); else recNum++; } // sysout.display ("recNum="+recNum+ "Rec="+ nameRecord[1]); return recNum; } }
/****************************************************************************** // JsrLineOut.java: * Sample Use: * JsrLineOut file1 = new JsrLineOut(); * String[] strfile1[2]; * strfile1[0] = "C:\reqlook.dxx" * file1.Write(strfile1[])) * *2000-06-25 add following methods to make more "object" like. * illustrated by "sample" use. * JsrLineOut file1 = new JsrLineOut(); * String file1Name = "output.txt"; * String file1Line; * ... * file1.write(file1Name, file1Line); * // if file1Name is differnet from last method call * // open new file (close old if one is open). * *2000-07-11 add the following to make even more object like. * file1.setName(file1Name); // call once for each file * file1.setLine(file1Line); // call for each line to be written * file1.setFlush(true/false); // if true cause flush() after each line written. * if (fileName.equals("")) fileName = lastName; //2000-07-28 */ import java.io.*; public class JsrLineOut { static final String COPYRIGHT = "Copyright 1998-2000, JSR Systems. See: www.Jsrsys.com/copyright."; boolean flushOn = false; int recNum = 0; String fileName = ""; String lastName = ""; //1.0 DataOutputStream outFileStream; //1.0 DataOutput outFile; BufferedWriter outFile; JsrSysout sysout; boolean displayOn; JsrUtil u = new JsrUtil(); public JsrLineOut() // null constructor { sysout = new JsrSysout(); displayOn = true; } public JsrLineOut(String noLog) // constructor to not log { displayOn = false; // used by JsrSysout itself } // to avoid recursive calls int open(String parmName) { if (recNum != 0) { close(); } if (displayOn) sysout.display("Open Output: " + parmName); // open code here fileName = parmName; recNum = 0; //1.0try {outFileStream = //1.0 new DataOutputStream(new FileOutputStream ( fileName)); //1.0 outFile = outFileStream; try {outFile = new BufferedWriter(new OutputStreamWriter (new FileOutputStream (fileName)),1000000); } catch ( IOException eIO ) {System.out.println("I/OError! " + fileName); recNum=-1; } return recNum; } int close() { if (recNum > 0) { if (displayOn) sysout.display("Close Output: " + u.padLeft(recNum,10) + " Records: "+ fileName +" "); //1.0 try {outFileStream.close(); // free up resource try {outFile.close(); // free up resource }catch ( IOException eIO ) {System.out.println("I/OError! " + fileName); } } recNum = - recNum; lastName = fileName; // save lastName if call w/o setName() fileName = ""; return recNum; } // 2000-07-11 add setName(String fileName) move file "open" check logic // add setLine(String fileLine) to do write. void setName(String parmName) { if (!parmName.equals(fileName)) { open(parmName); } } int setLine(String parmLine) { if (recNum >= 0) { if (fileName.equals("")) fileName = lastName; //2000-07-28 try { outFile.write(parmLine); outFile.newLine(); // platform dependent line separator if (flushOn) outFile.flush(); // flush after each line... } catch ( IOException eIO ) {System.out.println("I/OError! " + fileName); } } recNum++; return recNum; } void setFlush(boolean parmTF) { flushOn = parmTF; } // 2000-06-25 add following "object" method. // 2000-07-11 all code moved to setName/setLine(). int write(String parmName, String parmLine) { // if (displayOn) sysout.display("Write: " + parmName + parmLine); setName(parmName); return setLine(parmLine); } // following is kept for backward compatability int write(String nameRecord[]) { // if (displayOn) sysout.display("Write: " + nameRecord[0] + nameRecord[1]); setName(nameRecord[0]); return setLine(nameRecord[1]); } }
/* * JsrSortByKey */ public class JsrSortByKey { static final String COPYRIGHT = "Copyright 1998, JSR Systems. See: www.Jsrsys.com/copyright."; /* This is a generic version of C.A.R Hoare's Quick Sort * algorithm. This will handle arrays that are already * sorted, and arrays with duplicate keys. * * @param a a String array * @param lo0 left boundary of array partition * @param hi0 right boundary of array partition */ JsrSysout sysout = new JsrSysout(); int[] keys; // [0] = table length, [1],[2],[3],... column,length,A/D key triplets... // [1] is left column (1 is first column) // [2] is length of key segment // [3] 0 = sort ascending, else sort descending (usually -1). int i; String loKey; String hiKey; int iCompare; public void sort(String a[], int lkey[]) { keys = lkey; sysout.display("Sorting table of length: "+lkey[0]); for (i = 1; i < lkey.length; i+=3) { sysout.display("Column/length/A-D): "+lkey[i]+"/"+lkey[i+1]+"/"+lkey[i+2]); keys[i]--; keys[i+1] = keys[i] + keys[i+1]; //sysout.display("*substring.../A-D): "+lkey[i]+"/"+lkey[i+1]+"/"+lkey[i+2]); } JsrQuickSort(a, 0, lkey[0] - 1); } private int compareKeys(String aLo, String aMid) { iCompare = 0; for (i = 1; (i < keys.length && iCompare == 0); i+=3) { //sysout.display("compareKeys Index[i]="+i+" keys.length="+keys.length); loKey = aLo.substring(keys[i],keys[i+1]); hiKey = aMid.substring(keys[i],keys[i+1]); iCompare = loKey.compareTo(hiKey); if (iCompare != 0 && keys[i+2] != 0) iCompare = - iCompare; // if descending, modify result of compare. //sysout.display("Comparing: "+loKey+"/"+hiKey+"*="+iCompare); } return iCompare; } void JsrQuickSort(String a[], int lo0, int hi0) { int lo = lo0; int hi = hi0; String mid; if ( hi0 > lo0) { // Arbitrarily establishing partition element at the midpoint. mid = a[ ( lo0 + hi0 ) / 2 ]; // loop through the array until indices cross while( lo <= hi ) { // find the first element that is greater than or equal to // the partition element starting from the left Index. while( ( lo < hi0 ) && ( compareKeys(a[lo], mid) < 0 )) ++lo; // find an element that is smaller than or equal to // the partition element starting from the right Index. while( ( hi > lo0 ) && ( compareKeys(a[hi], mid) > 0 )) --hi; // if the indexes have not crossed, swap if( lo <= hi ) { swap(a, lo, hi); ++lo; --hi; } } // If the right index has not reached the left side of array // must now sort the left partition. if( lo0 < hi ) JsrQuickSort( a, lo0, hi ); /* If the left index has not reached the right side of array * must now sort the right partition. */ if( lo < hi0 ) JsrQuickSort( a, lo, hi0 ); } } private void swap(String a[], int i, int j) { String T; T = a[i]; a[i] = a[j]; a[j] = T; } }
/****************************************************************************** // JsrSysout.java: * to simplify display/accept logic for debugging. * Sample Use: * import JsrSysout; ... * JsrSysout sysout = new JsrSysout(); ... * sysout.display("Text to display"); // display line, stop at bottom of screen * sysout.accept ("Text to display"); // display line, return input. * sysout.speedometer("Text to display"); // display line, do NOT advance * // next display (or spedometer) will * // display in same space. * each instance of JsrSysout will save the last character entered from getQ. * if that character is "q" (113), lower case q, subsequent calls to that * instance will neither display (System.out.println), * nor getQ (System.in.read()). * 1998/08/18 modification. If user enters "R" (for Run), * displays will continue, but user will not be prompted for input. * * 1999-08-25 modifications: * d.syslog("syslog.txt"); // will log to file syslog.txt. * d.setNoStop(); // sets qBig to "R". * * 2000-06-25 add d.close() method so syslog can be closed if open. * 2000-07-11 add setFlush(true) call so syslog lines exist even if not closed * */ import java.io.*; public class JsrSysout { static final String COPYRIGHT = "Copyright 1998-2000, JSR Systems. See: www.Jsrsys.com/copyright."; static final int MAXLINELEN = 80; static final int MAXSCREEN = 21; static int qBig = ' '; // 81 "Q" to terminate all instances // 82 "R" run, do not query user any more int qChar = ' '; // 113 "q" to terminate this instance static int nLines = 0; static String syslogFileName = ""; // make syslog... static so any program can turn on logging of ALL calls static JsrLineOut syslogFile = new JsrLineOut("NoLog"); // any string in constructor will suppress recursive calls to JsrSysout // this is a "special" option of JsrLineOut used only by JsrSysout. JsrUtil u = new JsrUtil(); // for padRight method public JsrSysout() // null constructor { } void display(String displayString) { if (qChar != 113 && qBig != 81) { // System.out.println("Last q="+qChar+", Last Q="+qBig); nLines += (displayString.length()+MAXLINELEN)/MAXLINELEN; if (nLines > MAXSCREEN) getQ(displayString); else System.out.println(displayString); if (syslogFileName.length() > 0) { syslogFile.setLine(displayString); } } return; } void getQ(String displayString) { if (qChar != 113 && qBig != 81) { // System.out.println("Last q="+qChar+", Last Q="+qBig); System.out.println(displayString); if (syslogFileName.length() > 0) { syslogFile.setLine(displayString); } nLines = (displayString.length()+MAXLINELEN)/MAXLINELEN; if (qBig != 82) { System.out.println("Waiting for input..."+ "(Press [Enter], or \"q\", or \"Q\", "+ "or \"R\", then [Enter].)"); try {qChar = System.in.read();System.in.read();} catch (IOException eIO){} qBig = qChar; // if "Q" will terminate all instances. // if "q" will terminate this instance. } } return; } String accept(String display) { byte[] inByte = {0,1,2,3,4,5,6}; // create byte array String inCode; this.display(display); try {System.in.read(inByte);} catch (IOException eIO){} // this reads until get CR/LF, last two bytes are 13/10 inCode = new String(inByte); int i = 0; while (inByte[i] > 31) i++; // SPACE = 32, CR=13, LF=10. inCode = inCode.substring(0,i); // strip trailing CR/LF //this.display(inCode+"*"); return inCode; } void speedometer(String displayString) { System.out.print(u.padRight(displayString,78)+"\r"); System.out.flush(); } void setNoStop() { qBig = 'R'; return; } void syslog(String fileName) { syslogFileName = fileName; syslogFile.setName(fileName); syslogFile.setFlush(true); return; } void close() { if (syslogFileName != "") syslogFile.close(); return; } }
/****************************************************************************** // JsrUtil.java: padLeft, padRight, string or integer input. // output is padded or truncated as necessary. * Sample Use: * import JsrUtil; ... * JsrUtil u = new JsrUtil(); * paddedString = u.padLeft(oldString,10); // pad left with blanks * paddedString = u.padLeft(oldInt,10); // comma inserted every 3 bytes. * paddedString = u.padLeft(oldInt,10,2); // " + .00 (2 is # decimal points). // 2000-01-11 add support for long integers for above two methods. * zeroPadInt = u.padZero(oldInt,10); // pad left with zeroes. * paddedString = u.padRight(oldString,10); // pad right with blanks. * dateTime = u.getDateTime(); * modString = u.replaceAll(inString,fromString,toString); * fileRename = u.renameFile(oldFileName,newFileName); * int = u.getInt(inString); // makes String an int. * if not an Integer, returns Integer.MIN_VALUE * */ import java.util.*; import java.io.*; public class JsrUtil { static final String COPYRIGHT = "Copyright 1998, JSR Systems. See: www.Jsrsys.com/copyright."; static final char[] blank = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', ' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; // fifty (50) blanks for append... public JsrUtil() // null constructor { } String padLeft(String parmString, int parmLen) { String tempString; int addLen; StringBuffer sb = new StringBuffer(parmLen); // set capacity of initial buffer to desired length! if (parmString.length() > parmLen) tempString = parmString.substring(parmString.length()-parmLen, parmString.length()); else { // tempString = parmString; // while (tempString.length() < parmLen) // tempString = " "+tempString; addLen = parmLen - parmString.length(); while (addLen > 50) // char[] array is only 50 blanks. { sb = sb.append(blank,0,50); addLen -= 50; } // now append final set of blanks w/ original string on end tempString = sb.append(blank,0,addLen).append(parmString).toString(); } return tempString; } String padLeft(int parmInt, int parmLen) { Integer tempInt = new Integer(parmInt); String tempString = tempInt.toString(); int digitLen = tempString.length(); int numComma = (digitLen-1) / 3; int addLen = parmLen - digitLen - numComma; StringBuffer sb = new StringBuffer(parmLen); if (addLen > 0) { while (addLen > 50) // char[] array is only 50 blanks. { sb = sb.append(blank,0,50); addLen -= 50; } // now append final set of blanks w/ original string on end sb.append(blank,0,addLen).append(tempString); } else sb.append(tempString); // System.out.println("d/n/a:"+digitLen+"/"+numComma+"/"+addLen+sb); int commaPos = sb.length() - 3; while (numComma > 0) { sb.insert(commaPos,','); commaPos -= 3; numComma--; } // 2000-06-25 for integer input -only- put in ",". // also, do not truncate if longer than parmLen! // return padLeft(tempString, parmLen); return sb.toString(); } String padLeft(int parmInt, int parmLen, int parmDecimal) { Integer tempInt = new Integer(parmInt); String tempString = tempInt.toString(); while (tempString.length() < parmDecimal) tempString = "0"+tempString; // make sure there are parmDecimal zeroes. int digitLen = tempString.length(); int numComma = (digitLen - 1 - parmDecimal) / 3; int addLen = parmLen - digitLen - numComma - 1; StringBuffer sb = new StringBuffer(parmLen); if (addLen > 0) { while (addLen > 50) // char[] array is only 50 blanks. { sb = sb.append(blank,0,50); addLen -= 50; } // now append final set of blanks w/ original string on end sb.append(blank,0,addLen).append(tempString); } else sb.append(tempString); // System.out.println("d/n/a:"+digitLen+"/"+numComma+"/"+addLen+sb); int decimalPos = sb.length() - parmDecimal; sb.insert(decimalPos,'.'); int commaPos = sb.length() - 4 - parmDecimal; while (numComma > 0) { sb.insert(commaPos,','); commaPos -= 3; numComma--; } // 2000-11-27 added parmDecimal method... // 2000-06-25 for integer input -only- put in ",". // also, do not truncate if longer than parmLen! // return padLeft(tempString, parmLen); return sb.toString(); } // 2000-01-11 add support for long integers String padLeft(long parmInt, int parmLen) { Long tempInt = new Long(parmInt); String tempString = tempInt.toString(); int digitLen = tempString.length(); int numComma = (digitLen-1) / 3; int addLen = parmLen - digitLen - numComma; StringBuffer sb = new StringBuffer(parmLen); if (addLen > 0) { while (addLen > 50) // char[] array is only 50 blanks. { sb = sb.append(blank,0,50); addLen -= 50; } // now append final set of blanks w/ original string on end sb.append(blank,0,addLen).append(tempString); } else sb.append(tempString); // System.out.println("d/n/a:"+digitLen+"/"+numComma+"/"+addLen+sb); int commaPos = sb.length() - 3; while (numComma > 0) { sb.insert(commaPos,','); commaPos -= 3; numComma--; } return sb.toString(); } String padLeft(long parmInt, int parmLen, int parmDecimal) { Long tempInt = new Long(parmInt); String tempString = tempInt.toString(); while (tempString.length() < parmDecimal) tempString = "0"+tempString; // make sure there are parmDecimal zeroes. int digitLen = tempString.length(); int numComma = (digitLen - 1 - parmDecimal) / 3; int addLen = parmLen - digitLen - numComma - 1; StringBuffer sb = new StringBuffer(parmLen); if (addLen > 0) { while (addLen > 50) // char[] array is only 50 blanks. { sb = sb.append(blank,0,50); addLen -= 50; } // now append final set of blanks w/ original string on end sb.append(blank,0,addLen).append(tempString); } else sb.append(tempString); // System.out.println("d/n/a:"+digitLen+"/"+numComma+"/"+addLen+sb); int decimalPos = sb.length() - parmDecimal; sb.insert(decimalPos,'.'); int commaPos = sb.length() - 4 - parmDecimal; while (numComma > 0) { sb.insert(commaPos,','); commaPos -= 3; numComma--; } return sb.toString(); } // 2000-01-11 add support for long integers String padZero(int parmInt, int parmLen) { Integer tempInt = new Integer(parmInt); String tempString = tempInt.toString(); if (tempString.length() > parmLen) tempString = tempString.substring(tempString.length()-parmLen, tempString.length()); else { while (tempString.length() < parmLen) tempString = "0"+tempString; } return tempString; } String padRight(String parmString, int parmLen) { String tempString; int addLen; StringBuffer sb = new StringBuffer(parmLen); // set capacity of initial buffer to desired length! if (parmString.length() > parmLen) tempString = parmString.substring(0,parmLen+1); else { //tempString = parmString; //while (tempString.length() < parmLen) // tempString = tempString + " "; // 2000-06-24 replace above with following more efficient code // above code actualy generates: // tempString = new StringBuffer().append(tempString).append(" ").toString(); // (i.e., a new StringBuffer is allocated, data is put into it, // it is converted back to a string, for EACH character added) addLen = parmLen - parmString.length(); sb = sb.append(parmString); while (addLen > 50) // char[] array is only 50 blanks. { sb = sb.append(blank,0,50); addLen -= 50; } // now append final set of blanks tempString = sb.append(blank,0,addLen).toString(); } return tempString; } String getDateTime() { Calendar rightNow = Calendar.getInstance(); String today = rightNow.get(Calendar.YEAR)+"-"+ padZero(rightNow.get(Calendar.MONTH)+1,2)+"-"+ padZero(rightNow.get(Calendar.DAY_OF_MONTH),2)+"@"+ padZero(rightNow.get(Calendar.HOUR_OF_DAY),2)+":"+ padZero(rightNow.get(Calendar.MINUTE),2); // add 1 to month because Calendar returns 0 for January!!! return today; } // replaceAll() method modifies all occurences of "from" to "to" // special cases: toString = '*delete*', newString='*delete* if find fromString // fromString ends in '=', replace text following '=' // to first space, '>' or end-of-line. // example from: 'WIDTH=' to: '' // old: 'xxWIDTH=abc z' becomes: 'xxz' String replaceAll(String oldString, String from, String to) { int lenFrom = from.length(); int lenTo = to.length(); String newSB = new String(); char endFrom = from.charAt(lenFrom-1); char numChar = ' '; int where = 0; int last = 0; int lenOld = oldString.length(); where = oldString.indexOf(from); if (to.equals("*delete*") && where != -1) newSB = "*delete*"; else { while (where != -1) { newSB = newSB+oldString.substring(last,where)+to; last = where + lenFrom; if (endFrom == '=') { numChar = oldString.charAt(last); while (numChar > ' ' && numChar != '>' && last < lenOld) // skip to next white space or '>' or end of line { last++; numChar = oldString.charAt(last); //System.out.println("numChar="+numChar); } } where = oldString.indexOf(from,last); } newSB = newSB+oldString.substring(last,lenOld); } return newSB; } // end of replaceAll void renameFile(String oldFileName, String newFileName) { File oldFD = new File(oldFileName); File newFD = new File(newFileName); if (newFD.exists() == true) System.out.println("delete " + newFD+"=" + newFD.delete()); System.out.println("rename "+oldFD+ " to "+ newFD+"=" + oldFD.renameTo(newFD)); } // end of renameFile // getInt(String) -- so don't have to remember how to do it. int getInt( String parmNumber) { //Integer tempInt = new Integer(parmNumber); //return tempInt.intValue(); // old way. try { return Integer.parseInt(parmNumber);} catch (NumberFormatException e) { return Integer.MIN_VALUE; } } // end of getInt }
class JsrRTF { static final String COPYRIGHT = "Copyright 2000, JSR Systems. See: www.Jsrsys.com/copyright."; JsrSysout debug = new JsrSysout(); JsrUtil u = new JsrUtil(); String[] inputFile = {" ", " "}; JsrLineIn input = new JsrLineIn(); String[] outputFile = {" ", " "}; JsrLineOut output = new JsrLineOut(); String ccChar = "*"; String startLine = ""; public static void main(String parm[]) { JsrRTF jsr = new JsrRTF(); jsr.run(parm); return; } private void run(String parm[]) { debug.display("JsrRTF--"+COPYRIGHT); debug.display("JsrRTF--usage: JsrRTF <input file> <output file>"); if (parm.length < 2) { debug.display("JsrRTF--requires two parameters"); return; } inputFile [0] = parm[0]; outputFile [0] = parm[1]; debug.display("Reading from: " + inputFile[0]); debug.display("Writing to: " + outputFile[0]); outputFile[1] = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033"+ "{\\fonttbl {\\f0\\fmodern\\fprq1\\fcharset0 Lucida Console;}}"+ "{\\colortbl ;\\red0\\green0\\blue0;}"+ "\\uc1\\pard\\cf1\\ulnone\\f0\\fs16"+ "\\margl600\\margr200\\margt600\\margb600"; output.write(outputFile); int recNum = 0; while (0 <= (recNum=input.read(inputFile))) { if (inputFile[1].length() < 2) { ccChar = " "; inputFile[1] = " "; } else { ccChar = inputFile[1].substring(0,1); inputFile[1] = inputFile[1].substring(1); if (inputFile[1].indexOf("\\") != -1) inputFile[1] = u.replaceAll(inputFile[1],"\\","\\\\"); if (inputFile[1].indexOf("{") != -1) inputFile[1] = u.replaceAll(inputFile[1],"{","\\{"); if (inputFile[1].indexOf("}") != -1) inputFile[1] = u.replaceAll(inputFile[1],"}","\\}"); } startLine = ""; if (ccChar.equals(" ")) startLine = "\\par "; else if (ccChar.equals("+")) startLine = "\\par "; // can't do overstrike lines!d else if (ccChar.equals("0")) startLine = "\\par\\par "; else if (ccChar.equals("-")) startLine = "\\par\\par\\par "; else { startLine = "\\par "; if (ccChar.equals("1")) if (recNum != 1) startLine = "\\page "; else startLine = ""; } outputFile[1] = startLine + inputFile[1]; output.write(outputFile); } outputFile[1] = "}"; output.write(outputFile); output.close(); } }