Java Code Examples for com.thoughtworks.xstream.XStream#marshal()

The following examples show how to use com.thoughtworks.xstream.XStream#marshal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZigBeeDataStore.java    From com.zsmartsystems.zigbee with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void writeNode(ZigBeeNodeDao node) {
    XStream stream = openStream();
    File file = getFile(node.getIeeeAddress());

    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"))) {
        stream.marshal(node, new PrettyPrintWriter(writer));
        logger.info("{}: ZigBee saving network state complete.", node.getIeeeAddress());
    } catch (Exception e) {
        logger.error("{}: Error writing network state: ", node.getIeeeAddress(), e);
    }
}
 
Example 2
Source File: Circuit.java    From Digital with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Stores the circuit in the given file
 *
 * @param out the writer
 * @throws IOException IOException
 */
public void save(OutputStream out) throws IOException {
    try (Writer w = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
        XStream xStream = Circuit.getxStream();
        w.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        xStream.marshal(this, new PrettyPrintWriter(w));
    }
}
 
Example 3
Source File: TruthTable.java    From Digital with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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), StandardCharsets.UTF_8)) {
        out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        xStream.marshal(this, new PrettyPrintWriter(out));
    }
}
 
Example 4
Source File: FSM.java    From Digital with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Stores the circuit in the given file
 *
 * @param out the writer
 * @throws IOException IOException
 */
public void save(OutputStream out) throws IOException {
    try (Writer w = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
        XStream xStream = getxStream();
        w.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        xStream.marshal(this, new PrettyPrintWriter(w));
        modified = false;
        if (modifiedListener != null)
            modifiedListener.modifiedChanged(modified);
    }
}
 
Example 5
Source File: SettingsBase.java    From Digital with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void attributeChanged() {
    XStream xStream = Circuit.getxStream();
    try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8)) {
        out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        xStream.marshal(attributes, new PrettyPrintWriter(out));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: CircuitTransferable.java    From Digital with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new instance
 *
 * @param data the data to copy
 */
CircuitTransferable(ArrayList<Movable> data) {
    XStream xStream = Circuit.getxStream();
    try (StringWriter out = new StringWriter()) {
        out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        xStream.marshal(data, new PrettyPrintWriter(out));
        this.data = out.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: Resources.java    From Digital with GNU General Public License v3.0 5 votes vote down vote up
void save(OutputStream out) throws IOException {
    XStream xStream = getxStream();
    try (Writer w = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
        w.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
        xStream.marshal(resourceMap, new PrettyPrintWriter(w));
    }
}
 
Example 8
Source File: TransportFormat.java    From javamelody with Apache License 2.0 5 votes vote down vote up
static void writeToXml(Serializable serializable, BufferedOutputStream bufferedOutput)
		throws IOException {
	final XStream xstream = createXStream(false);
	// on wrappe avec un CompactWriter pour gagner 25% en taille de flux (retours chariots)
	// et donc un peu en performances
	final CompactWriter writer = new CompactWriter(
			new OutputStreamWriter(bufferedOutput, XML_CHARSET_NAME));
	try {
		xstream.marshal(serializable, writer);
	} finally {
		writer.close();
	}
}
 
Example 9
Source File: XMLConfigProvider.java    From audit4j-core with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * 
 */
@Override
public void generateConfig(T config, String filePath) throws ConfigurationException {
    XStream xstream = new XStream(new StaxDriver());
    xstream.alias("configuration", clazz);
    BufferedOutputStream stdout = null;
    try {
        stdout = new BufferedOutputStream(new FileOutputStream(filePath));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    xstream.marshal(config, new PrettyPrintWriter(new OutputStreamWriter(stdout)));
}
 
Example 10
Source File: XStreamXmlProvider.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
public void writeTo(Object o, Class<?> aClass, Type type, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> map, OutputStream stream)
        throws IOException, WebApplicationException {
    String encoding = getCharsetAsString(mediaType);
    XStream xStream = getXStream(o.getClass());
    xStream.marshal(o, new CompactWriter(new OutputStreamWriter(stream, encoding)));
}
 
Example 11
Source File: HibernateXmlConverter.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param writer
 * @param includeHistory
 * @param session
 * @throws DataAccessException
 * @throws HibernateException
 */
private void writeObjects(final Writer writer, final boolean includeHistory, final Session session, final boolean preserveIds)
    throws DataAccessException, HibernateException
    {
  // Container für die Objekte
  final List<Object> all = new ArrayList<Object>();
  final XStream stream = initXStream(session, true);
  final XStream defaultXStream = initXStream(session, false);

  session.flush();
  // Alles laden
  final List<Class< ? >> entities = new ArrayList<Class< ? >>();
  entities.addAll(HibernateEntities.instance().getOrderedEntities());
  entities.addAll(HibernateEntities.instance().getOrderedHistoryEntities());
  for (final Class< ? > entityClass : entities) {
    final String entitySimpleName = entityClass.getSimpleName();
    final String entityType = entityClass.getName();
    if (includeHistory == false && entityType.startsWith("de.micromata.hibernate.history.") == true) {
      // Skip history entries.
      continue;
    }
    List< ? > list = session.createQuery("select o from " + entityType + " o").setReadOnly(true).list();
    list = (List< ? >) CollectionUtils.select(list, PredicateUtils.uniquePredicate());
    final int size = list.size();
    log.info("Writing " + size + " objects");
    for (final Iterator< ? > it = list.iterator(); it.hasNext();) {
      final Object obj = it.next();
      if (log.isDebugEnabled()) {
        log.debug("loaded object " + obj);
      }
      Hibernate.initialize(obj);
      final Class< ? > targetClass = HibernateProxyHelper.getClassWithoutInitializingProxy(obj);
      final ClassMetadata classMetadata = session.getSessionFactory().getClassMetadata(targetClass);
      if (classMetadata == null) {
        log.fatal("Can't init " + obj + " of type " + targetClass);
        continue;
      }
      // initalisierung des Objekts...
      defaultXStream.marshal(obj, new CompactWriter(new NullWriter()));

      if (preserveIds == false) {
        // Nun kann die ID gelöscht werden
        classMetadata.setIdentifier(obj, null, EntityMode.POJO);
      }
      if (log.isDebugEnabled()) {
        log.debug("loading evicted object " + obj);
      }
      if (this.ignoreFromTopLevelListing.contains(targetClass) == false) {
        all.add(obj);
      }
    }
  }
  // und schreiben
  try {
    writer.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
  } catch (final IOException ex) {
    // ignore, will fail on stream.marshal()
  }
  log.info("Wrote " + all.size() + " objects");
  final MarshallingStrategy marshallingStrategy = new ProxyIdRefMarshallingStrategy();
  stream.setMarshallingStrategy(marshallingStrategy);
  stream.marshal(all, new PrettyPrintWriter(writer));
    }