KickJava   Java API By Example, From Geeks To Geeks.

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


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.feature;
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.internal.core.ifeature.IFeatureURL;
20 import org.eclipse.pde.internal.core.ifeature.IFeatureURLElement;
21 import org.w3c.dom.Node JavaDoc;
22 import org.w3c.dom.NodeList JavaDoc;
23
24 public class FeatureURL extends FeatureObject implements IFeatureURL {
25     private static final long serialVersionUID = 1L;
26     private IFeatureURLElement fUpdate;
27     private Vector JavaDoc fDiscoveries = new Vector JavaDoc();
28
29     public void addDiscovery(IFeatureURLElement discovery) throws CoreException {
30         ensureModelEditable();
31         fDiscoveries.add(discovery);
32         ((FeatureURLElement)discovery).setInTheModel(true);
33         fireStructureChanged(discovery, IModelChangedEvent.INSERT);
34     }
35     public void setUpdate(IFeatureURLElement update) throws CoreException {
36         ensureModelEditable();
37         if(fUpdate == update){
38             return;
39         }
40         if(fUpdate!=null){
41             ((FeatureURLElement)fUpdate).setInTheModel(false);
42         }
43         IFeatureURLElement oldValue = fUpdate;
44         fUpdate = update;
45         if(oldValue!=null){
46             fireStructureChanged(oldValue, IModelChangedEvent.REMOVE);
47         }
48         if(update!=null){
49             ((FeatureURLElement)update).setInTheModel(true);
50             fireStructureChanged(update, IModelChangedEvent.INSERT);
51         }
52     }
53     public IFeatureURLElement[] getDiscoveries() {
54         return (IFeatureURLElement[]) fDiscoveries
55                 .toArray(new IFeatureURLElement[fDiscoveries.size()]);
56     }
57     public IFeatureURLElement getUpdate() {
58         return fUpdate;
59     }
60     protected void parse(Node JavaDoc node) {
61         NodeList JavaDoc children = node.getChildNodes();
62         for (int i = 0; i < children.getLength(); i++) {
63             Node JavaDoc child = children.item(i);
64             if (child.getNodeType() == Node.ELEMENT_NODE) {
65                 String JavaDoc tag = child.getNodeName().toLowerCase(Locale.ENGLISH);
66                 int urlType = -1;
67                 if (tag.equals("update")) { //$NON-NLS-1$
68
urlType = IFeatureURLElement.UPDATE;
69                 } else if (tag.equals("discovery")) { //$NON-NLS-1$
70
urlType = IFeatureURLElement.DISCOVERY;
71                 }
72                 if (urlType != -1) {
73                     IFeatureURLElement element =
74                         getModel().getFactory().createURLElement(this, urlType);
75                     ((FeatureURLElement) element).parse(child);
76                     if (urlType == IFeatureURLElement.UPDATE) {
77                         ((FeatureURLElement)element).setInTheModel(true);
78                         fUpdate = element;
79                     }
80                     else if (urlType == IFeatureURLElement.DISCOVERY) {
81                         ((FeatureURLElement)element).setInTheModel(true);
82                         fDiscoveries.add(element);
83                     }
84                 }
85             }
86         }
87     }
88     public void removeDiscovery(IFeatureURLElement discovery)
89         throws CoreException {
90         ensureModelEditable();
91         fDiscoveries.remove(discovery);
92         ((FeatureURLElement)discovery).setInTheModel(false);
93         fireStructureChanged(discovery, IModelChangedEvent.REMOVE);
94     }
95     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
96         if(fUpdate == null && fDiscoveries.size() <=0){
97             return;
98         }
99         writer.println();
100         writer.println(indent + "<url>"); //$NON-NLS-1$
101
String JavaDoc indent2 = indent + Feature.INDENT;
102         if(fUpdate!=null) {
103             fUpdate.write(indent2, writer);
104         }
105         for (int i = 0; i < fDiscoveries.size(); i++) {
106             IFeatureURLElement element = (IFeatureURLElement) fDiscoveries.elementAt(i);
107             element.write(indent2, writer);
108         }
109         writer.println(indent + "</url>"); //$NON-NLS-1$
110
}
111 }
112
Popular Tags