1 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 ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.NodeList ; 42 import org.w3c.dom.Text ; 43 44 import java.util.Iterator ; 45 import java.util.List ; 46 47 48 54 public final class AddResource { 55 56 private static final int TEXT_NODE_TYPE = 3; 57 private static final String LINK_ELM = "link"; 58 59 private AddResource() { 60 61 } 62 63 65 73 public static void addJavaScriptToNode(Node parentNode, 74 String resourceDirectory, 75 String scriptFileName, 76 DOMContext domContext) { 77 Element jsElement = null; 79 List scripts = DOMContext.findChildrenWithNodeName((Element ) parentNode, 80 HTML.SCRIPT_ELEM); 81 Iterator scriptIterator = scripts.iterator(); 82 83 while (scriptIterator.hasNext()) { 84 Element next = (Element ) scriptIterator.next(); 85 if (next.getAttribute("src") 86 .equalsIgnoreCase(resourceDirectory + scriptFileName)) { 87 jsElement = next; 88 } 89 } 90 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 109 public static void addJavaScriptToHead(String resourceDirectory, 110 String scriptFileName, 111 DOMContext domContext) { 112 Document test = domContext.getDocument(); 114 NodeList heads = test.getElementsByTagName("head"); 115 Node headNode = null; 116 if (heads.getLength() > 0) { 117 headNode = heads.item(0); 118 } else { Element head = domContext.createElement("head"); 120 test.appendChild(head); 121 headNode = (Node ) head; 122 } 123 addJavaScriptToNode(headNode, resourceDirectory, scriptFileName, 124 domContext); 125 } 126 127 134 public static void addStyleSheetToHead(String resourceDirectory, 135 String cssFileName, 136 DOMContext domContext) { 137 138 Document test = domContext.getDocument(); 139 140 NodeList heads = test.getElementsByTagName("head"); 141 142 Node headNode = heads.item(0); 143 144 Element styleElement = null; 146 List links = DOMContext 147 .findChildrenWithNodeName((Element ) headNode, LINK_ELM); 148 Iterator linkIterator = links.iterator(); 149 150 while (linkIterator.hasNext()) { 151 Element next = (Element ) 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 175 public static void addInlineStyleToHead(String inlineStyle, 176 DOMContext domContext) { 177 Document test = domContext.getDocument(); 179 NodeList heads = test.getElementsByTagName("head"); 180 Node headNode = heads.item(0); 181 182 Element styleElement = null; 184 List styles = DOMContext 185 .findChildrenWithNodeName((Element ) headNode, HTML.STYLE_ELEM); 186 Iterator styleIterator = styles.iterator(); 187 188 while (styleIterator.hasNext()) { 189 Element next = (Element ) styleIterator.next(); 190 if (next.hasChildNodes()) { 191 if (next.getFirstChild().getNodeType() == TEXT_NODE_TYPE) { 192 Text styleText = (Text ) next.getFirstChild(); 193 if (styleText.getNodeValue() 194 .equalsIgnoreCase(inlineStyle)) { 195 styleElement = next; 196 } 197 } 198 } 199 } 200 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 inlineText = domContext.createTextNode(inlineStyle); 207 styleElement.appendChild(inlineText); 208 headNode.appendChild(styleElement); 209 } 210 } 211 212 } 213 | Popular Tags |