import java.io.*; import java.util.*; /* This class has a method CreateContract that simulates the * PDFfusion program that we wer told to use but not given access to * It takes a filename for a script as well as the filename for a template * and performs mailmerge to merge the script values into the template. * When it is finished, it writes the output to a file that has the name out.txt * This behavior could easily be modified to write to a particular filename. */ public class PDFfusion{ public static boolean CreateContract(String scriptName, String templateName){ //System.out.println(scriptName + " " + templateName); File script = new File(scriptName); File template = new File(templateName); if(!script.canRead() || !template.canRead()){ // maybe write out an error to an error file return false; } // create a HashTable to hold all the tag/value pairs. Hashtable myTags = new Hashtable(); fillHashTable(myTags, script); writeContract(myTags, template); return true; } private static void fillHashTable(Hashtable h, File script){ String line = ""; BufferedReader br = null; try{ br = new BufferedReader(new FileReader(script)); while(br.ready()){ try{ line = br.readLine(); }catch(IOException ex){ // write out some error System.err.println(ex.getMessage() + "error in reading from the file."); break; } StringTokenizer st = new StringTokenizer(line, Script.FIELD + Script.ENDFIELD + Script.VAL + Script.ENDVAL); // Assume that there are only two values per line. /*next two values can be used for debugging*/ String key = st.nextToken(); String val = st.nextToken(); h.put(key, val); } }catch(Exception e){} } private static void writeContract(Hashtable h, File template){ try{ BufferedReader br = new BufferedReader(new FileReader(template)); StringTokenizer st; PrintWriter pw = new PrintWriter(new FileWriter(CONTRACT_NAME)); String line; String currentToken= ""; while((line = br.readLine())!=null) { st = new StringTokenizer(line, " ", true); while(st.hasMoreTokens()) { currentToken = st.nextToken(); if(currentToken.charAt(0) == '<'){ if(currentToken.charAt(currentToken.length()-1) != '>'){ pw.print(((String)h.get(currentToken.substring(1,currentToken.length()-2))) + currentToken.charAt(currentToken.length()-1)); } else{ pw.print(h.get(currentToken.substring(1,currentToken.length()-1))); } } else pw.print(currentToken); } pw.println(); } br.close(); pw.close(); }catch(Exception e){} } private static final String CONTRACT_NAME = WebDocumentDistribution.PATH + "out.txt"; }