Search in sources :

Example 6 with StringInputStream

use of com.helger.commons.io.stream.StringInputStream in project ph-commons by phax.

the class ByteArrayOutputStreamProviderTest method testSimple.

@Test
public void testSimple() {
    final ByteArrayOutputStreamProvider aOSP = new ByteArrayOutputStreamProvider();
    final OutputStream aOS = aOSP.getOutputStream(EAppend.DEFAULT);
    assertNotNull(aOS);
    StreamHelper.copyInputStreamToOutputStreamAndCloseOS(new StringInputStream("Hiho", StandardCharsets.ISO_8859_1), aOS);
    assertEquals("Hiho", aOSP.getAsString(StandardCharsets.ISO_8859_1));
    assertArrayEquals("Hiho".getBytes(StandardCharsets.ISO_8859_1), aOSP.getBytes());
    // Close the underlying OS
    StreamHelper.close(aOSP.getWriter(StandardCharsets.UTF_8, EAppend.DEFAULT));
    // Reader/Writer
    StreamHelper.copyReaderToWriterAndCloseWriter(new NonBlockingStringReader("Hiho"), aOSP.getWriter(StandardCharsets.UTF_8, EAppend.DEFAULT));
    assertEquals("Hiho", aOSP.getAsString(StandardCharsets.UTF_8));
}
Also used : StringInputStream(com.helger.commons.io.stream.StringInputStream) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 7 with StringInputStream

use of com.helger.commons.io.stream.StringInputStream in project ph-commons by phax.

the class DOMReaderTest method testReadWithSchema.

