Merge branch 'develop' into taxonDescription
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / model / MementoHelper.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.model;
11
12 import java.io.BufferedReader;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.io.OutputStreamWriter;
20 import java.io.UnsupportedEncodingException;
21
22 import org.eclipse.ui.IMemento;
23 import org.eclipse.ui.WorkbenchException;
24 import org.eclipse.ui.XMLMemento;
25
26
27 /**
28 * <p>MementoHelper class.</p>
29 *
30 * @author n.hoffmann
31 * @created Mar 29, 2010
32 * @version 1.0
33 */
34 public class MementoHelper {
35
36 /**
37 * <p>readMementoFromFile</p>
38 *
39 * @param stateFile a {@link java.io.File} object.
40 * @return a org.eclipse.ui.IMemento object.
41 * @throws java.io.FileNotFoundException if any.
42 */
43 public static IMemento readMementoFromFile(File stateFile) throws FileNotFoundException{
44 FileInputStream input;
45 try {
46 input = new FileInputStream(stateFile);
47 BufferedReader reader = new BufferedReader(
48 new InputStreamReader(input, "utf-8")); //$NON-NLS-1$
49 return XMLMemento.createReadRoot(reader);
50 } catch (WorkbenchException e) {
51 MessagingUtils.error(MementoHelper.class, "Exception while reading the memento", e);
52 } catch (UnsupportedEncodingException e) {
53 MessagingUtils.error(MementoHelper.class, "Exception while reading the memento", e);
54 }
55
56 return null;
57 }
58
59 /*
60 * Save the workbench UI in a persistence file.
61 */
62 /**
63 * <p>saveMementoToFile</p>
64 *
65 * @param memento a org.eclipse.ui.IMemento object.
66 * @param stateFile a {@link java.io.File} object.
67 * @return a org.eclipse.ui.IMemento object.
68 */
69 public static IMemento saveMementoToFile(IMemento memento, File stateFile) {
70 if (stateFile == null || memento == null) {
71 return null;
72 }
73 try {
74 FileOutputStream stream = new FileOutputStream(stateFile);
75 OutputStreamWriter writer = new OutputStreamWriter(stream, "utf-8"); //$NON-NLS-1$
76
77 ((XMLMemento)memento).save(writer);
78
79 writer.close();
80 } catch (IOException e) {
81 stateFile.delete();
82 MessagingUtils.error(MementoHelper.class, "Could not save datasource state", e);
83 return null;
84 }
85
86 // Success !
87 return memento;
88 }
89 }