KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > util > dom > JivanDOMWriter


1 /*
2  * Copyright (C) 2001 Arno Schatz [arno@jivan.org]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: JivanDOMWriter.java,v 1.3 2004/01/29 18:02:35 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.util.dom;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.io.Writer JavaDoc;
26
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.log4j.Logger;
30 import org.jivan.html.document.DocumentFactory;
31 import org.jivan.html.document.DocumentManager;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.html.HTMLDocument;
35
36 /**
37  * This class is a specialized DOMWriter for Jivan. It calls the Jivan serializers
38  * for output to a Outputstream or java.io.Writer.
39  *
40  * The root node of the tree to be outputted must be either a Document or Element.
41  * This means DocumentFragments are not supported. (other node types don't
42  * really make sense here)
43  *
44  * After printout, the subtree is being repaired and ready to be used again.
45  * @author Arno, arno@jivan.org
46  */

47 public class JivanDOMWriter implements DOMWriter {
48
49     protected static final Logger logger = Logger.getLogger(JivanDOMWriter.class.getName());
50
51     // prevent the HTML pages from being cached by the browser
52
protected boolean preventCaching = false;
53
54     // how long can the page be cached by browser
55
protected String JavaDoc contentType = null; //csc_012804_1
56
protected String JavaDoc contentDisposition = null; //csc_012804_1
57
protected boolean leaveWriterOpen = false; //csc_012804_1
58
protected int maxAge = 0;
59
60     /**
61     * Createing a DOMWriter for outputting a DOM created by Jivan
62     */

63     public JivanDOMWriter() {
64         super();
65     }
66
67 //csc_012804_1_start
68
/**
69      * Set the content type (defaults to "text/html" or "text/xml" depending on the
70      * document type
71      */

72     public void setContentType(String JavaDoc icontentType) {
73         contentType = icontentType;
74     }
75
76     /**
77      * Get the content type
78      */

79     public String JavaDoc getContentType() {
80         return contentType;
81     }
82
83     /**
84      * Set the content disposition (ie. "inline; filename=foo.txt", defaults to null)
85      */

86     public void setContentDisposition(String JavaDoc icontentDisposition) {
87         contentDisposition = icontentDisposition;
88     }
89
90     /**
91      * Get the content disposition
92      */

93     public String JavaDoc getContentDisposition() {
94         return contentDisposition;
95     }
96
97     /**
98      * Set true if we want to leave the writer open (ie. for multiple writes)
99      */

100     public void setLeaveWriterOpen(boolean val) {
101         leaveWriterOpen = val;
102     }
103
104     /**
105      * return true if the writer is configured to leave the output stream open
106      */

107     public boolean getLeaveWriterOpen() {
108         return leaveWriterOpen;
109     }
110
111     /**
112      * Prepare the response object
113      *
114      * @param node the DOM node to be written out
115      * @param resp the HttpServletResponse object
116      */

117     public void prepareResponse(HttpServletResponse JavaDoc resp) throws IOException JavaDoc {
118         //set the content type and disposition
119
if (logger.isDebugEnabled()) logger.debug("Setting content type:"+contentType+" content disposition:"+contentDisposition);
120         if (contentType!=null) resp.setContentType(contentType);
121         if (contentDisposition!=null) resp.setHeader("Content-Disposition", contentDisposition);
122
123         //if we need to prevent caching
124
if (preventCaching) {
125             //add the appropriate headers to the response
126
if (logger.isDebugEnabled()) logger.debug("updating resp hdr to prevent caching");
127             resp.setHeader("Pragma","no-cache");
128             resp.setHeader("Cache-Control","no-cache");
129             resp.setDateHeader("Expires", System.currentTimeMillis());
130
131         //csc_061202.1 - added
132
//otherwise explicitly give it a max-age (this will generally allow browsers like
133
//IE to page back in history without reloading, but if the user actually revisits the
134
//URL, then it will still be reloaded)
135
} else {
136             resp.setHeader("Cache-Control","max-age="+maxAge);
137             resp.setDateHeader("Last-Modified", System.currentTimeMillis());
138         }
139     }
140 //csc_012804_1_end
141

142     /**
143      * Write a DOM to a ServletResponse object. This method will
144      * automatically set the content type for you.
145      * This method is copied from DefaultDOMWriter. It should really be
146      * abstracted from a common base class.
147      *
148      * @param node a DOM node, which belongs to the Document to be written out
149      * @param resp the HttpServletResponse object
150      */

151     public void write(Node JavaDoc node, HttpServletResponse JavaDoc resp) throws IOException JavaDoc {
152 //csc_012804_1_start
153
/*
154         //set the response type (TODO:need to support WML as well)
155         String responseType = "text/xml";
156         if (node instanceof HTMLDocument) responseType = "text/html";
157         if (logger.isDebugEnabled()) logger.debug("Setting the response type:"+responseType);
158         resp.setContentType(responseType);
159
160         //if we need to prevent caching
161         if (preventCaching) {
162             //add the appropriate headers to the response
163             if (logger.isDebugEnabled()) logger.debug("updating resp hdr to prevent caching");
164             resp.setHeader("Pragma","no-cache");
165             resp.setHeader("Cache-Control","no-cache");
166             resp.setDateHeader("Expires", System.currentTimeMillis());
167
168         //csc_061202.1 - added
169         //otherwise explicitly give it a max-age (this will generally allow browsers like
170         //IE to page back in history without reloading, but if the user actually revisits the
171         //URL, then it will still be reloaded)
172         } else {
173             resp.setHeader("Cache-Control","max-age="+maxAge);
174             resp.setDateHeader("Last-Modified", System.currentTimeMillis());
175         }
176 */

177         if (getContentType()==null) setContentType((node instanceof HTMLDocument) ? "text/html" : "text/xml");
178         prepareResponse(resp);
179 //csc_012804_1_end
180

181         //write the dom
182
write(node, resp.getWriter());
183     }
184
185     /* (non-Javadoc)
186      * @see org.enhydra.barracuda.core.util.dom.DOMWriter#write(org.w3c.dom.Node, java.io.OutputStream)
187      */

188     public void write(Node JavaDoc node, OutputStream JavaDoc out) throws IOException JavaDoc {
189         if (node.getNodeType()==Node.DOCUMENT_NODE) {
190             DocumentFactory.getInstance().docManFor(node).serialize(out);
191         } else {
192             write(node, new PrintWriter JavaDoc(out));
193         }
194     }
195
196     /**
197      * output of the Document of this node using a writer for the output.
198      * After serialization, the Document is being repaired.
199      */

200     public void write(Node JavaDoc node, Writer JavaDoc writer) throws IOException JavaDoc {
201         switch(node.getNodeType()) {
202             case Node.DOCUMENT_NODE:
203                 DocumentFactory.getInstance().docManFor(node).serialize(writer);
204                 break;
205             case Node.ELEMENT_NODE:
206                 DocumentManager man = DocumentFactory.getInstance().docManFor(node);
207                 writer.write(man.serialize((Element JavaDoc)node, true));
208                 break; //csc_012804_1 - shouldn't there be a break here???
209
default:
210                 throw new IllegalArgumentException JavaDoc("Only Elements and Dcument can be serialized, not "+node);
211         }
212         if (!leaveWriterOpen) writer.close(); //csc_012804_1
213
}
214
215     /**
216      * @return Returns the maxAge.
217      */

218     public int getMaxAge() {
219         return maxAge;
220     }
221
222     /**
223      * @param maxAge The maxAge to set.
224      */

225     public void setMaxAge(int maxAge) {
226         this.maxAge = maxAge;
227     }
228
229     /**
230      * @return Returns the preventCaching.
231      */

232     public boolean isPreventCaching() {
233         return preventCaching;
234     }
235
236     /**
237      * @param preventCaching The preventCaching to set.
238      */

239     public void setPreventCaching(boolean preventCaching) {
240         this.preventCaching = preventCaching;
241     }
242
243 }
Popular Tags