KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > cheatsheet > simple > SimpleCSModel


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.pde.internal.core.cheatsheet.simple;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16
17 import javax.xml.parsers.SAXParser JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.pde.core.IModelChangedEvent;
21 import org.eclipse.pde.core.ModelChangedEvent;
22 import org.eclipse.pde.internal.core.AbstractModel;
23 import org.eclipse.pde.internal.core.PDECore;
24 import org.eclipse.pde.internal.core.XMLDefaultHandler;
25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS;
26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel;
27 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30
31 /**
32  * SimpleCSModel
33  *
34  */

35 public class SimpleCSModel extends AbstractModel implements ISimpleCSModel {
36
37     private ISimpleCSModelFactory fFactory;
38     
39     private ISimpleCS fSimpleCS;
40     
41     /**
42      *
43      */

44     private static final long serialVersionUID = 1L;
45
46     /**
47      *
48      */

49     public SimpleCSModel() {
50         // NO-OP
51
}
52
53     /* (non-Javadoc)
54      * @see org.eclipse.pde.internal.core.AbstractModel#updateTimeStamp()
55      */

56     protected void updateTimeStamp() {
57         // NO-OP
58
}
59
60     /* (non-Javadoc)
61      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel#getFactory()
62      */

63     public ISimpleCSModelFactory getFactory() {
64         if (fFactory == null) {
65             fFactory = new SimpleCSModelFactory(this);
66         }
67         return fFactory;
68     }
69
70     /* (non-Javadoc)
71      * @see org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel#getSimpleCS()
72      */

73     public ISimpleCS getSimpleCS() {
74         if (fSimpleCS == null) {
75             fSimpleCS = getFactory().createSimpleCS();
76         }
77         return fSimpleCS;
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.pde.core.IModel#isInSync()
82      */

83     public boolean isInSync() {
84         return true;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.pde.core.IModel#load()
89      */

90     public void load() throws CoreException {
91         // NO-OP
92
}
93
94     /* (non-Javadoc)
95      * @see org.eclipse.pde.core.IModel#load(java.io.InputStream, boolean)
96      */

97     public void load(InputStream JavaDoc source, boolean outOfSync)
98             throws CoreException {
99         try {
100             SAXParser JavaDoc parser = getSaxParser();
101             XMLDefaultHandler handler = new XMLDefaultHandler();
102             parser.parse(source, handler);
103             if (handler.isPrepared()) {
104                 processDocument(handler.getDocument());
105                 setLoaded(true);
106             }
107         } catch (Exception JavaDoc e) {
108             setException(e);
109             PDECore.logException(e);
110         } finally {
111             try {
112                 if (source != null)
113                     source.close();
114             } catch (IOException JavaDoc e) {
115             }
116         }
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.pde.core.IModel#reload(java.io.InputStream, boolean)
121      */

122     public void reload(InputStream JavaDoc source, boolean outOfSync)
123             throws CoreException {
124         load(source, outOfSync);
125         fireModelChanged(
126                 new ModelChangedEvent(this,
127                     IModelChangedEvent.WORLD_CHANGED,
128                     new Object JavaDoc[] { fSimpleCS },
129                     null));
130     }
131
132     /* (non-Javadoc)
133      * @see org.eclipse.pde.core.IBaseModel#isEditable()
134      */

135     public boolean isEditable() {
136         return false;
137     }
138
139     /**
140      * @param doc
141      */

142     private void processDocument(Document JavaDoc doc) {
143         Element JavaDoc rootNode = doc.getDocumentElement();
144         if (fSimpleCS == null) {
145             fSimpleCS = getFactory().createSimpleCS();
146         } else {
147             fSimpleCS.reset();
148         }
149         fSimpleCS.parse(rootNode);
150     }
151     
152 }
153
Popular Tags