Skip to content
AbstractDataBinding.java 4.77 KiB
Newer Older
xtof's avatar
xtof committed
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;

/**
xtof's avatar
xtof committed
 *
 * <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> )
xtof's avatar
xtof committed
 * <p>
xtof's avatar
xtof committed
 * 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>
xtof's avatar
xtof committed
 */
public abstract class AbstractDataBinding implements Serializable {
xtof's avatar
xtof committed

    /**
     *
     */
    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;
    }