package com.gwesm.dao;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.XMLContext;
import com.gwesm.core.GWESM;
public class CastorDAO implements DAO {
XMLContext context;
public CastorDAO() {
super();
try {
this.context = new XMLContext();
Mapping mapping = new Mapping();
mapping.loadMapping(getClass().getClassLoader().getResource(
"resources/xml/gwesm.cas"));
this.context.addMapping(mapping);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public GWESM loadData(final String name) throws DAOException {
try {
Unmarshaller unmarshaller = this.context.createUnmarshaller();
unmarshaller.setValidation(false);
GWESM loadedGwesm = (GWESM) unmarshaller.unmarshal(new FileReader(
name));
return loadedGwesm;
} catch (Exception e) {
throw new DAOException("Error loading data", e);
}
}
@Override
public void saveData(final GWESM gwesm, final String name)
throws DAOException {
try {
Marshaller marshaller = this.context.createMarshaller();
marshaller.setValidation(false);
marshaller.setProperty("org.exolab.castor.indent", "true");
marshaller.setWriter(new FileWriter(name));
marshaller.marshal(gwesm);
} catch (Exception e) {
throw new DAOException("Error saving data", e);
}
}
}