001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.renderer;
042:
043: import java.io.IOException;
044: import java.net.URL;
045: import javax.faces.component.UIComponent;
046: import javax.faces.context.FacesContext;
047: import javax.faces.context.ResponseWriter;
048:
049: import com.sun.rave.web.ui.component.Head;
050: import com.sun.rave.web.ui.component.util.Util;
051: import com.sun.rave.web.ui.theme.Theme;
052: import com.sun.rave.web.ui.util.RenderingUtilities;
053: import com.sun.rave.web.ui.util.ThemeUtilities;
054:
055: /**
056: * <p>Renderer for a {@link Head} component.</p>
057: */
058:
059: public class HeadRenderer extends AbstractRenderer {
060:
061: /**
062: * <p>The set of String pass-through attributes to be rendered.</p>
063: */
064: private static final String stringAttributes[] = { "profile" }; //NOI18N
065:
066: /**
067: * <p>Render the appropriate element start, depending on whether the
068: * <code>for</code> property is set or not.</p>
069: *
070: * @param context <code>FacesContext</code> for the current request
071: * @param component component to render.
072: * @param writer <code>ResponseWriter</code> to which the element
073: * start should be rendered
074: *
075: * @exception IOException if an input/output error occurs
076: */
077: protected void renderStart(FacesContext context,
078: UIComponent component, ResponseWriter writer)
079: throws IOException {
080:
081: // Start the appropriate element
082: if (!RenderingUtilities.isPortlet(context)) {
083: writer.startElement("head", component); //NOI18N
084: }
085: }
086:
087: /**
088: * <p>Render the appropriate element attributes,
089: *
090: * @param context <code>FacesContext</code> for the current request
091: * @param component component to be rendered
092: * submitted value is to be stored
093: * @param writer <code>ResponseWriter</code> to which the element
094: * start should be rendered
095: *
096: * @exception IOException if an input/output error occurs
097: */
098: protected void renderAttributes(FacesContext context,
099: UIComponent component, ResponseWriter writer)
100: throws IOException {
101:
102: Head head = (Head) component;
103:
104: if (!RenderingUtilities.isPortlet(context)) {
105:
106: // Profile
107: addStringAttributes(context, component, writer,
108: stringAttributes);
109:
110: // Meta tags
111: writer.write("\n"); //NOI18N
112: renderMetaTag("no-cache", "Pragma", writer, head);
113: renderMetaTag("no-cache", "Cache-Control", writer, head);
114: renderMetaTag("no-store", "Cache-Control", writer, head);
115: renderMetaTag("max-age=0", "Cache-Control", writer, head);
116: renderMetaTag("1", "Expires", writer, head);
117:
118: // Title
119: String title = head.getTitle();
120: if (title == null) {
121: title = "";
122: }
123:
124: writer.startElement("title", head);
125: writer.write(title);
126: writer.endElement("title");
127: writer.write("\n"); //NOI18N
128:
129: // Base
130: if (head.isDefaultBase()) {
131: writer.startElement("base", head); //NOI18N
132: // TODO - verify the requirements w.r.t. printing this href
133: writer.writeURIAttribute("href", Util.getBase(context),
134: null); //NOI18N
135: writer.endElement("base"); //NOI18N
136: writer.write("\n"); //NOI18N
137: }
138:
139: // Link and Scripts
140: Theme theme = ThemeUtilities.getTheme(context);
141: //master link to always write out
142: RenderingUtilities.renderJavaScript(head, theme, context,
143: writer);
144: RenderingUtilities.renderStyleSheetLink(head, theme,
145: context, writer);
146:
147: writer.write(getCookieScript(context));
148: }
149: }
150:
151: private String getCookieScript(FacesContext context) {
152: String viewId = context.getViewRoot().getViewId();
153: String urlString = context.getApplication().getViewHandler()
154: .getActionURL(context, viewId);
155:
156: StringBuffer jsBuffer = new StringBuffer(256);
157: jsBuffer.append("\n<script type=\"text/javascript\">\nvar ");
158: jsBuffer.append("sjwuic_ScrollCookie");
159: jsBuffer.append(" = new sjwuic_ScrollCookie('");
160: jsBuffer.append(viewId);
161: jsBuffer.append("', '");
162: jsBuffer.append(urlString);
163: jsBuffer.append("'); \n</script>\n");
164: return jsBuffer.toString();
165: }
166:
167: /**
168: * <p>Render the appropriate element end, depending on whether the
169: * <code>for</code> property is set or not.</p>
170: *
171: * @param context <code>FacesContext</code> for the current request
172: * @param component component to be rendered
173: * @param writer <code>ResponseWriter</code> to which the element
174: * start should be rendered
175: *
176: * @exception IOException if an input/output error occurs
177: */
178: protected void renderEnd(FacesContext context,
179: UIComponent component, ResponseWriter writer)
180: throws IOException {
181:
182: // Start the appropriate element
183: if (!RenderingUtilities.isPortlet(context)) {
184: writer.endElement("head"); //NOI18N
185: writer.write("\n"); //NOI18N
186: }
187: }
188:
189: private void renderMetaTag(String content, String httpEquivalent,
190: ResponseWriter writer, Head head) throws IOException {
191:
192: writer.startElement("meta", head);
193: writer.writeAttribute("content", content, null);
194: writer.writeAttribute("http-equiv", httpEquivalent, null);
195: writer.endElement("meta");
196: writer.writeText("\n", null);
197: }
198:
199: }
|