1 11 package org.eclipse.help.internal.workingset; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStreamReader ; 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.SortedSet ; 22 import java.util.TreeSet ; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import javax.xml.parsers.ParserConfigurationException ; 27 import javax.xml.transform.OutputKeys ; 28 import javax.xml.transform.Transformer ; 29 import javax.xml.transform.TransformerException ; 30 import javax.xml.transform.TransformerFactory ; 31 import javax.xml.transform.dom.DOMSource ; 32 import javax.xml.transform.stream.StreamResult ; 33 34 import org.eclipse.core.runtime.IAdaptable; 35 import org.eclipse.core.runtime.IPath; 36 import org.eclipse.core.runtime.Platform; 37 import org.eclipse.help.internal.HelpPlugin; 38 import org.eclipse.help.internal.base.BaseHelpSystem; 39 import org.eclipse.help.internal.base.HelpBasePlugin; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 import org.xml.sax.InputSource ; 45 import org.xml.sax.SAXException ; 46 47 53 public class WorkingSetManager implements IHelpWorkingSetManager { 54 55 private static final String WORKING_SET_STATE_FILENAME = "workingsets.xml"; 58 private SortedSet workingSets = new TreeSet (new WorkingSetComparator()); 59 60 private AdaptableTocsArray root; 61 62 private static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory 63 .newInstance(); 64 65 private static final TransformerFactory transformerFactory = TransformerFactory 66 .newInstance(); 67 68 71 public WorkingSetManager() { 72 restoreState(); 73 } 74 75 public AdaptableTocsArray getRoot() { 76 if (root == null) 77 root = new AdaptableTocsArray(HelpPlugin.getTocManager().getTocs( 78 Platform.getNL())); 79 return root; 80 } 81 82 85 public void addWorkingSet(WorkingSet workingSet) { 86 if (workingSet == null || workingSets.contains(workingSet)) 87 return; 88 workingSets.add(workingSet); 89 saveState(); 90 } 91 92 95 public WorkingSet createWorkingSet(String name, 96 AdaptableHelpResource[] elements) { 97 return new WorkingSet(name, elements); 98 } 99 100 108 public boolean equals(Object object) { 109 if (this == object) { 110 return true; 111 } 112 if (object instanceof WorkingSetManager) { 113 WorkingSetManager workingSetManager = (WorkingSetManager) object; 114 return workingSetManager.workingSets.equals(workingSets); 115 } 116 return false; 117 } 118 119 123 public WorkingSet getWorkingSet(String name) { 124 if (name == null || workingSets == null) 125 return null; 126 127 Iterator iter = workingSets.iterator(); 128 while (iter.hasNext()) { 129 WorkingSet workingSet = (WorkingSet) iter.next(); 130 if (name.equals(workingSet.getName())) 131 return workingSet; 132 } 133 return null; 134 } 135 136 141 public int hashCode() { 142 return workingSets.hashCode(); 143 } 144 145 150 public WorkingSet[] getWorkingSets() { 151 return (WorkingSet[]) workingSets.toArray(new WorkingSet[workingSets 152 .size()]); 153 } 154 155 160 private File getWorkingSetStateFile() { 161 IPath path = HelpBasePlugin.getDefault().getStateLocation(); 162 path = path.append(WORKING_SET_STATE_FILENAME); 163 return path.toFile(); 164 } 165 166 169 public void removeWorkingSet(WorkingSet workingSet) { 170 workingSets.remove(workingSet); 171 saveState(); 172 } 173 174 177 public boolean restoreState() { 178 File stateFile = getWorkingSetStateFile(); 179 180 if (stateFile.exists()) { 181 try { 182 FileInputStream input = new FileInputStream (stateFile); 183 InputStreamReader reader = new InputStreamReader (input, "utf-8"); 185 InputSource inputSource = new InputSource (reader); 186 inputSource.setSystemId(stateFile.toString()); 187 188 DocumentBuilder parser = documentBuilderFactory 189 .newDocumentBuilder(); 190 Document d = parser.parse(inputSource); 191 192 Element rootElement = d.getDocumentElement(); 193 restoreWorkingSetState(rootElement); 194 input.close(); 195 196 return true; 197 } catch (ParserConfigurationException pce) { 198 HelpPlugin 199 .logError( 200 "DocumentBuilder implementation could not be loaded, to restore working set state.", pce); return false; 202 } catch (SAXException se) { 203 HelpBasePlugin 204 .logError( 205 "Error occurred parsing file " + stateFile.toString() + ", while restoring working set state.", se); return false; 207 } catch (IOException ioe) { 208 HelpBasePlugin 209 .logError( 210 "Error occurred parsing file " + stateFile.toString() + ", while restoring working set state.", ioe); return false; 212 } 213 } 214 return false; 215 } 216 217 224 private void restoreWorkingSetState(Element parent) { 225 NodeList workingSets = parent.getChildNodes(); 226 227 for (int i = 0; i < workingSets.getLength(); i++) { 228 if (workingSets.item(i).getNodeType() != Node.ELEMENT_NODE) 229 continue; 230 231 WorkingSet workingSet = restoreWorkingSet((Element ) workingSets 232 .item(i)); 233 if (workingSet != null) { 234 this.workingSets.add(workingSet); 235 } 236 } 237 } 238 239 245 private WorkingSet restoreWorkingSet(Element workingSetNode) { 246 247 String name = workingSetNode.getAttribute("name"); NodeList items = workingSetNode.getElementsByTagName("item"); List helpResources = new ArrayList (items.getLength()); 250 for (int i = 0; i < items.getLength(); i++) { 251 Element item = (Element ) items.item(i); 252 String href = item.getAttribute("toc"); if (href == null || href.length() == 0) 254 continue; 255 256 String child_pos = item.getAttribute("topic"); int pos = -1; 258 if (child_pos != null) { 259 try { 260 pos = Integer.parseInt(child_pos); 261 } catch (Exception e) { 262 } 263 } 264 265 AdaptableHelpResource toc = getAdaptableToc(href); 266 267 if (toc == null) 268 return null; 269 270 if (pos == -1) { 271 helpResources.add(toc); 273 } else { 274 AdaptableTopic[] topics = (AdaptableTopic[]) toc.getChildren(); 276 if (pos >= 0 && topics.length > pos) 277 helpResources.add(topics[pos]); 278 } 279 } 280 281 AdaptableHelpResource[] elements = new AdaptableHelpResource[helpResources 282 .size()]; 283 helpResources.toArray(elements); 284 285 WorkingSet ws = createWorkingSet(name, elements); 286 287 return ws; 288 } 289 290 293 public synchronized boolean saveState() { 294 File stateFile = null; 295 try { 296 DocumentBuilder docBuilder = documentBuilderFactory 297 .newDocumentBuilder(); 298 Document doc = docBuilder.newDocument(); 299 Element rootElement = doc.createElement("workingSets"); doc.appendChild(rootElement); 301 302 saveWorkingSetState(rootElement); 303 304 stateFile = getWorkingSetStateFile(); 305 stateFile.getParentFile().mkdir(); 306 FileOutputStream stream = new FileOutputStream (stateFile); 307 308 Transformer transformer = transformerFactory.newTransformer(); 309 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); DOMSource source = new DOMSource (doc); 312 StreamResult result = new StreamResult (stream); 313 314 transformer.transform(source, result); 315 stream.close(); 316 return true; 317 } catch (ParserConfigurationException pce) { 318 HelpPlugin.logError( 319 "DocumentBuilder implementation could not be loaded.", pce); return false; 321 } catch (TransformerException e) { 322 HelpPlugin.logError("Problems occurred while saving working sets."); return false; 324 } catch (IOException e) { 325 stateFile.delete(); 326 HelpPlugin.logError( 327 "Problems occurred while saving working set file."); return false; 329 } 330 } 331 332 338 private void saveWorkingSetState(Element parent) { 339 Iterator iterator = workingSets.iterator(); 340 341 while (iterator.hasNext()) { 342 WorkingSet workingSet = (WorkingSet) iterator.next(); 343 workingSet.saveState(parent); 344 } 345 } 346 347 354 public void workingSetChanged(WorkingSet changedWorkingSet) { 355 saveState(); 356 } 357 358 public AdaptableToc getAdaptableToc(String href) { 359 return getRoot().getAdaptableToc(href); 360 } 361 362 public AdaptableTopic getAdaptableTopic(String id) { 363 364 if (id == null || id.length() == 0) 365 return null; 366 367 int len = id.length(); 371 if (id.charAt(len - 1) == '_') { 372 String indexStr = id.substring(id.lastIndexOf('_', len - 2) + 1, 374 len - 1); 375 int index = 0; 376 try { 377 index = Integer.parseInt(indexStr); 378 } catch (Exception e) { 379 } 380 381 String tocStr = id.substring(0, id.lastIndexOf('_', len - 2)); 382 AdaptableToc toc = getAdaptableToc(tocStr); 383 if (toc == null) 384 return null; 385 IAdaptable[] topics = toc.getChildren(); 386 if (index < 0 || index >= topics.length) 387 return null; 388 return (AdaptableTopic) topics[index]; 389 } 390 391 return null; 392 } 393 394 public String getCurrentWorkingSet() { 395 return HelpBasePlugin.getDefault().getPluginPreferences().getString( 396 BaseHelpSystem.WORKING_SET); 397 } 398 399 public void setCurrentWorkingSet(String workingSet) { 400 HelpBasePlugin.getDefault().getPluginPreferences().setValue( 401 BaseHelpSystem.WORKING_SET, workingSet); 402 HelpBasePlugin.getDefault().savePluginPreferences(); 403 } 404 405 public void tocsChanged() { 406 saveState(); 407 root = null; 408 workingSets = new TreeSet (new WorkingSetComparator()); 409 restoreState(); 410 } 411 412 } 413 | Popular Tags |