use of com.thoughtworks.xstream.converters.Converter in project camel by apache.
the class RichInputConverter method marshal.
@Override
public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
if (source instanceof Map) {
@SuppressWarnings("unchecked") final Map<String, String> map = (Map) source;
for (final Map.Entry<String, String> e : map.entrySet()) {
writer.startNode(e.getKey());
writer.setValue(e.getValue());
writer.endNode();
}
} else {
final Class<?> clazz = source.getClass();
writer.startNode(clazz.getSimpleName());
final Converter converter = converterLookup.lookupConverterForType(source.getClass());
converter.marshal(source, writer, context);
writer.endNode();
}
}
use of com.thoughtworks.xstream.converters.Converter in project camel by apache.
the class AbstractXStreamWrapper method createXStream.
protected XStream createXStream(ClassResolver resolver, ClassLoader classLoader) {
if (xstreamDriver != null) {
xstream = new XStream(xstreamDriver);
} else {
xstream = new XStream();
}
if (mode != null) {
xstream.setMode(getModeFromString(mode));
}
ClassLoader xstreamLoader = xstream.getClassLoader();
if (classLoader != null && xstreamLoader instanceof CompositeClassLoader) {
((CompositeClassLoader) xstreamLoader).add(classLoader);
}
try {
if (this.implicitCollections != null) {
for (Entry<String, String[]> entry : this.implicitCollections.entrySet()) {
for (String name : entry.getValue()) {
xstream.addImplicitCollection(resolver.resolveMandatoryClass(entry.getKey()), name);
}
}
}
if (this.aliases != null) {
for (Entry<String, String> entry : this.aliases.entrySet()) {
xstream.alias(entry.getKey(), resolver.resolveMandatoryClass(entry.getValue()));
// It can turn the auto-detection mode off
xstream.processAnnotations(resolver.resolveMandatoryClass(entry.getValue()));
}
}
if (this.omitFields != null) {
for (Entry<String, String[]> entry : this.omitFields.entrySet()) {
for (String name : entry.getValue()) {
xstream.omitField(resolver.resolveMandatoryClass(entry.getKey()), name);
}
}
}
if (this.converters != null) {
for (String name : this.converters) {
Class<Converter> converterClass = resolver.resolveMandatoryClass(name, Converter.class);
Converter converter;
Constructor<Converter> con = null;
try {
con = converterClass.getDeclaredConstructor(new Class[] { XStream.class });
} catch (Exception e) {
//swallow as we null check in a moment.
}
if (con != null) {
converter = con.newInstance(xstream);
} else {
converter = converterClass.newInstance();
try {
Method method = converterClass.getMethod("setXStream", new Class[] { XStream.class });
if (method != null) {
ObjectHelper.invokeMethod(method, converter, xstream);
}
} catch (Throwable e) {
// swallow, as it just means the user never add an XStream setter, which is optional
}
}
xstream.registerConverter(converter);
}
}
addDefaultPermissions(xstream);
if (this.permissions != null) {
// permissions ::= pterm (',' pterm)* # consits of one or more terms
// pterm ::= aod? wterm # each term preceded by an optional sign
// aod ::= '+' | '-' # indicates allow or deny where allow if omitted
// wterm ::= a class name with optional wildcard characters
addPermissions(xstream, permissions);
}
} catch (Exception e) {
throw new RuntimeException("Unable to build XStream instance", e);
}
return xstream;
}
use of com.thoughtworks.xstream.converters.Converter in project ddf by codice.
the class TestTransactionMessageBodyReader method testReadInsertFrom.
@Test
public void testReadInsertFrom() throws Exception {
Converter mockConverter = mock(Converter.class);
when(mockConverter.canConvert(any(Metacard.class.getClass()))).thenReturn(true);
when(mockConverter.unmarshal(any(HierarchicalStreamReader.class), any(UnmarshallingContext.class))).thenReturn(mock(Metacard.class));
TransactionMessageBodyReader reader = new TransactionMessageBodyReader(mockConverter, CswQueryFactoryTest.getCswMetacardType(), registry);
CswTransactionRequest request = reader.readFrom(CswTransactionRequest.class, null, null, null, null, IOUtils.toInputStream(getInsertRequest(COUNT)));
assertThat(request, notNullValue());
assertThat(request.getInsertActions().size(), is(1));
assertThat(request.getDeleteActions().size(), is(0));
assertThat(request.getUpdateActions().size(), is(0));
InsertAction insertAction = request.getInsertActions().get(0);
assertThat(insertAction, notNullValue());
assertThat(insertAction.getRecords().size(), is(COUNT));
assertThat(request.getService(), is(CswConstants.CSW));
assertThat(request.getVersion(), is(CswConstants.VERSION_2_0_2));
assertThat(request.isVerbose(), is(true));
}
use of com.thoughtworks.xstream.converters.Converter in project ddf by codice.
the class TestTransactionMessageBodyReader method testReadInsertAndDeleteFrom.
@Test
public void testReadInsertAndDeleteFrom() throws IOException {
Converter mockConverter = mock(Converter.class);
when(mockConverter.canConvert(any(Metacard.class.getClass()))).thenReturn(true);
when(mockConverter.unmarshal(any(HierarchicalStreamReader.class), any(UnmarshallingContext.class))).thenReturn(mock(Metacard.class));
TransactionMessageBodyReader reader = new TransactionMessageBodyReader(mockConverter, CswQueryFactoryTest.getCswMetacardType(), registry);
CswTransactionRequest request = reader.readFrom(CswTransactionRequest.class, null, null, null, null, IOUtils.toInputStream(INSERT_AND_DELETE_REQUEST_XML));
assertThat(request, notNullValue());
assertThat(request.getDeleteActions().size(), is(1));
assertThat(request.getInsertActions().size(), is(1));
assertThat(request.getUpdateActions().size(), is(0));
DeleteAction deleteAction = request.getDeleteActions().get(0);
assertThat(deleteAction, notNullValue());
assertThat(deleteAction.getTypeName(), is(CswConstants.CSW_RECORD));
assertThat(deleteAction.getHandle(), is("something"));
assertThat(deleteAction.getConstraint(), notNullValue());
assertThat(deleteAction.getConstraint().getCqlText().trim(), is("title = 'foo'"));
InsertAction insertAction = request.getInsertActions().get(0);
assertThat(insertAction, notNullValue());
assertThat(insertAction.getRecords().size(), is(1));
assertThat(request.getService(), is(CswConstants.CSW));
assertThat(request.getVersion(), is(CswConstants.VERSION_2_0_2));
assertThat(request.isVerbose(), is(true));
}
Aggregations