use of com.thoughtworks.xstream.converters.reflection.FieldDictionary in project camel by apache.
the class DefaultCompositeApiClient method configureXStream.
static XStream configureXStream() {
final PureJavaReflectionProvider reflectionProvider = new PureJavaReflectionProvider(new FieldDictionary(new AnnotationFieldKeySorter()));
final XppDriver hierarchicalStreamDriver = new XppDriver(new NoNameCoder()) {
@Override
public HierarchicalStreamWriter createWriter(final Writer out) {
return new CompactWriter(out, getNameCoder());
}
};
final XStream xStream = new XStream(reflectionProvider, hierarchicalStreamDriver);
xStream.aliasSystemAttribute(null, "class");
xStream.ignoreUnknownElements();
XStreamUtils.addDefaultPermissions(xStream);
xStream.registerConverter(new DateTimeConverter());
xStream.setMarshallingStrategy(new TreeMarshallingStrategy());
xStream.processAnnotations(ADDITIONAL_TYPES);
return xStream;
}
use of com.thoughtworks.xstream.converters.reflection.FieldDictionary in project camel by apache.
the class SObjectBatchTest method shouldSerializeToXml.
@Test
public void shouldSerializeToXml() {
final String xml = //
"<batch>\n" + //
" <batchRequests>\n" + //
" <batchRequest>\n" + //
" <method>POST</method>\n" + //
" <url>v37.0/sobjects/Account/</url>\n" + //
" <richInput>\n" + //
" <Account>\n" + //
" <Name>NewAccountName</Name>\n" + //
" <Industry>Environmental</Industry>\n" + //
" </Account>\n" + //
" </richInput>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>DELETE</method>\n" + //
" <url>v37.0/sobjects/Account/001D000000K0fXOIAZ</url>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>GET</method>\n" + //
" <url>v37.0/sobjects/Account/001D000000K0fXOIAZ?fields=Name,BillingPostalCode</url>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>GET</method>\n" + //
" <url>v37.0/sobjects/Account/EPK/12345</url>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>GET</method>\n" + //
" <url>v37.0/sobjects/Account/001D000000K0fXOIAZ/CreatedBy?fields=Name</url>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>GET</method>\n" + //
" <url>v37.0/limits/</url>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>PATCH</method>\n" + //
" <url>v37.0/sobjects/Account/001D000000K0fXOIAZ</url>\n" + //
" <richInput>\n" + //
" <Account>\n" + //
" <Name>NewName</Name>\n" + //
" <AccountNumber>AC12345</AccountNumber>\n" + //
" </Account>\n" + //
" </richInput>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>PATCH</method>\n" + //
" <url>v37.0/sobjects/Account/EPK/12345</url>\n" + //
" <richInput>\n" + //
" <Account>\n" + //
" <Name>NewName</Name>\n" + //
" </Account>\n" + //
" </richInput>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>PATCH</method>\n" + //
" <url>v37.0/sobjects/Account/EPK/12345</url>\n" + //
" <richInput>\n" + //
" <Account>\n" + //
" <Name>NewName</Name>\n" + //
" </Account>\n" + //
" </richInput>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>PATCH</method>\n" + //
" <url>v37.0/some/url</url>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>GET</method>\n" + //
" <url>v37.0/query/?q=SELECT Name FROM Account</url>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>GET</method>\n" + //
" <url>v37.0/queryAll/?q=SELECT Name FROM Account</url>\n" + //
" </batchRequest>\n" + //
" <batchRequest>\n" + //
" <method>GET</method>\n" + //
" <url>v37.0/search/?q=FIND {joe}</url>\n" + //
" </batchRequest>\n" + //
" </batchRequests>\n" + "</batch>";
final PureJavaReflectionProvider reflectionProvider = new PureJavaReflectionProvider(new FieldDictionary(new AnnotationFieldKeySorter()));
final XStream xStream = new XStream(reflectionProvider);
xStream.aliasSystemAttribute(null, "class");
xStream.processAnnotations(SObjectBatch.class);
xStream.processAnnotations(batch.objectTypes());
final String serialized = xStream.toXML(batch);
assertEquals("Should serialize as expected by Salesforce", xml, serialized);
}
use of com.thoughtworks.xstream.converters.reflection.FieldDictionary 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();
}
}
Aggregations