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

The following examples show how to use com.thoughtworks.xstream.XStream#toXML() . These examples are extracted from open source projects. 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 Project: groovy   File: XStreamUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void serialize(final String name, final Object ast) {
    if (name == null || name.length() == 0) return;

    XStream xstream = new XStream(new StaxDriver());
    FileWriter astFileWriter = null;
    try {
        File astFile = astFile(name);
        if (astFile == null) {
            System.out.println("File-name for writing " + name + " AST could not be determined!");
            return;
        }
        astFileWriter = new FileWriter(astFile, false);
        xstream.toXML(ast, astFileWriter);
        System.out.println("Written AST to " + name + ".xml");

    } catch (Exception e) {
        System.out.println("Couldn't write to " + name + ".xml");
        e.printStackTrace();
    } finally {
        DefaultGroovyMethods.closeQuietly(astFileWriter);
    }
}
 
Example 2
Source Project: olat   File: EPSettingsManager.java    License: Apache License 2.0 6 votes vote down vote up
public void setSavedFilterSettings(final Identity ident, final List<EPFilterSettings> filterList) {
    final PropertyManager pm = PropertyManager.getInstance();
    PropertyImpl p = pm.findProperty(ident, null, null, EPORTFOLIO_CATEGORY, EPORTFOLIO_FILTER_SETTINGS);
    if (p == null) {
        p = pm.createUserPropertyInstance(ident, EPORTFOLIO_CATEGORY, EPORTFOLIO_FILTER_SETTINGS, null, null, null, null);
    }
    // don't persist filters without a name
    for (final Iterator<EPFilterSettings> iterator = filterList.iterator(); iterator.hasNext();) {
        final EPFilterSettings epFilterSettings = iterator.next();
        if (!StringHelper.containsNonWhitespace(epFilterSettings.getFilterName())) {
            iterator.remove();
        }
    }
    final XStream xStream = XStreamHelper.createXStreamInstance();
    xStream.aliasType(EP_FILTER_SETTINGS, EPFilterSettings.class);
    final String filterListXML = xStream.toXML(filterList);
    p.setTextValue(filterListXML);
    pm.saveProperty(p);
}
 
Example 3
Source Project: jease   File: XStreamFile.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Writes objects to XML-File.
 */
public void write(Collection<Object> objects) {
	try {
		XStream xstream = new XStream(new DomDriver());
		xstream.setMode(XStream.ID_REFERENCES);
		FileWriter writer = new FileWriter(filename);
		xstream.toXML(new ArrayList<>(objects), writer);
		writer.close();
	} catch (IOException e) {
		throw new RuntimeException(e.getMessage(), e);
	}
}
 
Example 4
public String toXML() {
  XStream xstream = XStreamInitializer.getInstance();
  //涉及到服务商模式的两个参数,在为空值时置为null,以免在请求时将空值传给微信服务器
  this.setSubAppId(StringUtils.trimToNull(this.getSubAppId()));
  this.setSubMchId(StringUtils.trimToNull(this.getSubMchId()));
  xstream.processAnnotations(this.getClass());
  return xstream.toXML(this);
}
 
Example 5
@Test
public void replace() {
  /* Mocks */
  Converter c1 = EasyMock.createMock(Converter.class);
  expect(c1.canConvert(Dummy.class)).andStubReturn(true);

  Converter c2 = EasyMock.createMock(Converter.class);
  expect(c2.canConvert(Dummy.class)).andStubReturn(true);

  c2.marshal(
      isA(Object.class), isA(HierarchicalStreamWriter.class), isA(MarshallingContext.class));
  EasyMock.expectLastCall().once();

  EasyMock.replay(c1, c2);

  /* XStream config */
  XStream xstream = XStreamFactory.getSecureXStream(new DomDriver());
  ReplaceableConverter resetable = new ReplaceableConverter(c1);
  xstream.registerConverter(resetable);

  /* Test it */
  resetable.reset();
  assertTrue("ReplaceableConverter was not properly reset", resetable.isReset());

  /*
   * This call should not reach any of the converters.
   */
  xstream.toXML(new Dummy());

  resetable.replace(c2);
  assertNotNull("Converter was not reactivated", xstream.toXML(new Dummy()));

  /*
   * Verify that the first converter is not called, and the second exactly
   * once
   */
  EasyMock.verify(c1, c2);
}
 