@Test
public void testReadWithSchema() {
    final Schema aSchema = XMLSchemaCache.getInstance().getSchema(new ClassPathResource("xml/schema1.xsd"));
    assertNotNull(aSchema);
    // read valid
    final String sValid = "<?xml version='1.0'?><root xmlns='http://www.example.org/schema1'><a>1</a><b>2</b></root>";
    Document doc = DOMReader.readXMLDOM(sValid, new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(sValid, new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(sValid, new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    DOMReader.readXMLDOM(new NonBlockingStringReader(sValid), new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sValid), new DOMReaderSettings().setErrorHandler(new CollectingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sValid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream(sValid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream(sValid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream(sValid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource(sValid), new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource(sValid), new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource(sValid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-valid.xml"), new DOMReaderSettings().setSchema(aSchema));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-valid.xml"), new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-valid.xml"), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(doc);
    // Read invalid (<c> tag is unknown)
    final String sInvalid = "<?xml version='1.0'?><root xmlns='http://www.example.org/schema1'><a>1</a><b>2</b><c>3</c></root>";
    doc = DOMReader.readXMLDOM(sInvalid, new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(sInvalid, new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sInvalid), new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sInvalid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream(sInvalid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream(sInvalid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource(sInvalid), new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource(sInvalid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-invalid.xml"), new DOMReaderSettings().setSchema(aSchema));
    assertNull(doc);
    doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-invalid.xml"), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
    assertNull(doc);
    try {
        DOMReader.readXMLDOM((Reader) null, new DOMReaderSettings());
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        DOMReader.readXMLDOM((InputStream) null, new DOMReaderSettings());
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        DOMReader.readXMLDOM((InputSource) null, new DOMReaderSettings());
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        DOMReader.readXMLDOM((IReadableResource) null, new DOMReaderSettings());
        fail();
    } catch (final NullPointerException ex) {
    }
}
Also used : StringInputStream(com.helger.commons.io.stream.StringInputStream) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) CollectingSAXErrorHandler(com.helger.xml.sax.CollectingSAXErrorHandler) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) Schema(javax.xml.validation.Schema) LoggingSAXErrorHandler(com.helger.xml.sax.LoggingSAXErrorHandler) Document(org.w3c.dom.Document) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 8 with StringInputStream

use of com.helger.commons.io.stream.StringInputStream in project ph-commons by phax.

the class DOMReaderTest method testReadXMLDOMInputSource.

/**
 * Test method readXMLDOM @ never
 */
@Test
public void testReadXMLDOMInputSource() {
    Document doc = DOMReader.readXMLDOM("<root/>");
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new NonBlockingStringReader("<root/>"));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource("<root/>"));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringSAXInputSource("<?xml version=\"1.0\"?>\n" + "<root/>"));
    assertNotNull(doc);
    doc = DOMReader.readXMLDOM(new StringInputStream("<?xml version=\"1.0\"?>\n<root/>", StandardCharsets.ISO_8859_1));
    assertNotNull(doc);
    try {
        // null reader not allowed
        DOMReader.readXMLDOM((InputSource) null);
        fail();
    } catch (final NullPointerException ex) {
    }
    // non-XML
    assertNull(DOMReader.readXMLDOM(new StringSAXInputSource("")));
    // non-XML
    assertNull(DOMReader.readXMLDOM(new StringSAXInputSource("<bla>")));
}
Also used : StringInputStream(com.helger.commons.io.stream.StringInputStream) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 9 with StringInputStream

use of com.helger.commons.io.stream.StringInputStream in project ph-commons by phax.

the class JsonParserTest method testReadMultipleObjectsFromStream.

/**
 * Does not work with InputStream, because the used StreamDecoder reads to
 * eagerly:
 *
 * <pre>
 *  StringInputStream(NonBlockingByteArrayInputStream).read(byte[], int, int) line: 216
 *  LoggingInputStream(FilterInputStream).read(byte[], int, int) line: 133
 *  LoggingInputStream.read(byte[], int, int) line: 109
 *  NonBlockingPushbackInputStream(FilterInputStream).read(byte[], int, int) line: 133
 *  NonBlockingPushbackInputStream.read(byte[], int, int) line: 197
 *  StreamDecoder.readBytes() line: 284 [local variables unavailable]
 *  StreamDecoder.implRead(char[], int, int) line: 326
 *  StreamDecoder.read(char[], int, int) line: 178
 *  StreamDecoder.read0() line: 127 [local variables unavailable]
 *  StreamDecoder.read() line: 112 [local variables unavailable]
 *  InputStreamReader.read() line: 168 [local variables unavailable]
 *  NonClosingReader(FilterReader).read() line: 65
 *  NonBlockingPushbackReader(FilterReader).read() line: 65
 *  NonBlockingPushbackReader.read() line: 111
 *  JsonParser._readChar() line: 240
 *  JsonParser._skipSpaces() line: 349
 *  JsonParser._readValue() line: 910
 *  JsonParser.parse() line: 985
 *  JsonReader.parseJson(Reader, IJsonParserHandler, IJsonParserCustomizeCallback, IJsonParseExceptionCallback) line: 156
 *  JsonReader.readJson(Reader, IJsonParserCustomizeCallback, IJsonParseExceptionCallback) line: 445
 *  JsonReader$Builder.read() line: 1138
 *  JsonParserTest.testReadMultipleObjectsFromStream() line: 137
 * </pre>
 *
 * @throws IOException
 *         hopefully never
 */
@Test
@Ignore
public void testReadMultipleObjectsFromStream() throws IOException {
    final String sJson = "{ 'a': 1}{ 'b':true}";
    try (final InputStream aIS = new LoggingInputStream(new StringInputStream(sJson, StandardCharsets.UTF_8))) {
        IJson aJson;
        int nCount = 0;
        do {
            LOGGER.info("Run " + nCount);
            aJson = JsonReader.builderMultiObject().source(aIS).read();
            if (aJson != null)
                ++nCount;
        } while (aJson != null);
        assertEquals(2, nCount);
    }
}
Also used : LoggingInputStream(com.helger.commons.io.stream.LoggingInputStream) StringInputStream(com.helger.commons.io.stream.StringInputStream) LoggingInputStream(com.helger.commons.io.stream.LoggingInputStream) StringInputStream(com.helger.commons.io.stream.StringInputStream) InputStream(java.io.InputStream) IJson(com.helger.json.IJson) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

StringInputStream (com.helger.commons.io.stream.StringInputStream)9 Test (org.junit.Test)9 Document (org.w3c.dom.Document)4 NonBlockingStringReader (com.helger.commons.io.stream.NonBlockingStringReader)3 InputStream (java.io.InputStream)3 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)2 StringSAXInputSource (com.helger.xml.sax.StringSAXInputSource)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 LoggingInputStream (com.helger.commons.io.stream.LoggingInputStream)1 NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)1 IJson (com.helger.json.IJson)1 ByteArrayDataSource (com.helger.mail.datasource.ByteArrayDataSource)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 IMicroNode (com.helger.xml.microdom.IMicroNode)1 MicroDocument (com.helger.xml.microdom.MicroDocument)1 CollectingSAXErrorHandler (com.helger.xml.sax.CollectingSAXErrorHandler)1 LoggingSAXErrorHandler (com.helger.xml.sax.LoggingSAXErrorHandler)1 OutputStream (java.io.OutputStream)1 InternetHeaders (javax.mail.internet.InternetHeaders)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1