KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > context > NormalModeSerializer


1 package com.icesoft.faces.context;
2
3 import com.icesoft.faces.application.StartupTime;
4 import com.icesoft.faces.util.DOMUtils;
5 import com.icesoft.faces.util.CoreUtils;
6 import com.icesoft.faces.context.effects.JavascriptContext;
7 import com.icesoft.jasper.Constants;
8 import org.w3c.dom.Document JavaDoc;
9 import org.w3c.dom.Node JavaDoc;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import javax.faces.context.ExternalContext;
14 import java.io.IOException JavaDoc;
15 import java.io.Writer JavaDoc;
16 import java.util.Map JavaDoc;
17
18 public class NormalModeSerializer implements DOMSerializer {
19
20     private static Log log = LogFactory.getLog(NormalModeSerializer.class);
21
22     private BridgeFacesContext context;
23     private Writer JavaDoc writer;
24
25     public NormalModeSerializer(BridgeFacesContext context, Writer JavaDoc writer) {
26         this.context = context;
27         this.writer = writer;
28     }
29
30     public void serialize(Document JavaDoc document) throws IOException JavaDoc {
31         Map JavaDoc requestMap = context.getExternalContext().getRequestMap();
32
33         if( isFragment(requestMap) ){
34             if( log.isDebugEnabled() ){
35                 log.debug( "treating request as a fragment" );
36             }
37
38             Node JavaDoc body = DOMUtils.getChildByNodeName(
39                     document.getDocumentElement(), "body");
40             if (null != body) {
41
42                 //We need to include, for now, ICE_EXTRAS all the time to
43
//ensure that it is available.
44
writer.write( makeScriptEntry(JavascriptContext.ICE_BRIDGE));
45                 writer.write( makeScriptEntry(JavascriptContext.ICE_EXTRAS));
46
47                 writer.write(DOMUtils.childrenToString(body));
48             }
49         } else {
50             if (log.isDebugEnabled()) {
51                 log.debug("treating request as a whole page (not a fragment)");
52             }
53
54             String JavaDoc publicID =
55                     (String JavaDoc) requestMap.get(DOMResponseWriter.DOCTYPE_PUBLIC);
56             String JavaDoc systemID =
57                     (String JavaDoc) requestMap.get(DOMResponseWriter.DOCTYPE_SYSTEM);
58             String JavaDoc root =
59                     (String JavaDoc) requestMap.get(DOMResponseWriter.DOCTYPE_ROOT);
60             String JavaDoc output =
61                     (String JavaDoc) requestMap.get(DOMResponseWriter.DOCTYPE_OUTPUT);
62             boolean prettyPrinting =
63                     Boolean.valueOf((String JavaDoc) requestMap
64                             .get(DOMResponseWriter.DOCTYPE_PRETTY_PRINTING))
65                             .booleanValue();
66
67             //todo: replace this with a complete new implementation that doesn't rely on xslt but can serialize xml, xhtml, and html.
68
if (output == null || ("html".equals(output) && !prettyPrinting)) {
69                 if (publicID != null && systemID != null && root != null) {
70                     writer.write(DOMUtils.DocumentTypetoString(publicID, systemID,
71                             root));
72                 }
73                 writer.write(DOMUtils.DOMtoString(document));
74             } else {
75                 //use a serializer. not as performant.
76
JAXPSerializer serializer =
77                         new JAXPSerializer(writer, publicID, systemID);
78                 if ("xml".equals(output)) {
79                     serializer.outputAsXML();
80                 } else {
81                     serializer.outputAsHTML();
82                 }
83                 if (prettyPrinting) {
84                     serializer.printPretty();
85                 }
86                 serializer.serialize(document);
87             }
88         }
89
90         writer.flush();
91     }
92
93     private String JavaDoc makeScriptEntry(String JavaDoc src) {
94         return "<script language='javascript' SRC='" +
95                CoreUtils.resolveResourceURL(context, src) +
96                "'></script>";
97     }
98
99     private boolean isFragment(Map JavaDoc requestMap){
100         //TODO - assuming we can handle portlets just like includes, then
101
//we can probably reduce the attributes that we check for. We need
102
//to be specific about when to use request URI and when to use servlet
103
//path.
104

105         String JavaDoc frag = (String JavaDoc) requestMap.get(Constants.INC_REQUEST_URI);
106         if( log.isDebugEnabled() ){
107             log.debug( Constants.INC_REQUEST_URI + " = " + frag );
108         }
109         if( frag != null ){
110             return true;
111         }
112
113         frag = (String JavaDoc)requestMap.get(Constants.INC_SERVLET_PATH);
114         if( log.isDebugEnabled() ){
115             log.debug( Constants.INC_SERVLET_PATH + " = " + frag );
116         }
117         if( frag != null ){
118             return true;
119         }
120
121         //This type of check should no longer be required. If we need
122
//to put a portlet specific attribute back in, then we should
123
//define our own.
124
frag = (String JavaDoc) requestMap.get("com.sun.faces.portlet.INIT");
125         if( frag != null ){
126             return true;
127         }
128
129         return false;
130     }
131 }
132
Popular Tags