00001 /* 00002 * DocumentDefinition.java 00003 * 00004 * Created on November 12, 2008, 2:33 PM 00005 * 00006 * Copyright 2009 Online Nederland Breedband B.V. 00007 * See the COPYRIGHT file for redistribution and use restrictions. 00008 * Authors: Guillermo Christen <guillermo.christen@is.online.nl> 00009 */ 00010 package dochelper; 00011 00012 import java.util.ArrayList; 00013 import java.util.Hashtable; 00014 import java.util.Set; 00015 import java.util.List; 00016 import java.util.Map; 00017 00031 public class DocumentDefinition { 00032 00033 00034 /* 00035 * List of recordsets to process 00036 */ 00037 protected ArrayList<RecordSet> recordSets; 00038 00039 /* 00040 * Hashtable of processors that should be applied to this document 00041 */ 00042 private Hashtable<String, Processor> processors; 00043 00044 /* 00045 * List of renderers to generate output for this document 00046 */ 00047 private ArrayList<Renderer> renderers; 00048 00049 /* 00050 * Document Result Tree (root node). 00051 */ 00052 private ResultNode _result = null; 00053 00057 String name; 00058 00059 00063 public DocumentDefinition() { 00064 recordSets = new ArrayList<RecordSet>(); 00065 renderers = new ArrayList<Renderer>(); 00066 processors = new Hashtable<String, Processor>(); 00067 } 00068 00074 public DocumentDefinition(String name) { 00075 this(); 00076 this.name = name; 00077 } 00078 00084 public ArrayList<RecordSet> getRecordSets() { 00085 return recordSets; 00086 } 00087 00094 public void setRecordSets(ArrayList<RecordSet> recordSets) { 00095 this.recordSets = recordSets; 00096 } 00097 00104 public void addRecordSet(RecordSet rs) { 00105 this.recordSets.add(rs); 00106 } 00107 00114 public void addProcessor(String key, Processor ps) { 00115 this.getProcessors().put(key, ps); 00116 } 00117 00125 public void addRenderer(Renderer r) { 00126 this.getRenderers().add(r); 00127 } 00128 00142 public void execute() { 00143 00144 //make a new root 00145 ResultNode doc = new ResultNode(null, null, this.name); 00146 00147 //execute each recordset 00148 for(int i = 0; i < recordSets.size(); i++) { 00149 RecordSet recordSet = recordSets.get(i); 00150 doc = recordSet.execute(doc); 00151 } 00152 00153 this.setDocument(doc); 00154 00155 } 00156 00163 public void render() { 00164 00165 //perform renders 00166 List<Renderer> renders = this.getRenderers(); 00167 if(renders!=null) { 00168 for(Renderer renderer : renders) { 00169 renderer.execute(this.getDocument()); 00170 } 00171 } 00172 00173 } 00174 00175 public void executeProcessors() { 00176 00177 //fetch all processors heys 00178 Map<String, Processor> processorrefs = this.getProcessors(); 00179 Set<String> keys = (processorrefs!=null)?processorrefs.keySet():null; 00180 00181 if(keys==null) return; 00182 00183 for(String key : keys) { 00184 //from each key get name of the node of the tree that 00185 //we want to operate on. (includes all children) 00186 String[] nodeids = key.split("\\|\\|"); 00187 String nodeToOperateOn = nodeids[1]; 00188 00189 //get the node from the name 00190 ResultNode node = this.getDocument().find(nodeToOperateOn); 00191 00192 //get the processor 00193 Processor processor = processorrefs.get(key); 00194 00195 node = processor.operate(node); 00196 } 00197 00198 } 00199 00204 public ResultNode getDocument() { 00205 return _result; 00206 } 00207 00215 public void setDocument(ResultNode result) { 00216 this._result = result; 00217 } 00218 00222 public Hashtable<String, Processor> getProcessors() { 00223 return processors; 00224 } 00225 00229 public ArrayList<Renderer> getRenderers() { 00230 return renderers; 00231 } 00232 00233 }