import java.io.*; import java.util.*; public class DatabaseGenerator implements Serializable { protected PrintWriter file; Database theSchema; CodeGenerator theGenerator; public DatabaseGenerator(CodeGenerator generator) { theGenerator = generator; theSchema = theGenerator.getTheSchema(); } protected void OpenFile(String filename) { try { file = new PrintWriter( new FileWriter(filename) ); theGenerator.RegisterFile(filename); } catch(IOException e) { System.err.println("GENJAVA-F-BADOUTPUT: " + filename + " - " + e.getMessage()); System.exit(-1); } } protected void GenerateFindTblIdx() { file.println(" /**"); file.println(" * Retrieves the ordinal index of this table from its abbreviation"); file.println(" */"); file.println(" public int FindTblIdx(String keybuf)"); file.println(" {"); file.println(" int retval = -1;"); file.println(" for( int i = 0; i < TableSeqList.length; i++ ) {"); file.println(" if( keybuf.substring(0, Key.abbsize).compareTo(TableSeqList[i].getAbbrev()) == 0 ) {"); file.println(" retval = i;"); file.println(" break;"); file.println(" }"); file.println(" }"); file.println(); file.println(" return retval;"); file.println(" }"); } protected void GenerateFindTblAbbr() { file.println(" /**"); file.println(" * Retrieves the abbreviation of this table from its ordinal index"); file.println(" */"); file.println(" public String FindTblAbbr(int idx)"); file.println(" {"); file.println(" if( idx < 0 || idx > TableSeqList.length )"); file.println(" return null;"); file.println(); file.println(" return TableSeqList[idx].getAbbrev();"); file.println(" }"); } protected void GenerateConstructor() { file.println(" public Database()"); file.println(" {"); file.println(" initialize();"); file.println(" }"); file.println(""); } protected void GenerateInitialize() { String linebuffer = new String(); TT TTcurr; int i = 0; file.println(" public void initialize()"); file.println(" {"); TTcurr = theSchema.TTtab.getFirstRow(); while ( TTcurr != null ) { linebuffer = " " + TTcurr.getTTabb() + "tab = new " + TTcurr.getTTabb() + "table(this);"; file.println(linebuffer); TTcurr = theSchema.TTtab.getNextRow(TTcurr); } file.println(); file.println(" TableSeqList = new TableSeqListType[numtables];"); file.println(); TTcurr = theSchema.TTtab.getFirstRow(); i = 0; while ( TTcurr != null ) { linebuffer = " TableSeqList[" + i + "] = new TableSeqListType( " + TTcurr.getTTabb() + "tab.getAbbrev() );"; file.println(linebuffer); TTcurr = theSchema.TTtab.getNextRow(TTcurr); i++; } file.println(" }"); } protected void GenerateLoad() { TT TTcurr; String linebuffer; file.println(" /**"); file.println(" * Loads the database from a file"); file.println(" */"); file.println(" public void load(String filename) throws IOException"); file.println(" {"); file.println(" String linebuffer;"); file.println(" String currToken;"); file.println(" String currAbb;"); file.println(" StringTokenizer tokens;"); file.println(" Key tempKey;"); file.println(" LineNumberReader inputFile = new LineNumberReader( new FileReader(filename) );"); file.println(); file.println(" /* added by AV-NT for logging */"); file.println(" if (GlobalVarsBean.getHcg_log()==1)"); file.println(" {"); file.println(" Pr_log.logwait();"); file.println(" Pr_log.logstr(\"LD \" +filename);"); file.println(" }"); file.println(" while((linebuffer = inputFile.readLine()) != null) {"); file.println(" tokens = new StringTokenizer(linebuffer);"); file.println(); file.println(" if( !tokens.hasMoreTokens() ) {"); file.println(" // Blank line"); file.println(" continue;"); file.println(" }"); file.println(); file.println(" currToken = tokens.nextToken();"); file.println(" tempKey = new Key(currToken, this);"); file.println(); file.println(" if( !tempKey.Valid() ) {"); file.println(" System.err.println(\"Warning: invalid key \\\"\" + currToken +"); file.println(" \"\\\" found in file \" + filename + \": line \" + inputFile.getLineNumber());"); file.println(" continue;"); file.println(" }"); file.println(); file.println(" currAbb = tempKey.getAbb();"); TTcurr = theSchema.TTtab.getFirstRow(); linebuffer = " if( currAbb.compareTo(\"" + TTcurr.getTTabb() + "\") == 0 ) {"; file.println(linebuffer); file.println(" " + TTcurr.getTTabb() + " " + TTcurr.getTTabb() + "temp = new " + TTcurr.getTTabb() + "(linebuffer, this);"); file.println(" " + TTcurr.getTTabb() + "temp.addRow();"); file.println(" }"); TTcurr = theSchema.TTtab.getNextRow(TTcurr); while ( TTcurr != null ) { linebuffer = " else if( currAbb.compareTo(\"" + TTcurr.getTTabb() + "\") == 0 ) {"; file.println(linebuffer); file.println(" " + TTcurr.getTTabb() + " " + TTcurr.getTTabb() + "temp = new " + TTcurr.getTTabb() + "(linebuffer, this);"); file.println(" " + TTcurr.getTTabb() + "temp.addRow();"); file.println(" }"); TTcurr = theSchema.TTtab.getNextRow(TTcurr); } file.println(" else {"); file.println(" System.err.println(\"Warning: \\\"\" + currAbb + "); file.println(" \"\\\" found in file \" + filename + \": line \" + inputFile.getLineNumber() +"); file.println(" \" matches no known table abbreviation\");"); file.println(" }"); file.println(" }"); file.println(" if (GlobalVarsBean.getHcg_log()!=2) {"); file.println(" BufferedWriter logdatafile = new BufferedWriter( new FileWriter(\"logdata.txt\"));"); file.println(" logdatafile.write(\"LogDataFile \"+filename);"); file.println(" logdatafile.newLine();"); file.println(" logdatafile.flush();"); file.println(" dump(logdatafile);"); file.println(" }"); file.println(" }"); file.println(); } protected void GenerateDump() { TT TTcurr; file.println(" /**"); file.println(" * Writes the contents of the database to the console"); file.println(" */"); file.println(" public void dump() throws IOException"); file.println(" {"); file.println(" BufferedWriter output = new BufferedWriter"); file.println(" ( new OutputStreamWriter(System.out) );"); file.println(" dump(output);"); file.println(" }"); file.println(); file.println(" /**"); file.println(" * Writes the contents of the database to the named file"); file.println(" */"); file.println(" public void dump(String filename) throws IOException"); file.println(" {"); file.println(" BufferedWriter output = new BufferedWriter"); file.println(" ( new FileWriter(filename) );"); file.println(" dump(output);"); file.println(" }"); file.println(); file.println(" /**"); file.println(" * Writes the contents of the database to the given BufferedWriter"); file.println(" */"); file.println(" public void dump(BufferedWriter output) throws IOException"); file.println(" {"); TTcurr = theSchema.TTtab.getFirstRow(); while ( TTcurr != null ) { file.println(" " + TTcurr.getTTabb() + "tab.dumpTable(output);"); TTcurr = theSchema.TTtab.getNextRow(TTcurr); } file.println(" }"); file.println(); } public void Generate() { String filename = new String(); String linebuffer = new String(); TT TTcurr; int i = 0; filename = new String(Genjava.directory); filename += Genjava.filesep; filename += "Database.java"; OpenFile( filename ); file.println("import java.util.*;"); file.println("import java.io.*;"); file.println(); file.println("public class Database implements Serializable"); file.println("{"); TTcurr = theSchema.TTtab.getFirstRow(); i = 0; while ( TTcurr != null ) { linebuffer = " public " + TTcurr.getTTabb() + "table " + TTcurr.getTTabb() + "tab;"; file.println(linebuffer); TTcurr = theSchema.TTtab.getNextRow(TTcurr); i++; } file.println(" final int numtables = " + i + ";"); file.println(); file.println(" public TableSeqListType[] TableSeqList;"); file.println(); GenerateFindTblIdx(); file.println(); GenerateFindTblAbbr(); file.println(); GenerateLoad(); GenerateDump(); GenerateConstructor(); GenerateInitialize(); // Close the class file.println("}"); file.close(); } }