KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > PDEExtensionRegistry


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStream JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.zip.ZipEntry JavaDoc;
22 import java.util.zip.ZipFile JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import javax.xml.parsers.SAXParser JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
45 import org.w3c.dom.Element JavaDoc;
46 import org.w3c.dom.Node JavaDoc;
47 import org.w3c.dom.NodeList JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
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")); //$NON-NLS-1$ //$NON-NLS-2$
57
}
58     
59     private static String JavaDoc CACHE_EXTENSION = ".extensions"; //$NON-NLS-1$
60

61     private static String JavaDoc ROOT_EXTENSIONS = "extensions"; //$NON-NLS-1$
62
private static String JavaDoc ELEMENT_BUNDLE = "bundle"; //$NON-NLS-1$
63
private static String JavaDoc ATTR_BUNDLE_ID = "bundleID"; //$NON-NLS-1$
64
private static String JavaDoc ELEMENT_EXTENSION = "extension"; //$NON-NLS-1$
65
private static String JavaDoc ELEMENT_EXTENSION_POINT = "extension-point"; //$NON-NLS-1$
66
private static String JavaDoc ATTR_SCHEMA = "schema"; //$NON-NLS-1$
67

68     private static SAXParser JavaDoc parser;
69     
70     private Map JavaDoc fExtensions = new HashMap JavaDoc();
71     
72     protected PDEExtensionRegistry() {
73     }
74     
75     protected PDEExtensionRegistry(PDEExtensionRegistry registry) {
76         this.fExtensions = new HashMap JavaDoc(registry.fExtensions);
77     }
78
79     protected void saveExtensions(State state, File JavaDoc dir) {
80         try {
81             File JavaDoc file = new File JavaDoc(dir, CACHE_EXTENSION); //$NON-NLS-1$
82
XMLPrintHandler.writeFile(createExtensionDocument(state), file);
83         } catch (IOException JavaDoc e) {
84         }
85     }
86     
87     public Node JavaDoc[] getExtensions(long bundleID) {
88         return getChildren(bundleID, ELEMENT_EXTENSION); //$NON-NLS-1$
89
}
90     
91     public Node JavaDoc[] getExtensionPoints(long bundleID) {
92         return getChildren(bundleID, ELEMENT_EXTENSION_POINT); //$NON-NLS-1$
93
}
94     
95     private Node JavaDoc[] getChildren(long bundleID, String JavaDoc tagName) {
96         ArrayList JavaDoc list = new ArrayList JavaDoc();
97         Element bundle = (Element)fExtensions.get(Long.toString(bundleID));
98         if (bundle != null) {
99             NodeList JavaDoc 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 JavaDoc[])list.toArray(new Node JavaDoc[list.size()]);
107     }
108
109     public Node JavaDoc[] getAllExtensions(long bundleID) {
110         ArrayList JavaDoc list = new ArrayList JavaDoc();
111         Element bundle = (Element)fExtensions.get(Long.toString(bundleID));
112         if (bundle != null) {
113             NodeList JavaDoc children = bundle.getChildNodes();
114             for (int i = 0; i < children.getLength(); i++) {
115                 String JavaDoc name = children.item(i).getNodeName();
116                 if (ELEMENT_EXTENSION.equals(name) || ELEMENT_EXTENSION_POINT.equals(name)) { //$NON-NLS-1$ //$NON-NLS-2$
117
list.add(children.item(i));
118                 }
119             }
120         }
121         return (Node JavaDoc[])list.toArray(new Node JavaDoc[list.size()]);
122     }
123     
124     protected Document JavaDoc createExtensionDocument(State state){
125         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
126         Document JavaDoc doc = null;
127         try {
128             doc = factory.newDocumentBuilder().newDocument();
129         } catch (ParserConfigurationException JavaDoc e) {
130             return null;
131         }
132         Element root = doc.createElement(ROOT_EXTENSIONS); //$NON-NLS-1$
133

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); //$NON-NLS-1$
138
element.setAttribute(ATTR_BUNDLE_ID, Long.toString(desc.getBundleId())); //$NON-NLS-1$
139
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 JavaDoc dir) {
150         long start = System.currentTimeMillis();
151         File JavaDoc file = new File JavaDoc(dir, CACHE_EXTENSION); //$NON-NLS-1$
152
if (file.exists() && file.isFile()) {
153             try {
154                 DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
155                 Document JavaDoc doc = factory.newDocumentBuilder().parse(file);
156                 Element root = doc.getDocumentElement();
157                 if (root != null) {
158                     NodeList JavaDoc 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 JavaDoc id = bundle.getAttribute(ATTR_BUNDLE_ID); //$NON-NLS-1$
163
fExtensions.put(id, bundle.getChildNodes());
164                         }
165                     }
166                 }
167                 if (DEBUG)
168                     System.out.println("Time to read extensions: " + (System.currentTimeMillis() - start) + " ms"); //$NON-NLS-1$ //$NON-NLS-2$
169
return true;
170             } catch (org.xml.sax.SAXException JavaDoc e) {
171                 PDECore.log(e);
172             } catch (IOException JavaDoc e) {
173                 PDECore.log(e);
174             } catch (ParserConfigurationException JavaDoc e) {
175                 PDECore.log(e);
176             }
177         }
178         return false;
179     }
180     
181     public static void writeExtensions(IPluginModelBase[] models, File JavaDoc destination) {
182         try {
183             DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
184             Document JavaDoc doc = builder.newDocument();
185         
186             Element root = doc.createElement(ROOT_EXTENSIONS); //$NON-NLS-1$
187
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); //$NON-NLS-1$
196
element.setAttribute(ATTR_BUNDLE_ID, Long.toString(models[i].getBundleDescription().getBundleId())); //$NON-NLS-1$
197
String JavaDoc 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 JavaDoc(destination, CACHE_EXTENSION)); //$NON-NLS-1$
209
} catch (ParserConfigurationException JavaDoc e) {
210         } catch (FactoryConfigurationError JavaDoc e) {
211         } catch (IOException JavaDoc e) {
212         }
213     }
214
215     public static Element writeExtensionPoint(Document JavaDoc doc, IPluginExtensionPoint extPoint) {
216         Element child = doc.createElement("extension-point"); //$NON-NLS-1$
217
if (extPoint.getId() != null)
218             child.setAttribute("id", PDEXMLHelper.getWritableString(extPoint.getId())); //$NON-NLS-1$
219
if (extPoint.getName() != null)
220             child.setAttribute("name", PDEXMLHelper.getWritableString(extPoint.getName())); //$NON-NLS-1$
221
if (extPoint.getSchema() != null)
222             child.setAttribute("schema", PDEXMLHelper.getWritableString(extPoint.getSchema())); //$NON-NLS-1$
223
if (extPoint instanceof PluginExtensionPoint)
224             child.setAttribute("line", Integer.toString(((PluginExtensionPoint)extPoint).getStartLine())); //$NON-NLS-1$
225
return child;
226     }
227     
228     public static Element writeExtension(Document JavaDoc doc, IPluginExtension extension) {
229         Element child = doc.createElement("extension"); //$NON-NLS-1$
230
if (extension.getPoint() != null)
231             child.setAttribute("point", PDEXMLHelper.getWritableString(extension.getPoint())); //$NON-NLS-1$
232
if (extension.getName() != null)
233             child.setAttribute("name", PDEXMLHelper.getWritableString(extension.getName())); //$NON-NLS-1$
234
if (extension.getId() != null)
235             child.setAttribute("id", PDEXMLHelper.getWritableString(extension.getId())); //$NON-NLS-1$
236
if (extension instanceof PluginExtension)
237             child.setAttribute("line", Integer.toString(((PluginExtension)extension).getStartLine())); //$NON-NLS-1$
238
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 JavaDoc 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 JavaDoc 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 JavaDoc jarFile = null;
269         InputStream JavaDoc stream = null;
270         try {
271             String JavaDoc filename = desc.getHost() == null ? "plugin.xml" : "fragment.xml"; //$NON-NLS-1$ //$NON-NLS-2$
272
String JavaDoc path = desc.getLocation();
273
274             File JavaDoc file = new File JavaDoc(path);
275             if (file.isFile()) {
276                 jarFile = new ZipFile JavaDoc(file, ZipFile.OPEN_READ);
277                 ZipEntry JavaDoc manifestEntry = jarFile.getEntry(filename);
278                 if (manifestEntry != null)
279                     stream = new BufferedInputStream JavaDoc(jarFile.getInputStream(manifestEntry));
280             } else if (file.isDirectory()) {
281                 File JavaDoc manifest = new File JavaDoc(file, filename);
282                 if (manifest.exists() && manifest.isFile()) {
283                     stream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(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 JavaDoc e) {
292         } catch (ParserConfigurationException JavaDoc e) {
293         } catch (SAXException JavaDoc e) {
294         } catch (FactoryConfigurationError JavaDoc e) {
295         } finally {
296             try {
297                 if (stream != null)
298                     stream.close();
299             } catch (IOException JavaDoc e1) {
300             }
301             try {
302                 if (jarFile != null)
303                     jarFile.close();
304             } catch (IOException JavaDoc e2) {
305             }
306         }
307     }
308
309 }
310
Popular Tags