Refine search
public static boolean toXML(Object object, File file) { XStream xStream = new XStream(); OutputStream outputStream = null; Writer writer = null; try { outputStream = new FileOutputStream(file); writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8")); xStream.toXML(object, writer); } catch (Exception exp) { log.error(null, exp); return false; } finally { close(writer); close(outputStream); } return true; }
map.put("island","faranga"); XStream magicApi = new XStream(); magicApi.registerConverter(new MapEntryConverter()); magicApi.alias("root", Map.class); String xml = magicApi.toXML(map); System.out.println("Result of tweaked XStream toXml()"); System.out.println(xml); Map<String, String> extractedMap = (Map<String, String>) magicApi.fromXML(xml); assert extractedMap.get("name").equals("chris"); assert extractedMap.get("island").equals("faranga");
/** * Persists a list of installing plugins; this is used in the case Jenkins fails mid-installation and needs to be restarted * @param installingPlugins */ public static synchronized void persistInstallStatus(List<UpdateCenterJob> installingPlugins) { File installingPluginsFile = getInstallingPluginsFile(); if(installingPlugins == null || installingPlugins.isEmpty()) { installingPluginsFile.delete(); return; } LOGGER.fine("Writing install state to: " + installingPluginsFile.getAbsolutePath()); Map<String,String> statuses = new HashMap<String,String>(); for(UpdateCenterJob j : installingPlugins) { if(j instanceof InstallationJob && j.getCorrelationId() != null) { // only include install jobs with a correlation id (directly selected) InstallationJob ij = (InstallationJob)j; InstallationStatus status = ij.status; String statusText = status.getType(); if(status instanceof Installing) { // flag currently installing plugins as pending statusText = "Pending"; } statuses.put(ij.plugin.name, statusText); } } try { String installingPluginXml = new XStream().toXML(statuses); FileUtils.write(installingPluginsFile, installingPluginXml); } catch (IOException e) { LOGGER.log(SEVERE, "Failed to save " + installingPluginsFile.getAbsolutePath(), e); } }
public static <T> T serializeAndDeserializeWithXStream(T input) { XStream xStream = new XStream(); xStream.setMode(XStream.ID_REFERENCES); if (input != null) { xStream.processAnnotations(input.getClass()); } XStream.setupDefaultSecurity(xStream); xStream.addPermission(new AnyTypePermission()); String xmlString = xStream.toXML(input); return (T) xStream.fromXML(xmlString); }
@Test public void testEncodingDecodingWithoutMetaData() throws Exception { Applications applications = InstanceInfoGenerator.newBuilder(10, 2).withMetaData(false).build().toApplications(); XStream xstream = JsonXStream.getInstance(); String jsonDocument = xstream.toXML(applications); Applications decodedApplications = (Applications) xstream.fromXML(jsonDocument); assertThat(EurekaEntityComparators.equal(decodedApplications, applications), is(true)); }
/** * Serialize an object including the XStream to the given Writer as pretty-printed XML. * <p> * Warning: XStream will serialize itself into this XML stream. To read such an XML code, you * should use {@link XStreamer#fromXML(Reader)} or one of the other overloaded * methods. Since a lot of internals are written into the stream, you cannot expect to use such * an XML to work with another XStream version or with XStream running on different JDKs and/or * versions. We have currently no JDK 1.3 support, nor will the PureReflectionConverter work * with a JDK less than 1.5. * </p> * * @throws IOException if an error occurs reading from the Writer. * @throws com.thoughtworks.xstream.XStreamException if the object cannot be serialized * @since 1.2 */ public void toXML(final XStream xstream, final Object obj, final Writer out) throws IOException { final XStream outer = new XStream(); XStream.setupDefaultSecurity(outer); final ObjectOutputStream oos = outer.createObjectOutputStream(out); try { oos.writeObject(xstream); oos.flush(); xstream.toXML(obj, out); } finally { oos.close(); } }
protected <S extends Score, W extends TestScoreWrapper<S>> void assertSerializeAndDeserialize(S expectedScore, W input) { XStream xStream = new XStream(); xStream.setMode(XStream.ID_REFERENCES); xStream.processAnnotations(input.getClass()); XStream.setupDefaultSecurity(xStream); xStream.allowTypesByRegExp(new String[]{"org\\.optaplanner\\.\\w+\\.config\\..*", "org\\.optaplanner\\.persistence\\.xstream\\..*\\$Test\\w+ScoreWrapper"}); String xmlString = xStream.toXML(input); W output = (W) xStream.fromXML(xmlString); assertEquals(expectedScore, output.getScore()); String regex; if (expectedScore != null) { regex = "<([\\w\\-\\.]+)( id=\"\\d+\")?>" // Start of element + "\\s*<score( id=\"\\d+\")?>" + expectedScore.toString().replaceAll("\\[", "\\\\[").replaceAll("\\]", "\\\\]") // Score + "</score>" + "\\s*</\\1>"; // End of element } else { regex = "<([\\w\\-\\.]+)( id=\"\\d+\")?/>"; // Start and end of element } if (!xmlString.matches(regex)) { fail("Regular expression match failed.\nExpected regular expression: " + regex + "\nActual string: " + xmlString); } }
@Test public void testEncodingDecodingWithMetaData() throws Exception { Applications applications = InstanceInfoGenerator.newBuilder(10, 2).withMetaData(true).build().toApplications(); XStream xstream = JsonXStream.getInstance(); String jsonDocument = xstream.toXML(applications); Applications decodedApplications = (Applications) xstream.fromXML(jsonDocument); assertThat(EurekaEntityComparators.equal(decodedApplications, applications), is(true)); }
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); } }
@Test public void testEncodingDecodingWithoutMetaData() throws Exception { Applications applications = InstanceInfoGenerator.newBuilder(10, 2).withMetaData(false).build().toApplications(); XStream xstream = XmlXStream.getInstance(); String xmlDocument = xstream.toXML(applications); Applications decodedApplications = (Applications) xstream.fromXML(xmlDocument); assertThat(EurekaEntityComparators.equal(decodedApplications, applications), is(true)); }
XStream xstream = new XStream(); xstream.alias("person", Person.class); xstream.alias("persons", PersonList.class); xstream.addImplicitCollection(PersonList.class, "list"); PersonList list = new PersonList(); list.add(new Person("ABC",12,"address")); list.add(new Person("XYZ",20,"address2")); String xml = xstream.toXML(list);
@Test public void testEncodingDecodingWithMetaData() throws Exception { Applications applications = InstanceInfoGenerator.newBuilder(10, 2).withMetaData(true).build().toApplications(); XStream xstream = XmlXStream.getInstance(); String xmlDocument = xstream.toXML(applications); Applications decodedApplications = (Applications) xstream.fromXML(xmlDocument); assertThat(EurekaEntityComparators.equal(decodedApplications, applications), is(true)); }
/** * Stores {@link IgniteConfiguration} to file as xml. * * @param cfg Ignite Configuration. * @param fileName A name of file where the configuration was stored. * @param resetMarshaller Reset marshaller configuration to default. * @param resetDiscovery Reset discovery configuration to default. * @throws IOException If failed. * @see #readCfgFromFileAndDeleteFile(String) */ public static void storeToFile(IgniteConfiguration cfg, String fileName, boolean resetMarshaller, boolean resetDiscovery) throws IOException, IgniteCheckedException { try(OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName))) { IgniteConfiguration cfg0 = new IgniteConfiguration(cfg); if (resetMarshaller) cfg0.setMarshaller(null); if (resetDiscovery) cfg0.setDiscoverySpi(null); cfg0.setWorkDirectory(U.defaultWorkDirectory()); cfg0.setMBeanServer(null); cfg0.setGridLogger(null); new XStream().toXML(cfg0, out); } }