Search in sources :

Example 26 with PrettyPrintWriter

use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project Digital by hneemann.

the class TruthTable method save.

/**
 * Writes the table to the given file.
 *
 * @param filename the file
 * @throws IOException IOException
 */
public void save(File filename) throws IOException {
    XStream xStream = getxStream();
    try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), "utf-8")) {
        out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        xStream.marshal(this, new PrettyPrintWriter(out));
    }
}
Also used : XStream(com.thoughtworks.xstream.XStream) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter)

Example 27 with PrettyPrintWriter

use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter 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)

Example 28 with PrettyPrintWriter

use of com.thoughtworks.xstream.io.xml.PrettyPrintWriter in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZigBeeNetworkStateSerializerImpl method serialize.

/**
 * Serializes the network state.
 *
 * @param networkState the network state
 */
@Override
public void serialize(final ZigBeeNetworkManager networkState) {
    XStream stream = openStream();
    final List<ZigBeeNodeDao> destinations = new ArrayList<ZigBeeNodeDao>();
    for (ZigBeeNode node : networkState.getNodes()) {
        ZigBeeNodeDao nodeDao = node.getDao();
        destinations.add(nodeDao);
    }
    final File file = new File(networkId);
    try {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        stream.marshal(destinations, new PrettyPrintWriter(writer));
        writer.flush();
        writer.close();
    } catch (IOException e) {
        logger.error("Error writing network state", e);
    }
    logger.info("ZigBee saving network state complete.");
}
Also used : ZigBeeNodeDao(com.zsmartsystems.zigbee.dao.ZigBeeNodeDao) XStream(com.thoughtworks.xstream.XStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) ZigBeeNode(com.zsmartsystems.zigbee.ZigBeeNode) OutputStreamWriter(java.io.OutputStreamWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Aggregations

PrettyPrintWriter (com.thoughtworks.xstream.io.xml.PrettyPrintWriter)28 XStream (com.thoughtworks.xstream.XStream)13 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)12 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)12 StringWriter (java.io.StringWriter)12 Metacard (ddf.catalog.data.Metacard)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 Test (org.junit.Test)8 IOException (java.io.IOException)6 Writer (java.io.Writer)6 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)5 FileOutputStream (java.io.FileOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 QuickWriter (com.thoughtworks.xstream.core.util.QuickWriter)3 NoNameCoder (com.thoughtworks.xstream.io.naming.NoNameCoder)3 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)3 XppDriver (com.thoughtworks.xstream.io.xml.XppDriver)3 FileWriter (java.io.FileWriter)3 OutputStreamWriter (java.io.OutputStreamWriter)3 ArrayList (java.util.ArrayList)3