KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > feature > AbstractFeatureModel


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.feature;
12
13 import java.io.InputStream JavaDoc;
14
15 import javax.xml.parsers.SAXParser JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.pde.core.IModelChangedEvent;
19 import org.eclipse.pde.core.ModelChangedEvent;
20 import org.eclipse.pde.internal.core.AbstractNLModel;
21 import org.eclipse.pde.internal.core.PDECore;
22 import org.eclipse.pde.internal.core.XMLCopyrightHandler;
23 import org.eclipse.pde.internal.core.XMLDefaultHandler;
24 import org.eclipse.pde.internal.core.ifeature.IFeature;
25 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
26 import org.eclipse.pde.internal.core.ifeature.IFeatureModelFactory;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 public abstract class AbstractFeatureModel
32     extends AbstractNLModel
33     implements IFeatureModel {
34     protected transient Feature feature;
35     private transient IFeatureModelFactory factory;
36     private boolean enabled = true;
37
38     public AbstractFeatureModel() {
39         super();
40     }
41     public IFeature getFeature() {
42         if (feature == null) {
43             Feature f = new Feature();
44             f.model = this;
45             this.feature = f;
46         }
47         return feature;
48     }
49     public IFeatureModelFactory getFactory() {
50         if (factory == null)
51             factory = new FeatureFactory(this);
52         return factory;
53     }
54     public String JavaDoc getInstallLocation() {
55         return null;
56     }
57     public boolean isEditable() {
58         return true;
59     }
60     public boolean isEnabled() {
61         return enabled;
62     }
63     public void load(InputStream JavaDoc stream, boolean outOfSync) throws CoreException {
64         try {
65             SAXParser JavaDoc parser = getSaxParser();
66             XMLDefaultHandler handler = new XMLDefaultHandler();
67             XMLCopyrightHandler chandler = new XMLCopyrightHandler(handler);
68             parser.setProperty("http://xml.org/sax/properties/lexical-handler", chandler); //$NON-NLS-1$
69
parser.parse(stream, handler);
70             if (handler.isPrepared()) {
71                 processDocument(handler.getDocument());
72                 String JavaDoc copyright = chandler.getCopyright();
73                 if (copyright != null)
74                     feature.setCopyright(copyright);
75                 setLoaded(true);
76                 if (!outOfSync)
77                     updateTimeStamp();
78             }
79         } catch (SAXException JavaDoc e) {
80         } catch (Exception JavaDoc e) {
81             PDECore.logException(e);
82         }
83     }
84     
85     public boolean isValid() {
86         if (!isLoaded()) return false;
87         IFeature feature = getFeature();
88         return feature!=null && feature.isValid();
89     }
90
91     private void processDocument(Document JavaDoc doc) {
92         Node JavaDoc rootNode = doc.getDocumentElement();
93         if (feature == null) {
94             feature = new Feature();
95             feature.model = this;
96         } else {
97             feature.reset();
98         }
99         feature.parse(rootNode);
100     }
101     public void reload(InputStream JavaDoc stream, boolean outOfSync)
102         throws CoreException {
103         if (feature != null)
104             feature.reset();
105         load(stream, outOfSync);
106         fireModelChanged(
107             new ModelChangedEvent(this,
108                 IModelChangedEvent.WORLD_CHANGED,
109                 new Object JavaDoc[] { feature },
110                 null));
111     }
112     public void setEnabled(boolean enabled) {
113         this.enabled = enabled;
114     }
115 }
116
Popular Tags