Search in sources :

Example 1 with XStream12FieldKeySorter

use of com.thoughtworks.xstream.converters.reflection.XStream12FieldKeySorter 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();
    }
}
Also used : XStream(com.thoughtworks.xstream.XStream) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) XStream12FieldKeySorter(com.thoughtworks.xstream.converters.reflection.XStream12FieldKeySorter) FileWriter(java.io.FileWriter) XmlFriendlyNameCoder(com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder) ObjectOutputStream(java.io.ObjectOutputStream) StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) ProgressListener(gate.event.ProgressListener) FieldDictionary(com.thoughtworks.xstream.converters.reflection.FieldDictionary) XStream11NameCoder(com.thoughtworks.xstream.io.xml.XStream11NameCoder) FileOutputStream(java.io.FileOutputStream) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) StatusListener(gate.event.StatusListener) SunUnsafeReflectionProvider(com.thoughtworks.xstream.converters.reflection.SunUnsafeReflectionProvider)

Aggregations

XStream (com.thoughtworks.xstream.XStream)1 FieldDictionary (com.thoughtworks.xstream.converters.reflection.FieldDictionary)1 SunUnsafeReflectionProvider (com.thoughtworks.xstream.converters.reflection.SunUnsafeReflectionProvider)1 XStream12FieldKeySorter (com.thoughtworks.xstream.converters.reflection.XStream12FieldKeySorter)1 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)1 PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)1 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)1 XStream11NameCoder (com.thoughtworks.xstream.io.xml.XStream11NameCoder)1 XmlFriendlyNameCoder (com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder)1 ProgressListener (gate.event.ProgressListener)1 StatusListener (gate.event.StatusListener)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 ObjectOutputStream (java.io.ObjectOutputStream)1