00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package dochelper;
00011
00012 import dochelper.exceptions.IllegalParamterTypeException;
00013 import dochelper.exceptions.InitializationException;
00014 import dochelper.exceptions.InvalidValueException;
00015 import dochelper.exceptions.MissingArgumentException;
00016 import dochelper.exceptions.ResourceUnavailableException;
00017 import java.io.BufferedWriter;
00018 import java.io.File;
00019 import java.io.FileWriter;
00020 import java.io.IOException;
00021 import java.io.Writer;
00022 import java.util.HashMap;
00023 import java.util.Hashtable;
00024
00033 public class FileRenderer implements Renderer {
00034
00035
00036
00037
00041 protected String OUTPUT_FILE = "outputfile";
00042
00043
00047 protected HashMap<String,Object> params = new HashMap<String,Object>();
00048
00049
00053 protected Hashtable<String,DocumentSection> docsections;
00054
00056 public FileRenderer() {
00057 docsections = new Hashtable<String, DocumentSection>();
00058 }
00059
00072 public void setParam(String paramName, Object value) throws IllegalParamterTypeException {
00073
00074 if(value instanceof String)
00075 params.put(paramName, value);
00076 else
00077 throw new IllegalParamterTypeException(paramName);
00078 }
00079
00092 public void checkParams() throws InvalidValueException, MissingArgumentException, ResourceUnavailableException {
00093
00094
00095 if(!params.containsKey(OUTPUT_FILE))
00096 throw new MissingArgumentException(OUTPUT_FILE);
00097
00098
00099
00100 }
00101
00107 public void init() throws InitializationException, ResourceUnavailableException {}
00108
00120 public void execute(ResultNode doc) throws ResourceUnavailableException {
00121
00122
00123 Writer output = null;
00124
00125
00126 String outfile = (String)params.get(OUTPUT_FILE);
00127
00128 File file = new File( outfile);
00129 try {
00130 output = new BufferedWriter(new FileWriter(file));
00131
00132
00133 String document = render(doc);
00134 output.write(document);
00135 output.close();
00136 }
00137 catch(IOException ioe) {
00138 throw new ResourceUnavailableException(ioe);
00139 }
00140 }
00141
00150 public void addDocumentSection(DocumentSection docsection, String key) {
00151 docsections.put(key, docsection);
00152 }
00153
00164 private String render(ResultNode node) {
00165
00166 String value = "";
00167
00168
00169 DocumentSection docsection = docsections.get(node._name);
00170
00171
00172 if(docsection != null){
00173
00174 docsection.setParams(node.getParams());
00175 value = docsection.getSTART();
00176 }
00177
00178
00179 if(node.getChildren() == null || node.getChildren().size() == 0)
00180 return (node.getValue()!=null)?node.getValue():"";
00181
00182
00183 for(ResultNode child : node.getChildren()) {
00184 value += render(child);
00185 }
00186
00187
00188 if(docsection != null)
00189 value += docsection.getEND();
00190
00191
00192 return value;
00193
00194 }
00195
00196 }