Example 6
@Test
public void reset() {
  /* Mocks */
  Converter c1 = EasyMock.createMock(Converter.class);
  expect(c1.canConvert(Dummy.class)).andStubReturn(true);

  c1.marshal(
      isA(Object.class), isA(HierarchicalStreamWriter.class), isA(MarshallingContext.class));
  EasyMock.expectLastCall().once();

  EasyMock.replay(c1);

  /* XStream config */
  XStream xstream = XStreamFactory.getSecureXStream(new DomDriver());
  ReplaceableConverter resetable = new ReplaceableConverter(c1);
  xstream.registerConverter(resetable);

  /* Test it */
  assertFalse("ReplaceableConverter was not properly set up", resetable.isReset());

  assertNotNull("Converter cannot convert", xstream.toXML(new Dummy()));

  resetable.reset();
  assertTrue("ReplaceableConverter was not properly reset", resetable.isReset());

  /*
   * This call should not reach the converter.
   */
  xstream.toXML(new Dummy());

  /*
   * Verify that the converter was used exactly once, i.e. it was not
   * called while it was inactive.
   */
  EasyMock.verify(c1);
}
 
Example 7
public void testMarshal() throws Exception {
    DataField d = new DataField(1000);
    for (int i = 0; i < 10; i++)
        d.setData(i, i);

    XStream xStream = getxStream();

    String xml = xStream.toXML(d);
    assertEquals("<dataField>0,1,2,3,4,5,6,7,8,9</dataField>", xml);
}
 
Example 8
@Override
   public void close() {
	try {
		XStream xstream = new XStream(new DomDriver());
		xstream.setMode(XStream.ID_REFERENCES);
		FileWriter writer = new FileWriter(filename);
		xstream.toXML(objects, writer);
		writer.close();
	} catch (Exception e) {
		throw new RuntimeException(e.getMessage(), e);
	}
}
 
Example 9
Source Project: olat   File: XStreamHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Write a an object to an output stream. UTF-8 is used as encoding in the XML declaration. It is useful to set attribute and field mappers to allow later refactoring
 * of the class!
 * 
 * @param xStream
 *            The (configured) xStream.
 * @param os
 * @param obj
 *            the object to be serialized
 */
public static void writeObject(XStream xStream, OutputStream os, Object obj) {
    try {
        OutputStreamWriter osw = new OutputStreamWriter(os, ENCODING);
        String data = xStream.toXML(obj);
        data = "<?xml version=\"1.0\" encoding=\"" + ENCODING + "\"?>\n" + data; // give a decent header with the encoding used
        osw.write(data);
        osw.close();
    } catch (Exception e) {
        throw new OLATRuntimeException(XStreamHelper.class, "Could not write object to stream.", e);
    } finally {
        FileUtils.closeSafely(os);
    }
}
 
Example 10
Source Project: EasyEE   File: XmlUtil.java    License: MIT License 5 votes vote down vote up
/**
 * java bean 转化为xml
 * 
 * @param object
 * @param objClass
 * @return
 */
public static String parseBeanToXml(Object object, Class<?> objClass) {
	String xmlString = "";
	XStream xStream = new XStream(new DomDriver());
	xStream.processAnnotations(objClass);
	xmlString = xStream.toXML(object);
	xmlString = header + xmlString;
	return xmlString;
}
 
