3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.model
;
13 import java
.io
.BufferedReader
;
15 import java
.io
.FileInputStream
;
16 import java
.io
.FileNotFoundException
;
17 import java
.io
.FileOutputStream
;
18 import java
.io
.IOException
;
19 import java
.io
.InputStreamReader
;
20 import java
.io
.OutputStreamWriter
;
21 import java
.io
.UnsupportedEncodingException
;
23 import org
.eclipse
.ui
.IMemento
;
24 import org
.eclipse
.ui
.WorkbenchException
;
25 import org
.eclipse
.ui
.XMLMemento
;
29 * <p>MementoHelper class.</p>
32 * @created Mar 29, 2010
35 public class MementoHelper
{
38 * <p>readMementoFromFile</p>
40 * @param stateFile a {@link java.io.File} object.
41 * @return a org.eclipse.ui.IMemento object.
42 * @throws java.io.FileNotFoundException if any.
44 public static IMemento
readMementoFromFile(File stateFile
) throws FileNotFoundException
{
45 FileInputStream input
;
47 input
= new FileInputStream(stateFile
);
48 BufferedReader reader
= new BufferedReader(
49 new InputStreamReader(input
, "utf-8")); //$NON-NLS-1$
50 return XMLMemento
.createReadRoot(reader
);
51 } catch (WorkbenchException e
) {
52 MessagingUtils
.error(MementoHelper
.class, "Exception while reading the memento", e
);
53 } catch (UnsupportedEncodingException e
) {
54 MessagingUtils
.error(MementoHelper
.class, "Exception while reading the memento", e
);
61 * Save the workbench UI in a persistence file.
64 * <p>saveMementoToFile</p>
66 * @param memento a org.eclipse.ui.IMemento object.
67 * @param stateFile a {@link java.io.File} object.
68 * @return a org.eclipse.ui.IMemento object.
70 public static IMemento
saveMementoToFile(IMemento memento
, File stateFile
) {
71 if (stateFile
== null || memento
== null) {
75 FileOutputStream stream
= new FileOutputStream(stateFile
);
76 OutputStreamWriter writer
= new OutputStreamWriter(stream
, "utf-8"); //$NON-NLS-1$
78 ((XMLMemento
)memento
).save(writer
);
81 } catch (IOException e
) {
83 MessagingUtils
.error(MementoHelper
.class, "Could not save datasource state", e
);