1 20 package org.enhydra.barracuda.core.util.dom; 21 22 import java.io.*; 23 import javax.servlet.*; 24 import javax.servlet.http.*; 25 26 import org.apache.log4j.*; 27 28 import org.w3c.dom.*; 29 import org.w3c.dom.html.*; 30 31 import org.w3c.tidy.Tidy; 32 33 import org.enhydra.barracuda.core.comp.*; 34 import org.enhydra.xml.io.*; 35 36 37 50 public class DefaultDOMWriter implements DOMWriter { 51 52 protected static final Logger logger = Logger.getLogger(DefaultDOMWriter.class.getName()); 53 54 59 public static String DEFAULT_OO_PUBLIC_ID = null; 60 61 66 public static String DEFAULT_OO_SYSTEM_ID = null; 67 68 71 public static boolean DEFAULT_PRINT_PRETTY = false; 72 73 76 public static boolean DEFAULT_PREVENT_CACHING = false; 77 78 81 public static int DEFAULT_MAX_AGE = 0; 82 83 protected DOMFormatter dfm = null; 84 protected OutputOptions oo = null; 85 protected String contentType = null; protected String contentDisposition = null; protected boolean printPretty = false; 88 protected boolean preventCaching = false; 89 protected boolean leaveWriterOpen = false; 90 protected int maxAge = 0; 92 95 public DefaultDOMWriter() { 96 this(null, DEFAULT_PRINT_PRETTY, DEFAULT_PREVENT_CACHING, DEFAULT_MAX_AGE); 97 } 98 99 104 public DefaultDOMWriter(OutputOptions oo) { 105 this(oo, DEFAULT_PRINT_PRETTY, DEFAULT_PREVENT_CACHING, DEFAULT_MAX_AGE); 106 } 107 108 113 public DefaultDOMWriter(boolean printPretty) { 114 this(null, printPretty, DEFAULT_PREVENT_CACHING, DEFAULT_MAX_AGE); 115 } 116 117 128 public DefaultDOMWriter(OutputOptions ioo, boolean iprintPretty, boolean ipreventCaching, int imaxAge) { 129 setOutputOptions(ioo); 130 setPrettyPrint(iprintPretty); 131 setPreventCaching(ipreventCaching); 132 setMaxAge(imaxAge); 133 } 134 135 140 public void setContentType(String icontentType) { 141 contentType = icontentType; 142 } 143 144 147 public String getContentType() { 148 return contentType; 149 } 150 151 154 public void setContentDisposition(String icontentDisposition) { 155 contentDisposition = icontentDisposition; 156 } 157 158 161 public String getContentDisposition() { 162 return contentDisposition; 163 } 164 165 168 public void setLeaveWriterOpen(boolean val) { 169 leaveWriterOpen = val; 170 } 171 172 175 public boolean getLeaveWriterOpen() { 176 return leaveWriterOpen; 177 } 178 179 185 public void prepareResponse(HttpServletResponse resp) throws IOException { 186 if (logger.isDebugEnabled()) logger.debug("Setting content type:"+contentType+" content disposition:"+contentDisposition); 188 if (contentType!=null) resp.setContentType(contentType); 189 if (contentDisposition!=null) resp.setHeader("Content-Disposition", contentDisposition); 190 191 if (preventCaching) { 193 if (logger.isDebugEnabled()) logger.debug("updating resp hdr to prevent caching"); 195 resp.setHeader("Pragma","no-cache"); 196 resp.setHeader("Cache-Control","no-cache"); 197 resp.setDateHeader("Expires", System.currentTimeMillis()); 198 199 } else { 203 resp.setHeader("Cache-Control","max-age="+maxAge); 204 resp.setDateHeader("Last-Modified", System.currentTimeMillis()); 205 } 206 } 207 209 216 public void write(Node node, HttpServletResponse resp) throws IOException { 217 242 if (getContentType()==null) setContentType((node instanceof HTMLDocument) ? "text/html" : "text/xml"); 243 prepareResponse(resp); 244 246 write(node, resp.getWriter()); 248 } 249 250 256 public void write(Node node, OutputStream out) throws IOException { 257 write(node, new PrintWriter(out)); 258 } 259 260 266 public void write(Node node, Writer writer) throws IOException { 267 if (dfm==null) dfm = new DOMFormatter(); 269 270 OutputOptions localoo = null; 271 if (oo != null) localoo = oo; 273 if (localoo == null) localoo = getDefaultOutputOptions(node.getOwnerDocument()); 274 275 dfm.setOutputOptions(localoo); 277 278 if (!printPretty) { 280 if (logger.isDebugEnabled()) logger.debug("Writing DOM directly to writer"); 282 dfm.write(node, writer); 283 if (!leaveWriterOpen) writer.close(); 284 } else { 285 byte[] bytes = dfm.toBytes(node); 287 288 if (logger.isDebugEnabled()) logger.debug("Formatting for pretty print"); 290 Tidy tidy = new Tidy(); 291 tidy.setIndentContent(true); 292 tidy.setSpaces(2); 293 tidy.setQuiet(true); 294 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 295 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 296 tidy.parse(bais, baos); 297 298 if (logger.isDebugEnabled()) logger.debug("Writing DOM to writer"); 300 writer.write(baos.toString()); 301 if (!leaveWriterOpen) writer.close(); 302 } 303 } 304 305 public void setPrettyPrint(boolean val) { 306 this.printPretty = val; 307 } 308 309 public void setPreventCaching(boolean val) { 310 this.preventCaching = val; 311 } 312 313 public void setMaxAge(int imax) { 315 this.maxAge = imax; 316 } 317 318 public void setOutputOptions(OutputOptions ioo) { 320 this.oo = ioo; 321 } 322 323 326 public OutputOptions getOutputOptions() { 327 return oo; 328 } 329 330 public static OutputOptions getDefaultOutputOptions(Document doc) { 331 OutputOptions doo = DOMFormatter.getDefaultOutputOptions(doc); 334 if (DEFAULT_OO_PUBLIC_ID!=null || DEFAULT_OO_SYSTEM_ID!=null) doo.setOmitDocType(false); 335 if (DEFAULT_OO_PUBLIC_ID!=null) doo.setPublicId(DEFAULT_OO_PUBLIC_ID); 336 if (DEFAULT_OO_SYSTEM_ID!=null) doo.setSystemId(DEFAULT_OO_SYSTEM_ID); 337 return doo; 338 } 340 341 380 381 } 382 | Popular Tags |