001: /**********************************************************************************
002: *
003: * Header:
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2003, 2004 The Sakai Foundation.
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.jsf.renderer;
022:
023: import java.io.IOException;
024:
025: import javax.faces.component.UIComponent;
026: import javax.faces.component.UIOutput;
027: import javax.faces.component.UIViewRoot;
028: import javax.faces.context.FacesContext;
029: import javax.faces.context.ResponseWriter;
030: import javax.faces.render.Renderer;
031: import javax.servlet.http.HttpServletRequest;
032:
033: import org.sakaiproject.jsf.util.RendererUtil;
034:
035: public class ViewRenderer extends Renderer {
036: public boolean supportsComponentType(UIComponent component) {
037: // this should be just UIViewRoot, but since that's not working now...
038: return (component instanceof UIViewRoot)
039: || (component instanceof UIOutput);
040: }
041:
042: public void encodeBegin(FacesContext context, UIComponent component)
043: throws IOException {
044: HttpServletRequest req = (HttpServletRequest) context
045: .getExternalContext().getRequest();
046:
047: ResponseWriter writer = context.getResponseWriter();
048:
049: if (!renderAsFragment(context)) {
050: // The stylesheets and javascripts to include are really the portal's responsibility
051: // so get them from the portal through the request attributes.
052: // Any tool-specific stylesheets need to be placed after Sakai's base CSS (so that
053: // tool-specific overrides can take place), but before the installation's skin CSS
054: // (so that the tool can be skinned).
055: String headBaseCss = (String) req
056: .getAttribute("sakai.html.head.css.base");
057: if (headBaseCss == null || headBaseCss.length() == 0) {
058: // include default stylesheet
059: headBaseCss = "<link href=\"/sakai-jsf-resource/css/sakai.css\" type=\"text/css\" rel=\"stylesheet\" media=\"all\" />\n";
060: }
061: String toolCssHref = (String) RendererUtil.getAttribute(
062: context, component, "toolCssHref");
063: if (toolCssHref != null) {
064: toolCssHref = "<link href=\""
065: + toolCssHref
066: + "\" type=\"text/css\" rel=\"stylesheet\" media=\"all\" />\n";
067: }
068: String headSkinCss = (String) req
069: .getAttribute("sakai.html.head.css.skin");
070: String headJs = (String) req
071: .getAttribute("sakai.html.head.js");
072: String bodyonload = (String) req
073: .getAttribute("sakai.html.body.onload");
074:
075: writer
076: .write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
077: writer
078: .write("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\n");
079: writer.write("<head>\n");
080: String title = (String) RendererUtil.getAttribute(context,
081: component, "title");
082: if (title != null) {
083: writer.write("<title>");
084: writer.write(title);
085: writer.write("</title>\n");
086: }
087:
088: writer.write(headBaseCss);
089: if (toolCssHref != null)
090: writer.write(toolCssHref);
091: if (headSkinCss != null)
092: writer.write(headSkinCss);
093: if (headJs != null)
094: writer.write(headJs);
095:
096: writer.write("</head>\n");
097:
098: writer.write("<body");
099:
100: if (bodyonload != null && bodyonload.length() > 0) {
101: writer.write(" onload=\"");
102: writer.write(bodyonload);
103: writer.write("\"");
104: }
105: writer.write(">\n");
106:
107: }
108:
109: writer.write("<div class=\"portletBody\">\n");
110: }
111:
112: /**
113: * @param context FacesContext for the request we are processing
114: * @param component UIComponent to be rendered
115: * @exception IOException if an input/output error occurs while rendering
116: * @exception NullPointerException if <code>context</code> or <code>component</code> is null
117: */
118: public void encodeEnd(FacesContext context, UIComponent component)
119: throws IOException {
120: ResponseWriter writer = context.getResponseWriter();
121:
122: writer.write("</div>");
123: if (!renderAsFragment(context)) {
124: writer.write("</body></html>");
125: }
126: }
127:
128: /** Looks at Sakai-specific attributes to determine if the view should
129: * render HTML, HEAD, BODY; if the request attribute "sakai.fragment"="true",
130: * then don't render HTML, HEAD, BODY, etc.
131: * @param context
132: * @return
133: */
134: protected static boolean renderAsFragment(FacesContext context) {
135: String fragStr = (String) ((HttpServletRequest) context
136: .getExternalContext().getRequest())
137: .getAttribute("sakai.fragment");
138: return (fragStr != null && "true".equals(fragStr));
139: }
140:
141: }
|