1 11 package org.eclipse.jdt.internal.corext.util; 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.io.OutputStream ; 19 import java.util.Collection ; 20 import java.util.Hashtable ; 21 import java.util.Iterator ; 22 import java.util.LinkedHashMap ; 23 import java.util.Map ; 24 import java.util.Set ; 25 26 import javax.xml.parsers.DocumentBuilder ; 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 import javax.xml.parsers.ParserConfigurationException ; 29 import javax.xml.transform.OutputKeys ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerException ; 32 import javax.xml.transform.TransformerFactory ; 33 import javax.xml.transform.TransformerFactoryConfigurationError ; 34 import javax.xml.transform.dom.DOMSource ; 35 import javax.xml.transform.stream.StreamResult ; 36 37 import org.eclipse.core.runtime.CoreException; 38 import org.eclipse.core.runtime.IPath; 39 import org.eclipse.core.runtime.IStatus; 40 41 import org.eclipse.jdt.internal.corext.CorextMessages; 42 43 import org.eclipse.jdt.internal.ui.JavaPlugin; 44 import org.eclipse.jdt.internal.ui.JavaUIException; 45 import org.eclipse.jdt.internal.ui.JavaUIStatus; 46 47 import org.w3c.dom.Document ; 48 import org.w3c.dom.Element ; 49 import org.w3c.dom.Node ; 50 import org.w3c.dom.NodeList ; 51 import org.xml.sax.InputSource ; 52 import org.xml.sax.SAXException ; 53 54 61 public abstract class History { 62 63 private static final String DEFAULT_ROOT_NODE_NAME= "histroyRootNode"; private static final String DEFAULT_INFO_NODE_NAME= "infoNode"; private static final int MAX_HISTORY_SIZE= 60; 66 67 private static JavaUIException createException(Throwable t, String message) { 68 return new JavaUIException(JavaUIStatus.createError(IStatus.ERROR, message, t)); 69 } 70 71 private final Map fHistory; 72 private final Hashtable fPositions; 73 private final String fFileName; 74 private final String fRootNodeName; 75 private final String fInfoNodeName; 76 77 public History(String fileName, String rootNodeName, String infoNodeName) { 78 fHistory= new LinkedHashMap (80, 0.75f, true) { 79 private static final long serialVersionUID= 1L; 80 protected boolean removeEldestEntry(Map.Entry eldest) { 81 return size() > MAX_HISTORY_SIZE; 82 } 83 }; 84 fFileName= fileName; 85 fRootNodeName= rootNodeName; 86 fInfoNodeName= infoNodeName; 87 fPositions= new Hashtable (MAX_HISTORY_SIZE); 88 } 89 90 public History(String fileName) { 91 this(fileName, DEFAULT_ROOT_NODE_NAME, DEFAULT_INFO_NODE_NAME); 92 } 93 94 public synchronized void accessed(Object object) { 95 fHistory.put(getKey(object), object); 96 rebuildPositions(); 97 } 98 99 public synchronized boolean contains(Object object) { 100 return fHistory.containsKey(getKey(object)); 101 } 102 103 public synchronized boolean containsKey(Object key) { 104 return fHistory.containsKey(key); 105 } 106 107 public synchronized boolean isEmpty() { 108 return fHistory.isEmpty(); 109 } 110 111 public synchronized Object remove(Object object) { 112 Object removed= fHistory.remove(getKey(object)); 113 rebuildPositions(); 114 return removed; 115 } 116 117 public synchronized Object removeKey(Object key) { 118 Object removed= fHistory.remove(key); 119 rebuildPositions(); 120 return removed; 121 } 122 123 132 public synchronized float getNormalizedPosition(Object key) { 133 if (!containsKey(key)) 134 return 0.0f; 135 136 int pos= ((Integer )fPositions.get(key)).intValue() + 1; 137 138 return (float)pos / (float)fHistory.size(); 140 } 141 142 150 public synchronized int getPosition(Object key) { 151 if (!containsKey(key)) 152 return -1; 153 154 return ((Integer )fPositions.get(key)).intValue(); 155 } 156 157 public synchronized void load() { 158 IPath stateLocation= JavaPlugin.getDefault().getStateLocation().append(fFileName); 159 File file= new File (stateLocation.toOSString()); 160 if (file.exists()) { 161 InputStreamReader reader= null; 162 try { 163 reader = new InputStreamReader (new FileInputStream (file), "utf-8"); load(new InputSource (reader)); 165 } catch (IOException e) { 166 JavaPlugin.log(e); 167 } catch (CoreException e) { 168 JavaPlugin.log(e); 169 } finally { 170 try { 171 if (reader != null) 172 reader.close(); 173 } catch (IOException e) { 174 JavaPlugin.log(e); 175 } 176 } 177 } 178 } 179 180 public synchronized void save() { 181 IPath stateLocation= JavaPlugin.getDefault().getStateLocation().append(fFileName); 182 File file= new File (stateLocation.toOSString()); 183 OutputStream out= null; 184 try { 185 out= new FileOutputStream (file); 186 save(out); 187 } catch (IOException e) { 188 JavaPlugin.log(e); 189 } catch (CoreException e) { 190 JavaPlugin.log(e); 191 } catch (TransformerFactoryConfigurationError e) { 192 JavaPlugin.log(e); 195 } finally { 196 try { 197 if (out != null) { 198 out.close(); 199 } 200 } catch (IOException e) { 201 JavaPlugin.log(e); 202 } 203 } 204 } 205 206 protected Set getKeys() { 207 return fHistory.keySet(); 208 } 209 210 protected Collection getValues() { 211 return fHistory.values(); 212 } 213 214 220 protected abstract void setAttributes(Object object, Element element); 221 222 227 protected abstract Object createFromElement(Element element); 228 229 235 protected abstract Object getKey(Object object); 236 237 private void rebuildPositions() { 238 fPositions.clear(); 239 Collection values= fHistory.values(); 240 int pos=0; 241 for (Iterator iter= values.iterator(); iter.hasNext();) { 242 Object element= iter.next(); 243 fPositions.put(getKey(element), new Integer (pos)); 244 pos++; 245 } 246 } 247 248 private void load(InputSource inputSource) throws CoreException { 249 Element root; 250 try { 251 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 252 root = parser.parse(inputSource).getDocumentElement(); 253 } catch (SAXException e) { 254 throw createException(e, Messages.format(CorextMessages.History_error_read, fFileName)); 255 } catch (ParserConfigurationException e) { 256 throw createException(e, Messages.format(CorextMessages.History_error_read, fFileName)); 257 } catch (IOException e) { 258 throw createException(e, Messages.format(CorextMessages.History_error_read, fFileName)); 259 } 260 261 if (root == null) return; 262 if (!root.getNodeName().equalsIgnoreCase(fRootNodeName)) { 263 return; 264 } 265 NodeList list= root.getChildNodes(); 266 int length= list.getLength(); 267 for (int i= 0; i < length; ++i) { 268 Node node= list.item(i); 269 if (node.getNodeType() == Node.ELEMENT_NODE) { 270 Element type= (Element ) node; 271 if (type.getNodeName().equalsIgnoreCase(fInfoNodeName)) { 272 Object object= createFromElement(type); 273 if (object != null) { 274 fHistory.put(getKey(object), object); 275 } 276 } 277 } 278 } 279 rebuildPositions(); 280 } 281 282 private void save(OutputStream stream) throws CoreException { 283 try { 284 DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); 285 DocumentBuilder builder= factory.newDocumentBuilder(); 286 Document document= builder.newDocument(); 287 288 Element rootElement = document.createElement(fRootNodeName); 289 document.appendChild(rootElement); 290 291 Iterator values= getValues().iterator(); 292 while (values.hasNext()) { 293 Object object= values.next(); 294 Element element= document.createElement(fInfoNodeName); 295 setAttributes(object, element); 296 rootElement.appendChild(element); 297 } 298 299 Transformer transformer=TransformerFactory.newInstance().newTransformer(); 300 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource (document); 304 StreamResult result = new StreamResult (stream); 305 306 transformer.transform(source, result); 307 } catch (TransformerException e) { 308 throw createException(e, Messages.format(CorextMessages.History_error_serialize, fFileName)); 309 } catch (ParserConfigurationException e) { 310 throw createException(e, Messages.format(CorextMessages.History_error_serialize, fFileName)); 311 } 312 } 313 314 } 315 | Popular Tags |