1 11 package org.eclipse.ui.internal.cheatsheets.data; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 import java.util.Hashtable ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.Path; 28 import org.eclipse.core.runtime.Status; 29 import org.eclipse.osgi.util.NLS; 30 import org.eclipse.ui.IMemento; 31 import org.eclipse.ui.XMLMemento; 32 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin; 33 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource; 34 import org.eclipse.ui.internal.cheatsheets.Messages; 35 import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager; 36 import org.eclipse.ui.internal.cheatsheets.views.CoreItem; 37 import org.eclipse.ui.internal.cheatsheets.views.SubItemCompositeHolder; 38 import org.eclipse.ui.internal.cheatsheets.views.ViewItem; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.NamedNodeMap ; 41 import org.xml.sax.InputSource ; 42 43 public class CheatSheetSaveHelper { 44 45 protected IPath savePath; 48 49 private static final String DOT_XML = ".xml"; 51 54 public CheatSheetSaveHelper() { 55 super(); 56 savePath = CheatSheetPlugin.getPlugin().getStateLocation(); 57 } 58 59 60 70 public Properties createProperties(int currentItemNum, ArrayList items, 71 boolean buttonIsDown, ArrayList expandRestoreStates, String csID, String contentPath) { 72 Properties props = new Properties (); 73 Hashtable subcompletedTable = new Hashtable (10); 74 Hashtable subskippedTable = new Hashtable (10); 75 76 int buttonState = 0; 77 if (buttonIsDown) 78 buttonState = 1; 79 80 props.put(IParserTags.ID, csID); 81 props.put(IParserTags.CURRENT, Integer.toString(currentItemNum)); 82 if (contentPath != null) { 83 props.put(IParserTags.CONTENT_URL, contentPath); 84 } 85 ArrayList completedList = new ArrayList (); 86 ArrayList expandedList = new ArrayList (); 87 88 if (expandRestoreStates == null) 89 expandRestoreStates = new ArrayList (); 90 91 for (int i = 0; i < items.size(); i++) { 93 ViewItem item = (ViewItem) items.get(i); 94 if (item.isCompleted()) { 95 completedList.add(Integer.toString(i)); 96 } 97 if (item.isExpanded()) { 98 expandedList.add(Integer.toString(i)); 99 } 100 101 if (item instanceof CoreItem) { 102 CoreItem withsubs = (CoreItem) item; 103 ArrayList compList = withsubs 104 .getListOfSubItemCompositeHolders(); 105 if (compList != null) { 106 StringBuffer skippedsubItems = new StringBuffer (); 107 StringBuffer completedsubItems = new StringBuffer (); 108 for (int j = 0; j < compList.size(); j++) { 109 SubItemCompositeHolder sch = (SubItemCompositeHolder) compList 110 .get(j); 111 if (sch.isCompleted()) 112 completedsubItems.append(Integer.toString(j) + ","); if (sch.isSkipped()) 114 skippedsubItems.append(Integer.toString(j) + ","); } 116 if (completedsubItems.toString().length() > 0) { 117 String csi = completedsubItems.toString(); 118 if (csi.endsWith(",")) csi = csi.substring(0, csi.length() - 1); 120 subcompletedTable.put(Integer.toString(i), csi); 121 122 } 123 if (skippedsubItems.toString().length() > 0) { 124 String csi = skippedsubItems.toString(); 125 if (csi.endsWith(",")) csi = csi.substring(0, csi.length() - 1); 127 subskippedTable.put(Integer.toString(i), csi); 128 } 129 } 130 } 131 } 132 133 props.put(IParserTags.COMPLETED, completedList); 135 props.put(IParserTags.EXPANDED, expandedList); 136 props.put(IParserTags.EXPANDRESTORE, expandRestoreStates); 137 props.put(IParserTags.BUTTON, Integer.toString(buttonState)); 138 if (subcompletedTable != null) 139 props.put(IParserTags.SUBITEMCOMPLETED, subcompletedTable); 140 if (subskippedTable != null) 141 props.put(IParserTags.SUBITEMSKIPPED, subskippedTable); 142 143 return props; 144 } 145 146 149 protected String getAttributeWithName(NamedNodeMap map, String name) { 150 try { 151 return map.getNamedItem(name).getNodeValue(); 152 } catch (Exception e) { 153 return null; 154 } 155 } 156 157 public Path getStateFile(String csID) { 158 return getStateFile(csID, savePath); 159 } 160 161 protected Path getStateFile(String csID, IPath rootPath) { 162 return new Path(rootPath.append(csID + ".xml").toOSString()); } 164 165 protected Document readXMLFile(URL url) { 169 InputStream is = null; 170 InputSource source = null; 171 172 try { 173 is = url.openStream(); 174 if (is != null) { 175 source = new InputSource (is); 176 } 177 } catch (Exception e) { 178 return null; 179 } 180 181 if (source == null) 182 return null; 183 184 try { 185 DocumentBuilder documentBuilder = CheatSheetPlugin.getPlugin() 186 .getDocumentBuilder(); 187 return documentBuilder.parse(source); 188 } catch (Exception e) { 189 } finally { 190 try { 191 if (is != null) 192 is.close(); 193 } catch (IOException ioe) { 194 } 195 } 196 197 return null; 198 } 199 200 205 public IStatus saveState(Properties properties, CheatSheetManager csm) { 206 String csID = (String ) properties.get(IParserTags.ID); 207 XMLMemento writeMemento = XMLMemento.createWriteRoot(IParserTags.CHEATSHEET_STATE); 208 IStatus status = saveToMemento(properties, csm, writeMemento); 209 if (!status.isOK()) { 210 return status; 211 } 212 return CheatSheetPlugin.getPlugin().saveMemento(writeMemento, csID + DOT_XML); 213 } 214 215 public IStatus saveToMemento(Properties properties, CheatSheetManager csm, IMemento writeMemento) { 216 217 String csID = (String ) properties.get(IParserTags.ID); 218 try { 219 writeMemento.putString(IParserTags.BUTTONSTATE, (String ) properties 220 .get(IParserTags.BUTTON)); 221 writeMemento.putString(IParserTags.ITEM, (String ) properties.get(IParserTags.CURRENT)); 222 writeMemento.putString(IParserTags.ID, (String )properties.get(IParserTags.ID)); 223 String contentPath = (String )properties.get(IParserTags.CONTENT_URL); 224 if (contentPath != null) { 225 writeMemento.putString(IParserTags.CONTENT_URL, contentPath); 226 } 227 228 addListOfStringsToMemento(writeMemento, properties, IParserTags.COMPLETED); 229 addListOfStringsToMemento(writeMemento, properties, IParserTags.EXPANDED); 230 addListOfStringsToMemento(writeMemento, properties, IParserTags.EXPANDRESTORE); 231 232 addMapToMemento(writeMemento, csm.getData(), IParserTags.MANAGERDATA); 233 addMapToMemento(writeMemento, (Map )properties.get(IParserTags.SUBITEMCOMPLETED), IParserTags.SUBITEMCOMPLETED); 234 addMapToMemento(writeMemento, (Map )properties.get(IParserTags.SUBITEMSKIPPED), IParserTags.SUBITEMSKIPPED); 235 236 } catch (Exception e) { 237 String message = NLS.bind(Messages.ERROR_SAVING_STATEFILE_URL, 238 (new Object [] { csID })); 239 IStatus status = new Status(IStatus.ERROR, 240 ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, 241 message, e); 242 return status; 243 } 245 return Status.OK_STATUS; 246 } 247 248 252 public Properties loadState(String csID) { 253 XMLMemento readMemento = CheatSheetPlugin.getPlugin().readMemento(csID + DOT_XML); 254 if (readMemento == null) { 255 return null; 256 } 257 return loadFromMemento(readMemento); 258 } 259 260 public Properties loadFromMemento(IMemento memento) { 261 Properties properties = new Properties (); 262 properties.put(IParserTags.BUTTON, memento.getString(IParserTags.BUTTONSTATE)); 263 properties.put(IParserTags.CURRENT, memento.getString(IParserTags.ITEM)); 264 properties.put(IParserTags.ID, memento.getString(IParserTags.ID)); 265 String contentURL = memento.getString(IParserTags.CONTENT_URL); 266 if (contentURL != null) { 267 properties.put(IParserTags.CONTENT_URL, contentURL); 268 } 269 270 getListOfStringsFromMemento(memento, properties, IParserTags.COMPLETED); 271 getListOfStringsFromMemento(memento, properties, IParserTags.EXPANDED); 272 getListOfStringsFromMemento(memento, properties, IParserTags.EXPANDRESTORE); 273 274 getMapFromMemento(memento, properties, IParserTags.SUBITEMCOMPLETED); 275 getMapFromMemento(memento, properties, IParserTags.SUBITEMSKIPPED); 276 getMapFromMemento(memento, properties, IParserTags.MANAGERDATA); 277 return properties; 278 } 279 280 private void addListOfStringsToMemento(IMemento memento, Properties properties, String key) { 281 List list = (List )properties.get(key); 282 if (list == null) { 283 return; 284 } 285 for (Iterator iter = list.iterator(); iter.hasNext();) { 286 IMemento childMemento = memento.createChild(key); 287 childMemento.putString(IParserTags.ITEM,(String )iter.next()); 288 } 289 } 290 291 292 private void addMapToMemento(IMemento memento, Map map, String mapName) { 293 if (map == null) { 294 return; 295 } 296 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { 297 IMemento childMemento = memento.createChild(mapName); 298 String itemKey = (String )iter.next(); 299 childMemento.putString(IParserTags.MANAGERDATAKEY,(itemKey)); 300 childMemento.putString(IParserTags.MANAGERDATAVALUE,(String )map.get(itemKey)); 301 } 302 } 303 304 305 private void getMapFromMemento(IMemento memento, Properties properties, String mapName) { 306 IMemento[] children = memento.getChildren(mapName); 307 Map map = new Hashtable (); 308 for (int i = 0; i < children.length; i++) { 309 map.put(children[i].getString(IParserTags.MANAGERDATAKEY), 310 children[i].getString(IParserTags.MANAGERDATAVALUE)); 311 } 312 properties.put(mapName, map); 313 } 314 315 private void getListOfStringsFromMemento(IMemento memento, Properties properties, String key) { 316 IMemento[] children = memento.getChildren(key); 317 List list = new ArrayList (); 318 for (int i = 0; i < children.length; i++) { 319 list.add(children[i].getString(IParserTags.ITEM)); 320 } 321 properties.put(key, list); 322 } 323 324 } 325 | Popular Tags |