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.feature;
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.pde.core.IModelChangedEvent;
018: import org.eclipse.pde.core.ModelChangedEvent;
019: import org.eclipse.pde.internal.core.AbstractNLModel;
020: import org.eclipse.pde.internal.core.PDECore;
021: import org.eclipse.pde.internal.core.XMLCopyrightHandler;
022: import org.eclipse.pde.internal.core.XMLDefaultHandler;
023: import org.eclipse.pde.internal.core.ifeature.IFeature;
024: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
025: import org.eclipse.pde.internal.core.ifeature.IFeatureModelFactory;
026: import org.w3c.dom.Document;
027: import org.w3c.dom.Node;
028: import org.xml.sax.SAXException;
029:
030: public abstract class AbstractFeatureModel extends AbstractNLModel
031: implements IFeatureModel {
032: protected transient Feature feature;
033: private transient IFeatureModelFactory factory;
034: private boolean enabled = true;
035:
036: public AbstractFeatureModel() {
037: super ();
038: }
039:
040: public IFeature getFeature() {
041: if (feature == null) {
042: Feature f = new Feature();
043: f.model = this ;
044: this .feature = f;
045: }
046: return feature;
047: }
048:
049: public IFeatureModelFactory getFactory() {
050: if (factory == null)
051: factory = new FeatureFactory(this );
052: return factory;
053: }
054:
055: public String getInstallLocation() {
056: return null;
057: }
058:
059: public boolean isEditable() {
060: return true;
061: }
062:
063: public boolean isEnabled() {
064: return enabled;
065: }
066:
067: public void load(InputStream stream, boolean outOfSync)
068: throws CoreException {
069: try {
070: SAXParser parser = getSaxParser();
071: XMLDefaultHandler handler = new XMLDefaultHandler();
072: XMLCopyrightHandler chandler = new XMLCopyrightHandler(
073: handler);
074: parser
075: .setProperty(
076: "http://xml.org/sax/properties/lexical-handler", chandler); //$NON-NLS-1$
077: parser.parse(stream, handler);
078: if (handler.isPrepared()) {
079: processDocument(handler.getDocument());
080: String copyright = chandler.getCopyright();
081: if (copyright != null)
082: feature.setCopyright(copyright);
083: setLoaded(true);
084: if (!outOfSync)
085: updateTimeStamp();
086: }
087: } catch (SAXException e) {
088: } catch (Exception e) {
089: PDECore.logException(e);
090: }
091: }
092:
093: public boolean isValid() {
094: if (!isLoaded())
095: return false;
096: IFeature feature = getFeature();
097: return feature != null && feature.isValid();
098: }
099:
100: private void processDocument(Document doc) {
101: Node rootNode = doc.getDocumentElement();
102: if (feature == null) {
103: feature = new Feature();
104: feature.model = this ;
105: } else {
106: feature.reset();
107: }
108: feature.parse(rootNode);
109: }
110:
111: public void reload(InputStream stream, boolean outOfSync)
112: throws CoreException {
113: if (feature != null)
114: feature.reset();
115: load(stream, outOfSync);
116: fireModelChanged(new ModelChangedEvent(this ,
117: IModelChangedEvent.WORLD_CHANGED,
118: new Object[] { feature }, null));
119: }
120:
121: public void setEnabled(boolean enabled) {
122: this.enabled = enabled;
123: }
124: }
|