Project

General

Profile

Download (2.4 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

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

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

    
87
		// Success !
88
		return memento;
89
	}	
90
}
(28-28/38)