/* * TemplateHandler.java * * Created on 26. maaliskuuta 2003, 11:29 */ package kottarainenbeans; import java.beans.*; import java.util.*; import java.io.*; /** Purpose is to complete different template files that could contain several * sections. You must tell filepath of the template while creating this handler. * After that you can use method "CompleteTemplate" as many time as neeted to * get parsed String. * Read that method documentation to get more information how to use it. * Below is short example of template file: *
 * -- cut here --
 * [first_section]
 * My name is {:name:}.
 *
 * [second_section]
 * {:firstname:} {:secondname:} ({:OtherNames:})
 * -- cut here --
 * 
* @author Sampsa Lintunen - Kottarainen */ public class TemplateHandler extends Object implements java.io.Serializable { private String fileName; private Properties allSections = new Properties(); private String mime=""; /** Creates new TemplateHandler * @param FileName Filepath of the template file */ public TemplateHandler(String fileName) { this.setFileName(fileName); //the right path must also be brought!!! } /** Getter for property mime. * @return Value of property mime. * */ public String getMime() { return this.mime; } /** Setter for property mime. * @param mime New value of property mime. * */ public void setMime(String mime) { this.mime = mime; } /** * @param FileName */ private void setFileName(String fileName) { this.fileName = fileName; int index=fileName.lastIndexOf("."); if( index!=-1) { setMime(fileName.substring(index+1)); } } /** Path get method. * @return The path of the tempalate file */ public String getFileName() { return this.fileName; } /** Parse standard template-file using properties table. * @return Parsed string * @param section which part of template you want to parse * @param properties values that template macros will be replaced * @throws IOException File handling error. * @throws FileNotFoundException File does not exists. */ public String CompleteTemplate(String section, Properties properties) { try { ParseSections(); } catch (FileNotFoundException e) { return "Tiedostoa ei löytynyt"; } catch (IOException b) { return "Tiedoston lukuvirhe"; } String completed = ReplaceVars(allSections.getProperty(section, "Section does not exist"),properties); return completed; } /** Parses string includes macros. * @return Parsed text. * @param text Text to Complete. Includes macros that begins by * "{:" and ends by ":}" * @param properties Table that includes key values connected to text * macros (text between tags). Table values are texts * that macros are replased; */ public String ReplaceVars(String text, Properties properties) { int beginIndex, endIndex; String rawText = text; if (rawText.equals("")) { return ""; } String finalText =""; String macroName = ""; beginIndex = rawText.indexOf("{:"); endIndex = rawText.indexOf(":}"); while(beginIndex != -1 && endIndex > beginIndex) { if (beginIndex != 0) { finalText = finalText+rawText.substring(0,beginIndex); } macroName = rawText.substring(beginIndex+2,endIndex); finalText = finalText+ RemoveSlashes(properties.getProperty(macroName,"")); rawText = rawText.substring(endIndex+2); beginIndex = rawText.indexOf("{:"); endIndex = rawText.indexOf(":}"); } finalText = finalText + rawText; return finalText; } private void ParseSections() throws IOException,FileNotFoundException { allSections = new Properties(); FileInputStream f; f = new FileInputStream(fileName); InputStreamReader isr = new InputStreamReader(f); BufferedReader buffer = new BufferedReader(isr); String row = buffer.readLine(); String sectionname = ""; int endIndex; String lineBreak = ""; while (row != null) { if (row.length() == 0) { row = buffer.readLine(); continue; } if (row.charAt(0) == '[') { endIndex = row.indexOf(']'); sectionname = row.substring(1,endIndex); lineBreak = ""; } else { row = RemoveSlashes(row); allSections.setProperty(sectionname, allSections.getProperty(sectionname,"")+lineBreak+row); if(getMime().equals("htmt")) lineBreak = "\n"; else lineBreak = " "; } row = buffer.readLine(); } buffer.close(); isr.close(); f.close(); } private String RemoveSlashes(String text) { StringBuffer sb = new StringBuffer(text); int i= sb.indexOf("\\'"); while(i >= 0) { sb = sb.deleteCharAt(i); i= sb.indexOf("\\'"); } i= sb.indexOf("\\\""); while(i >= 0) { sb = sb.deleteCharAt(i); i= sb.indexOf("\\\""); } i= sb.indexOf(" "); while(i >= 0) { sb = sb.deleteCharAt(i); i= sb.indexOf(" "); } return sb.toString(); } }