1 11 12 package org.eclipse.ui.internal.intro.impl.model.util; 13 14 import java.util.Enumeration ; 15 import java.util.Properties ; 16 import java.util.Vector ; 17 18 import org.eclipse.core.runtime.FileLocator; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage; 24 import org.eclipse.ui.internal.intro.impl.model.IntroExtensionContent; 25 import org.eclipse.ui.internal.intro.impl.model.url.IntroURLParser; 26 import org.eclipse.ui.internal.intro.impl.util.Log; 27 import org.osgi.framework.Bundle; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 33 34 38 public class ModelUtil { 39 40 private static String TAG_BODY = "body"; private static String TAG_HEAD = "head"; private static String TAG_BASE = "base"; public static String TAG_DIV = "div"; public static String TAG_HEAD_LINK = "link"; private static String TAG_PARAM = "param"; private static String ATT_SRC = "src"; private static String ATT_HREF = "href"; private static String ATT_CITE = "cite"; private static String ATT_LONGDESC = "longdesc"; private static String ATT_DATA = "data"; private static String ATT_CODEBASE = "codebase"; private static String ATT_VALUE = "value"; private static String ATT_VALUE_TYPE = "valuetype"; private static String ATT_REL = "rel"; private static String ATT_TYPE = "type"; 57 58 59 62 63 73 public static String resolveURL(String url, String pluginId) { 74 Bundle bundle = null; 75 if (pluginId != null) 76 bundle = Platform.getBundle(pluginId); 78 return resolveURL("", url, bundle); } 80 81 82 83 93 public static String resolveURL(String url, IConfigurationElement element) { 94 Bundle bundle = BundleUtil.getBundleFromConfigurationElement(element); 95 return resolveURL("", url, bundle); } 97 98 99 100 103 public static String resolveURL(String base, String url, Bundle bundle) { 104 if (url == null) 106 return null; 107 IntroURLParser parser = new IntroURLParser(url); 108 if (parser.hasProtocol()) 109 return url; 110 return BundleUtil.getResolvedResourceLocation(base, url, bundle); 112 } 113 114 118 public static void ensureFileURLsExist(Bundle bundle, String contentFile) { 119 try { 120 FileLocator.toFileURL(bundle.getEntry("/")); } catch (Exception e) { 122 if (contentFile != null) 123 Log.error("Failed to extract Intro content folder for: " + contentFile, e); 125 } 126 } 127 128 129 134 public static String getParentFolderToString(String contentFilePath) { 135 IPath path = getParentFolderPath(contentFilePath); 136 return path.toString(); 137 } 138 139 140 144 145 150 public static String getParentFolderOSString(String contentFilePath) { 151 IPath path = getParentFolderPath(contentFilePath); 152 return path.toOSString(); 153 } 154 155 158 public static IPath getParentFolderPath(String contentFilePath) { 159 IPath path = new Path(contentFilePath); 160 path = path.removeLastSegments(1).addTrailingSeparator(); 161 return path; 162 } 163 164 165 166 167 public static void insertBase(Document dom, String baseURL) { 168 NodeList headList = dom.getElementsByTagName(TAG_HEAD); 170 Element head = (Element) headList.item(0); 171 NodeList baseList = head.getElementsByTagName(TAG_BASE); 172 if (baseList.getLength() == 0) { 173 Element base = dom.createElement(TAG_BASE); 175 base.setAttribute(ATT_HREF, baseURL); 176 head.insertBefore(base, head.getFirstChild()); 177 } 178 } 179 180 181 public static Element getBase(Document dom) { 182 NodeList headList = dom.getElementsByTagName(TAG_HEAD); 184 Element head = (Element) headList.item(0); 185 NodeList baseList = head.getElementsByTagName(TAG_BASE); 186 if (baseList.getLength() == 0) 187 return null; 189 190 return (Element) baseList.item(baseList.getLength() - 1); 191 192 } 193 194 195 public static void insertStyle(Document dom, String cssUrl) { 197 NodeList headList = dom.getElementsByTagName(TAG_HEAD); 199 Element head = null; 200 NodeList styleList = null; 202 if (headList.getLength() >= 1) { 204 head = (Element) headList.item(0); 205 styleList = head.getElementsByTagName(TAG_HEAD_LINK); 206 for (int i = 0; i < styleList.getLength(); i++) { 207 Element style = (Element) styleList.item(0); 208 String styleString = style.getAttribute(ATT_HREF); 209 if (styleString.equals(cssUrl)) 210 return; 211 } 212 } 213 214 Element styleToAdd = dom.createElement(TAG_HEAD_LINK); 216 styleToAdd.setAttribute(ATT_HREF, cssUrl); 217 styleToAdd.setAttribute(ATT_REL, "stylesheet"); styleToAdd.setAttribute(ATT_TYPE, "text/css"); if (styleList != null && styleList.getLength() >= 1) 220 styleList.item(0).getParentNode().insertBefore(styleToAdd, 221 styleList.item(0)); 222 else 223 head.appendChild(styleToAdd); 224 225 } 226 227 233 public static Element getBodyElement(Document dom) { 234 NodeList bodyList = dom.getElementsByTagName(TAG_BODY); 236 Element body = (Element) bodyList.item(0); 237 return body; 238 } 239 240 241 242 public static Element createElement(Document dom, String elementName, 243 Properties attributes) { 244 245 Element element = dom.createElementNS("", elementName); if (attributes != null) { 249 Enumeration e = attributes.keys(); 250 while (e.hasMoreElements()) { 251 String key = (String ) e.nextElement(); 252 element.setAttribute(key, attributes.getProperty(key)); 253 } 254 } 255 return element; 256 } 257 258 public static Element createAndAppendChild(Element parentElement, 259 String elementName, Properties attributes) { 260 261 Element element = createElement(parentElement.getOwnerDocument(), 262 elementName, attributes); 263 parentElement.appendChild(element); 264 return element; 265 } 266 267 268 269 276 public static Element[] getElementsByTagName(Element parent, String tagName) { 277 NodeList allChildElements = parent.getElementsByTagName(tagName); 278 Vector vector = new Vector (); 279 for (int i = 0; i < allChildElements.getLength(); i++) { 280 Element aElement = (Element) allChildElements.item(i); 282 if (aElement.getParentNode().equals(parent)) 283 vector.add(aElement); 285 } 286 Element[] filteredElements = new Element[vector.size()]; 287 vector.copyInto(filteredElements); 288 return filteredElements; 289 } 290 291 297 public static Element[] getElementsByTagName(Document dom, String tagName) { 298 NodeList allChildElements = dom.getElementsByTagName(tagName); 299 Vector vector = new Vector (); 300 for (int i = 0; i < allChildElements.getLength(); i++) { 301 Element aElement = (Element) allChildElements.item(i); 303 if (aElement.getParentNode().equals(dom.getDocumentElement())) 304 vector.add(aElement); 307 } 308 Element[] filteredElements = new Element[vector.size()]; 309 vector.copyInto(filteredElements); 310 return filteredElements; 311 } 312 313 314 324 public static Element getElementById(Document dom, String id, 325 String localElementName) { 326 327 NodeList children = dom.getElementsByTagNameNS("*", localElementName); for (int i = 0; i < children.getLength(); i++) { 329 Element element = (Element) children.item(i); 330 if (element.getAttribute("id").equals(id)) return element; 332 } 333 return null; 335 336 } 337 338 public static Element getElementById(Document dom, String id) { 339 return getElementById(dom, id, "*"); } 341 342 public static void updateResourceAttributes(Element element, 343 AbstractIntroPage page) { 344 updateResourceAttributes(element, page.getBase(), page.getBundle()); 345 } 346 347 348 public static void updateResourceAttributes(Element element, 349 IntroExtensionContent extensionContent) { 350 updateResourceAttributes(element, extensionContent.getBase(), 351 extensionContent.getBundle()); 352 } 353 354 361 private static void updateResourceAttributes(Element element, String base, 362 Bundle bundle) { 363 NodeList children = element.getElementsByTagName("*"); for (int i = 0; i < children.getLength(); i++) { 366 Element child = (Element) children.item(i); 367 doUpdateResourceAttributes(child, base, bundle); 368 } 369 } 370 371 private static void doUpdateResourceAttributes(Element element, 372 String base, Bundle bundle) { 373 qualifyAttribute(element, ATT_SRC, base, bundle); 374 qualifyAttribute(element, ATT_HREF, base, bundle); 375 qualifyAttribute(element, ATT_CITE, base, bundle); 376 qualifyAttribute(element, ATT_LONGDESC, base, bundle); 377 qualifyAttribute(element, ATT_CODEBASE, base, bundle); 378 qualifyAttribute(element, ATT_DATA, base, bundle); 379 qualifyValueAttribute(element, base, bundle); 380 } 381 382 private static void qualifyAttribute(Element element, String attributeName, 383 String base, Bundle bundle) { 384 if (element.hasAttribute(attributeName)) { 385 String attributeValue = element.getAttribute(attributeName); 386 if (new IntroURLParser(attributeValue).hasProtocol()) 387 return; 388 389 String attributePath = BundleUtil.getResolvedResourceLocation(base, 391 attributeValue, bundle); 392 element.setAttribute(attributeName, attributePath); 393 } 394 } 395 396 private static void qualifyValueAttribute(Element element, String base, 397 Bundle bundle) { 398 if (element.hasAttribute(ATT_VALUE) 399 && element.hasAttribute(ATT_VALUE_TYPE) 400 && element.getAttribute(ATT_VALUE_TYPE).equals("ref") && element.getLocalName().equals(TAG_PARAM)) { 402 String value = element.getAttribute(ATT_VALUE); 403 if (new IntroURLParser(value).hasProtocol()) 404 return; 405 String attributePath = BundleUtil.getResolvedResourceLocation(base, 407 value, bundle); 408 element.setAttribute(ATT_VALUE, attributePath); 409 } 410 } 411 412 413 417 public static Node [] getArray(NodeList nodeList) { 418 Node [] nodes = new Node [nodeList.getLength()]; 419 for (int i = 0; i < nodeList.getLength(); i++) 420 nodes[i] = nodeList.item(i); 421 return nodes; 422 } 423 424 425 429 public static void removeAllElements(Document dom, String elementLocalName) { 430 NodeList elements = dom.getElementsByTagNameNS("*", elementLocalName); 433 Node [] elementsArray = ModelUtil.getArray(elements); 435 for (int i = 0; i < elementsArray.length; i++) { 436 Node element = elementsArray[i]; 437 element.getParentNode().removeChild(element); 438 } 439 440 } 441 442 443 444 } 445 | Popular Tags |