KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > utils > AddResource


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.utils;
35
36 import com.icesoft.faces.context.DOMContext;
37 import com.icesoft.faces.renderkit.dom_html_basic.HTML;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42 import org.w3c.dom.Text JavaDoc;
43
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46
47
48 /**
49  * This utility class uses DOM methods to render javascript and css resources to
50  * the page.
51  *
52  * @author gmccleary
53  */

54 public final class AddResource {
55
56     private static final int TEXT_NODE_TYPE = 3;
57     private static final String JavaDoc LINK_ELM = "link";
58
59     private AddResource() {
60
61     }
62
63     // Methods to Add Javascript and CSS resources
64

65     /**
66      * Adds the given javascript to the given node
67      *
68      * @param parentNode
69      * @param resourceDirectory
70      * @param scriptFileName
71      * @param domContext
72      */

73     public static void addJavaScriptToNode(Node JavaDoc parentNode,
74                                            String JavaDoc resourceDirectory,
75                                            String JavaDoc scriptFileName,
76                                            DOMContext domContext) {
77         // scan the given node for script elements
78
Element JavaDoc jsElement = null;
79         List JavaDoc scripts = DOMContext.findChildrenWithNodeName((Element JavaDoc) parentNode,
80                                                            HTML.SCRIPT_ELEM);
81         Iterator JavaDoc scriptIterator = scripts.iterator();
82
83         while (scriptIterator.hasNext()) {
84             Element JavaDoc next = (Element JavaDoc) scriptIterator.next();
85             if (next.getAttribute("src")
86                     .equalsIgnoreCase(resourceDirectory + scriptFileName)) {
87                 jsElement = next;
88             }
89         }
90         // if js script already exists, then don't add it
91
if (jsElement == null) {
92             jsElement =
93                     domContext.getDocument().createElement(HTML.SCRIPT_ELEM);
94             jsElement.setAttribute(HTML.SRC_ATTR,
95                                    resourceDirectory + scriptFileName);
96             jsElement.setAttribute("language", "javascript");
97
98             parentNode.appendChild(jsElement);
99         }
100     }
101
102     /**
103      * Adds the given javascript resource to the head node
104      *
105      * @param resourceDirectory
106      * @param scriptFileName
107      * @param domContext
108      */

109     public static void addJavaScriptToHead(String JavaDoc resourceDirectory,
110                                            String JavaDoc scriptFileName,
111                                            DOMContext domContext) {
112         // extract the head node from the document
113
Document JavaDoc test = domContext.getDocument();
114         NodeList JavaDoc heads = test.getElementsByTagName("head");
115         Node JavaDoc headNode = null;
116         if (heads.getLength() > 0) {
117             headNode = heads.item(0);
118         } else { // no head exists , create one
119
Element JavaDoc head = domContext.createElement("head");
120             test.appendChild(head);
121             headNode = (Node JavaDoc) head;
122         }
123         addJavaScriptToNode(headNode, resourceDirectory, scriptFileName,
124                             domContext);
125     }
126
127     /**
128      * Adds the given stylesheet resource to the head
129      *
130      * @param resourceDirectory
131      * @param cssFileName
132      * @param domContext
133      */

134     public static void addStyleSheetToHead(String JavaDoc resourceDirectory,
135                                            String JavaDoc cssFileName,
136                                            DOMContext domContext) {
137
138         Document JavaDoc test = domContext.getDocument();
139
140         NodeList JavaDoc heads = test.getElementsByTagName("head");
141
142         Node JavaDoc headNode = heads.item(0);
143
144         // if stylesheet link already exists, then don't add it
145
Element JavaDoc styleElement = null;
146         List JavaDoc links = DOMContext
147                 .findChildrenWithNodeName((Element JavaDoc) headNode, LINK_ELM);
148         Iterator JavaDoc linkIterator = links.iterator();
149
150         while (linkIterator.hasNext()) {
151             Element JavaDoc next = (Element JavaDoc) linkIterator.next();
152             if (next.getAttribute("href")
153                     .equalsIgnoreCase(resourceDirectory + cssFileName)) {
154                 styleElement = next;
155             }
156         }
157
158         if (styleElement == null) {
159             styleElement = domContext.getDocument().createElement(LINK_ELM);
160             styleElement.setAttribute(HTML.HREF_ATTR,
161                                       resourceDirectory + cssFileName);
162             styleElement.setAttribute("type", "text/css");
163             styleElement.setAttribute("rel", "stylesheet");
164
165             headNode.appendChild(styleElement);
166         }
167     }
168
169     /**
170      * Adds the given inline style to the head
171      *
172      * @param inlineStyle
173      * @param domContext
174      */

175     public static void addInlineStyleToHead(String JavaDoc inlineStyle,
176                                             DOMContext domContext) {
177         // extract the head from the document element
178
Document JavaDoc test = domContext.getDocument();
179         NodeList JavaDoc heads = test.getElementsByTagName("head");
180         Node JavaDoc headNode = heads.item(0);
181
182         // extract style elements from head
183
Element JavaDoc styleElement = null;
184         List JavaDoc styles = DOMContext
185                 .findChildrenWithNodeName((Element JavaDoc) headNode, HTML.STYLE_ELEM);
186         Iterator JavaDoc styleIterator = styles.iterator();
187
188         while (styleIterator.hasNext()) {
189             Element JavaDoc next = (Element JavaDoc) styleIterator.next();
190             if (next.hasChildNodes()) {
191                 if (next.getFirstChild().getNodeType() == TEXT_NODE_TYPE) {
192                     Text JavaDoc styleText = (Text JavaDoc) next.getFirstChild();
193                     if (styleText.getNodeValue()
194                             .equalsIgnoreCase(inlineStyle)) {
195                         styleElement = next;
196                     }
197                 }
198             }
199         }
200         // if inline style already exists, then don't add it
201
if (styleElement == null) {
202             styleElement =
203                     domContext.getDocument().createElement(HTML.STYLE_ELEM);
204             styleElement.setAttribute("type", "text/css");
205             styleElement.setAttribute("rel", "stylesheet");
206             Text JavaDoc inlineText = domContext.createTextNode(inlineStyle);
207             styleElement.appendChild(inlineText);
208             headNode.appendChild(styleElement);
209         }
210     }
211
212 }
213
Popular Tags