Newer
Older
package org.josast.config.databinding;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;
import java.util.logging.Logger;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
/**
*
* <b>Description : This class allows to load and sav data on a XML file. The
* castor API is used for the data binding. The default name for the file is the
* name of the class with ".xml" for the extension</b> <br>
* need a specific library : org.exolab.castor.xml <br>
* <b>JOSAST</b> : Java Open Source Amateur Satellite Toolbox <br>
* The aim of the project is to create a set of tools for amateur satellite
* purpose. All this tools could be used together to create specific software.
* <b>JOSAST</b> project is managed by AVMDTI
* (<A HREF="http://www.avmdti.org">http://www.avmdti.org </a> ) <br>
* This software is an open source software. Please read the <b><i>JOSAST
* licence</b></i><BR>
* (<A HREF="http://www.avmdti.org">http://www.avmdti.org </a> )
* for more information contact
* <a href="mailto:josast@avmdti.org">josast@avmdti.org</a>
* </p>
*
* @author <a href="mailto:mercier.josast@avmdti.org">mercier</a>
* @version 1.0
* <p>
* <b><i> Source Update </b></i>
* </p>
* <br>
* Version : date : name : comments <br>
* V1 : 3 mars 2004 : C. Mercier : create file <br>
* V2 : 01 mai 2019 : C. Mercier : Change log system
* <p>
*/
public abstract class AbstractDataBinding implements Serializable {
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
*
*/
private static final long serialVersionUID = 8410353753997330889L;
private transient String folder = ".\\";
private Logger log = Logger.getLogger("AmsatLogger");
/**
* This method read data from the file. The method return an object.
*
* @return object.
*/
public Object load() {
String fileName = folder + "\\" + this.getClass().getName() + ".xml";
Object o = null;
Reader reader = null;
File f = new File(fileName);
if (f.exists()) {
try {
reader = new FileReader(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
o = Unmarshaller.unmarshal(this.getClass(), reader);
} catch (MarshalException e) {
e.printStackTrace();
} catch (ValidationException e1) {
e1.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
log.warning("file not found");
}
return o;
}
/**
* This method save all parameters of the class in an XML file.
*/
public void save() {
String fileName = folder + "\\" + this.getClass().getName() + ".xml";
File file = new File(fileName);
Writer writer = null;
try {
writer = new FileWriter(file);
} catch (IOException e) {
e.printStackTrace();
}
try {
Marshaller.marshal(this, writer);
} catch (MarshalException | ValidationException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
log.info("file : " + file.getAbsoluteFile());
}
/**
* This method returns true if a configuration file already exist.
* @return true if the configuration file exist.
*/
public boolean isExist() {
String FileName = folder + "\\" + this.getClass().getName() + ".xml";
File f = new File(FileName);
return f.exists();
}
/**
* return the folder where the configuration file is read or save
*
* @return
*/
public String getFolder() {
return folder;
}
/**
* set the folder where the configuration file is read or save. If the
* folder do not exist, the folder is created.
*
* @param folder
* @return true if the folder exist else folder is not accessible
*/
public boolean setFolder(final String folder) {
this.folder = folder;
File f = new File(folder);
boolean value = true;
if (!f.exists()) {
value = f.mkdir();
}
return value;
}