/** * Loads the contents of this file into a new object. */ public Object read() throws IOException { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Reading "+file); } try (InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()))) { return xs.fromXML(in); } catch (RuntimeException | Error e) { throw new IOException("Unable to read "+file,e); } }
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; }
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);
private static XStream config_WxMpXmlMessage() { XStream xstream = XStreamInitializer.getInstance(); xstream.processAnnotations(WxMpXmlMessage.class); xstream.processAnnotations(WxMpXmlMessage.ScanCodeInfo.class); xstream.processAnnotations(WxMpXmlMessage.SendPicsInfo.class); xstream.processAnnotations(WxMpXmlMessage.SendPicsInfo.Item.class); xstream.processAnnotations(WxMpXmlMessage.SendLocationInfo.class); xstream.aliasField("MsgID", WxMpXmlMessage.class, "msgId"); return xstream; }
/** * Returns a list of any plugins that are persisted in the installing list */ @SuppressWarnings("unchecked") public static synchronized @CheckForNull Map<String,String> getPersistedInstallStatus() { File installingPluginsFile = getInstallingPluginsFile(); if(installingPluginsFile == null || !installingPluginsFile.exists()) { return null; } return (Map<String,String>)new XStream().fromXML(installingPluginsFile); }
/** * Builds the {@link XStream} setup which is used to read/write {@link SolverConfig solver configs} and benchmark configs. * It should never be used to read/write {@link PlanningSolution solutions}. * Use XStreamSolutionFileIO for that instead. * @return never null. */ public static XStream buildXStream() { XStream xStream = new XStream(); xStream.setMode(XStream.ID_REFERENCES); xStream.aliasSystemAttribute("xStreamId", "id"); xStream.aliasSystemAttribute("xStreamRef", "reference"); xStream.processAnnotations(SolverConfig.class); XStream.setupDefaultSecurity(xStream); xStream.allowTypesByRegExp(new String[]{"org\\.optaplanner\\.\\w+\\.config\\..*"}); return xStream; }
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");
@Test public void whenMethodReturnsAComplexTypeSerializesIt() { XStream x = new XStream(); x.alias("friend", Friend.class); x.alias("player", Player.class); x.registerConverter(new MethodValueSupportConverter(new ReflectionConverter(x.getMapper(), x.getReflectionProvider())), XStream.PRIORITY_LOW); String content = x.toXML(new Friend()); assertThat(content, is(equalTo("<friend>\n <player>\n <name>custom_name</name>\n </player>\n</friend>"))); }
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)); }
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); } }
/** * @deprecated Uses default encoding yet fails to write an encoding header. Prefer {@link #toXMLUTF8}. */ @Deprecated @Override public void toXML(Object obj, OutputStream out) { super.toXML(obj, out); }
public static String serialise(ProjectSlackNotificationsBean project){ XStream xstream = new XStream(new JsonHierarchicalStreamDriver()); xstream.setMode(XStream.NO_REFERENCES); xstream.alias("projectSlacknotificationConfig", ProjectSlackNotificationsBean.class); /* For some reason, the items are coming back as "@name" and "@value" * so strip those out with a regex. */ return xstream.toXML(project); }
/** * Adds an alias to use instead of the fully qualified class name. * * @param name The alias to use * @param type The Class to use the alias for * @see XStream#alias(String, Class) */ public void addAlias(String name, Class type) { xStream.alias(name, type); }
private static XStream createXStream(boolean json) { final XStream xstream; if (json) { // format json xstream = new XStream(new JsonHierarchicalStreamDriver()); xstream.setMode(XStream.NO_REFERENCES); } else { // sinon format xml, utilise la dépendance XPP3 par défaut xstream = new XStream(); } for (final Map.Entry<String, Class<?>> entry : XStreamAlias.getMap().entrySet()) { xstream.alias(entry.getKey(), entry.getValue()); } final MapConverter mapConverter = new MapConverter(xstream.getMapper()) { /** {@inheritDoc} */ @SuppressWarnings("rawtypes") @Override public boolean canConvert(Class type) { return true; // Counter.requests est bien une map } }; xstream.registerLocalConverter(Counter.class, "requests", mapConverter); xstream.registerLocalConverter(Counter.class, "rootCurrentContextsByThreadId", mapConverter); return xstream; } }
public static XStream newInstance() { final QNameMap qnames = new QNameMap(); final DcsPullDriver driver = new DcsPullDriver(qnames); // The XStream Driver final XStream x = new XStream(driver); x.setMode(XStream.NO_REFERENCES); x.addDefaultImplementation(IngestReport.class, IngestReport.class); x.alias(IngestReportConverter.E_INGEST_REPORT, IngestReport.class); x.registerConverter(new IngestReportConverter()); qnames.registerMapping(new QName(null, IngestReportConverter.E_INGEST_REPORT), IngestReport.class); return x; }
@Override public WxMpPayCallback getJSSDKCallbackData(String xmlData) { try { XStream xstream = XStreamInitializer.getInstance(); xstream.alias("xml", WxMpPayCallback.class); WxMpPayCallback wxMpCallback = (WxMpPayCallback) xstream.fromXML(xmlData); return wxMpCallback; } catch (Exception e){ e.printStackTrace(); } return new WxMpPayCallback(); }
public XStreamSolutionFileIO(Class... xStreamAnnotatedClasses) { xStream = new XStream(); xStream.setMode(XStream.ID_REFERENCES); xStream.processAnnotations(xStreamAnnotatedClasses); XStream.setupDefaultSecurity(xStream); // Presume the XML file comes from a trusted source so it works out of the box. See class javadoc. xStream.addPermission(new AnyTypePermission()); }
@Test public void versioning() { Person person = new Person(); person.setName("Davide"); XStream xStream = new XStream(new DomDriver()); xStream.registerConverter(new PersonConverter()); xStream.alias("person", Person.class); System.out.println(xStream.toXML(person)); }
xs.ignoreUnknownElements(); xs.alias("global", GeoServerInfo.class); xs.alias("settings", SettingsInfo.class); xs.alias("logging", LoggingInfo.class); xs.alias("jai", JAIInfo.class); xs.alias("coverageAccess", CoverageAccessInfo.class); xs.alias("catalog", Catalog.class); xs.alias("namespace", NamespaceInfo.class); xs.alias("workspace", WorkspaceInfo.class); xs.alias("dataStore", DataStoreInfo.class); xs.alias("wmsStore", WMSStoreInfo.class); xs.alias("wmtsStore", WMTSStoreInfo.class); xs.alias("coverageStore", CoverageStoreInfo.class); xs.alias("style", StyleInfo.class); xs.alias("legend", LegendInfo.class); xs.alias("featureType", FeatureTypeInfo.class); xs.alias("coverage", CoverageInfo.class); xs.alias("wmsLayer", WMSLayerInfo.class); xs.alias("wmtsLayer", WMTSLayerInfo.class); xs.alias("coverageDimension", CoverageDimensionInfo.class); xs.alias("coverageBand", CoverageBand.class); xs.alias("inputCoverageBand", InputCoverageBand.class); xs.alias("metadataLink", MetadataLinkInfo.class); xs.alias("dataLink", DataLinkInfo.class); xs.alias("attribute", AttributeTypeInfo.class); xs.alias("layer", LayerInfo.class); xs.alias("layerGroup", LayerGroupInfo.class); xs.alias("published", PublishedInfo.class);