private Document parseXmlString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader(xml)); return documentBuilder.parse(inputSource); }
private static BufferedReader getBufferedReader(String source) { return new BufferedReader(new StringReader(source)); }
protected final Document parse(String value) throws IllegalArgumentException { try { return documentBuilder().parse(new InputSource(new StringReader(value))); } catch (Exception e) { throw new IllegalArgumentException("Failed to parse JSON String as XML: "+e.getMessage(), e); } }
@Test public void copyToString() throws IOException { String content = "content"; StringReader in = new StringReader(content); String result = FileCopyUtils.copyToString(in); assertEquals(content, result); }
/** * Goes through the parser with the given text to make sure it doesn't yield any error. */ public void parseTest(String candidate) { try { BufferedReader r = new BufferedReader(new StringReader(candidate)); String line; while ((line=r.readLine())!=null) { if (line.startsWith("#")) continue; // comment parse(line); } } catch (IOException e) { throw new IllegalArgumentException(e); // can't happen but just in case } }
@Test public void contentHandlerDocumentNoNamespacePrefixes() throws Exception { handler = new DomContentHandler(result); expected = documentBuilder.parse(new InputSource(new StringReader(XML_1))); xmlReader.setContentHandler(handler); xmlReader.parse(new InputSource(new StringReader(XML_1))); assertThat("Invalid result", result, isSimilarTo(expected)); }
String xml = "<message>HELLO!</message>"; org.jdom.input.SAXBuilder saxBuilder = new SAXBuilder(); try { org.jdom.Document doc = saxBuilder.build(new StringReader(xml)); String message = doc.getRootElement().getText(); System.out.println(message); } catch (JDOMException e) { // handle JDOMException } catch (IOException e) { // handle IOException }
@Test public void escape_value() throws Exception { StringWriter output = new StringWriter(); CsvWriter writer = CsvWriter.of(output); writer.values("no double-quotes", "contains \"double-quotes\"", "contains , commas"); writer.close(); BufferedReader reader = new BufferedReader(new StringReader(output.toString())); assertThat(reader.readLine()).isEqualTo("no double-quotes,\"contains \"\"double-quotes\"\"\",\"contains , commas\""); assertThat(reader.readLine()).isNull(); }
protected final void print( PrintStream out, Throwable exception, String linePrefix ) { if ( exception != null ) { out.println( ", exception:" ); ByteArrayOutputStream buf = new ByteArrayOutputStream(); PrintStream sbuf = new PrintStream( buf ); exception.printStackTrace( sbuf ); sbuf.flush(); BufferedReader reader = new BufferedReader( new StringReader( buf.toString() ) ); try { String line = reader.readLine(); while ( line != null ) { out.print( linePrefix ); out.print( '\t' ); out.println( line ); line = reader.readLine(); } out.print( linePrefix ); } catch ( IOException e ) { throw new RuntimeException( e ); } } } }
@Test public void testContentEquals_Reader_Reader() throws Exception { { final StringReader input1 = new StringReader(""); assertTrue(IOUtils.contentEquals(input1, input1)); } { final StringReader input1 = new StringReader("ABC"); assertTrue(IOUtils.contentEquals(input1, input1)); } assertTrue(IOUtils.contentEquals(new StringReader(""), new StringReader(""))); assertTrue(IOUtils.contentEquals(new BufferedReader(new StringReader("")), new BufferedReader(new StringReader("")))); assertTrue(IOUtils.contentEquals(new StringReader("ABC"), new StringReader("ABC"))); assertFalse(IOUtils.contentEquals(new StringReader("ABCD"), new StringReader("ABC"))); assertFalse(IOUtils.contentEquals(new StringReader("ABC"), new StringReader("ABCD"))); }
InputSource source1 = new InputSource(new StringReader(xml)); InputSource source2 = new InputSource(new StringReader(xml)); String msg = xpath.evaluate("/resp/msg", source); String status = xpath.evaluate("/resp/status", source2); System.out.println("msg=" + msg + ";" + "status=" + status);
@Test public void testIterable() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache test = dmf.compile(new StringReader("{{#values}}\ntest: {{value}}\n{{/values}}"), "test"); Node invert = test.invert("test: sam\ntest: fred\n"); Node node = new Node(); Node sam = new Node(); sam.put("value", value("sam")); Node fred = new Node(); fred.put("value", value("fred")); node.put("values", list(asList(sam, fred))); assertEquals(node, invert); StringWriter sw = new StringWriter(); test.execute(sw, invert).close(); System.out.println(sw); }
private void tokenizeDate(String inputDate) { tokens = new ArrayList<>(); Pattern pat = Pattern.compile("[-]"); if (inputDate == null) { System.out.println("Null input date"); } Matcher m = pat.matcher(inputDate); String str = m.replaceAll(" - "); str = str.replaceAll(",", " "); PTBTokenizer<Word> tokenizer = PTBTokenizer.newPTBTokenizer(new BufferedReader(new StringReader(str))); while (tokenizer.hasNext()) { Word nextToken = tokenizer.next(); tokens.add(nextToken.toString()); } if(DEBUG) { System.out.println("tokens:" + tokens); } }
/** * Test of parse method, of class RazorHistoryParser. */ @Test public void testParseEmpty() throws Exception { String output = ""; History result = instance.parseContents(new BufferedReader(new StringReader(output))); assertEquals(0, result.getHistoryEntries().size()); }
@Test @SuppressWarnings("deprecation") public void unmarshalSAXSource() throws Exception { XMLReader reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING))); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
/** * Read an XML document from the given String. * @see #getReader() */ public Document readDocumentFromString(String documentContent) { InputSource is = new InputSource(new StringReader(documentContent)); try { return this.reader.doLoadDocument(is, getResource()); } catch (Exception ex) { throw new BeanDefinitionStoreException("Failed to read XML document", ex); } }
public static Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); }
@Test public void copyFromReader() throws IOException { String content = "content"; StringReader in = new StringReader(content); StringWriter out = new StringWriter(); int count = FileCopyUtils.copy(in, out); assertEquals(content.length(), count); assertEquals(content, out.toString()); }
public static List<String> convertStringToListOfLines(String string) throws IOException { final List<String> lines = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new StringReader(string))) { String line = null; while ((line = reader.readLine()) != null) { lines.add(line); } } return lines; } }
@Test public void contentHandlerDocumentNamespacePrefixes() throws Exception { xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); handler = new DomContentHandler(result); expected = documentBuilder.parse(new InputSource(new StringReader(XML_1))); xmlReader.setContentHandler(handler); xmlReader.parse(new InputSource(new StringReader(XML_1))); assertThat("Invalid result", result, isSimilarTo(expected)); }