Project

General

Profile

Download (2.43 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
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.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.model;
12

    
13
import java.io.BufferedReader;
14
import java.io.File;
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;
22

    
23
import org.eclipse.ui.IMemento;
24
import org.eclipse.ui.WorkbenchException;
25
import org.eclipse.ui.XMLMemento;
26

    
27
import eu.etaxonomy.taxeditor.store.StoreUtil;
28

    
29
/**
30
 * <p>MementoHelper class.</p>
31
 *
32
 * @author n.hoffmann
33
 * @created Mar 29, 2010
34
 * @version 1.0
35
 */
36
public class MementoHelper {
37
	
38
	/**
39
	 * <p>readMementoFromFile</p>
40
	 *
41
	 * @param stateFile a {@link java.io.File} object.
42
	 * @return a org.eclipse.ui.IMemento object.
43
	 * @throws java.io.FileNotFoundException if any.
44
	 */
45
	public static IMemento readMementoFromFile(File stateFile) throws FileNotFoundException{
46
		FileInputStream input;
47
		try {
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);
56
		}
57

    
58
		return null;
59
	}
60
	
61
	/*
62
	 * Save the workbench UI in a persistence file.
63
	 */
64
	/**
65
	 * <p>saveMementoToFile</p>
66
	 *
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.
70
	 */
71
	public static IMemento saveMementoToFile(IMemento memento, File stateFile) {
72
		if (stateFile == null || memento == null) {
73
			return null;
74
		}
75
		try {
76
			FileOutputStream stream = new FileOutputStream(stateFile);
77
			OutputStreamWriter writer = new OutputStreamWriter(stream, "utf-8"); //$NON-NLS-1$
78
			
79
			((XMLMemento)memento).save(writer);
80
			
81
			writer.close();
82
		} catch (IOException e) {
83
			stateFile.delete();
84
			StoreUtil.error(MementoHelper.class, "Could not save datasource state", e);
85
			return null;
86
		}
87

    
88
		// Success !
89
		return memento;
90
	}	
91
}
(23-23/32)