001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.plugin;
011:
012: import java.io.InputStream;
013:
014: import javax.xml.parsers.SAXParser;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.osgi.service.resolver.BundleDescription;
018: import org.eclipse.pde.core.IModelChangedEvent;
019: import org.eclipse.pde.core.ModelChangedEvent;
020: import org.eclipse.pde.core.plugin.IExtensions;
021: import org.eclipse.pde.core.plugin.IExtensionsModelFactory;
022: import org.eclipse.pde.core.plugin.IPluginAttribute;
023: import org.eclipse.pde.core.plugin.IPluginBase;
024: import org.eclipse.pde.core.plugin.IPluginElement;
025: import org.eclipse.pde.core.plugin.IPluginExtension;
026: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
027: import org.eclipse.pde.core.plugin.IPluginImport;
028: import org.eclipse.pde.core.plugin.IPluginLibrary;
029: import org.eclipse.pde.core.plugin.IPluginModelBase;
030: import org.eclipse.pde.core.plugin.IPluginModelFactory;
031: import org.eclipse.pde.core.plugin.IPluginObject;
032: import org.eclipse.pde.internal.core.AbstractNLModel;
033: import org.eclipse.pde.internal.core.PDEState;
034:
035: public abstract class AbstractPluginModelBase extends AbstractNLModel
036: implements IPluginModelBase, IPluginModelFactory {
037: protected IPluginBase fPluginBase;
038: private boolean enabled;
039: private BundleDescription fBundleDescription;
040: protected boolean fAbbreviated;
041:
042: public AbstractPluginModelBase() {
043: super ();
044: }
045:
046: public abstract String getInstallLocation();
047:
048: public abstract IPluginBase createPluginBase();
049:
050: public IExtensions createExtensions() {
051: return createPluginBase();
052: }
053:
054: public IExtensionsModelFactory getFactory() {
055: return this ;
056: }
057:
058: public IPluginModelFactory getPluginFactory() {
059: return this ;
060: }
061:
062: public IPluginBase getPluginBase() {
063: return getPluginBase(true);
064: }
065:
066: public IPluginBase getPluginBase(boolean createIfMissing) {
067: if (fPluginBase == null && createIfMissing) {
068: fPluginBase = createPluginBase();
069: setLoaded(true);
070: }
071: return fPluginBase;
072: }
073:
074: public void load(InputStream stream, boolean outOfSync)
075: throws CoreException {
076: load(stream, outOfSync, new PluginHandler(fAbbreviated));
077: }
078:
079: public void load(InputStream stream, boolean outOfSync,
080: PluginHandler handler) throws CoreException {
081: if (fPluginBase == null)
082: fPluginBase = createPluginBase();
083:
084: ((PluginBase) fPluginBase).reset();
085: setLoaded(false);
086: try {
087: SAXParser parser = getSaxParser();
088: parser.parse(stream, handler);
089: ((PluginBase) fPluginBase).load(handler
090: .getDocumentElement(), handler.getSchemaVersion());
091: setLoaded(true);
092: if (!outOfSync)
093: updateTimeStamp();
094: } catch (Exception e) {
095: }
096: }
097:
098: public void load(BundleDescription description, PDEState state) {
099: setBundleDescription(description);
100: IPluginBase base = getPluginBase();
101: if (base instanceof Plugin)
102: ((Plugin) base).load(description, state);
103: else
104: ((Fragment) base).load(description, state);
105: updateTimeStamp();
106: setLoaded(true);
107: }
108:
109: public IExtensions getExtensions() {
110: return getPluginBase();
111: }
112:
113: public IExtensions getExtensions(boolean createIfMissing) {
114: return getPluginBase(createIfMissing);
115: }
116:
117: public boolean isEnabled() {
118: return enabled;
119: }
120:
121: public boolean isFragmentModel() {
122: return false;
123: }
124:
125: public void reload(InputStream stream, boolean outOfSync)
126: throws CoreException {
127: load(stream, outOfSync);
128: fireModelChanged(new ModelChangedEvent(this ,
129: IModelChangedEvent.WORLD_CHANGED,
130: new Object[] { fPluginBase }, null));
131: }
132:
133: public void setEnabled(boolean newEnabled) {
134: enabled = newEnabled;
135: }
136:
137: public String toString() {
138: IPluginBase pluginBase = getPluginBase();
139: if (pluginBase != null)
140: return pluginBase.getId();
141: return super .toString();
142: }
143:
144: protected abstract void updateTimeStamp();
145:
146: public IPluginAttribute createAttribute(IPluginElement element) {
147: PluginAttribute attribute = new PluginAttribute();
148: attribute.setModel(this );
149: attribute.setParent(element);
150: return attribute;
151: }
152:
153: public IPluginElement createElement(IPluginObject parent) {
154: PluginElement element = new PluginElement();
155: element.setModel(this );
156: element.setParent(parent);
157: return element;
158: }
159:
160: public IPluginExtension createExtension() {
161: PluginExtension extension = new PluginExtension();
162: extension.setParent(getPluginBase());
163: extension.setModel(this );
164: return extension;
165: }
166:
167: public IPluginExtensionPoint createExtensionPoint() {
168: PluginExtensionPoint extensionPoint = new PluginExtensionPoint();
169: extensionPoint.setModel(this );
170: extensionPoint.setParent(getPluginBase());
171: return extensionPoint;
172: }
173:
174: public IPluginImport createImport() {
175: PluginImport iimport = new PluginImport();
176: iimport.setModel(this );
177: iimport.setParent(getPluginBase());
178: return iimport;
179: }
180:
181: public IPluginImport createImport(String pluginId) {
182: PluginImport iimport = new PluginImport(this , pluginId);
183: iimport.setParent(getPluginBase());
184: return iimport;
185: }
186:
187: public IPluginLibrary createLibrary() {
188: PluginLibrary library = new PluginLibrary();
189: library.setModel(this );
190: library.setParent(getPluginBase());
191: return library;
192: }
193:
194: public boolean isValid() {
195: if (!isLoaded())
196: return false;
197: if (fPluginBase == null)
198: return false;
199: return fPluginBase.isValid();
200: }
201:
202: public boolean isBundleModel() {
203: return false;
204: }
205:
206: /* (non-Javadoc)
207: * @see org.eclipse.pde.core.IModel#dispose()
208: */
209: public void dispose() {
210: fBundleDescription = null;
211: super .dispose();
212: }
213:
214: /* (non-Javadoc)
215: * @see org.eclipse.pde.core.plugin.IPluginModelBase#getBundleDescription()
216: */
217: public BundleDescription getBundleDescription() {
218: return fBundleDescription;
219: }
220:
221: /* (non-Javadoc)
222: * @see org.eclipse.pde.core.plugin.IPluginModelBase#setBundleDescription(org.eclipse.osgi.service.resolver.BundleDescription)
223: */
224: public void setBundleDescription(BundleDescription description) {
225: fBundleDescription = description;
226: }
227:
228: }
|