1 /*
2 * Copyright (c) 2011-2024 PrimeFaces Extensions
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22 package org.primefaces.extensions.component.head;
23
24 import java.io.IOException;
25
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.ExternalContext;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.ResponseWriter;
30
31 import org.primefaces.extensions.util.Attrs;
32
33 /**
34 * Renderer for the {@link ExtHead} component which extends PF {@link org.primefaces.renderkit.HeadRenderer}.
35 * <p>
36 * Ordering of rendered resources:
37 * </p>
38 *
39 * <pre>
40 * - first facet if defined
41 * - Theme CSS
42 * - JSF, PF, PF Extensions CSS resources
43 * - middle facet if defined
44 * - JSF, PF, PF Extensions JS resources
45 * - title
46 * - shortcut icon
47 * - h:head content (encoded by super class at encodeChildren)
48 * - last facet if defined
49 * </pre>
50 *
51 * @author Thomas Andraschko
52 * @since 0.2
53 */
54 public class ExtHeadRenderer extends org.primefaces.renderkit.HeadRenderer {
55
56 @Override
57 public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
58 final ResponseWriter writer = context.getResponseWriter();
59 final ExtHead extHead = (ExtHead) component;
60
61 // encode title and shortcut icon
62 encodeTitle(extHead, writer);
63 encodeShortcutIcon(context, extHead, writer);
64
65 super.encodeEnd(context, component);
66 }
67
68 private void encodeTitle(final ExtHead extHead, final ResponseWriter writer) throws IOException {
69 if (extHead.getTitle() != null) {
70 writer.startElement(Attrs.TITLE, null);
71 writer.writeText(extHead.getTitle(), Attrs.TITLE);
72 writer.endElement(Attrs.TITLE);
73 }
74 }
75
76 private void encodeShortcutIcon(final FacesContext context, final ExtHead extHead, final ResponseWriter writer) throws IOException {
77 if (extHead.getShortcutIcon() != null) {
78 ExternalContext externalContext = context.getExternalContext();
79 writer.startElement("link", null);
80 writer.writeAttribute("rel", "shortcut icon", null);
81 writer.writeAttribute("href", externalContext.encodeResourceURL(extHead.getShortcutIcon()), null);
82 writer.endElement("link");
83 }
84 }
85 }