1 20 package org.enhydra.barracuda.core.util.dom; 21 22 import java.io.*; 24 import javax.servlet.http.*; 25 26 27 import org.apache.log4j.*; 29 import org.enhydra.barracuda.core.util.dom.*; 30 import org.w3c.dom.*; 31 import org.w3c.dom.html.*; 32 import org.w3c.dom.CharacterData ; 34 38 public class CommaSeparatedDOMWriter implements DOMWriter { 39 40 private static final Class CLASS = CommaSeparatedDOMWriter.class; 42 private static final Logger logger = Logger.getLogger(CLASS); 43 44 public static final String DOCUMENT_TYPE = "table"; 46 public static final String ELEMENT_ROW = "tr"; 47 public static final String ELEMENT_HEADER = "th"; 48 public static final String ELEMENT_COLUMN = "td"; 49 50 51 protected String contentType = "text/plain"; 53 protected String contentDisposition = null; 54 protected boolean preventCaching = false; 55 protected boolean leaveWriterOpen = false; 56 protected int maxAge = 0; 57 58 63 public CommaSeparatedDOMWriter() { 64 this(null, null); 65 } 66 67 73 public CommaSeparatedDOMWriter(String icontentType, String icontentDisposition) { 74 setContentType(icontentType); 75 setContentDisposition(icontentDisposition); 76 } 77 78 83 public void setContentType(String icontentType) { 84 contentType = icontentType; 85 } 86 87 90 public String getContentType() { 91 return contentType; 92 } 93 94 97 public void setContentDisposition(String icontentDisposition) { 98 contentDisposition = icontentDisposition; 99 } 100 101 104 public String getContentDisposition() { 105 return contentDisposition; 106 } 107 108 111 public void setLeaveWriterOpen(boolean val) { 112 leaveWriterOpen = val; 113 } 114 115 118 public boolean getLeaveWriterOpen() { 119 return leaveWriterOpen; 120 } 121 122 123 129 public void prepareResponse(HttpServletResponse resp) throws IOException { 130 if (logger.isDebugEnabled()) logger.debug("Setting content type:"+contentType+" content disposition:"+contentDisposition); 132 if (contentType!=null) resp.setContentType(contentType); 133 if (contentDisposition!=null) resp.setHeader("Content-Disposition", contentDisposition); 134 135 if (preventCaching) { 137 if (logger.isDebugEnabled()) logger.debug("updating resp hdr to prevent caching"); 139 resp.setHeader("Pragma","no-cache"); 140 resp.setHeader("Cache-Control","no-cache"); 141 resp.setDateHeader("Expires", System.currentTimeMillis()); 142 143 } else { 148 resp.setHeader("Cache-Control","max-age="+maxAge); 149 resp.setDateHeader("Last-Modified", System.currentTimeMillis()); 150 } 151 } 152 154 161 public void write(Node node, HttpServletResponse resp) throws IOException { 162 178 if (getContentType()==null) setContentType((node instanceof HTMLDocument) ? "text/html" : "text/xml"); 179 prepareResponse(resp); 180 182 write(node, resp.getWriter()); 184 } 185 186 192 public void write(Node node, OutputStream out) throws IOException { 193 write(node, new OutputStreamWriter(out)); 194 } 195 196 202 public void write(Node node, Writer writer) throws IOException { 203 try { writeAll(node, writer); } 204 catch (DOMException e) { 205 logger.error("Error while writing node", e); 206 } 207 if (!leaveWriterOpen) writer.close(); 208 } 209 210 protected void writeAll(Node node, Writer writer) throws IOException, DOMException { 211 if (node==null) { 212 return; 213 } else if (node instanceof Element) { 214 String name = node.getNodeName(); 215 if (name.equals(ELEMENT_ROW)) { 216 writeRow(node.getChildNodes(), writer); 217 } else if (name.equals(ELEMENT_HEADER)) { 218 writeHeader(node.getChildNodes(), writer); 219 } else if (name.equals(ELEMENT_COLUMN)) { 220 writeColumn(node.getChildNodes(), writer); 221 } else { 222 writeAll(node.getChildNodes(), writer); 223 } 224 } else if (node instanceof CharacterData ) { 225 if (!(node instanceof Comment)) { 226 writer.write(node.getNodeValue()); 227 } 228 } else if (node instanceof Document) { 229 if (logger.isInfoEnabled()) logger.info("Writing document: "+node.getClass()); 230 writeAll(node.getChildNodes(), writer); 231 } else if (node instanceof DocumentType) { 232 String name = node.getNodeName(); 233 if (!name.equals(DOCUMENT_TYPE)) { 234 throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Document type not supported; required \""+DOCUMENT_TYPE+"\", found \""+name+"\""); 235 } 236 } else { 237 logger.warn("Unrecognized node type: "+node.getClass()); 238 writeAll(node.getChildNodes(), writer); 239 } 240 } 241 242 protected void writeAll(NodeList nodes, Writer writer) throws IOException, DOMException { 243 int len = nodes.getLength(); 244 for (int i = 0; i < len; i++) { 245 if (Thread.interrupted()) throw new IOException("Thread interrupted while generating output"); 246 writeAll(nodes.item(i), writer); 247 } 248 } 249 250 protected void writeRow(NodeList nodes, Writer writer) throws IOException, DOMException { 251 StringWriter sw = new StringWriter(1000); 252 writeAll(nodes, sw); 253 sw.close(); 254 255 StringBuffer sb = sw.getBuffer(); 256 sb.setCharAt(sb.length()-1, '\n'); 257 writer.write(sb.toString()); 258 } 259 260 protected void writeHeader(NodeList nodes, Writer writer) throws IOException, DOMException { 261 writeColumn(nodes, writer); 263 } 264 265 protected void writeColumn(NodeList nodes, Writer writer) throws IOException, DOMException { 266 StringWriter sw = new StringWriter(100); 267 writeAll(nodes, sw); 268 sw.close(); 269 270 String data = sw.toString(); 271 writer.write('"'); 272 writer.write(data.replaceAll("\"", "\"\"")); 273 writer.write("\","); 274 } 275 } 276 | Popular Tags |