1 20 package org.enhydra.barracuda.core.event.helper; 21 22 import java.io.*; 23 import java.util.*; 24 import javax.servlet.*; 25 import javax.servlet.http.*; 26 27 import org.apache.log4j.*; 28 import org.w3c.dom.*; 29 import org.w3c.dom.html.*; 30 31 import org.enhydra.barracuda.core.comp.*; 32 import org.enhydra.barracuda.core.util.dom.*; 33 import org.enhydra.barracuda.core.event.*; 34 import org.enhydra.barracuda.core.view.*; 35 import org.enhydra.barracuda.plankton.*; 36 37 import org.enhydra.xml.io.*; 38 39 42 public abstract class BlockIterateHandler extends DefaultBaseEventListener { 43 44 public static Logger localLogger = Logger.getLogger(BlockIterateHandler.class.getName()); 46 protected static final String NODE_ITERATOR_FLAG = "Dir::Block_Iterate.".toLowerCase(); 47 48 protected ViewContext vc = null; 51 protected BlockIterator bi = null; 52 53 56 58 59 public void setViewContext(ViewContext ivc) { 61 vc = ivc; 62 } 63 64 public ViewContext getViewContext() { 65 return vc; 66 } 67 68 public void updateModelInTemplate(TemplateModel model) { 69 if (bi!=null) bi.updateModelInTemplate(model); 70 } 71 72 public DOMWriter getDOMWriter() { 73 DefaultDOMWriter dw = new DefaultDOMWriter(); 74 dw.setLeaveWriterOpen(true); 75 return dw; 76 } 77 78 83 public void initHandler() { 84 } 85 86 public abstract Class getTemplateClass(); 87 88 public abstract BlockIterator getIterator(String key); 89 90 93 public void handleViewEvent(ViewEventContext vec) throws EventException, ServletException, IOException { 94 long bmillis = 0; 95 if (localLogger.isInfoEnabled()) bmillis = System.currentTimeMillis(); 100 if (localLogger.isInfoEnabled()) localLogger.info("Handling ViewEvent in "+this); 101 102 try { 103 if (localLogger.isDebugEnabled()) localLogger.debug("Create the ViewContext"); 105 setViewContext(new DefaultViewContext(vec)); 106 HttpServletResponse resp = vec.getResponse(); 107 PrintWriter out = resp.getWriter(); 108 109 initHandler(); 111 112 if (localLogger.isDebugEnabled()) localLogger.debug("Getting the DOM Template"); 114 Document page = DefaultDOMLoader.getGlobalInstance().getDOM(getTemplateClass(), vec.getViewCapabilities().getClientLocale()); 117 134 getDOMWriter().prepareResponse(resp); 136 138 DOMWriter dw = getDOMWriter(); 145 if (dw instanceof DefaultDOMWriter && page.getDoctype()==null) { 146 OutputOptions oo = ((DefaultDOMWriter) dw).getOutputOptions(); 147 if (oo==null) oo = DefaultDOMWriter.getDefaultOutputOptions(page); 148 149 if (!oo.getOmitDocType()) { 150 out.print("<!DOCTYPE "); 151 out.print(page.getDocumentElement().getNodeName()); 152 153 String publicId = oo.getPublicId(); 154 if (publicId!=null) out.print(" PUBLIC \""+publicId+"\""); 155 156 String systemId = oo.getSystemId(); 157 if (systemId!=null) out.print(" \""+systemId+"\""); 158 159 out.println(">"); 160 } 161 } 162 164 165 printNode(page, out, 0, (page instanceof HTMLDocument)); out.close(); 169 170 if (localLogger.isInfoEnabled()) localLogger.info("ViewEvent handled! (rendered in "+(System.currentTimeMillis()-bmillis)+" millis)"); 171 172 } catch (RenderException e) { 173 if (localLogger.isInfoEnabled()) localLogger.info("Unexpected RenderException:"+e); 175 throw new EventException ("Unexpected RenderException:"+e, e); 176 177 } catch (IOException e) { 178 localLogger.warn("Unexpected IOException:"+e); 179 e.printStackTrace(); 180 throw e; 181 182 } catch (RuntimeException e) { 183 localLogger.warn("Unexpected RuntimeException:"+e); 184 e.printStackTrace(); 185 throw e; 186 187 } finally { 188 vc = null; 190 bi = null; 191 192 System.gc(); 194 } 195 196 } 197 198 public void printNode(Node node, PrintWriter out, int depth, boolean isHtml) throws RenderException, IOException { 199 if (node instanceof Element) { 201 Element el = (Element) node; 202 String blockIteratorName = null; 204 Attr attr = el.getAttributeNode("class"); 205 if (attr!=null) { 206 String value = attr.getValue(); 207 int spos = value.toLowerCase().indexOf(NODE_ITERATOR_FLAG); 208 if (spos>-1) { 209 int lpos = value.length(); 210 int epos = value.indexOf(" ", spos+NODE_ITERATOR_FLAG.length()); 211 if (epos<0 || epos>lpos) epos = lpos; 212 blockIteratorName = value.substring(spos+NODE_ITERATOR_FLAG.length(), epos); 213 value = value.substring(0,spos)+(epos<lpos ? value.substring(epos+1, lpos) : ""); 214 attr.setValue(value); 215 } 216 } 217 218 if (blockIteratorName!=null) { 220 if (localLogger.isDebugEnabled()) localLogger.debug("Found Block_Iterator directive:"+blockIteratorName); 221 int cntr = 0; 222 bi = getIterator(blockIteratorName); 223 if (bi!=null) { 224 try { 225 bi.preIterate(); 228 while (bi.hasNext()) { 230 Node templateNode = node.cloneNode(true); 232 Node newNode = bi.next(getViewContext(), templateNode); 235 cntr++; 236 if (localLogger.isDebugEnabled()) localLogger.debug("Got block "+cntr+", node: "+newNode); 237 if (newNode==null) continue; 238 239 DOMWriter dw = getDOMWriter(); 241 dw.setLeaveWriterOpen(true); 242 dw.write(newNode, out); 243 if (localLogger.isDebugEnabled()) localLogger.debug("Successfully rendered node!"); 244 } 245 } finally { 246 if (bi!=null) bi.postIterate(); 249 bi = null; 250 } 251 } else { 252 if (localLogger.isDebugEnabled()) localLogger.warn("Failed to locate corresponding BlockIterator class!"); 253 out.println(""); 254 out.println(""); 255 out.println("<!-- Missing Iterator: "+blockIteratorName+" -->"); 256 out.println(""); 257 } 258 259 } else { 261 266 String tag = el.getTagName().toLowerCase(); 273 boolean printNode = true; 274 DOMWriter dw = getDOMWriter(); 275 if (dw instanceof CommaSeparatedDOMWriter) { 276 printNode = (!tag.equals(CommaSeparatedDOMWriter.DOCUMENT_TYPE) && 277 !tag.equals(CommaSeparatedDOMWriter.ELEMENT_ROW) && 278 !tag.equals(CommaSeparatedDOMWriter.ELEMENT_HEADER) && 279 !tag.equals(CommaSeparatedDOMWriter.ELEMENT_COLUMN)); 280 } 281 282 if (printNode) { 284 out.print("<"+el.getTagName()); 285 String sep = " "; 286 NamedNodeMap nnm = el.getAttributes(); 287 for (int i=0, max=nnm.getLength(); i<max; i++) { 288 attr = (Attr) nnm.item(i); 289 out.print(sep+attr); 290 } 291 out.print(">"); 292 } 293 294 printChildNodes(node, out, depth, isHtml); 296 297 if (printNode) { 299 if (!isHtml || 300 (!tag.equals("area") && !tag.equals("base") && !tag.equals("basefont") && 301 !tag.equals("br") && !tag.equals("col") && !tag.equals("frame") && 302 !tag.equals("hr") && !tag.equals("image") && !tag.equals("input") && 303 !tag.equals("isindex") && !tag.equals("link") && !tag.equals("meta") && 304 !tag.equals("param"))) { 305 out.print("</"+el.getTagName()+">"); 306 } 307 } 308 } 309 310 321 323 } else if (node.hasChildNodes()) { 325 printChildNodes(node, out, depth, isHtml); 327 328 } else { 330 DOMWriter dw = getDOMWriter(); 334 dw.setLeaveWriterOpen(true); 335 dw.write(node, out); 336 } 338 } 339 340 public void printChildNodes(Node node, PrintWriter out, int depth, boolean isHtml) throws RenderException, IOException { 341 Node child = node.getFirstChild(); 342 if (child==null) return; 343 do { 344 printNode(child, out, depth+1, isHtml); 345 child = child.getNextSibling(); 346 } while (child!=null); 347 } 348 } 349 | Popular Tags |