Example 11
/**
 * Produces an XML serialisation of this {@link ObjectWrapper} instance, that
 * can be deserialised by using the {@link #ObjectWrapper(String)} constructor.
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
  // Use XML 1.1 in case the XML representation includes characters
  // that 1.0 forbids (typically control characters).
  XStream xstream = new XStream(new XML11StaxDriver());
  StringWriter out  = new StringWriter();
  xstream.toXML(this, out);
  return out.toString();
}
 
Example 12
Source Project: olat   File: EPSettingsManager.java    License: Apache License 2.0 5 votes vote down vote up
public void setArtefactAttributeConfig(final Identity ident, final Map<String, Boolean> artAttribConfig) {
    final PropertyManager pm = PropertyManager.getInstance();
    PropertyImpl p = pm.findProperty(ident, null, null, EPORTFOLIO_CATEGORY, EPORTFOLIO_ARTEFACTS_ATTRIBUTES);
    if (p == null) {
        p = pm.createUserPropertyInstance(ident, EPORTFOLIO_CATEGORY, EPORTFOLIO_ARTEFACTS_ATTRIBUTES, null, null, null, null);
    }
    final XStream xStream = XStreamHelper.createXStreamInstance();
    final String artAttribXML = xStream.toXML(artAttribConfig);
    p.setTextValue(artAttribXML);
    pm.saveProperty(p);
}
 
Example 13
public String serializeWorkspace(Schema schema) {   
  // update the checksum
  connectionModel.getSelectedSchemaModel().setSchemaChecksum(
      connectionModel.getSelectedSchemaModel().recalculateSchemaChecksum());
  
  List<Object> systemObjects = new ArrayList<Object>();
  AggList aggList = getAggList(); 
  XStream xstream = getXStream(schema);
  systemObjects.add(SERIALIZATION_VERSION);
  systemObjects.add(connectionModel.getDatabaseMeta());
  systemObjects.add(connectionModel.getSelectedSchemaModel());
  systemObjects.add(aggList);
  return xstream.toXML(systemObjects);
}
 
Example 14
Source Project: weChatRobot   File: MessageUtil.java    License: MIT License 5 votes vote down vote up
/**
 * 对象转换成xml字符串
 */
public static <T> String ObjectToXml(T t) {
    if (t == null) {
        return StringUtils.EMPTY;
    }
    XStream xStream = XmlUtil.getXstream();
    xStream.alias("xml", t.getClass());
    if (t instanceof NewsMessage) {
        xStream.alias("item", Article.class);
    }
    return xStream.toXML(t);
}
 
Example 15
Source Project: ZenQuery   File: ResultSetController.java    License: Apache License 2.0 5 votes vote down vote up
@RequestMapping(
        value = "/{id}",
        method = RequestMethod.GET,
        produces = { "application/xml; charset=utf-8" })
public @ResponseBody
String currentQueryAsXML(
        @PathVariable Integer id
) {
    List<Map<String, Object>> rows = getRows(id, null, null);

    XStream stream = getXMLStream();

    return stream.toXML(rows.toArray());
}
 
Example 16
public static String success(String msg) {
  WxPayNotifyResponse response = new WxPayNotifyResponse(SUCCESS, msg);
  XStream xstream = XStreamInitializer.getInstance();
  xstream.autodetectAnnotations(true);
  return xstream.toXML(response);
}
 
Example 17
Source Project: wechat-core   File: MessageUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static String toXml(Object message) {
    XStream xstream = newXStreamInstance();
    xstream.processAnnotations(message.getClass());
    return xstream.toXML(message);
}
 
Example 18
public String serializeDomain( Domain domain ) {

    XStream xstream = new XStream( new DomDriver() ); //$NON-NLS-1$
    return xstream.toXML( domain );
  }
 
Example 19
Source Project: ET_Redux   File: SesarSample.java    License: Apache License 2.0 3 votes vote down vote up
public String serializeForUploadToSesar() {
    XStream xstream = getXStreamWriter();

    String xml = xstream.toXML(this);
    // fix double underscores
    xml = xml.replace("__", "_");

    xml = "<samples>" + xml + "</samples>";

    System.out.println(xml);

    return xml;
}
 
Example 20
Source Project: wechat-core   File: MessageUtils.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 图片消息转xml
 *
 * @param imageMessage
 * @return xml字符串
 */
public static String imageMessageToXml(ImageResponseMessage imageMessage) {
    XStream xstream = newXStreamInstance();
    xstream.processAnnotations(imageMessage.getClass());
    return xstream.toXML(imageMessage);
}