KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > site > AbstractSiteModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.site;
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.AbstractModel;
21 import org.eclipse.pde.internal.core.PDECore;
22 import org.eclipse.pde.internal.core.XMLDefaultHandler;
23 import org.eclipse.pde.internal.core.isite.ISite;
24 import org.eclipse.pde.internal.core.isite.ISiteModel;
25 import org.eclipse.pde.internal.core.isite.ISiteModelFactory;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28
29 public abstract class AbstractSiteModel
30     extends AbstractModel
31     implements ISiteModel {
32     protected transient Site site;
33     private transient ISiteModelFactory factory;
34     private boolean enabled = true;
35
36     public AbstractSiteModel() {
37         super();
38     }
39     public ISite getSite() {
40         if (site == null) {
41             Site s = new Site();
42             s.model = this;
43             this.site = s;
44         }
45         return site;
46     }
47     public ISiteModelFactory getFactory() {
48         if (factory == null)
49             factory = new SiteModelFactory(this);
50         return factory;
51     }
52     public String JavaDoc getInstallLocation() {
53         return null;
54     }
55     public boolean isEditable() {
56         return true;
57     }
58     public boolean isEnabled() {
59         return enabled;
60     }
61     public boolean isValid() {
62         if (!isLoaded() || site==null) return false;
63         return site.isValid();
64     }
65     public void load(InputStream JavaDoc stream, boolean outOfSync) throws CoreException {
66         try {
67             SAXParser JavaDoc parser = getSaxParser();
68             XMLDefaultHandler handler = new XMLDefaultHandler();
69             parser.parse(stream, handler);
70             if (handler.isPrepared()) {
71                 processDocument(handler.getDocument());
72                 setLoaded(true);
73                 if (!outOfSync)
74                     updateTimeStamp();
75             }
76         } catch (Exception JavaDoc e) {
77             PDECore.logException(e);
78         }
79     }
80
81     private void processDocument(Document JavaDoc doc) {
82         Node JavaDoc rootNode = doc.getDocumentElement();
83         if (site == null) {
84             site = new Site();
85             site.model = this;
86         } else {
87             site.reset();
88         }
89         site.parse(rootNode);
90     }
91     public void reload(InputStream JavaDoc stream, boolean outOfSync)
92         throws CoreException {
93         if (site != null)
94             site.reset();
95         load(stream, outOfSync);
96         fireModelChanged(
97             new ModelChangedEvent(this,
98                 IModelChangedEvent.WORLD_CHANGED,
99                 new Object JavaDoc[] { site },
100                 null));
101     }
102     public void setEnabled(boolean enabled) {
103         this.enabled = enabled;
104     }
105 }
106
Popular Tags