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.IOException;
013: import java.io.InputStream;
014: import java.net.URL;
015:
016: import javax.xml.parsers.FactoryConfigurationError;
017: import javax.xml.parsers.ParserConfigurationException;
018: import javax.xml.parsers.SAXParser;
019:
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.osgi.service.resolver.BundleDescription;
022: import org.eclipse.pde.core.IModelChangedEvent;
023: import org.eclipse.pde.core.ModelChangedEvent;
024: import org.eclipse.pde.core.plugin.IExtensions;
025: import org.eclipse.pde.core.plugin.IExtensionsModel;
026: import org.eclipse.pde.core.plugin.IExtensionsModelFactory;
027: import org.eclipse.pde.core.plugin.IPluginAttribute;
028: import org.eclipse.pde.core.plugin.IPluginElement;
029: import org.eclipse.pde.core.plugin.IPluginExtension;
030: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
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: import org.xml.sax.SAXException;
035:
036: public abstract class AbstractExtensionsModel extends AbstractNLModel
037: implements IExtensionsModel, IExtensionsModelFactory {
038: protected Extensions fExtensions;
039:
040: public IExtensionsModelFactory getFactory() {
041: return this ;
042: }
043:
044: protected Extensions createExtensions() {
045: Extensions extensions = new Extensions(!isEditable());
046: extensions.setModel(this );
047: return extensions;
048: }
049:
050: public IExtensions getExtensions() {
051: return getExtensions(true);
052: }
053:
054: public IExtensions getExtensions(boolean createIfMissing) {
055: if (fExtensions == null && createIfMissing) {
056: fExtensions = createExtensions();
057: setLoaded(true);
058: }
059: return fExtensions;
060: }
061:
062: public abstract URL getNLLookupLocation();
063:
064: protected URL[] getNLLookupLocations() {
065: URL locations[] = { getNLLookupLocation() };
066: return locations;
067: }
068:
069: public synchronized void load(InputStream stream, boolean outOfSync)
070: throws CoreException {
071:
072: if (fExtensions == null) {
073: fExtensions = createExtensions();
074: fExtensions.setModel(this );
075: }
076: fExtensions.reset();
077: setLoaded(false);
078: try {
079: // TODO: possibly remove this work.
080: // Need a good way to "setLoaded()" value
081: // With the way we do it, we might be able to claim it is always loaded.
082: SAXParser parser = getSaxParser();
083: PluginHandler handler = new PluginHandler(true);
084: parser.parse(stream, handler);
085: fExtensions.load(handler.getSchemaVersion());
086: setLoaded(true);
087: if (!outOfSync)
088: updateTimeStamp();
089: } catch (ParserConfigurationException e) {
090: } catch (SAXException e) {
091: } catch (FactoryConfigurationError e) {
092: } catch (IOException e) {
093: }
094: }
095:
096: // loaded from workspace when creating workspace model
097: public void load(BundleDescription desc, PDEState state) {
098: fExtensions = createExtensions();
099: fExtensions.setModel(this );
100: updateTimeStamp();
101: setLoaded(true);
102: }
103:
104: public void reload(InputStream stream, boolean outOfSync)
105: throws CoreException {
106: load(stream, outOfSync);
107: fireModelChanged(new ModelChangedEvent(this ,
108: IModelChangedEvent.WORLD_CHANGED,
109: new Object[] { fExtensions }, null));
110: }
111:
112: protected abstract void updateTimeStamp();
113:
114: public IPluginAttribute createAttribute(IPluginElement element) {
115: PluginAttribute attribute = new PluginAttribute();
116: attribute.setModel(this );
117: attribute.setParent(element);
118: return attribute;
119: }
120:
121: public IPluginElement createElement(IPluginObject parent) {
122: PluginElement element = new PluginElement();
123: element.setModel(this );
124: element.setParent(parent);
125: return element;
126: }
127:
128: public IPluginExtension createExtension() {
129: PluginExtension extension = new PluginExtension();
130: extension.setParent(getExtensions());
131: extension.setModel(this );
132: return extension;
133: }
134:
135: public IPluginExtensionPoint createExtensionPoint() {
136: PluginExtensionPoint extensionPoint = new PluginExtensionPoint();
137: extensionPoint.setModel(this );
138: extensionPoint.setParent(getExtensions());
139: return extensionPoint;
140: }
141:
142: public boolean isValid() {
143: if (!isLoaded())
144: return false;
145: if (fExtensions == null)
146: return false;
147: return fExtensions.isValid();
148: }
149: }
|