001: /*******************************************************************************
002: * Copyright (c) 2002, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.data;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.net.URL;
015: import java.util.ArrayList;
016: import java.util.Hashtable;
017: import java.util.Iterator;
018: import java.util.List;
019: import java.util.Map;
020: import java.util.Properties;
021:
022: import javax.xml.parsers.DocumentBuilder;
023:
024: import org.eclipse.core.runtime.IPath;
025: import org.eclipse.core.runtime.IStatus;
026: import org.eclipse.core.runtime.Path;
027: import org.eclipse.core.runtime.Status;
028: import org.eclipse.osgi.util.NLS;
029: import org.eclipse.ui.IMemento;
030: import org.eclipse.ui.XMLMemento;
031: import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
032: import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
033: import org.eclipse.ui.internal.cheatsheets.Messages;
034: import org.eclipse.ui.internal.cheatsheets.views.CheatSheetManager;
035: import org.eclipse.ui.internal.cheatsheets.views.CoreItem;
036: import org.eclipse.ui.internal.cheatsheets.views.SubItemCompositeHolder;
037: import org.eclipse.ui.internal.cheatsheets.views.ViewItem;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.NamedNodeMap;
040: import org.xml.sax.InputSource;
041:
042: public class CheatSheetSaveHelper {
043:
044: // Get the path to the cheatsheet folder in the .metadata folder of
045: // workspace.
046: protected IPath savePath;
047:
048: private static final String DOT_XML = ".xml"; //$NON-NLS-1$
049:
050: /**
051: * Constructor for CheatSheetSaveHelper.
052: */
053: public CheatSheetSaveHelper() {
054: super ();
055: savePath = CheatSheetPlugin.getPlugin().getStateLocation();
056: }
057:
058: /**
059: * Create the properties used to save the state of a cheatsheet
060: * @param currentItemNum the current item
061: * @param items a list of the items in this cheatsheet
062: * @param buttonIsDown
063: * @param expandRestoreStates
064: * @param csID the cheatsheet id
065: * @param contentPath will be null if the cheatsheet was launched using information from
066: * the registry, otherwise it is the url of the cheatsheet content file.
067: */
068: public Properties createProperties(int currentItemNum,
069: ArrayList items, boolean buttonIsDown,
070: ArrayList expandRestoreStates, String csID,
071: String contentPath) {
072: Properties props = new Properties();
073: Hashtable subcompletedTable = new Hashtable(10);
074: Hashtable subskippedTable = new Hashtable(10);
075:
076: int buttonState = 0;
077: if (buttonIsDown)
078: buttonState = 1;
079:
080: props.put(IParserTags.ID, csID);
081: props
082: .put(IParserTags.CURRENT, Integer
083: .toString(currentItemNum));
084: if (contentPath != null) {
085: props.put(IParserTags.CONTENT_URL, contentPath);
086: }
087: ArrayList completedList = new ArrayList();
088: ArrayList expandedList = new ArrayList();
089:
090: if (expandRestoreStates == null)
091: expandRestoreStates = new ArrayList();
092:
093: // Assemble lists of expanded items and completed items.
094: for (int i = 0; i < items.size(); i++) {
095: ViewItem item = (ViewItem) items.get(i);
096: if (item.isCompleted()) {
097: completedList.add(Integer.toString(i));
098: }
099: if (item.isExpanded()) {
100: expandedList.add(Integer.toString(i));
101: }
102:
103: if (item instanceof CoreItem) {
104: CoreItem withsubs = (CoreItem) item;
105: ArrayList compList = withsubs
106: .getListOfSubItemCompositeHolders();
107: if (compList != null) {
108: StringBuffer skippedsubItems = new StringBuffer();
109: StringBuffer completedsubItems = new StringBuffer();
110: for (int j = 0; j < compList.size(); j++) {
111: SubItemCompositeHolder sch = (SubItemCompositeHolder) compList
112: .get(j);
113: if (sch.isCompleted())
114: completedsubItems.append(Integer
115: .toString(j)
116: + ","); //$NON-NLS-1$
117: if (sch.isSkipped())
118: skippedsubItems.append(Integer.toString(j)
119: + ","); //$NON-NLS-1$
120: }
121: if (completedsubItems.toString().length() > 0) {
122: String csi = completedsubItems.toString();
123: if (csi.endsWith(",")) //$NON-NLS-1$
124: csi = csi.substring(0, csi.length() - 1);
125: subcompletedTable.put(Integer.toString(i), csi);
126:
127: }
128: if (skippedsubItems.toString().length() > 0) {
129: String csi = skippedsubItems.toString();
130: if (csi.endsWith(",")) //$NON-NLS-1$
131: csi = csi.substring(0, csi.length() - 1);
132: subskippedTable.put(Integer.toString(i), csi);
133: }
134: }
135: }
136: }
137:
138: // put expanded item list, completed list, button state
139: props.put(IParserTags.COMPLETED, completedList);
140: props.put(IParserTags.EXPANDED, expandedList);
141: props.put(IParserTags.EXPANDRESTORE, expandRestoreStates);
142: props.put(IParserTags.BUTTON, Integer.toString(buttonState));
143: if (subcompletedTable != null)
144: props.put(IParserTags.SUBITEMCOMPLETED, subcompletedTable);
145: if (subskippedTable != null)
146: props.put(IParserTags.SUBITEMSKIPPED, subskippedTable);
147:
148: return props;
149: }
150:
151: /**
152: * Method parses attribute from named node map. Returns value as string.
153: */
154: protected String getAttributeWithName(NamedNodeMap map, String name) {
155: try {
156: return map.getNamedItem(name).getNodeValue();
157: } catch (Exception e) {
158: return null;
159: }
160: }
161:
162: public Path getStateFile(String csID) {
163: return getStateFile(csID, savePath);
164: }
165:
166: protected Path getStateFile(String csID, IPath rootPath) {
167: return new Path(rootPath.append(csID + ".xml").toOSString()); //$NON-NLS-1$
168: }
169:
170: // Attempts to read an xml file from the provided url. Returns a Dom
171: // Document object if parses ok,
172: // returns null if the parse or read fails.
173: protected Document readXMLFile(URL url) {
174: InputStream is = null;
175: InputSource source = null;
176:
177: try {
178: is = url.openStream();
179: if (is != null) {
180: source = new InputSource(is);
181: }
182: } catch (Exception e) {
183: return null;
184: }
185:
186: if (source == null)
187: return null;
188:
189: try {
190: DocumentBuilder documentBuilder = CheatSheetPlugin
191: .getPlugin().getDocumentBuilder();
192: return documentBuilder.parse(source);
193: } catch (Exception e) {
194: } finally {
195: try {
196: if (is != null)
197: is.close();
198: } catch (IOException ioe) {
199: }
200: }
201:
202: return null;
203: }
204:
205: /**
206: * @param saveProperties
207: * @param contentPath
208: * @param csm
209: */
210: public IStatus saveState(Properties properties,
211: CheatSheetManager csm) {
212: String csID = (String) properties.get(IParserTags.ID);
213: XMLMemento writeMemento = XMLMemento
214: .createWriteRoot(IParserTags.CHEATSHEET_STATE);
215: IStatus status = saveToMemento(properties, csm, writeMemento);
216: if (!status.isOK()) {
217: return status;
218: }
219: return CheatSheetPlugin.getPlugin().saveMemento(writeMemento,
220: csID + DOT_XML);
221: }
222:
223: public IStatus saveToMemento(Properties properties,
224: CheatSheetManager csm, IMemento writeMemento) {
225:
226: String csID = (String) properties.get(IParserTags.ID);
227: try {
228: writeMemento.putString(IParserTags.BUTTONSTATE,
229: (String) properties.get(IParserTags.BUTTON));
230: writeMemento.putString(IParserTags.ITEM,
231: (String) properties.get(IParserTags.CURRENT));
232: writeMemento.putString(IParserTags.ID, (String) properties
233: .get(IParserTags.ID));
234: String contentPath = (String) properties
235: .get(IParserTags.CONTENT_URL);
236: if (contentPath != null) {
237: writeMemento.putString(IParserTags.CONTENT_URL,
238: contentPath);
239: }
240:
241: addListOfStringsToMemento(writeMemento, properties,
242: IParserTags.COMPLETED);
243: addListOfStringsToMemento(writeMemento, properties,
244: IParserTags.EXPANDED);
245: addListOfStringsToMemento(writeMemento, properties,
246: IParserTags.EXPANDRESTORE);
247:
248: addMapToMemento(writeMemento, csm.getData(),
249: IParserTags.MANAGERDATA);
250: addMapToMemento(writeMemento, (Map) properties
251: .get(IParserTags.SUBITEMCOMPLETED),
252: IParserTags.SUBITEMCOMPLETED);
253: addMapToMemento(writeMemento, (Map) properties
254: .get(IParserTags.SUBITEMSKIPPED),
255: IParserTags.SUBITEMSKIPPED);
256:
257: } catch (Exception e) {
258: String message = NLS.bind(
259: Messages.ERROR_SAVING_STATEFILE_URL,
260: (new Object[] { csID }));
261: IStatus status = new Status(IStatus.ERROR,
262: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
263: IStatus.OK, message, e);
264: return status;
265: //CheatSheetPlugin.getPlugin().getLog().log(status);
266: }
267: return Status.OK_STATUS;
268: }
269:
270: /**
271: * @param csID The id of this cheatsheet
272: * @return The state of this cheatsheet or null
273: */
274: public Properties loadState(String csID) {
275: XMLMemento readMemento = CheatSheetPlugin.getPlugin()
276: .readMemento(csID + DOT_XML);
277: if (readMemento == null) {
278: return null;
279: }
280: return loadFromMemento(readMemento);
281: }
282:
283: public Properties loadFromMemento(IMemento memento) {
284: Properties properties = new Properties();
285: properties.put(IParserTags.BUTTON, memento
286: .getString(IParserTags.BUTTONSTATE));
287: properties.put(IParserTags.CURRENT, memento
288: .getString(IParserTags.ITEM));
289: properties.put(IParserTags.ID, memento
290: .getString(IParserTags.ID));
291: String contentURL = memento.getString(IParserTags.CONTENT_URL);
292: if (contentURL != null) {
293: properties.put(IParserTags.CONTENT_URL, contentURL);
294: }
295:
296: getListOfStringsFromMemento(memento, properties,
297: IParserTags.COMPLETED);
298: getListOfStringsFromMemento(memento, properties,
299: IParserTags.EXPANDED);
300: getListOfStringsFromMemento(memento, properties,
301: IParserTags.EXPANDRESTORE);
302:
303: getMapFromMemento(memento, properties,
304: IParserTags.SUBITEMCOMPLETED);
305: getMapFromMemento(memento, properties,
306: IParserTags.SUBITEMSKIPPED);
307: getMapFromMemento(memento, properties, IParserTags.MANAGERDATA);
308: return properties;
309: }
310:
311: private void addListOfStringsToMemento(IMemento memento,
312: Properties properties, String key) {
313: List list = (List) properties.get(key);
314: if (list == null) {
315: return;
316: }
317: for (Iterator iter = list.iterator(); iter.hasNext();) {
318: IMemento childMemento = memento.createChild(key);
319: childMemento.putString(IParserTags.ITEM, (String) iter
320: .next());
321: }
322: }
323:
324: private void addMapToMemento(IMemento memento, Map map,
325: String mapName) {
326: if (map == null) {
327: return;
328: }
329: for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
330: IMemento childMemento = memento.createChild(mapName);
331: String itemKey = (String) iter.next();
332: childMemento.putString(IParserTags.MANAGERDATAKEY,
333: (itemKey));
334: childMemento.putString(IParserTags.MANAGERDATAVALUE,
335: (String) map.get(itemKey));
336: }
337: }
338:
339: private void getMapFromMemento(IMemento memento,
340: Properties properties, String mapName) {
341: IMemento[] children = memento.getChildren(mapName);
342: Map map = new Hashtable();
343: for (int i = 0; i < children.length; i++) {
344: map
345: .put(
346: children[i]
347: .getString(IParserTags.MANAGERDATAKEY),
348: children[i]
349: .getString(IParserTags.MANAGERDATAVALUE));
350: }
351: properties.put(mapName, map);
352: }
353:
354: private void getListOfStringsFromMemento(IMemento memento,
355: Properties properties, String key) {
356: IMemento[] children = memento.getChildren(key);
357: List list = new ArrayList();
358: for (int i = 0; i < children.length; i++) {
359: list.add(children[i].getString(IParserTags.ITEM));
360: }
361: properties.put(key, list);
362: }
363:
364: }
|