KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > cheatsheet > comp > CompCSModel


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.comp;
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.comp.ICompCS;
26 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModel;
27 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModelFactory;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30
31 /**
32  * CompCSModel
33  *
34  */

35 public class CompCSModel extends AbstractModel implements ICompCSModel {
36
37     private ICompCSModelFactory fFactory;
38     
39     private ICompCS fCompCS;
40     
41     /**
42      *
43      */

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

49     public CompCSModel() {
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.comp.ICompCSModel#getCompCS()
62      */

63     public ICompCS getCompCS() {
64         if (fCompCS == null) {
65             fCompCS = getFactory().createCompCS();
66         }
67         return fCompCS;
68     }
69
70     /* (non-Javadoc)
71      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModel#getFactory()
72      */

73     public ICompCSModelFactory getFactory() {
74         if (fFactory == null) {
75             fFactory = new CompCSModelFactory(this);
76         }
77         return fFactory;
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
100         try {
101             SAXParser JavaDoc parser = getSaxParser();
102             XMLDefaultHandler handler = new XMLDefaultHandler();
103             parser.parse(source, handler);
104             if (handler.isPrepared()) {
105                 processDocument(handler.getDocument());
106                 setLoaded(true);
107             }
108         } catch (Exception JavaDoc e) {
109             setException(e);
110             PDECore.logException(e);
111         } finally {
112             try {
113                 if (source != null) {
114                     source.close();
115                 }
116             } catch (IOException JavaDoc e) {
117                 // Ignore
118
}
119         }
120
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.pde.core.IModel#reload(java.io.InputStream, boolean)
125      */

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

140     public boolean isEditable() {
141         return false;
142     }
143
144     /**
145      * @param doc
146      */

147     private void processDocument(Document JavaDoc doc) {
148         Element JavaDoc rootNode = doc.getDocumentElement();
149         if (fCompCS == null) {
150             fCompCS = getFactory().createCompCS();
151         } else {
152             fCompCS.reset();
153         }
154         fCompCS.parse(rootNode);
155     }
156     
157 }
158
Popular Tags