KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintWriter JavaDoc;
14 import java.util.Locale JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.pde.core.IModelChangedEvent;
19 import org.eclipse.pde.core.IWritable;
20 import org.eclipse.pde.internal.core.isite.ISite;
21 import org.eclipse.pde.internal.core.isite.ISiteArchive;
22 import org.eclipse.pde.internal.core.isite.ISiteCategoryDefinition;
23 import org.eclipse.pde.internal.core.isite.ISiteDescription;
24 import org.eclipse.pde.internal.core.isite.ISiteFeature;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27
28 public class Site extends SiteObject implements ISite {
29     private static final long serialVersionUID = 1L;
30     final static String JavaDoc INDENT = " "; //$NON-NLS-1$
31
private Vector JavaDoc features = new Vector JavaDoc();
32     private Vector JavaDoc archives = new Vector JavaDoc();
33     private Vector JavaDoc categoryDefs = new Vector JavaDoc();
34     private String JavaDoc type;
35     private String JavaDoc url;
36     private String JavaDoc mirrorsUrl;
37     private ISiteDescription description;
38
39     /**
40      * @see org.eclipse.pde.internal.core.isite.ISite#setType(java.lang.String)
41      */

42     public void setType(String JavaDoc type) throws CoreException {
43         ensureModelEditable();
44         Object JavaDoc oldValue = this.type;
45         this.type = type;
46         firePropertyChanged(P_TYPE, oldValue, type);
47     }
48
49     /**
50      * @see org.eclipse.pde.internal.core.isite.ISite#getType()
51      */

52     public String JavaDoc getType() {
53         return type;
54     }
55
56     /**
57      * @see org.eclipse.pde.internal.core.isite.ISite#setURL(java.net.URL)
58      */

59     public void setURL(String JavaDoc url) throws CoreException {
60         ensureModelEditable();
61         Object JavaDoc oldValue = this.url;
62         this.url = url;
63         firePropertyChanged(P_URL, oldValue, url);
64     }
65
66     /**
67      * @see org.eclipse.pde.internal.core.isite.ISite#getURL()
68      */

69     public String JavaDoc getURL() {
70         return url;
71     }
72
73     /**
74      * @see org.eclipse.pde.internal.core.isite.ISite#setMirrorsURL(String)
75      */

76     public void setMirrorsURL(String JavaDoc url) throws CoreException {
77         ensureModelEditable();
78         Object JavaDoc oldValue = this.mirrorsUrl;
79         this.mirrorsUrl = url;
80         firePropertyChanged(P_MIRRORS_URL, oldValue, url);
81     }
82
83     /**
84      * @see org.eclipse.pde.internal.core.isite.ISite#getMirrorsURL()
85      */

86     public String JavaDoc getMirrorsURL() {
87         return mirrorsUrl;
88     }
89     
90     /**
91      * @see org.eclipse.pde.internal.core.isite.ISite#getDescription()
92      */

93     public ISiteDescription getDescription() {
94         return description;
95     }
96
97     /**
98      * @see org.eclipse.pde.internal.core.isite.ISite#setDescription(org.eclipse.pde.internal.core.isite.ISiteDescription)
99      */

100     public void setDescription(ISiteDescription description)
101         throws CoreException {
102         ensureModelEditable();
103         Object JavaDoc oldValue = this.description;
104         this.description = description;
105         firePropertyChanged(P_DESCRIPTION, oldValue, description);
106     }
107
108     /**
109      * @see org.eclipse.pde.internal.core.isite.ISite#addFeatures(org.eclipse.pde.internal.core.isite.ISiteFeature)
110      */

111     public void addFeatures(ISiteFeature[] newFeatures) throws CoreException {
112         ensureModelEditable();
113         for (int i = 0; i < newFeatures.length; i++) {
114             ISiteFeature feature = newFeatures[i];
115             ((SiteFeature) feature).setInTheModel(true);
116             features.add(newFeatures[i]);
117         }
118         fireStructureChanged(newFeatures, IModelChangedEvent.INSERT);
119     }
120
121     /**
122      * @see org.eclipse.pde.internal.core.isite.ISite#addArchives(org.eclipse.pde.internal.core.isite.ISiteArchive)
123      */

124     public void addArchives(ISiteArchive[] archs) throws CoreException {
125         ensureModelEditable();
126         for (int i = 0; i < archs.length; i++) {
127             ISiteArchive archive = archs[i];
128             ((SiteArchive) archive).setInTheModel(true);
129             archives.add(archs[i]);
130         }
131         fireStructureChanged(archs, IModelChangedEvent.INSERT);
132     }
133
134     /**
135      * @see org.eclipse.pde.internal.core.isite.ISite#addCategoryDefinitions(org.eclipse.pde.internal.core.isite.ISiteCategoryDefinition)
136      */

137     public void addCategoryDefinitions(ISiteCategoryDefinition[] defs)
138         throws CoreException {
139         ensureModelEditable();
140         for (int i = 0; i < defs.length; i++) {
141             ISiteCategoryDefinition def = defs[i];
142             ((SiteCategoryDefinition) def).setInTheModel(true);
143             categoryDefs.add(defs[i]);
144         }
145         fireStructureChanged(defs, IModelChangedEvent.INSERT);
146     }
147
148     /**
149      * @see org.eclipse.pde.internal.core.isite.ISite#removeFeatures(org.eclipse.pde.internal.core.isite.ISiteFeature)
150      */

151     public void removeFeatures(ISiteFeature[] newFeatures)
152         throws CoreException {
153         ensureModelEditable();
154         for (int i = 0; i < newFeatures.length; i++) {
155             ISiteFeature feature = newFeatures[i];
156             ((SiteFeature) feature).setInTheModel(false);
157             features.remove(newFeatures[i]);
158         }
159         fireStructureChanged(newFeatures, IModelChangedEvent.REMOVE);
160     }
161
162     /**
163      * @see org.eclipse.pde.internal.core.isite.ISite#removeArchives(org.eclipse.pde.internal.core.isite.ISiteArchive)
164      */

165     public void removeArchives(ISiteArchive[] archs) throws CoreException {
166         ensureModelEditable();
167         for (int i = 0; i < archs.length; i++) {
168             ISiteArchive archive = archs[i];
169             ((SiteArchive) archive).setInTheModel(false);
170             archives.remove(archs[i]);
171         }
172         fireStructureChanged(archs, IModelChangedEvent.REMOVE);
173     }
174
175     /**
176      * @see org.eclipse.pde.internal.core.isite.ISite#removeCategoryDefinitions(org.eclipse.pde.internal.core.isite.ISiteCategoryDefinition)
177      */

178     public void removeCategoryDefinitions(ISiteCategoryDefinition[] defs)
179         throws CoreException {
180         ensureModelEditable();
181         for (int i = 0; i < defs.length; i++) {
182             ISiteCategoryDefinition def = defs[i];
183             ((SiteCategoryDefinition) def).setInTheModel(false);
184             categoryDefs.remove(defs[i]);
185         }
186         fireStructureChanged(defs, IModelChangedEvent.REMOVE);
187     }
188
189     /**
190      * @see org.eclipse.pde.internal.core.isite.ISite#getFeatures()
191      */

192     public ISiteFeature[] getFeatures() {
193         return (ISiteFeature[]) features.toArray(
194             new ISiteFeature[features.size()]);
195     }
196
197     /**
198      * @see org.eclipse.pde.internal.core.isite.ISite#getArchives()
199      */

200     public ISiteArchive[] getArchives() {
201         return (ISiteArchive[]) archives.toArray(
202             new ISiteArchive[archives.size()]);
203     }
204
205     /**
206      * @see org.eclipse.pde.internal.core.isite.ISite#getCategoryDefinitions()
207      */

208     public ISiteCategoryDefinition[] getCategoryDefinitions() {
209         return (ISiteCategoryDefinition[]) categoryDefs.toArray(
210             new ISiteCategoryDefinition[categoryDefs.size()]);
211     }
212     protected void reset() {
213         archives.clear();
214         categoryDefs.clear();
215         features.clear();
216         description = null;
217         type = null;
218         url = null;
219         mirrorsUrl = null;
220     }
221     protected void parse(Node JavaDoc node) {
222         type = getNodeAttribute(node, "type"); //$NON-NLS-1$
223
url = getNodeAttribute(node, "url"); //$NON-NLS-1$
224
mirrorsUrl = getNodeAttribute(node, "mirrorsURL"); //$NON-NLS-1$
225
NodeList JavaDoc children = node.getChildNodes();
226         for (int i = 0; i < children.getLength(); i++) {
227             Node JavaDoc child = children.item(i);
228             if (child.getNodeType() == Node.ELEMENT_NODE) {
229                 parseChild(child);
230             }
231         }
232     }
233
234     protected void parseChild(Node JavaDoc child) {
235         String JavaDoc tag = child.getNodeName().toLowerCase(Locale.ENGLISH);
236         if (tag.equals("feature")) { //$NON-NLS-1$
237
ISiteFeature feature = getModel().getFactory().createFeature();
238             ((SiteFeature) feature).parse(child);
239             ((SiteFeature) feature).setInTheModel(true);
240             features.add(feature);
241         } else if (tag.equals("archive")) { //$NON-NLS-1$
242
ISiteArchive archive = getModel().getFactory().createArchive();
243             ((SiteArchive) archive).parse(child);
244             ((SiteArchive) archive).setInTheModel(true);
245             archives.add(archive);
246         } else if (tag.equals("category-def")) { //$NON-NLS-1$
247
ISiteCategoryDefinition def =
248                 getModel().getFactory().createCategoryDefinition();
249             ((SiteCategoryDefinition) def).parse(child);
250             ((SiteCategoryDefinition) def).setInTheModel(true);
251             categoryDefs.add(def);
252         } else if (tag.equals("description")) { //$NON-NLS-1$
253
if (description != null)
254                 return;
255             description = getModel().getFactory().createDescription(this);
256             ((SiteDescription) description).parse(child);
257             ((SiteDescription) description).setInTheModel(true);
258         }
259     }
260     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
261         throws CoreException {
262         if (name.equals(P_TYPE)) {
263             setType(newValue != null ? newValue.toString() : null);
264         } else if (name.equals(P_URL)) {
265             setURL(newValue != null ? newValue.toString() : null);
266         } else if (name.equals(P_MIRRORS_URL)) {
267             setMirrorsURL(newValue != null ? newValue.toString() : null);
268         } else if (
269             name.equals(P_DESCRIPTION)
270                 && newValue instanceof ISiteDescription) {
271             setDescription((ISiteDescription) newValue);
272         } else
273             super.restoreProperty(name, oldValue, newValue);
274     }
275     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
276         writer.print(indent + "<site"); //$NON-NLS-1$
277
String JavaDoc indent2 = indent + INDENT;
278         String JavaDoc indenta = indent + INDENT + INDENT;
279         writeIfDefined(indenta, writer, "type", getType()); //$NON-NLS-1$
280
writeIfDefined(indenta, writer, "url", getURL()); //$NON-NLS-1$
281
writeIfDefined(indenta, writer, "mirrorsURL", getMirrorsURL()); //$NON-NLS-1$
282
writer.println(">"); //$NON-NLS-1$
283

284         if (description != null) {
285             description.write(indent2, writer);
286         }
287         writeChildren(indent2, features, writer);
288         writeChildren(indent2, archives, writer);
289         writeChildren(indent2, categoryDefs, writer);
290         writer.println(indent + "</site>"); //$NON-NLS-1$
291
}
292     
293     public boolean isValid() {
294         for (int i=0; i<features.size(); i++) {
295             ISiteFeature feature = (ISiteFeature)features.get(i);
296             if (!feature.isValid()) return false;
297         }
298         for (int i=0; i<archives.size(); i++) {
299             ISiteArchive archive = (ISiteArchive)archives.get(i);
300             if (!archive.isValid()) return false;
301         }
302         for (int i=0; i<categoryDefs.size(); i++) {
303             ISiteCategoryDefinition def = (ISiteCategoryDefinition)categoryDefs.get(i);
304             if (!def.isValid()) return false;
305         }
306         return true;
307     }
308     
309     
310     
311     private void writeChildren(
312         String JavaDoc indent,
313         Vector JavaDoc children,
314         PrintWriter JavaDoc writer) {
315         for (int i = 0; i < children.size(); i++) {
316             IWritable writable = (IWritable) children.get(i);
317             writable.write(indent, writer);
318         }
319     }
320     private void writeIfDefined(
321         String JavaDoc indent,
322         PrintWriter JavaDoc writer,
323         String JavaDoc attName,
324         String JavaDoc attValue) {
325         if (attValue == null || attValue.length() <= 0)
326             return;
327         writer.println();
328         writer.print(indent + attName + "=\"" + SiteObject.getWritableString(attValue) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
329
}
330     
331 }
332
Popular Tags