1 11 package org.eclipse.update.internal.ui.model; 12 13 import java.io.*; 14 import java.net.*; 15 import java.util.ArrayList ; 16 import java.util.StringTokenizer ; 17 import java.util.Vector ; 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 fileName, Vector 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 bookmarks) { 51 ArrayList result = new ArrayList (); 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 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 bookmarks, IPath path) { 66 Object [] array = bookmarks.toArray(); 67 return find(array, path); 68 } 69 70 private static NamedModelObject find(Object [] array, IPath path) { 71 String 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 bookmarks) { 89 if (root.getNodeName().equals("bookmarks")) { NodeList children = root.getChildNodes(); 91 processChildren(children, null, bookmarks); 92 } 93 } 94 private static void processChildren( 95 NodeList children, 96 BookmarkFolder folder, 97 Vector 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")) { object = createSite(child); 105 } else if (child.getNodeName().equals("folder")) { 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 name = getAttribute(child, "name"); URL url = null; 123 try { 124 url = new URL(getAttribute(child, "url")); } catch (MalformedURLException e) { 126 } 127 128 String web = getAttribute(child, "web"); boolean webBookmark = (web != null && web.equals("true")); 131 String sel = getAttribute(child, "selected"); boolean selected = (sel != null && sel.equals("true")); 134 SiteBookmark bookmark = new SiteBookmark(name, url, webBookmark, selected); 135 136 String local = getAttribute(child, "local"); bookmark.setLocal(local != null && local.equals("true")); 139 String ign = getAttribute(child, "ignored-categories"); if (ign != null) { 141 StringTokenizer stok = new StringTokenizer (ign, ","); ArrayList array = new ArrayList (); 143 while (stok.hasMoreTokens()) { 144 String tok = stok.nextToken(); 145 array.add(tok); 146 } 147 bookmark.setIgnoredCategories((String []) array.toArray(new String [array.size()])); 148 } 149 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 createDescription(Node child) { 162 String description = ""; 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 name = getAttribute(child, "name"); folder.setName(name); 177 if (child.hasChildNodes()) 178 processChildren(child.getChildNodes(), folder, null); 179 return folder; 180 } 181 182 public static void store(String fileName, Vector 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"); writer = new PrintWriter(osw); 190 writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); writer.println("<bookmarks>"); for (int i = 0; i < bookmarks.size(); i++) { 193 Object obj = bookmarks.get(i); 194 writeObject(" ", obj, writer); } 196 } catch (IOException e) { 197 UpdateUI.logException(e, false); 198 } finally { 199 writer.println("</bookmarks>"); 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 indent, 218 Object obj, 219 PrintWriter writer) { 220 if (obj instanceof SiteBookmark) { 221 SiteBookmark bookmark = (SiteBookmark) obj; 222 String name = bookmark.getName(); 223 String url = bookmark.getURL().toString(); 224 String web = bookmark.isWebBookmark()?"true":"false"; String sel = bookmark.isSelected()?"true":"false"; String local = bookmark.isLocal() ? "true" : "false"; String [] ign = bookmark.getIgnoredCategories(); 228 StringBuffer wign = new StringBuffer (); 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 + "\""); if (wign.length() > 0) 236 writer.print(" ignored-categories=\""+wign.toString()+"\""); if (bookmark.getDescription() != null) { 238 writer.println(">"); writer.print(indent+" <description>"); writer.print(UpdateManagerUtils.getWritableXMLString(bookmark.getDescription())); 241 writer.println("</description>"); writer.println(indent +"</site>"); } else { 244 writer.println("/>"); } 246 } else if (obj instanceof BookmarkFolder) { 247 BookmarkFolder folder = (BookmarkFolder) obj; 248 String name = folder.getName(); 249 writer.println(indent + "<folder name=\"" + UpdateManagerUtils.getWritableXMLString(name) + "\">"); Object [] children = folder.getChildren(folder); 251 String indent2 = indent + " "; for (int i = 0; i < children.length; i++) { 253 writeObject(indent2, children[i], writer); 254 } 255 writer.println(indent + "</folder>"); } 257 } 258 259 private static String getAttribute(Node node, String name) { 260 NamedNodeMap atts = node.getAttributes(); 261 Node att = atts.getNamedItem(name); 262 if (att != null) { 263 return att.getNodeValue(); 264 } 265 return ""; } 267 private static void processFolder(BookmarkFolder folder, ArrayList result) { 268 Object [] 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 obj, ArrayList 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 |