1 11 package org.eclipse.ui.internal.intro.universal; 12 13 import java.io.IOException ; 14 import java.io.PrintWriter ; 15 import java.io.StringReader ; 16 import java.util.Enumeration ; 17 import java.util.Hashtable ; 18 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.ParserConfigurationException ; 22 23 import org.eclipse.core.runtime.IConfigurationElement; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.ui.internal.intro.impl.model.loader.IntroContentParser; 26 import org.eclipse.ui.internal.intro.universal.util.BundleUtil; 27 import org.eclipse.ui.internal.intro.universal.util.Log; 28 import org.osgi.framework.Bundle; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 import org.xml.sax.InputSource ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.SAXParseException ; 36 37 38 public class IntroData { 39 private String productId; 40 private Hashtable pages=new Hashtable (); 41 private boolean active; 42 43 public IntroData(String productId, String fileNameOrData, boolean active) { 44 this.productId = productId; 45 this.active = active; 46 if (fileNameOrData!=null) 47 initialize(fileNameOrData); 48 } 49 50 public String getProductId() { 51 return productId; 52 } 53 54 public PageData getPage(String pageId) { 55 return (PageData)pages.get(pageId); 56 } 57 58 public boolean isActive() { 59 return active; 60 } 61 62 private void initialize(String fileNameOrData) { 63 Document doc = parse(fileNameOrData); 64 if (doc == null) 65 return; 66 Element root = doc.getDocumentElement(); 67 NodeList pages = root.getChildNodes(); 68 for (int i = 0; i < pages.getLength(); i++) { 69 Node node = pages.item(i); 70 if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("page")) { loadPage((Element) node); 72 } 73 } 74 } 75 76 private void loadPage(Element page) { 77 PageData pd = new PageData(page); 78 pages.put(pd.getId(), pd); 79 } 80 81 public void addImplicitContent() { 82 IConfigurationElement [] elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.intro.configExtension"); for (int i=0; i<elements.length; i++) { 84 IConfigurationElement element = elements[i]; 85 if (element.getName().equals("configExtension")) { String cid = element.getAttribute("configId"); if (cid!=null && cid.equals("org.eclipse.ui.intro.universalConfig")) { addCandidate(element); 89 } 90 } 91 } 92 } 93 94 private void addCandidate(IConfigurationElement element) { 95 String fileName = element.getAttribute("content"); if (fileName==null) 97 return; 98 String bundleId = element.getDeclaringExtension().getNamespaceIdentifier(); 99 Bundle bundle = Platform.getBundle(bundleId); 100 if (bundle==null) 101 return; 102 String content = BundleUtil.getResolvedResourceLocation("", fileName, bundle); 104 IntroContentParser parser = new IntroContentParser(content); 105 Document dom = parser.getDocument(); 106 if (dom==null) 108 return; 109 Element root = dom.getDocumentElement(); 110 Element extension = null; 111 NodeList children = root.getChildNodes(); 112 for (int i=0; i<children.getLength(); i++) { 113 Node child = children.item(i); 114 if (child.getNodeType()==Node.ELEMENT_NODE) { 115 Element el = (Element)child; 116 if (el.getNodeName().equalsIgnoreCase("extensionContent")) { extension = el; 118 break; 119 } 120 } 121 } 122 if (extension==null) 123 return; 124 String id = extension.getAttribute("id"); String name = extension.getAttribute("name"); String path = extension.getAttribute("path"); if (id==null || path==null) 128 return; 129 int at = path.lastIndexOf("/@"); if (at == -1) 131 return; 132 if (path.charAt(path.length()-1)!='@') 133 return; 134 String pageId = path.substring(0, at); 135 PageData pd = (PageData)pages.get(pageId); 136 if (pd==null) { 137 pd = new PageData(pageId); 138 pages.put(pageId, pd); 139 } 140 pd.addImplicitExtension(id, name); 141 } 142 143 private Document parse(String fileNameOrData) { 144 Document document = null; 145 try { 146 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 147 docFactory.setValidating(false); 148 docFactory.setNamespaceAware(true); 150 docFactory.setExpandEntityReferences(false); 151 DocumentBuilder parser = docFactory.newDocumentBuilder(); 152 153 if (fileNameOrData.charAt(0)=='<') { 154 StringReader reader = new StringReader (fileNameOrData); 156 document = parser.parse(new InputSource (reader)); 157 } 158 else 159 document = parser.parse(fileNameOrData); 160 return document; 161 162 } catch (SAXParseException spe) { 163 StringBuffer buffer = new StringBuffer ("IntroData error in line "); buffer.append(spe.getLineNumber()); 165 buffer.append(", uri "); buffer.append(spe.getSystemId()); 167 buffer.append("\n"); buffer.append(spe.getMessage()); 169 170 Exception x = spe; 172 if (spe.getException() != null) 173 x = spe.getException(); 174 Log.error(buffer.toString(), x); 175 176 } catch (SAXException sxe) { 177 Exception x = sxe; 178 if (sxe.getException() != null) 179 x = sxe.getException(); 180 Log.error(x.getMessage(), x); 181 182 } catch (ParserConfigurationException pce) { 183 Log.error(pce.getMessage(), pce); 185 186 } catch (IOException ioe) { 187 Log.error(ioe.getMessage(), ioe); 188 } 189 return null; 190 } 191 192 public void write(PrintWriter writer) { 193 writer.println("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); writer.println("<extensions>"); for (Enumeration keys = pages.keys(); keys.hasMoreElements();) { 196 String id = (String )keys.nextElement(); 197 PageData pd = (PageData)pages.get(id); 198 pd.write(writer, " "); } 200 writer.println("</extensions>"); } 202 } 203 | Popular Tags |