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
;
27 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
30 * <p>MementoHelper class.</p>
33 * @created Mar 29, 2010
36 public class MementoHelper
{
39 * <p>readMementoFromFile</p>
41 * @param stateFile a {@link java.io.File} object.
42 * @return a org.eclipse.ui.IMemento object.
43 * @throws java.io.FileNotFoundException if any.
45 public static IMemento
readMementoFromFile(File stateFile
) throws FileNotFoundException
{
46 FileInputStream input
;
48 input
= new FileInputStream(stateFile
);
49 BufferedReader reader
= new BufferedReader(
50 new InputStreamReader(input
, "utf-8")); //$NON-NLS-1$
51 return XMLMemento
.createReadRoot(reader
);
52 } catch (WorkbenchException e
) {
53 StoreUtil
.error(MementoHelper
.class, "Exception while reading the memento", e
);
54 } catch (UnsupportedEncodingException e
) {
55 StoreUtil
.error(MementoHelper
.class, "Exception while reading the memento", e
);
62 * Save the workbench UI in a persistence file.
65 * <p>saveMementoToFile</p>
67 * @param memento a org.eclipse.ui.IMemento object.
68 * @param stateFile a {@link java.io.File} object.
69 * @return a org.eclipse.ui.IMemento object.
71 public static IMemento
saveMementoToFile(IMemento memento
, File stateFile
) {
72 if (stateFile
== null || memento
== null) {
76 FileOutputStream stream
= new FileOutputStream(stateFile
);
77 OutputStreamWriter writer
= new OutputStreamWriter(stream
, "utf-8"); //$NON-NLS-1$
79 ((XMLMemento
)memento
).save(writer
);
82 } catch (IOException e
) {
84 StoreUtil
.error(MementoHelper
.class, "Could not save datasource state", e
);