1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.Dictionary ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.parsers.FactoryConfigurationError ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 26 import org.eclipse.osgi.service.resolver.BundleDescription; 27 import org.eclipse.osgi.util.ManifestElement; 28 import org.eclipse.pde.core.plugin.IPlugin; 29 import org.eclipse.pde.core.plugin.IPluginBase; 30 import org.eclipse.pde.core.plugin.IPluginLibrary; 31 import org.eclipse.pde.core.plugin.IPluginModelBase; 32 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase; 33 import org.osgi.framework.BundleException; 34 import org.osgi.framework.Constants; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 40 public class PDEAuxiliaryState { 41 42 private static String CACHE_EXTENSION = ".pluginInfo"; 44 private static String ATTR_BUNDLE_ID = "bundleID"; private static String ATTR_BUNDLE_STRUCTURE = "isBundle"; private static String ATTR_CLASS = "class"; private static String ATTR_EXPORTED = "exported"; private static String ATTR_EXTENSIBLE_API = "hasExtensibleAPI"; private static String ATTR_LOCALIZATION = "localization"; private static String ATTR_NAME = "name"; private static String ATTR_PATCH = "patch"; private static String ATTR_PROJECT = "project"; private static String ATTR_PROVIDER = "provider"; 55 private static String ELEMENT_BUNDLE = "bundle"; private static String ELEMENT_LIB = "library"; private static String ELEMENT_ROOT = "map"; 59 protected Map fPluginInfos = new HashMap (); 60 61 protected PDEAuxiliaryState() { 62 } 63 64 protected PDEAuxiliaryState(PDEAuxiliaryState state) { 65 this.fPluginInfos = new HashMap (state.fPluginInfos); 66 } 67 68 class PluginInfo { 69 String name; 70 String providerName; 71 String className; 72 boolean hasExtensibleAPI; 73 boolean isPatchFragment; 74 boolean hasBundleStructure; 75 String [] libraries; 76 String project; 77 String localization; 78 } 79 80 private void createPluginInfo(Element element) { 81 PluginInfo info = new PluginInfo(); 82 if (element.hasAttribute(ATTR_NAME)) 83 info.name = element.getAttribute(ATTR_NAME); 84 if (element.hasAttribute(ATTR_PROVIDER)) 85 info.providerName = element.getAttribute(ATTR_PROVIDER); 86 if (element.hasAttribute(ATTR_CLASS)) 87 info.className = element.getAttribute(ATTR_CLASS); 88 info.hasExtensibleAPI = "true".equals(element.getAttribute(ATTR_EXTENSIBLE_API)); info.isPatchFragment = "true".equals(element.getAttribute(ATTR_PATCH)); info.hasBundleStructure = !"false".equals(element.getAttribute(ATTR_BUNDLE_STRUCTURE)); if (element.hasAttribute(ATTR_PROJECT)) 92 info.project = element.getAttribute(ATTR_PROJECT); 93 if (element.hasAttribute(ATTR_LOCALIZATION)) 94 info.localization = element.getAttribute(ATTR_LOCALIZATION); 95 96 NodeList libs = element.getChildNodes(); 97 ArrayList list = new ArrayList (libs.getLength()); 98 for (int i = 0; i < libs.getLength(); i++) { 99 if (libs.item(i).getNodeType() == Node.ELEMENT_NODE) { 100 Element lib = (Element)libs.item(i); 101 list.add(lib.getAttribute(ATTR_NAME)); 102 } 103 } 104 info.libraries = (String [])list.toArray(new String [list.size()]); 105 fPluginInfos.put(element.getAttribute(ATTR_BUNDLE_ID), info); 106 } 107 108 public String getClassName(long bundleID) { 109 PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID)); 110 return info == null ? null : info.className; 111 } 112 113 public boolean hasExtensibleAPI(long bundleID) { 114 PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID)); 115 return info == null ? false : info.hasExtensibleAPI; 116 } 117 118 public boolean isPatchFragment(long bundleID) { 119 PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID)); 120 return info == null ? false : info.isPatchFragment; 121 } 122 public boolean hasBundleStructure(long bundleID) { 123 PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID)); 124 return info == null ? false : info.hasBundleStructure; 125 } 126 127 public String getPluginName(long bundleID) { 128 PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID)); 129 return info == null ? null : info.name; 130 } 131 132 public String getProviderName(long bundleID) { 133 PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID)); 134 return info == null ? null : info.providerName; 135 } 136 137 public String [] getLibraryNames(long bundleID) { 138 PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID)); 139 return info == null ? new String [0] : info.libraries; 140 } 141 142 public String getBundleLocalization(long bundleID) { 143 PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID)); 144 return info == null ? null : info.localization; 145 } 146 147 public String getProject(long bundleID) { 148 PluginInfo info = (PluginInfo)fPluginInfos.get(Long.toString(bundleID)); 149 return info == null ? null : info.project; 150 } 151 152 protected void savePluginInfo(File dir) { 153 try { 154 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 155 Document doc = factory.newDocumentBuilder().newDocument(); 156 Element root = doc.createElement(ELEMENT_ROOT); 157 158 Iterator iter = fPluginInfos.keySet().iterator(); 159 while (iter.hasNext()) { 160 String key = iter.next().toString(); 161 Element element = doc.createElement(ELEMENT_BUNDLE); 162 element.setAttribute(ATTR_BUNDLE_ID, key); 163 PluginInfo info = (PluginInfo)fPluginInfos.get(key); 164 if (info.className != null) 165 element.setAttribute(ATTR_CLASS, info.className); 166 if (info.providerName != null) 167 element.setAttribute(ATTR_PROVIDER, info.providerName); 168 if (info.name != null) 169 element.setAttribute(ATTR_NAME, info.name); 170 if (info.hasExtensibleAPI) 171 element.setAttribute(ATTR_EXTENSIBLE_API, "true"); if (info.isPatchFragment) 173 element.setAttribute(ATTR_PATCH, "true"); if (!info.hasBundleStructure) 175 element.setAttribute(ATTR_BUNDLE_STRUCTURE, "false"); if (info.localization != null) 177 element.setAttribute(ATTR_LOCALIZATION, info.localization); 178 if (info.libraries != null) { 179 for (int i = 0; i < info.libraries.length; i++) { 180 Element lib = doc.createElement(ELEMENT_LIB); 181 lib.setAttribute(ATTR_NAME, info.libraries[i]); 182 element.appendChild(lib); 183 } 184 } 185 root.appendChild(element); 186 } 187 doc.appendChild(root); 188 XMLPrintHandler.writeFile(doc, new File (dir, CACHE_EXTENSION)); 189 } catch (Exception e) { 190 PDECore.log(e); 191 } 192 } 193 194 protected boolean readPluginInfoCache(File dir) { 195 File file = new File (dir, CACHE_EXTENSION); 196 if (file.exists() && file.isFile()) { 197 try { 198 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 199 Document doc = factory.newDocumentBuilder().parse(file); 200 Element root = doc.getDocumentElement(); 201 if (root != null) { 202 NodeList list = root.getChildNodes(); 203 for (int i = 0; i < list.getLength(); i++) { 204 if (list.item(i).getNodeType() == Node.ELEMENT_NODE) 205 createPluginInfo((Element)list.item(i)); 206 } 207 } 208 return true; 209 } catch (org.xml.sax.SAXException e) { 210 PDECore.log(e); 211 } catch (IOException e) { 212 PDECore.log(e); 213 } catch (ParserConfigurationException e) { 214 PDECore.log(e); 215 } 216 } 217 return false; 218 } 219 220 public static void writePluginInfo(IPluginModelBase[] models, File destination) { 221 try { 222 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 223 Document doc = builder.newDocument(); 224 225 Element root = doc.createElement(ELEMENT_ROOT); 226 doc.appendChild(root); 227 for (int i = 0; i < models.length; i++) { 228 IPluginBase plugin = models[i].getPluginBase(); 229 BundleDescription desc = models[i].getBundleDescription(); 230 Element element = doc.createElement(ELEMENT_BUNDLE); 231 element.setAttribute(ATTR_BUNDLE_ID, Long.toString(desc.getBundleId())); 232 element.setAttribute(ATTR_PROJECT, models[i].getUnderlyingResource().getProject().getName()); 233 if (plugin instanceof IPlugin && ((IPlugin)plugin).getClassName() != null) 234 element.setAttribute(ATTR_CLASS, ((IPlugin)plugin).getClassName()); 235 if (plugin.getProviderName() != null) 236 element.setAttribute(ATTR_PROVIDER, plugin.getProviderName()); 237 if (plugin.getName() != null) 238 element.setAttribute(ATTR_NAME, plugin.getName()); 239 if (ClasspathUtilCore.hasExtensibleAPI(models[i])) 240 element.setAttribute(ATTR_EXTENSIBLE_API, "true"); else if (ClasspathUtilCore.isPatchFragment(models[i])) 242 element.setAttribute(ATTR_PATCH, "true"); if (!(models[i] instanceof IBundlePluginModelBase)) 244 element.setAttribute(ATTR_BUNDLE_STRUCTURE, "false"); if (models[i] instanceof IBundlePluginModelBase) { 246 String localization = ((IBundlePluginModelBase)models[i]).getBundleLocalization(); 247 if (localization != null) 248 element.setAttribute(ATTR_LOCALIZATION, localization); 249 } 250 IPluginLibrary[] libraries = plugin.getLibraries(); 251 for (int j = 0; j < libraries.length; j++) { 252 Element lib = doc.createElement(ELEMENT_LIB); 253 lib.setAttribute(ATTR_NAME, libraries[j].getName()); 254 if (!libraries[j].isExported()) 255 lib.setAttribute(ATTR_EXPORTED, "false"); element.appendChild(lib); 257 } 258 root.appendChild(element); 259 } 260 XMLPrintHandler.writeFile(doc, new File (destination, CACHE_EXTENSION)); 261 } catch (ParserConfigurationException e) { 262 } catch (FactoryConfigurationError e) { 263 } catch (IOException e) { 264 } 265 } 266 267 protected void addAuxiliaryData(BundleDescription desc, Dictionary manifest, boolean hasBundleStructure) { 268 PluginInfo info = new PluginInfo(); 269 info.name = (String )manifest.get(Constants.BUNDLE_NAME); 270 info.providerName = (String )manifest.get(Constants.BUNDLE_VENDOR); 271 272 String className = (String )manifest.get(ICoreConstants.PLUGIN_CLASS); 273 info.className = className != null ? className : (String )manifest.get(Constants.BUNDLE_ACTIVATOR); 274 info.libraries = getClasspath(manifest); 275 info.hasExtensibleAPI = "true".equals(manifest.get(ICoreConstants.EXTENSIBLE_API)); info.isPatchFragment = "true".equals(manifest.get(ICoreConstants.PATCH_FRAGMENT)); info.localization = (String )manifest.get(Constants.BUNDLE_LOCALIZATION); 278 info.hasBundleStructure = hasBundleStructure; 279 fPluginInfos.put(Long.toString(desc.getBundleId()), info); 280 } 281 282 protected String [] getClasspath(Dictionary manifest) { 283 String fullClasspath = (String ) manifest.get(Constants.BUNDLE_CLASSPATH); 284 String [] result = new String [0]; 285 try { 286 if (fullClasspath != null) { 287 ManifestElement[] classpathEntries = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, fullClasspath); 288 result = new String [classpathEntries.length]; 289 for (int i = 0; i < classpathEntries.length; i++) { 290 result[i] = classpathEntries[i].getValue(); 291 } 292 } 293 } catch (BundleException e) { 294 } 295 return result; 296 } 297 298 protected void clear() { 299 fPluginInfos.clear(); 300 } 301 302 303 } 304 | Popular Tags |