Search in sources :

Example 1 with HierarchicalStreamWriter

use of com.thoughtworks.xstream.io.HierarchicalStreamWriter 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;
}
Also used : AnnotationFieldKeySorter(org.apache.camel.component.salesforce.api.dto.AnnotationFieldKeySorter) TreeMarshallingStrategy(com.thoughtworks.xstream.core.TreeMarshallingStrategy) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) XppDriver(com.thoughtworks.xstream.io.xml.XppDriver) XStream(com.thoughtworks.xstream.XStream) FieldDictionary(com.thoughtworks.xstream.converters.reflection.FieldDictionary) DateTimeConverter(org.apache.camel.component.salesforce.api.utils.DateTimeConverter) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 2 with HierarchicalStreamWriter

use of com.thoughtworks.xstream.io.HierarchicalStreamWriter in project camel by apache.

the class ApprovalRequestsTest method shouldSerializeAsXml.

@Test
public void shouldSerializeAsXml() {
    final String xml = //
    "<ProcessApprovalRequest>" + //
    "<requests>" + //
    "<actionType>Submit</actionType>" + //
    "<contextActorId>005D00000015rZy</contextActorId>" + //
    "<contextId>001D000000I8mIm</contextId>" + //
    "<comments>this is a test 1</comments>" + //
    "<nextApproverIds>005D00000015rY9</nextApproverIds>" + //
    "<processDefinitionNameOrId>PTO_Request_Process</processDefinitionNameOrId>" + //
    "<skipEntryCriteria>true</skipEntryCriteria>" + //
    "</requests>" + //
    "<requests>" + //
    "<actionType>Submit</actionType>" + //
    "<contextActorId>005D00000015rZy</contextActorId>" + //
    "<contextId>001D000000I8dIm</contextId>" + //
    "<comments>this is a test 2</comments>" + //
    "<nextApproverIds>005D00000015xY9</nextApproverIds>" + //
    "<processDefinitionNameOrId>PTO_Request_Process</processDefinitionNameOrId>" + //
    "<skipEntryCriteria>true</skipEntryCriteria>" + //
    "</requests>" + "</ProcessApprovalRequest>";
    final XStream xStream = new XStream(new XppDriver(new NoNameCoder()) {

        @Override
        public HierarchicalStreamWriter createWriter(final Writer out) {
            return new CompactWriter(out, getNameCoder());
        }
    });
    xStream.ignoreUnknownElements();
    XStreamUtils.addDefaultPermissions(xStream);
    xStream.registerConverter(new DateTimeConverter());
    xStream.processAnnotations(ApprovalRequests.class);
    final String serialized = xStream.toXML(requests);
    assertEquals("Approval requests should serialize as XML", xml, serialized);
}
Also used : CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) XppDriver(com.thoughtworks.xstream.io.xml.XppDriver) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) DateTimeConverter(org.apache.camel.component.salesforce.api.utils.DateTimeConverter) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) Writer(java.io.Writer) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) Test(org.junit.Test)

Example 3 with HierarchicalStreamWriter

use of com.thoughtworks.xstream.io.HierarchicalStreamWriter in project maven-plugins by apache.

the class EvaluateMojo method getXStream.

/**
     * @return lazy loading xstream object.
     */
private XStream getXStream() {
    if (xstream == null) {
        xstream = new XStream();
        addAlias(xstream);
        // handle Properties a la Maven
        xstream.registerConverter(new PropertiesConverter() {

            /** {@inheritDoc} */
            public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
                return Properties.class == type;
            }

            /** {@inheritDoc} */
            public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
                Properties properties = (Properties) source;
                // sort
                Map<?, ?> map = new TreeMap<Object, Object>(properties);
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    writer.startNode(entry.getKey().toString());
                    writer.setValue(entry.getValue().toString());
                    writer.endNode();
                }
            }
        });
    }
    return xstream;
}
Also used : JarEntry(java.util.jar.JarEntry) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Properties(java.util.Properties) PropertiesConverter(com.thoughtworks.xstream.converters.collections.PropertiesConverter) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 4 with HierarchicalStreamWriter

use of com.thoughtworks.xstream.io.HierarchicalStreamWriter in project ddf by codice.

the class TestCswTransformProvider method testMarshalOtherSchema.

@Test
public void testMarshalOtherSchema() throws Exception {
    when(mockMetacardManager.getTransformerByProperty(TransformerManager.SCHEMA, OTHER_SCHEMA)).thenReturn(mockMetacardTransformer);
    when(mockMetacardTransformer.transform(any(Metacard.class), any(Map.class))).thenReturn(new BinaryContentImpl(IOUtils.toInputStream(getRecord()), new MimeType(MediaType.APPLICATION_XML)));
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.SCHEMA);
    context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, OTHER_SCHEMA);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.marshal(getMetacard(), writer, context);
    // Verify the context arguments were set correctly
    verify(mockMetacardManager, times(1)).getTransformerByProperty(captor.capture(), captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(OTHER_SCHEMA));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) HashMap(java.util.HashMap) Map(java.util.Map) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 5 with HierarchicalStreamWriter

use of com.thoughtworks.xstream.io.HierarchicalStreamWriter in project ddf by codice.

the class TestCswTransformProvider method testMarshalNoTransformers.

@Test(expected = ConversionException.class)
public void testMarshalNoTransformers() throws Exception {
    when(mockMetacardManager.getTransformerBySchema(anyString())).thenReturn(null);
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    provider.marshal(getMetacard(), writer, context);
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Test(org.junit.Test)

Aggregations

HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)11 Test (org.junit.Test)7 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)6 XStream (com.thoughtworks.xstream.XStream)4 StringWriter (java.io.StringWriter)4 Writer (java.io.Writer)4 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)3 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)3 XppDriver (com.thoughtworks.xstream.io.xml.XppDriver)3 Map (java.util.Map)3 NoNameCoder (com.thoughtworks.xstream.io.naming.NoNameCoder)2 CompactWriter (com.thoughtworks.xstream.io.xml.CompactWriter)2 Metacard (ddf.catalog.data.Metacard)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)2 HashMap (java.util.HashMap)2 MimeType (javax.activation.MimeType)2 DateTimeConverter (org.apache.camel.component.salesforce.api.utils.DateTimeConverter)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1