1 11 package org.eclipse.help.ui.internal.views; 12 13 import java.io.*; 14 import java.util.ArrayList ; 15 import java.util.Hashtable ; 16 import java.util.Observable ; 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 implements IHelpUIConstants { 26 private ArrayList descriptors; 27 28 private EngineTypeDescriptor[] engineTypes; 29 30 private static final String USER_FILE = "userSearches.xml"; private static final String ATT_ENGINE_TYPE_ID = "engineTypeId"; 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 (); 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 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 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"); PrintWriter writer = new PrintWriter(osw); 97 writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); writer.println("<engines>"); 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>"); 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 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 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 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 loadEngineTypes(IConfigurationElement[] elements) { 163 Hashtable result = new Hashtable (); 164 ArrayList list = new ArrayList (); 165 for (int i = 0; i < elements.length; i++) { 166 IConfigurationElement element = elements[i]; 167 if (element.getName().equals("engineType")) { EngineTypeDescriptor etdesc = new EngineTypeDescriptor(element); 169 String 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 document = parser.parse(new InputSource(r)); 188 189 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 } catch (IOException e) { 199 } catch (SAXException e) { 201 } 203 } 204 205 208 public void load(String 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")); 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 id = getAttribute(node, ATT_ID); 229 String engineTypeId = getAttribute(node, ATT_ENGINE_TYPE_ID); 230 EngineTypeDescriptor etdesc = findEngineType(engineTypeId); 231 String label = getAttribute(node, ATT_LABEL); 232 String 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 computeNewId(String typeId) { 244 ArrayList used = new ArrayList (); 245 for (int i=0; i<descriptors.size(); i++) { 246 EngineDescriptor ed = (EngineDescriptor)descriptors.get(i); 247 if (!ed.isUserDefined()) continue; 248 String edTypeId = ed.getEngineTypeId(); 249 if (typeId.equals(edTypeId)) { 250 String edId = ed.getId(); 251 int loc = edId.lastIndexOf('.'); 252 if (loc!= -1) { 253 String cvalue = edId.substring(loc+1); 254 int ivalue = Integer.parseInt(cvalue); 255 used.add(new Integer (ivalue)); 256 } 257 } 258 } 259 for (int i=1; i<Integer.MAX_VALUE; i++) { 260 if (!isUsed(i, used)) { 261 return typeId+"."+"user."+i; } 263 } 264 return typeId; 265 } 266 267 private boolean isUsed(int value, ArrayList used) { 268 for (int i=0; i<used.size(); i++) { 269 Integer iv = (Integer )used.get(i); 270 if (iv.intValue()==value) 271 return true; 272 } 273 return false; 274 } 275 276 private String 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 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 indent = " "; String attIndent = indent + indent; 295 writer.print(indent); 296 writer.println("<engine "); saveAttribute(writer, attIndent, "id", desc.getId()); saveAttribute(writer, attIndent, ATT_ENGINE_TYPE_ID, desc.getEngineTypeId()); 299 saveAttribute(writer, attIndent, ATT_LABEL, desc.getLabel()); 300 writer.println(">"); saveDescription(writer, indent+indent, desc.getDescription()); 302 writer.print(indent); 303 writer.println("</engine>"); } 305 306 private String getAttribute(Node node, String 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 indent, String name, String value) { 314 if (value==null) 315 return; 316 writer.print(indent); 317 writer.print(name); 318 writer.print("=\""); writer.print(value); 320 writer.println("\""); } 322 private void saveDescription(PrintWriter writer, String indent, String desc) { 323 if (desc==null) 324 return; 325 writer.print(indent); 326 writer.println("<description>"); writer.println(desc); 328 writer.print(indent); 329 writer.println("</description>"); } 331 332 private EngineTypeDescriptor findEngineType(String 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 |