import java.io.*; import java.util.Vector; /** LetterTemplate a class to hold the template of a letter **/ public class LetterTemplate extends PersistentObject { String Body, Name, tagStart, tagEnd; int curpos; public LetterTemplate( Vector v ) { tagStart = "<<"; tagEnd = ">>"; ID = Integer.parseInt( (String) v.elementAt( 0 ) ); Name = (String) v.elementAt( 1 ); Body = (String) v.elementAt( 2 ); curpos = 0; } public LetterTemplate() { tagStart = "<<"; tagEnd = ">>"; ID = -1; Name = ""; Body = ""; curpos = 0; } public void update( Vector v ) { ID = Integer.parseInt( (String) v.elementAt( 0 ) ); Name = (String) v.elementAt( 1 ); Body = (String) v.elementAt( 2 ); System.out.println("updating name:" + Name + " body: " + Body); curpos = 0; } public String replaceTag( String body, String tag, String replace ) { int pos = searchString( body, tagStart + tag + tagEnd, 0 ); System.out.println( "replace:" + replace); if ( pos != -1 ) { String newBody = body.substring( 0, pos ) + replace; newBody += body.substring( pos + ( tagStart + tag + tagEnd ).length(), body.length()); System.out.println( "new Body:" + newBody); return newBody; } return null; } public int searchString( String body, String find, int cp ) { System.out.println("body: " + body + " find: " + find); for ( int i = cp; i < body.length(); i++ ) { int tmp = i, j = 0; for ( ; j < find.length(); j++ ) { if ( body.charAt( tmp ) != find.charAt( j ) ) break; tmp++; } if ( j == find.length() ) return i; } return -1; } public void setcurpos( int i ) { curpos = i; } public boolean equals( Vector v ) { if ( ( Integer.parseInt( (String) v.elementAt( 0 ) ) ) == ID ) return true; return false; } public String getNextTag() { int pos = searchString( Body, tagStart, curpos ); if ( pos != -1 ) { int endpos = searchString( Body, tagEnd, pos ); if ( endpos != -1 ) { curpos = endpos + tagEnd.length(); return Body.substring( pos + tagStart.length(), endpos ); } } return null; } public void persist( DataOutputStream file ) { // write the ID String temp = Integer.toString( ID ); writeField( file, temp); // write the name of the template writeField( file, Name ); //write the body of the template writeField( file, Body ); } public void retrieve( DataInputStream file ) { // read the ID String temp = readNextField( file ); if ( temp == null ) return; ID = Integer.parseInt( temp ); // read the name of template Name = readNextField( file ); // read the body of the template Body = readNextField( file ); } public String getName() { return Name; } public void setName( String s ) { Name = s; } public void setBody( String s) { Body = s; } public String getTemplate() { return new String(Body); } }