1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.BufferedInputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import java.util.zip.ZipEntry ; 22 import java.util.zip.ZipFile ; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import javax.xml.parsers.FactoryConfigurationError ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.SAXParserFactory ; 30 31 import org.eclipse.core.runtime.Platform; 32 import org.eclipse.osgi.service.resolver.BundleDescription; 33 import org.eclipse.osgi.service.resolver.State; 34 import org.eclipse.pde.core.plugin.IPluginAttribute; 35 import org.eclipse.pde.core.plugin.IPluginBase; 36 import org.eclipse.pde.core.plugin.IPluginElement; 37 import org.eclipse.pde.core.plugin.IPluginExtension; 38 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 39 import org.eclipse.pde.core.plugin.IPluginModelBase; 40 import org.eclipse.pde.core.plugin.IPluginObject; 41 import org.eclipse.pde.internal.core.plugin.PluginExtension; 42 import org.eclipse.pde.internal.core.plugin.PluginExtensionPoint; 43 import org.eclipse.pde.internal.core.util.PDEXMLHelper; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.Element ; 46 import org.w3c.dom.Node ; 47 import org.w3c.dom.NodeList ; 48 import org.xml.sax.SAXException ; 49 50 public class PDEExtensionRegistry { 51 52 protected static boolean DEBUG = false; 53 54 static { 55 DEBUG = PDECore.getDefault().isDebugging() 56 && "true".equals(Platform.getDebugOption("org.eclipse.pde.core/cache")); } 58 59 private static String CACHE_EXTENSION = ".extensions"; 61 private static String ROOT_EXTENSIONS = "extensions"; private static String ELEMENT_BUNDLE = "bundle"; private static String ATTR_BUNDLE_ID = "bundleID"; private static String ELEMENT_EXTENSION = "extension"; private static String ELEMENT_EXTENSION_POINT = "extension-point"; private static String ATTR_SCHEMA = "schema"; 68 private static SAXParser parser; 69 70 private Map fExtensions = new HashMap (); 71 72 protected PDEExtensionRegistry() { 73 } 74 75 protected PDEExtensionRegistry(PDEExtensionRegistry registry) { 76 this.fExtensions = new HashMap (registry.fExtensions); 77 } 78 79 protected void saveExtensions(State state, File dir) { 80 try { 81 File file = new File (dir, CACHE_EXTENSION); XMLPrintHandler.writeFile(createExtensionDocument(state), file); 83 } catch (IOException e) { 84 } 85 } 86 87 public Node [] getExtensions(long bundleID) { 88 return getChildren(bundleID, ELEMENT_EXTENSION); } 90 91 public Node [] getExtensionPoints(long bundleID) { 92 return getChildren(bundleID, ELEMENT_EXTENSION_POINT); } 94 95 private Node [] getChildren(long bundleID, String tagName) { 96 ArrayList list = new ArrayList (); 97 Element bundle = (Element)fExtensions.get(Long.toString(bundleID)); 98 if (bundle != null) { 99 NodeList children = bundle.getChildNodes(); 100 for (int i = 0; i < children.getLength(); i++) { 101 if (tagName.equals(children.item(i).getNodeName())) { 102 list.add(children.item(i)); 103 } 104 } 105 } 106 return (Node [])list.toArray(new Node [list.size()]); 107 } 108 109 public Node [] getAllExtensions(long bundleID) { 110 ArrayList list = new ArrayList (); 111 Element bundle = (Element)fExtensions.get(Long.toString(bundleID)); 112 if (bundle != null) { 113 NodeList children = bundle.getChildNodes(); 114 for (int i = 0; i < children.getLength(); i++) { 115 String name = children.item(i).getNodeName(); 116 if (ELEMENT_EXTENSION.equals(name) || ELEMENT_EXTENSION_POINT.equals(name)) { list.add(children.item(i)); 118 } 119 } 120 } 121 return (Node [])list.toArray(new Node [list.size()]); 122 } 123 124 protected Document createExtensionDocument(State state){ 125 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 126 Document doc = null; 127 try { 128 doc = factory.newDocumentBuilder().newDocument(); 129 } catch (ParserConfigurationException e) { 130 return null; 131 } 132 Element root = doc.createElement(ROOT_EXTENSIONS); 134 BundleDescription[] bundles = state.getBundles(); 135 for (int i = 0; i < bundles.length; i++) { 136 BundleDescription desc = bundles[i]; 137 Element element = doc.createElement(ELEMENT_BUNDLE); element.setAttribute(ATTR_BUNDLE_ID, Long.toString(desc.getBundleId())); parseExtensions(desc, element); 140 if (element.hasChildNodes()) { 141 root.appendChild(element); 142 fExtensions.put(Long.toString(desc.getBundleId()), element); 143 } 144 } 145 doc.appendChild(root); 146 return doc; 147 } 148 149 protected boolean readExtensionsCache(File dir) { 150 long start = System.currentTimeMillis(); 151 File file = new File (dir, CACHE_EXTENSION); if (file.exists() && file.isFile()) { 153 try { 154 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 155 Document doc = factory.newDocumentBuilder().parse(file); 156 Element root = doc.getDocumentElement(); 157 if (root != null) { 158 NodeList bundles = root.getChildNodes(); 159 for (int i = 0; i < bundles.getLength(); i++) { 160 if (bundles.item(i).getNodeType() == Node.ELEMENT_NODE) { 161 Element bundle = (Element)bundles.item(i); 162 String id = bundle.getAttribute(ATTR_BUNDLE_ID); fExtensions.put(id, bundle.getChildNodes()); 164 } 165 } 166 } 167 if (DEBUG) 168 System.out.println("Time to read extensions: " + (System.currentTimeMillis() - start) + " ms"); return true; 170 } catch (org.xml.sax.SAXException e) { 171 PDECore.log(e); 172 } catch (IOException e) { 173 PDECore.log(e); 174 } catch (ParserConfigurationException e) { 175 PDECore.log(e); 176 } 177 } 178 return false; 179 } 180 181 public static void writeExtensions(IPluginModelBase[] models, File destination) { 182 try { 183 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 184 Document doc = builder.newDocument(); 185 186 Element root = doc.createElement(ROOT_EXTENSIONS); doc.appendChild(root); 188 189 for (int i = 0; i < models.length; i++) { 190 IPluginBase plugin = models[i].getPluginBase(); 191 IPluginExtension[] extensions = plugin.getExtensions(); 192 IPluginExtensionPoint[] extPoints = plugin.getExtensionPoints(); 193 if (extensions.length == 0 && extPoints.length == 0) 194 continue; 195 Element element = doc.createElement(ELEMENT_BUNDLE); element.setAttribute(ATTR_BUNDLE_ID, Long.toString(models[i].getBundleDescription().getBundleId())); String schema = plugin.getSchemaVersion(); 198 if (schema != null) 199 element.setAttribute(ATTR_SCHEMA, schema); 200 for (int j = 0; j < extensions.length; j++) { 201 element.appendChild(writeExtension(doc, extensions[j])); 202 } 203 for (int j = 0; j < extPoints.length; j++) { 204 element.appendChild(writeExtensionPoint(doc, extPoints[j])); 205 } 206 root.appendChild(element); 207 } 208 XMLPrintHandler.writeFile(doc, new File (destination, CACHE_EXTENSION)); } catch (ParserConfigurationException e) { 210 } catch (FactoryConfigurationError e) { 211 } catch (IOException e) { 212 } 213 } 214 215 public static Element writeExtensionPoint(Document doc, IPluginExtensionPoint extPoint) { 216 Element child = doc.createElement("extension-point"); if (extPoint.getId() != null) 218 child.setAttribute("id", PDEXMLHelper.getWritableString(extPoint.getId())); if (extPoint.getName() != null) 220 child.setAttribute("name", PDEXMLHelper.getWritableString(extPoint.getName())); if (extPoint.getSchema() != null) 222 child.setAttribute("schema", PDEXMLHelper.getWritableString(extPoint.getSchema())); if (extPoint instanceof PluginExtensionPoint) 224 child.setAttribute("line", Integer.toString(((PluginExtensionPoint)extPoint).getStartLine())); return child; 226 } 227 228 public static Element writeExtension(Document doc, IPluginExtension extension) { 229 Element child = doc.createElement("extension"); if (extension.getPoint() != null) 231 child.setAttribute("point", PDEXMLHelper.getWritableString(extension.getPoint())); if (extension.getName() != null) 233 child.setAttribute("name", PDEXMLHelper.getWritableString(extension.getName())); if (extension.getId() != null) 235 child.setAttribute("id", PDEXMLHelper.getWritableString(extension.getId())); if (extension instanceof PluginExtension) 237 child.setAttribute("line", Integer.toString(((PluginExtension)extension).getStartLine())); IPluginObject[] children = extension.getChildren(); 239 for (int i = 0; i < children.length; i++) { 240 child.appendChild(writeElement(doc, (IPluginElement)children[i])); 241 } 242 return child; 243 } 244 245 public static Element writeElement(Document doc, IPluginElement element) { 246 Element child = doc.createElement(element.getName()); 247 IPluginAttribute[] attrs = element.getAttributes(); 248 for (int i = 0; i < attrs.length; i++) { 249 child.setAttribute(attrs[i].getName(), PDEXMLHelper.getWritableString(attrs[i].getValue())); 250 } 251 IPluginObject[] elements = element.getChildren(); 252 for (int i = 0; i < elements.length; i++) { 253 child.appendChild(writeElement(doc, (IPluginElement)elements[i])); 254 } 255 return child; 256 } 257 258 protected void clear() { 259 fExtensions.clear(); 260 } 261 262 public String getSchemaVersion(long bundleID) { 263 Element bundle = (Element)fExtensions.get(Long.toString(bundleID)); 264 return bundle == null ? null : bundle.getAttribute(ATTR_SCHEMA); 265 } 266 267 public static synchronized void parseExtensions(BundleDescription desc, Element parent) { 268 ZipFile jarFile = null; 269 InputStream stream = null; 270 try { 271 String filename = desc.getHost() == null ? "plugin.xml" : "fragment.xml"; String path = desc.getLocation(); 273 274 File file = new File (path); 275 if (file.isFile()) { 276 jarFile = new ZipFile (file, ZipFile.OPEN_READ); 277 ZipEntry manifestEntry = jarFile.getEntry(filename); 278 if (manifestEntry != null) 279 stream = new BufferedInputStream (jarFile.getInputStream(manifestEntry)); 280 } else if (file.isDirectory()) { 281 File manifest = new File (file, filename); 282 if (manifest.exists() && manifest.isFile()) { 283 stream = new BufferedInputStream (new FileInputStream (manifest)); 284 } 285 } 286 if (stream != null) { 287 if (parser == null) 288 parser = SAXParserFactory.newInstance().newSAXParser(); 289 parser.parse(stream, new ExtensionsHandler(parent)); 290 } 291 } catch (IOException e) { 292 } catch (ParserConfigurationException e) { 293 } catch (SAXException e) { 294 } catch (FactoryConfigurationError e) { 295 } finally { 296 try { 297 if (stream != null) 298 stream.close(); 299 } catch (IOException e1) { 300 } 301 try { 302 if (jarFile != null) 303 jarFile.close(); 304 } catch (IOException e2) { 305 } 306 } 307 } 308 309 } 310 | Popular Tags |