use of com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider 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.PureJavaReflectionProvider 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.PureJavaReflectionProvider in project jgnash by ccavanaugh.
the class BinaryContainer method writeBinary.
/**
* Writes an XML file given a collection of StoredObjects. TrashObjects and
* objects marked for removal are not written. If the file already exists,
* it will be overwritten.
*
* @param objects Collection of StoredObjects to write
* @param path file to write
*/
static synchronized void writeBinary(@NotNull final Collection<StoredObject> objects, @NotNull final Path path) {
final Logger logger = Logger.getLogger(BinaryContainer.class.getName());
if (!Files.exists(path.getParent())) {
try {
Files.createDirectories(path.getParent());
logger.info("Created missing directories");
} catch (final IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
createBackup(path);
List<StoredObject> list = new ArrayList<>();
list.addAll(query(objects, Budget.class));
list.addAll(query(objects, Config.class));
list.addAll(query(objects, CommodityNode.class));
list.addAll(query(objects, ExchangeRate.class));
list.addAll(query(objects, RootAccount.class));
list.addAll(query(objects, Reminder.class));
// remove any objects marked for removal
list.removeIf(StoredObject::isMarkedForRemoval);
// sort the list
list.sort(new StoredObjectComparator());
logger.info("Writing Binary file");
try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(path))) {
final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new BinaryStreamDriver()));
try (final ObjectOutputStream out = xstream.createObjectOutputStream(os)) {
out.writeObject(list);
out.flush();
}
// forcibly flush before letting go of the resources to help older windows systems write correctly
os.flush();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
logger.info("Writing Binary file complete");
}
use of com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider in project jgnash by ccavanaugh.
the class XMLContainer method writeXML.
/**
* Writes an XML file given a collection of StoredObjects. TrashObjects and
* objects marked for removal are not written. If the file already exists,
* it will be overwritten.
*
* @param objects Collection of StoredObjects to write
* @param path file to write
*/
static synchronized void writeXML(final Collection<StoredObject> objects, final Path path) {
Logger logger = Logger.getLogger(XMLContainer.class.getName());
if (!Files.exists(path.getParent())) {
try {
Files.createDirectories(path.getParent());
logger.info("Created missing directories");
} catch (final IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
createBackup(path);
List<StoredObject> list = new ArrayList<>();
list.addAll(query(objects, Budget.class));
list.addAll(query(objects, Config.class));
list.addAll(query(objects, CommodityNode.class));
list.addAll(query(objects, ExchangeRate.class));
list.addAll(query(objects, RootAccount.class));
list.addAll(query(objects, Reminder.class));
// remove any objects marked for removal
list.removeIf(StoredObject::isMarkedForRemoval);
// sort the list
list.sort(new StoredObjectComparator());
logger.info("Writing XML file");
try (final Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
writer.write("<?fileFormat " + Engine.CURRENT_MAJOR_VERSION + "." + Engine.CURRENT_MINOR_VERSION + "?>\n");
final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new KXml2Driver()));
try (final ObjectOutputStream out = xstream.createObjectOutputStream(new PrettyPrintWriter(writer))) {
out.writeObject(list);
// forcibly flush before letting go of the resources to help older windows systems write correctly
out.flush();
} catch (final Exception e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
} catch (final IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
logger.info("Writing XML file complete");
}
use of com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider in project hudson-2.x by hudson.
the class RobustReflectionConverter method writeValueToImplicitCollection.
private Map writeValueToImplicitCollection(UnmarshallingContext context, Object value, Map implicitCollections, Object result, String itemFieldName) {
String fieldName = mapper.getFieldNameForItemTypeAndName(context.getRequiredType(), value.getClass(), itemFieldName);
if (fieldName != null) {
if (implicitCollections == null) {
// lazy instantiation
implicitCollections = new HashMap();
}
Collection collection = (Collection) implicitCollections.get(fieldName);
if (collection == null) {
Class fieldType = mapper.defaultImplementationOf(reflectionProvider.getFieldType(result, fieldName, null));
if (!Collection.class.isAssignableFrom(fieldType)) {
throw new ObjectAccessException("Field " + fieldName + " of " + result.getClass().getName() + " is configured for an implicit Collection, but field is of type " + fieldType.getName());
}
if (pureJavaReflectionProvider == null) {
pureJavaReflectionProvider = new PureJavaReflectionProvider();
}
collection = (Collection) pureJavaReflectionProvider.newInstance(fieldType);
reflectionProvider.writeField(result, fieldName, collection, null);
implicitCollections.put(fieldName, collection);
}
collection.add(value);
}
return implicitCollections;
}
Aggregations