Search in sources :

Example 1 with StringInputStreamProvider

use of com.helger.commons.io.streamprovider.StringInputStreamProvider in project ph-commons by phax.

the class MicroReaderTest method testSimple.

@Test
public void testSimple() {
    final String s = "<?xml version=\"1.0\"?>" + "<verrryoot>" + "<root xmlns=\"myuri\">" + "<child xmlns=\"\">" + "<a:child2 xmlns:a=\"foo\">Value text - no entities!</a:child2>" + "</child>" + "</root>" + "</verrryoot>";
    IMicroDocument aDoc = MicroReader.readMicroXML(s);
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new StringSAXInputSource(s));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new NonBlockingStringReader(s));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new StringInputStreamProvider(s, StandardCharsets.ISO_8859_1));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new NonBlockingByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(s, new SAXReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(aDoc);
    final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream();
    new MicroSerializer().write(aDoc, baos);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + CRLF + "<verrryoot>" + CRLF + INDENT + "<root xmlns=\"myuri\">" + CRLF + INDENT + INDENT + "<ns0:child xmlns:ns0=\"\">" + CRLF + INDENT + INDENT + INDENT + "<ns1:child2 xmlns:ns1=\"foo\">Value text - no entities!</ns1:child2>" + CRLF + INDENT + INDENT + "</ns0:child>" + CRLF + INDENT + "</root>" + CRLF + "</verrryoot>" + CRLF, baos.getAsString(StandardCharsets.UTF_8));
    final String sXHTML = "<content>" + "<div class=\"css1\">" + "<span class=\"css2\">" + "<span>Text1 " + "<span>Text1b</span>" + "</span>" + " " + "<span>Text1c</span>" + "<span class=\"css3\">" + "<span>Text2</span>" + "</span>" + "</span>" + "</div>" + "</content>";
    final IMicroDocument docXHTML = MicroReader.readMicroXML(new NonBlockingStringReader(sXHTML));
    assertNotNull(docXHTML);
    final String sResult = MicroWriter.getNodeAsString(docXHTML, new XMLWriterSettings().setIndent(EXMLSerializeIndent.NONE));
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<content>" + "<div class=\"css1\">" + "<span class=\"css2\"><span>Text1 <span>Text1b</span></span> <span>Text1c</span>" + "<span class=\"css3\"><span>Text2</span></span>" + "</span>" + "</div>" + "</content>", sResult);
}
Also used : NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) LoggingSAXErrorHandler(com.helger.xml.sax.LoggingSAXErrorHandler) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) StringInputStreamProvider(com.helger.commons.io.streamprovider.StringInputStreamProvider) ISAXReaderSettings(com.helger.xml.serialize.read.ISAXReaderSettings) SAXReaderSettings(com.helger.xml.serialize.read.SAXReaderSettings) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Test(org.junit.Test)

Example 2 with StringInputStreamProvider

use of com.helger.commons.io.streamprovider.StringInputStreamProvider in project ph-commons by phax.

the class XMLMapHandlerTest method testReadInvalid.

@Test
public void testReadInvalid() {
    final ICommonsMap<String, String> aMap = new CommonsHashMap<>();
    final IReadableResource aRes = new ClassPathResource("xml/buildinfo.xml");
    try {
        XMLMapHandler.readMap((IHasInputStream) null);
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        XMLMapHandler.readMap((IHasInputStream) null, aMap);
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        XMLMapHandler.readMap((InputStream) null);
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        XMLMapHandler.readMap((InputStream) null, aMap);
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        XMLMapHandler.readMap(aRes, null);
        fail();
    } catch (final NullPointerException ex) {
    }
    String sXML = "<root><map value='b'/><map key='a'/><map key='c' value='d' /><map key='c' value='e' /></root>";
    final Map<String, String> aMap2 = XMLMapHandler.readMap(new StringInputStreamProvider(sXML, StandardCharsets.ISO_8859_1));
    assertNotNull(aMap2);
    assertEquals(1, aMap2.size());
    assertEquals("e", aMap2.get("c"));
    sXML = "<?xml version=\"1.0\"?>";
    assertNull(XMLMapHandler.readMap(new StringInputStreamProvider(sXML, StandardCharsets.ISO_8859_1)));
}
Also used : CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) IReadableResource(com.helger.commons.io.resource.IReadableResource) StringInputStreamProvider(com.helger.commons.io.streamprovider.StringInputStreamProvider) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 3 with StringInputStreamProvider

use of com.helger.commons.io.streamprovider.StringInputStreamProvider in project ph-commons by phax.

the class XMLListHandlerTest method testRead.

@Test
public void testRead() {
    ICommonsList<String> aList = new CommonsArrayList<>();
    final IReadableResource aRes = new ClassPathResource("xml/list.xml");
    XMLListHandler.readList(aRes, aList);
    assertEquals(3, aList.size());
    assertTrue(aList.contains("item1"));
    assertNotNull(XMLListHandler.readList(aRes));
    assertNull(XMLListHandler.readList(new ClassPathResource("test1.txt")));
    assertEquals(3, XMLListHandler.readList(aRes).size());
    assertTrue(XMLListHandler.readList(aRes).contains("item1"));
    assertTrue(XMLListHandler.writeList(aList, new NonBlockingByteArrayOutputStream()).isSuccess());
    final String sXML = "<root><item/><item value='a' /><item value='a' /></root>";
    aList = XMLListHandler.readList(new StringInputStreamProvider(sXML, StandardCharsets.ISO_8859_1));
    assertNotNull(aList);
    assertEquals(2, aList.size());
    assertEquals("a", aList.get(0));
    assertEquals("a", aList.get(1));
    final ICommonsSet<String> aSet = new CommonsHashSet<>();
    XMLListHandler.readList(new StringInputStreamProvider(sXML, StandardCharsets.ISO_8859_1), aSet);
    assertEquals(1, aSet.size());
    assertTrue(aSet.contains("a"));
    assertTrue(XMLListHandler.writeList(aList, new NonBlockingByteArrayOutputStream()).isSuccess());
    assertTrue(XMLListHandler.writeList(aList, new ByteArrayOutputStreamProvider()).isSuccess());
}
Also used : ByteArrayOutputStreamProvider(com.helger.commons.io.streamprovider.ByteArrayOutputStreamProvider) IReadableResource(com.helger.commons.io.resource.IReadableResource) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) StringInputStreamProvider(com.helger.commons.io.streamprovider.StringInputStreamProvider) CommonsHashSet(com.helger.commons.collection.impl.CommonsHashSet) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Aggregations

StringInputStreamProvider (com.helger.commons.io.streamprovider.StringInputStreamProvider)3 Test (org.junit.Test)3 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 IReadableResource (com.helger.commons.io.resource.IReadableResource)2 NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)1 CommonsHashSet (com.helger.commons.collection.impl.CommonsHashSet)1 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)1 NonBlockingStringReader (com.helger.commons.io.stream.NonBlockingStringReader)1 ByteArrayOutputStreamProvider (com.helger.commons.io.streamprovider.ByteArrayOutputStreamProvider)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 LoggingSAXErrorHandler (com.helger.xml.sax.LoggingSAXErrorHandler)1 StringSAXInputSource (com.helger.xml.sax.StringSAXInputSource)1 ISAXReaderSettings (com.helger.xml.serialize.read.ISAXReaderSettings)1 SAXReaderSettings (com.helger.xml.serialize.read.SAXReaderSettings)1 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)1