KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > EngineDescriptorManager


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.help.ui.internal.views;
12
13 import java.io.*;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Observable JavaDoc;
17
18 import javax.xml.parsers.*;
19
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.help.ui.internal.*;
22 import org.w3c.dom.*;
23 import org.xml.sax.*;
24
25 public class EngineDescriptorManager extends Observable JavaDoc implements IHelpUIConstants {
26     private ArrayList JavaDoc descriptors;
27
28     private EngineTypeDescriptor[] engineTypes;
29
30     private static final String JavaDoc USER_FILE = "userSearches.xml"; //$NON-NLS-1$
31
private static final String JavaDoc ATT_ENGINE_TYPE_ID = "engineTypeId"; //$NON-NLS-1$
32

33     public static class DescriptorEvent {
34         private EngineDescriptor desc;
35         private int kind;
36         public DescriptorEvent(EngineDescriptor desc, int kind) {
37             this.desc = desc;
38             this.kind = kind;
39         }
40         public EngineDescriptor getDescriptor() {
41             return desc;
42         }
43         public int getKind() {
44             return kind;
45         }
46     }
47
48     public EngineDescriptorManager() {
49         descriptors = new ArrayList JavaDoc();
50         load();
51     }
52     
53     public void add(EngineDescriptor desc) {
54         descriptors.add(desc);
55         this.setChanged();
56         this.notifyObservers(new DescriptorEvent(desc, ADD));
57     }
58
59     public void remove(EngineDescriptor desc) {
60         descriptors.remove(desc);
61         this.setChanged();
62         this.notifyObservers(new DescriptorEvent(desc, REMOVE));
63     }
64
65     public void notifyPropertyChange(EngineDescriptor desc) {
66         this.setChanged();
67         this.notifyObservers(new DescriptorEvent(desc, CHANGE));
68     }
69     
70     public EngineDescriptor[] getDescriptors() {
71         return (EngineDescriptor[]) descriptors
72                 .toArray(new EngineDescriptor[descriptors.size()]);
73     }
74     
75     public EngineDescriptor findEngine(String JavaDoc engineId) {
76         for (int i=0; i<descriptors.size(); i++) {
77             EngineDescriptor desc = (EngineDescriptor)descriptors.get(i);
78             if (desc.getId().equals(engineId))
79                 return desc;
80         }
81         return null;
82     }
83
84     public EngineTypeDescriptor[] getEngineTypes() {
85         return engineTypes;
86     }
87
88     public void save() {
89         IPath stateLoc = HelpUIPlugin.getDefault().getStateLocation();
90         String JavaDoc fileName = stateLoc.append(USER_FILE).toOSString();
91         FileOutputStream fos = null;
92         OutputStreamWriter osw = null;
93         try {
94             fos = new FileOutputStream(fileName);
95             osw = new OutputStreamWriter(fos, "UTF8"); //$NON-NLS-1$
96
PrintWriter writer = new PrintWriter(osw);
97             writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
98
writer.println("<engines>"); //$NON-NLS-1$
99
for (int i = 0; i < descriptors.size(); i++) {
100                 EngineDescriptor desc = (EngineDescriptor) descriptors.get(i);
101                 if (desc.isUserDefined()) {
102                     save(writer, desc);
103                 }
104             }
105             writer.println("</engines>"); //$NON-NLS-1$
106
writer.flush();
107         }
108         catch (IOException e) {
109             HelpUIPlugin.logError(Messages.EngineDescriptorManager_errorSaving, e);
110         }
111         finally {
112             if (osw!=null) {
113                 try {
114                     osw.close();
115                 }
116                 catch (IOException e) {
117                 }
118             }
119             if (fos!=null) {
120                 try {
121                     fos.close();
122                 }
123                 catch (IOException e) {
124                 }
125             }
126         }
127     }
128
129     public void load() {
130         loadFromExtensionRegistry();
131         IPath stateLoc = HelpUIPlugin.getDefault().getStateLocation();
132         String JavaDoc fileName = stateLoc.append(USER_FILE).toOSString();
133         try {
134             load(fileName);
135         }
136         catch (IOException e) {
137             HelpUIPlugin.logError(Messages.EngineDescriptorManager_errorLoading, e);
138         }
139     }
140
141     private void loadFromExtensionRegistry() {
142         IConfigurationElement[] elements = Platform.getExtensionRegistry()
143                 .getConfigurationElementsFor(ENGINE_EXP_ID);
144         Hashtable JavaDoc engineTypes = loadEngineTypes(elements);
145         for (int i = 0; i < elements.length; i++) {
146             IConfigurationElement element = elements[i];
147             if (element.getName().equals(TAG_ENGINE)) {
148                 EngineDescriptor desc = new EngineDescriptor(element);
149                 String JavaDoc engineId = desc.getEngineTypeId();
150                 if (engineId != null) {
151                     EngineTypeDescriptor etdesc = (EngineTypeDescriptor) engineTypes
152                             .get(engineId);
153                     if (etdesc != null) {
154                         desc.setEngineType(etdesc);
155                         descriptors.add(desc);
156                     }
157                 }
158             }
159         }
160     }
161
162     private Hashtable JavaDoc loadEngineTypes(IConfigurationElement[] elements) {
163         Hashtable JavaDoc result = new Hashtable JavaDoc();
164         ArrayList JavaDoc list = new ArrayList JavaDoc();
165         for (int i = 0; i < elements.length; i++) {
166             IConfigurationElement element = elements[i];
167             if (element.getName().equals("engineType")) { //$NON-NLS-1$
168
EngineTypeDescriptor etdesc = new EngineTypeDescriptor(element);
169                 String JavaDoc id = etdesc.getId();
170                 if (id != null) {
171                     list.add(etdesc);
172                     result.put(etdesc.getId(), etdesc);
173                 }
174             }
175         }
176         engineTypes = (EngineTypeDescriptor[]) list
177                 .toArray(new EngineTypeDescriptor[list.size()]);
178         return result;
179     }
180
181     public void load(Reader r) {
182         Document document = null;
183         try {
184             DocumentBuilder parser = DocumentBuilderFactory.newInstance()
185                     .newDocumentBuilder();
186             // parser.setProcessNamespace(true);
187
document = parser.parse(new InputSource(r));
188
189             // Strip out any comments first
190
Node root = document.getFirstChild();
191             while (root.getNodeType() == Node.COMMENT_NODE) {
192                 document.removeChild(root);
193                 root = document.getFirstChild();
194             }
195             load(document, (Element) root);
196         } catch (ParserConfigurationException e) {
197             // ignore
198
} catch (IOException e) {
199             // ignore
200
} catch (SAXException e) {
201             // ignore
202
}
203     }
204
205     /*
206      * (non-Javadoc) Method declared on IDialogSettings.
207      */

208     public void load(String JavaDoc fileName) throws IOException {
209         File file = new File(fileName);
210         if (!file.exists()) return;
211         FileInputStream stream = new FileInputStream(file);
212         BufferedReader reader = new BufferedReader(new InputStreamReader(
213                 stream, "utf-8"));//$NON-NLS-1$
214
load(reader);
215         reader.close();
216     }
217
218     private void load(Document doc, Element root) {
219         NodeList engines = root.getElementsByTagName(TAG_ENGINE);
220         for (int i=0; i<engines.getLength(); i++) {
221             Node node = engines.item(i);
222             loadUserEntry(node);
223         }
224     }
225
226     private void loadUserEntry(Node node) {
227         EngineDescriptor edesc = new EngineDescriptor(this);
228         String JavaDoc id = getAttribute(node, ATT_ID);
229         String JavaDoc engineTypeId = getAttribute(node, ATT_ENGINE_TYPE_ID);
230         EngineTypeDescriptor etdesc = findEngineType(engineTypeId);
231         String JavaDoc label = getAttribute(node, ATT_LABEL);
232         String JavaDoc desc = getDescription(node);
233         if (etdesc == null)
234             return;
235         edesc.setEngineType(etdesc);
236         edesc.setUserDefined(true);
237         edesc.setId(id);
238         edesc.setLabel(label);
239         edesc.setDescription(desc);
240         descriptors.add(edesc);
241     }
242
243     public String JavaDoc computeNewId(String JavaDoc typeId) {
244         ArrayList JavaDoc used = new ArrayList JavaDoc();
245         for (int i=0; i<descriptors.size(); i++) {
246             EngineDescriptor ed = (EngineDescriptor)descriptors.get(i);
247             if (!ed.isUserDefined()) continue;
248             String JavaDoc edTypeId = ed.getEngineTypeId();
249             if (typeId.equals(edTypeId)) {
250                 String JavaDoc edId = ed.getId();
251                 int loc = edId.lastIndexOf('.');
252                 if (loc!= -1) {
253                     String JavaDoc cvalue = edId.substring(loc+1);
254                     int ivalue = Integer.parseInt(cvalue);
255                     used.add(new Integer JavaDoc(ivalue));
256                 }
257             }
258         }
259         for (int i=1; i<Integer.MAX_VALUE; i++) {
260             if (!isUsed(i, used)) {
261                 return typeId+"."+"user."+i; //$NON-NLS-1$ //$NON-NLS-2$
262
}
263         }
264         return typeId;
265     }
266     
267     private boolean isUsed(int value, ArrayList JavaDoc used) {
268         for (int i=0; i<used.size(); i++) {
269             Integer JavaDoc iv = (Integer JavaDoc)used.get(i);
270             if (iv.intValue()==value)
271                 return true;
272         }
273         return false;
274     }
275
276     private String JavaDoc getDescription(Node node) {
277         NodeList list = ((Element)node).getElementsByTagName(TAG_DESC);
278         if (list.getLength()==1) {
279             Node desc = list.item(0);
280             NodeList children = desc.getChildNodes();
281             for (int i=0; i<children.getLength(); i++) {
282                 Node child = children.item(i);
283                 if (child.getNodeType()==Node.TEXT_NODE) {
284                     String JavaDoc text = child.getNodeValue();
285                     return text.trim();
286                 }
287             }
288         }
289         return null;
290     }
291     
292     private void save(PrintWriter writer, EngineDescriptor desc) {
293         String JavaDoc indent = " "; //$NON-NLS-1$
294
String JavaDoc attIndent = indent + indent;
295         writer.print(indent);
296         writer.println("<engine "); //$NON-NLS-1$
297
saveAttribute(writer, attIndent, "id", desc.getId()); //$NON-NLS-1$
298
saveAttribute(writer, attIndent, ATT_ENGINE_TYPE_ID, desc.getEngineTypeId());
299         saveAttribute(writer, attIndent, ATT_LABEL, desc.getLabel());
300         writer.println(">"); //$NON-NLS-1$
301
saveDescription(writer, indent+indent, desc.getDescription());
302         writer.print(indent);
303         writer.println("</engine>"); //$NON-NLS-1$
304
}
305     
306     private String JavaDoc getAttribute(Node node, String JavaDoc name) {
307         Node att = node.getAttributes().getNamedItem(name);
308         if (att!=null)
309             return att.getNodeValue();
310         return null;
311     }
312     
313     private void saveAttribute(PrintWriter writer, String JavaDoc indent, String JavaDoc name, String JavaDoc value) {
314         if (value==null)
315             return;
316         writer.print(indent);
317         writer.print(name);
318         writer.print("=\""); //$NON-NLS-1$
319
writer.print(value);
320         writer.println("\""); //$NON-NLS-1$
321
}
322     private void saveDescription(PrintWriter writer, String JavaDoc indent, String JavaDoc desc) {
323         if (desc==null)
324             return;
325         writer.print(indent);
326         writer.println("<description>"); //$NON-NLS-1$
327
writer.println(desc);
328         writer.print(indent);
329         writer.println("</description>"); //$NON-NLS-1$
330
}
331
332     private EngineTypeDescriptor findEngineType(String JavaDoc id) {
333         if (id == null)
334             return null;
335         for (int i = 0; i < engineTypes.length; i++) {
336             EngineTypeDescriptor etd = engineTypes[i];
337             if (etd.getId().equals(id))
338                 return etd;
339         }
340         return null;
341     }
342 }
343
Popular Tags