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);
}
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)));
}
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());
}
Aggregations