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 ; 9 import org.w3c.dom.Node ; 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 ; 15 import java.io.Writer ; 16 import java.util.Map ; 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 writer; 24 25 public NormalModeSerializer(BridgeFacesContext context, Writer writer) { 26 this.context = context; 27 this.writer = writer; 28 } 29 30 public void serialize(Document document) throws IOException { 31 Map 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 body = DOMUtils.getChildByNodeName( 39 document.getDocumentElement(), "body"); 40 if (null != body) { 41 42 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 publicID = 55 (String ) requestMap.get(DOMResponseWriter.DOCTYPE_PUBLIC); 56 String systemID = 57 (String ) requestMap.get(DOMResponseWriter.DOCTYPE_SYSTEM); 58 String root = 59 (String ) requestMap.get(DOMResponseWriter.DOCTYPE_ROOT); 60 String output = 61 (String ) requestMap.get(DOMResponseWriter.DOCTYPE_OUTPUT); 62 boolean prettyPrinting = 63 Boolean.valueOf((String ) requestMap 64 .get(DOMResponseWriter.DOCTYPE_PRETTY_PRINTING)) 65 .booleanValue(); 66 67 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 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 makeScriptEntry(String src) { 94 return "<script language='javascript' SRC='" + 95 CoreUtils.resolveResourceURL(context, src) + 96 "'></script>"; 97 } 98 99 private boolean isFragment(Map requestMap){ 100 105 String frag = (String ) 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 )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 frag = (String ) requestMap.get("com.sun.faces.portlet.INIT"); 125 if( frag != null ){ 126 return true; 127 } 128 129 return false; 130 } 131 } 132 | Popular Tags |