import java.util.*; import java.io.*; /* * This class holds all the fields that are needed to be filled in * for a specific pdf template. The Template is constructed by parsing * a pdf template document and finding which fields need to be filled. * The fields are initially empty. */ public class TemplateData{ public TemplateData(File template){ // read in and parse a pdf file making field objects that have only a field name myFileName = template.getAbsolutePath(); System.out.println("TemplateData " + myFileName); myPosition = 0; myFields = new LinkedList(); parsePDFTemplate(template); } /* This method sets a pointer to the first element in the collection of fields */ public void first(){ myPosition = 0; } /* This method moves the pointer to the next element in the collection of fields. */ public void next(){ myPosition++; } /* Returns true if the collection still has more elements to be visited. */ public boolean hasNext(){ return myPosition < myFields.size(); } /* Returns the element that is currently pointed to in the collection. */ public Object currentElement(){ return myFields.get(myPosition); } /* This method allows the adding of another field object to the collection. * @param Field j -The field to be added. * @return boolean successful - If the field already exists in the collection, it won't * be added. */ private boolean addField(Field j){ // adds the field, j to the end of the collection, unless the field already exists. if(myFields.contains(j)) return false; else{ myFields.add(j); return true; } } public String getTemplateName () { return myFileName; } private boolean parsePDFTemplate(File f){ // Pre: f is a pdf template document // post: the file has been parsed and the empty fields have been // represented by Field objects. try{ BufferedReader br = new BufferedReader(new FileReader(f)); StringTokenizer st; String line; String currentToken= ""; while((line = br.readLine())!=null) { System.out.println(line); st = new StringTokenizer(line); while(st.hasMoreTokens()) { currentToken = st.nextToken(); if(currentToken.charAt(0) == '<') if(currentToken.charAt(currentToken.length()-1) != '>'){ Field temp = new Field (currentToken.substring(1,currentToken.length()-2)); System.out.println(temp); if(!myFields.contains(temp)) myFields.add(temp); } else{ Field temp = new Field (currentToken.substring(1,currentToken.length()-1)); if(!myFields.contains(temp)) myFields.add(temp); } } } br.close(); } catch (Exception e) { System.err.println(e.getMessage()); } return false; } private LinkedList myFields; private String myFileName; private int myPosition; }