KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > plugin > AbstractExtensionsModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.plugin;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
19 import javax.xml.parsers.SAXParser JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.osgi.service.resolver.BundleDescription;
23 import org.eclipse.pde.core.IModelChangedEvent;
24 import org.eclipse.pde.core.ModelChangedEvent;
25 import org.eclipse.pde.core.plugin.IExtensions;
26 import org.eclipse.pde.core.plugin.IExtensionsModel;
27 import org.eclipse.pde.core.plugin.IExtensionsModelFactory;
28 import org.eclipse.pde.core.plugin.IPluginAttribute;
29 import org.eclipse.pde.core.plugin.IPluginElement;
30 import org.eclipse.pde.core.plugin.IPluginExtension;
31 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
32 import org.eclipse.pde.core.plugin.IPluginObject;
33 import org.eclipse.pde.internal.core.AbstractNLModel;
34 import org.eclipse.pde.internal.core.PDEState;
35 import org.xml.sax.SAXException JavaDoc;
36
37 public abstract class AbstractExtensionsModel
38     extends AbstractNLModel
39     implements IExtensionsModel, IExtensionsModelFactory {
40     protected Extensions fExtensions;
41
42     public IExtensionsModelFactory getFactory() {
43         return this;
44     }
45     
46     protected Extensions createExtensions() {
47         Extensions extensions = new Extensions();
48         extensions.setModel(this);
49         return extensions;
50     }
51
52     public IExtensions getExtensions() {
53         return getExtensions(true);
54     }
55     public IExtensions getExtensions(boolean createIfMissing) {
56         if (fExtensions == null && createIfMissing) {
57             fExtensions = createExtensions();
58             setLoaded(true);
59         }
60         return fExtensions;
61     }
62
63     public abstract URL JavaDoc getNLLookupLocation();
64
65     protected URL JavaDoc[] getNLLookupLocations() {
66         URL JavaDoc locations [] = { getNLLookupLocation() };
67         return locations;
68     }
69
70     public synchronized void load(InputStream JavaDoc stream, boolean outOfSync)
71         throws CoreException {
72
73         if (fExtensions == null) {
74             fExtensions = createExtensions();
75             fExtensions.setModel(this);
76         }
77         fExtensions.reset();
78         setLoaded(false);
79         try {
80             SAXParser JavaDoc parser = getSaxParser();
81             PluginHandler handler = new PluginHandler(true);
82             parser.parse(stream, handler);
83             fExtensions.load(handler.getDocumentElement(), handler.getSchemaVersion());
84             setLoaded(true);
85             if (!outOfSync)
86                 updateTimeStamp();
87         } catch (ParserConfigurationException JavaDoc e) {
88         } catch (SAXException JavaDoc e) {
89         } catch (FactoryConfigurationError JavaDoc e) {
90         } catch (IOException JavaDoc e) {
91         }
92     }
93     
94     public void load(BundleDescription desc, PDEState state) {
95         fExtensions = createExtensions();
96         fExtensions.setModel(this);
97         fExtensions.load(state, desc.getBundleId());
98         updateTimeStamp();
99         setLoaded(true);
100     }
101
102     public void reload(InputStream JavaDoc stream, boolean outOfSync)
103         throws CoreException {
104         load(stream, outOfSync);
105         fireModelChanged(
106             new ModelChangedEvent(this,
107                 IModelChangedEvent.WORLD_CHANGED,
108                 new Object JavaDoc[] { fExtensions },
109                 null));
110     }
111     protected abstract void updateTimeStamp();
112
113     public IPluginAttribute createAttribute(IPluginElement element) {
114         PluginAttribute attribute = new PluginAttribute();
115         attribute.setModel(this);
116         attribute.setParent(element);
117         return attribute;
118     }
119     public IPluginElement createElement(IPluginObject parent) {
120         PluginElement element = new PluginElement();
121         element.setModel(this);
122         element.setParent(parent);
123         return element;
124     }
125     public IPluginExtension createExtension() {
126         PluginExtension extension = new PluginExtension();
127         extension.setParent(getExtensions());
128         extension.setModel(this);
129         return extension;
130     }
131     public IPluginExtensionPoint createExtensionPoint() {
132         PluginExtensionPoint extensionPoint = new PluginExtensionPoint();
133         extensionPoint.setModel(this);
134         extensionPoint.setParent(getExtensions());
135         return extensionPoint;
136     }
137     
138     public boolean isValid() {
139         if (!isLoaded()) return false;
140         if (fExtensions==null) return false;
141         return fExtensions.isValid();
142     }
143 }
144
Popular Tags