use of com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder in project gate-core by GateNLP.
the class PersistenceManager method saveObjectToFile.
/**
* Save the given object to the given file.
*
* @param obj The object to persist.
* @param file The file where to persist to
* @param usegatehome if true (recommended) use $gatehome$ and $resourceshome$ instead of
* $relpath$ in any saved path URLs if the location of that URL is inside GATE home or
* inside the resources home directory (if set).
* @param warnaboutgatehome if true, issue a warning message when a saved URL uses $gatehome$
* or $resourceshome$.
* @throws PersistenceException
* @throws IOException
*/
public static void saveObjectToFile(Object obj, File file, boolean usegatehome, boolean warnaboutgatehome) throws PersistenceException, IOException {
ProgressListener pListener = (ProgressListener) Gate.getListeners().get("gate.event.ProgressListener");
StatusListener sListener = (gate.event.StatusListener) Gate.getListeners().get("gate.event.StatusListener");
long startTime = System.currentTimeMillis();
if (pListener != null)
pListener.progressChanged(0);
// The object output stream is used for native serialization,
// but the xstream and filewriter are used for XML serialization.
ObjectOutputStream oos = null;
com.thoughtworks.xstream.XStream xstream = null;
HierarchicalStreamWriter writer = null;
warnAboutGateHome.get().addFirst(warnaboutgatehome);
useGateHome.get().addFirst(usegatehome);
startPersistingTo(file);
try {
if (Gate.getUseXMLSerialization()) {
// Just create the xstream and the filewriter that will later be
// used to serialize objects.
xstream = new XStream(new SunUnsafeReflectionProvider(new FieldDictionary(new XStream12FieldKeySorter())), new StaxDriver(new XStream11NameCoder())) {
@Override
protected boolean useXStream11XmlFriendlyMapper() {
return true;
}
};
FileWriter fileWriter = new FileWriter(file);
writer = new PrettyPrintWriter(fileWriter, new XmlFriendlyNameCoder("-", "_"));
} else {
oos = new ObjectOutputStream(new FileOutputStream(file));
}
Object persistentList = getPersistentRepresentation(Gate.getCreoleRegister().getPlugins());
Object persistentObject = getPersistentRepresentation(obj);
if (Gate.getUseXMLSerialization()) {
// We need to put the urls and the application itself together
// as xstreams can only hold one object.
GateApplication gateApplication = new GateApplication();
// gateApplication.workspace = new File("cache");
gateApplication.urlList = persistentList;
gateApplication.application = persistentObject;
// Then do the actual serialization.
xstream.marshal(gateApplication, writer);
} else {
// This is for native serialization.
oos.writeObject(persistentList);
// now write the object
oos.writeObject(persistentObject);
}
} finally {
finishedPersisting();
if (oos != null) {
oos.flush();
oos.close();
}
if (writer != null) {
// Just make sure that all the xml is written, and the file
// closed.
writer.flush();
writer.close();
}
long endTime = System.currentTimeMillis();
if (sListener != null)
sListener.statusChanged("Storing completed in " + NumberFormat.getInstance().format((double) (endTime - startTime) / 1000) + " seconds");
if (pListener != null)
pListener.processFinished();
}
}
Aggregations