Example usage for javax.xml.transform.stream StreamSource StreamSource

List of usage examples for javax.xml.transform.stream StreamSource StreamSource

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamSource StreamSource.

Prototype

public StreamSource() 

Source Link

Document

Zero-argument default constructor.

Usage

From source file:Main.java

public static Source createSource(final String message, final String charFormat) throws IOException {
    StreamSource source = new StreamSource();
    byte[] msgByte = message.getBytes(charFormat);
    ByteArrayInputStream in = new ByteArrayInputStream(msgByte);
    source.setInputStream(in);//w w w .j  a v a  2s .  c om
    return source;
}

From source file:Main.java

public static Schema getSchema(String schemaString) {
    Schema schema = null;//from   w  ww .  j  a  va  2 s.c o m
    try {
        if (schemaString != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            StreamSource ss = new StreamSource();
            ss.setReader(new StringReader(schemaString));
            schema = sf.newSchema(ss);
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to create schemma from string: " + schemaString, e);
    }
    return schema;
}

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/*w w  w . ja  v 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));
}