Example usage for org.springframework.oxm.xstream XStreamMarshaller unmarshal

List of usage examples for org.springframework.oxm.xstream XStreamMarshaller unmarshal

Introduction

In this page you can find the example usage for org.springframework.oxm.xstream XStreamMarshaller unmarshal.

Prototype

@Override
public final Object unmarshal(Source source) throws IOException, XmlMappingException 

Source Link

Document

Unmarshals the given provided javax.xml.transform.Source into an object graph.

Usage

From source file:org.georchestra.security.Proxy.java

public void init() throws Exception {
    if (targets != null) {
        for (String url : targets.values()) {
            new URL(url); // test that it is a valid URL
        }/*from  w  w  w  .  ja  v a2s.  c  om*/
    }
    if (proxyPermissionsFile != null) {
        Closer closer = Closer.create();
        try {
            final ClassLoader classLoader = Proxy.class.getClassLoader();
            InputStream inStream = closer.register(classLoader.getResourceAsStream(proxyPermissionsFile));
            Map<String, Class<?>> aliases = Maps.newHashMap();
            aliases.put(Permissions.class.getSimpleName().toLowerCase(), Permissions.class);
            aliases.put(UriMatcher.class.getSimpleName().toLowerCase(), UriMatcher.class);
            XStreamMarshaller unmarshaller = new XStreamMarshaller();
            unmarshaller.setAliasesByType(aliases);
            setProxyPermissions((Permissions) unmarshaller.unmarshal(new StreamSource(inStream)));
        } finally {
            closer.close();
        }
    }
    // georchestra datadir autoconfiguration
    // dependency injection / properties setter() are made by Spring before
    // init() call
    if ((georchestraConfiguration != null) && (georchestraConfiguration.activated())) {
        logger.info("geOrchestra configuration detected, reconfiguration in progress ...");

        Properties pTargets = georchestraConfiguration.loadCustomPropertiesFile("targets-mapping");

        targets.clear();
        for (String target : pTargets.stringPropertyNames()) {
            targets.put(target, pTargets.getProperty(target));
        }
        logger.info("Done.");
    }
}

From source file:org.dataconservancy.packaging.tool.ser.XStreamMarshallerFactoryTest.java

/**
 * Insures that the XStreamMarshaller produced by the XStreamMarshallerFactory uses the specified encoding setting;
 * that is, the platform default is <em>not</em> being used.
 *
 * @throws Exception//from  ww w .  j av a2 s. c o  m
 */
@Test
public void testEncodingTest() throws Exception {
    // Holders
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    StreamResult streamResult = new StreamResult(sink);
    StreamSource source = new StreamSource();

    // Byte sequences for XML elements
    byte[] openString = "<string>".getBytes();
    byte[] closeString = "</string>".getBytes();

    underTest.setEncoding("UTF-8");
    XStreamMarshaller utf8Marshaller = underTest.newInstance();

    underTest.setEncoding("ISO-8859-1");
    XStreamMarshaller iso8859Marshaller = underTest.newInstance();

    // Our literal, a "LATIN SMALL LETTER N WITH TILDE"
    // The two byte sequence 0xC3 0xB1 in UTF-8
    // The single byte sequence 0xF1 in ISO-8859-1
    // Java Unicode literal: \u00F1

    String literal = "\u00F1";

    // "<string>".length = 8 characters, so check the 8th and 9th element of the
    // byte array

    // 0xF1 written in UTF-8 should be 0xC3B1
    utf8Marshaller.marshal(literal, streamResult);
    assertEquals(0xC3, 0x000000FF & sink.toByteArray()[openString.length]);
    assertEquals(0xB1, 0x000000FF & sink.toByteArray()[openString.length + 1]);

    sink.reset();

    // 0xF1 written in ISO-8859-1 should be 0xF1
    iso8859Marshaller.marshal(literal, streamResult);
    assertEquals(0xF1, 0x000000FF & sink.toByteArray()[openString.length]);

    // Bytes representing 0xF1 encoded as UTF-8
    byte[] utf8SourceBytes = new byte[openString.length + 2 + closeString.length];
    System.arraycopy(openString, 0, utf8SourceBytes, 0, openString.length);
    utf8SourceBytes[openString.length] = (byte) 0xc3;
    utf8SourceBytes[openString.length + 1] = (byte) 0xb1;
    System.arraycopy(closeString, 0, utf8SourceBytes, openString.length + 2, closeString.length);

    // Bytes representing 0xF1 encoded as ISO-8859-1
    byte[] iso88591bytes = new byte[openString.length + 2 + closeString.length];
    System.arraycopy(openString, 0, iso88591bytes, 0, openString.length);
    iso88591bytes[openString.length] = (byte) 0xF1;
    System.arraycopy(closeString, 0, iso88591bytes, openString.length + 1, closeString.length);

    // The UTF-8 configured marshaller should be able to unmarshal the utf-8 bytes
    source.setInputStream(new ByteArrayInputStream(utf8SourceBytes));
    assertEquals(literal, utf8Marshaller.unmarshal(source));

    // The ISO-8859-1 configured marshaller should be able to unmarshal the ISO-8859-1 bytes
    source.setInputStream(new ByteArrayInputStream(iso88591bytes));
    assertEquals(literal, iso8859Marshaller.unmarshal(source));

    // But if the ISO-8859-1 marshaller tries to unmarshal utf-8 bytes...
    source.setInputStream(new ByteArrayInputStream(utf8SourceBytes));
    assertNotEquals(literal, iso8859Marshaller.unmarshal(source));

    // Or if the UTF-8 marshaller tries to unmarshal iso-8859-1 bytes...
    source.setInputStream(new ByteArrayInputStream(iso88591bytes));
    assertNotEquals(literal, utf8Marshaller.unmarshal(source));
}