2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.model
;
12 import java
.io
.BufferedReader
;
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
;
22 import org
.eclipse
.ui
.IMemento
;
23 import org
.eclipse
.ui
.WorkbenchException
;
24 import org
.eclipse
.ui
.XMLMemento
;
28 * <p>MementoHelper class.</p>
31 * @created Mar 29, 2010
34 public class MementoHelper
{
37 * <p>readMementoFromFile</p>
39 * @param stateFile a {@link java.io.File} object.
40 * @return a org.eclipse.ui.IMemento object.
41 * @throws java.io.FileNotFoundException if any.
43 public static IMemento
readMementoFromFile(File stateFile
) throws FileNotFoundException
{
44 FileInputStream input
;
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
);
60 * Save the workbench UI in a persistence file.
63 * <p>saveMementoToFile</p>
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.
69 public static IMemento
saveMementoToFile(IMemento memento
, File stateFile
) {
70 if (stateFile
== null || memento
== null) {
74 FileOutputStream stream
= new FileOutputStream(stateFile
);
75 OutputStreamWriter writer
= new OutputStreamWriter(stream
, "utf-8"); //$NON-NLS-1$
77 ((XMLMemento
)memento
).save(writer
);
80 } catch (IOException e
) {
82 MessagingUtils
.error(MementoHelper
.class, "Could not save datasource state", e
);