KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > model > BookmarkUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.update.internal.ui.model;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.ArrayList JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17 import java.util.Vector JavaDoc;
18
19 import javax.xml.parsers.*;
20
21 import org.eclipse.core.runtime.*;
22 import org.eclipse.update.internal.core.*;
23 import org.eclipse.update.internal.ui.*;
24 import org.w3c.dom.*;
25 import org.xml.sax.*;
26
27 public class BookmarkUtil {
28     private static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
29
30     public static void parse(String JavaDoc fileName, Vector JavaDoc bookmarks) {
31         File file = new File(fileName);
32         if (!file.exists())
33             return;
34
35         try {
36             documentBuilderFactory.setNamespaceAware(true);
37             DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
38             Document doc = parser.parse(file);
39             Node root = doc.getDocumentElement();
40             processRoot(root, bookmarks);
41         } catch (ParserConfigurationException e) {
42             UpdateUI.logException(e);
43         } catch (SAXException e) {
44             UpdateUI.logException(e);
45         } catch (IOException e) {
46             UpdateUI.logException(e);
47         }
48     }
49
50     public static SiteBookmark[] getBookmarks(Vector JavaDoc bookmarks) {
51         ArrayList JavaDoc result = new ArrayList JavaDoc();
52         for (int i = 0; i < bookmarks.size(); i++) {
53             processEntry(bookmarks.get(i), result);
54         }
55         return (SiteBookmark[]) result.toArray(new SiteBookmark[result.size()]);
56     }
57
58     public static BookmarkFolder getFolder(Vector JavaDoc bookmarks, IPath path) {
59         NamedModelObject object = find(bookmarks, path);
60         if (object != null && object instanceof BookmarkFolder)
61             return (BookmarkFolder) object;
62         return null;
63     }
64
65     public static NamedModelObject find(Vector JavaDoc bookmarks, IPath path) {
66         Object JavaDoc[] array = bookmarks.toArray();
67         return find(array, path);
68     }
69
70     private static NamedModelObject find(Object JavaDoc[] array, IPath path) {
71         String JavaDoc name = path.segment(0);
72         for (int i = 0; i < array.length; i++) {
73             NamedModelObject obj = (NamedModelObject) array[i];
74             if (obj.getName().equals(name)) {
75                 if (obj instanceof BookmarkFolder) {
76                     if (path.segmentCount() > 1) {
77                         IPath childPath = path.removeFirstSegments(1);
78                         BookmarkFolder folder = (BookmarkFolder) obj;
79                         return find(folder.getChildren(null), childPath);
80                     }
81                 }
82                 return obj;
83             }
84         }
85         return null;
86     }
87
88     private static void processRoot(Node root, Vector JavaDoc bookmarks) {
89         if (root.getNodeName().equals("bookmarks")) { //$NON-NLS-1$
90
NodeList children = root.getChildNodes();
91             processChildren(children, null, bookmarks);
92         }
93     }
94     private static void processChildren(
95         NodeList children,
96         BookmarkFolder folder,
97         Vector JavaDoc bookmarks) {
98         UpdateModel model = UpdateUI.getDefault().getUpdateModel();
99         for (int i = 0; i < children.getLength(); i++) {
100             Node child = children.item(i);
101             NamedModelObject object = null;
102             if (child.getNodeType() == Node.ELEMENT_NODE) {
103                 if (child.getNodeName().equals("site")) { //$NON-NLS-1$
104
object = createSite(child);
105                 } else if (child.getNodeName().equals("folder")) { //$NON-NLS-1$
106
object = createFolder(child);
107                 }
108             }
109             if (object != null) {
110                 if (folder != null) {
111                     folder.addChild(object);
112                 } else {
113                     bookmarks.add(object);
114                 }
115                 object.setModel(model);
116             }
117         }
118     }
119
120     private static SiteBookmark createSite(Node child) {
121         String JavaDoc name = getAttribute(child, "name"); //$NON-NLS-1$
122
URL url = null;
123         try {
124             url = new URL(getAttribute(child, "url")); //$NON-NLS-1$
125
} catch (MalformedURLException e) {
126         }
127
128         String JavaDoc web = getAttribute(child, "web"); //$NON-NLS-1$
129
boolean webBookmark = (web != null && web.equals("true")); //$NON-NLS-1$
130

131         String JavaDoc sel = getAttribute(child, "selected"); //$NON-NLS-1$
132
boolean selected = (sel != null && sel.equals("true")); //$NON-NLS-1$
133

134         SiteBookmark bookmark = new SiteBookmark(name, url, webBookmark, selected);
135
136         String JavaDoc local = getAttribute(child, "local"); //$NON-NLS-1$
137
bookmark.setLocal(local != null && local.equals("true")); //$NON-NLS-1$
138

139         String JavaDoc ign = getAttribute(child, "ignored-categories"); //$NON-NLS-1$
140
if (ign != null) {
141             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(ign, ","); //$NON-NLS-1$
142
ArrayList JavaDoc array = new ArrayList JavaDoc();
143             while (stok.hasMoreTokens()) {
144                 String JavaDoc tok = stok.nextToken();
145                 array.add(tok);
146             }
147             bookmark.setIgnoredCategories((String JavaDoc[]) array.toArray(new String JavaDoc[array.size()]));
148         }
149         // read description
150
NodeList children = child.getChildNodes();
151         for (int i = 0; i < children.getLength(); i++) {
152             Node node = children.item(i);
153             if (node.getNodeType() == Node.ELEMENT_NODE) {
154                 bookmark.setDescription(createDescription(node));
155                 break;
156             }
157         }
158         return bookmark;
159     }
160
161     private static String JavaDoc createDescription(Node child) {
162         String JavaDoc description = ""; //$NON-NLS-1$
163
NodeList children = child.getChildNodes();
164         for (int i = 0; i < children.getLength(); i++) {
165             Node node = children.item(i);
166             if (node.getNodeType() == Node.TEXT_NODE)
167                 description += node.getNodeValue();
168         }
169         return description;
170     }
171
172         
173     private static BookmarkFolder createFolder(Node child) {
174         BookmarkFolder folder = new BookmarkFolder();
175         String JavaDoc name = getAttribute(child, "name"); //$NON-NLS-1$
176
folder.setName(name);
177         if (child.hasChildNodes())
178             processChildren(child.getChildNodes(), folder, null);
179         return folder;
180     }
181
182     public static void store(String JavaDoc fileName, Vector JavaDoc bookmarks) {
183         FileOutputStream fos = null;
184         OutputStreamWriter osw = null;
185         PrintWriter writer = null;
186         try {
187             fos = new FileOutputStream(fileName);
188             osw = new OutputStreamWriter(fos, "UTF8"); //$NON-NLS-1$
189
writer = new PrintWriter(osw);
190             writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
191
writer.println("<bookmarks>"); //$NON-NLS-1$
192
for (int i = 0; i < bookmarks.size(); i++) {
193                 Object JavaDoc obj = bookmarks.get(i);
194                 writeObject(" ", obj, writer); //$NON-NLS-1$
195
}
196         } catch (IOException e) {
197             UpdateUI.logException(e, false);
198         } finally {
199             writer.println("</bookmarks>"); //$NON-NLS-1$
200
writer.flush();
201             writer.close();
202             try {
203                 if (osw != null)
204                     osw.close();
205             } catch (IOException e1) {
206                 UpdateUI.logException(e1, false);
207             }
208             try {
209                 if (fos != null)
210                     fos.close();
211             } catch (IOException e2) {
212                 UpdateUI.logException(e2, false);
213             }
214         }
215     }
216     private static void writeObject(
217         String JavaDoc indent,
218         Object JavaDoc obj,
219         PrintWriter writer) {
220         if (obj instanceof SiteBookmark) {
221             SiteBookmark bookmark = (SiteBookmark) obj;
222             String JavaDoc name = bookmark.getName();
223             String JavaDoc url = bookmark.getURL().toString();
224             String JavaDoc web = bookmark.isWebBookmark()?"true":"false"; //$NON-NLS-1$ //$NON-NLS-2$
225
String JavaDoc sel = bookmark.isSelected()?"true":"false"; //$NON-NLS-1$ //$NON-NLS-2$
226
String JavaDoc local = bookmark.isLocal() ? "true" : "false"; //$NON-NLS-1$ //$NON-NLS-2$
227
String JavaDoc [] ign = bookmark.getIgnoredCategories();
228             StringBuffer JavaDoc wign = new StringBuffer JavaDoc();
229             for (int i = 0; i < ign.length; i++) {
230                 if (i > 0)
231                     wign.append(',');
232                 wign.append(ign[i]);
233             }
234             writer.print(indent + "<site name=\"" + UpdateManagerUtils.getWritableXMLString(name) + "\" url=\"" + url + "\" web=\"" + web + "\" selected=\"" + sel + "\" local=\"" + local + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
235
if (wign.length() > 0)
236                 writer.print(" ignored-categories=\""+wign.toString()+"\""); //$NON-NLS-1$ //$NON-NLS-2$
237
if (bookmark.getDescription() != null) {
238                 writer.println(">"); //$NON-NLS-1$
239
writer.print(indent+" <description>"); //$NON-NLS-1$
240
writer.print(UpdateManagerUtils.getWritableXMLString(bookmark.getDescription()));
241                 writer.println("</description>"); //$NON-NLS-1$
242
writer.println(indent +"</site>"); //$NON-NLS-1$
243
} else {
244                 writer.println("/>"); //$NON-NLS-1$
245
}
246         } else if (obj instanceof BookmarkFolder) {
247             BookmarkFolder folder = (BookmarkFolder) obj;
248             String JavaDoc name = folder.getName();
249             writer.println(indent + "<folder name=\"" + UpdateManagerUtils.getWritableXMLString(name) + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
250
Object JavaDoc[] children = folder.getChildren(folder);
251             String JavaDoc indent2 = indent + " "; //$NON-NLS-1$
252
for (int i = 0; i < children.length; i++) {
253                 writeObject(indent2, children[i], writer);
254             }
255             writer.println(indent + "</folder>"); //$NON-NLS-1$
256
}
257     }
258
259     private static String JavaDoc getAttribute(Node node, String JavaDoc name) {
260         NamedNodeMap atts = node.getAttributes();
261         Node att = atts.getNamedItem(name);
262         if (att != null) {
263             return att.getNodeValue();
264         }
265         return ""; //$NON-NLS-1$
266
}
267     private static void processFolder(BookmarkFolder folder, ArrayList JavaDoc result) {
268         Object JavaDoc[] children = folder.getChildren(folder);
269         for (int i = 0; i < children.length; i++) {
270             processEntry(children[i], result);
271         }
272     }
273     private static void processEntry(Object JavaDoc obj, ArrayList JavaDoc result) {
274         if (obj instanceof SiteBookmark)
275             result.add(obj);
276         else if (obj instanceof BookmarkFolder) {
277             processFolder((BookmarkFolder) obj, result);
278         }
279     }
280 }
281
Popular Tags