Java 8 StringWriter - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 StringWriter class.

This code example shows how to use the following methods:toString
        final String original = toString(fileName);
 
        final InputStream is = getResource(fileName);
        final BundledPatch patch = PatchBundleXml.parse(is);
 
        final StringWriter writer = new StringWriter();
        PatchBundleXml.marshal(writer, patch);
        final String marshalled = writer.toString();
 
        System.out.println(original);
        System.out.println(marshalled);
 
        XMLUtils.compareXml(original, marshalled, false);
    } 
 
    static InputStream getResource(final String name) throws IOException {
        final URL resource = PatchBundleXmlUnitTestCase.class.getClassLoader().getResource(name);
        assertNotNull(name, resource);
        return resource.openStream();
    } 
10
CodeRank
This code example shows how to use the following methods:toString, write
    private static final Formatter THE_FORMATTER = new Formatter() {
        @Override 
        public String format(LogRecord r) { 
            Throwable thrown = r.getThrown(); 
            if (thrown != null) { 
                StringWriter sw = new StringWriter(); 
                PrintWriter pw = new PrintWriter(sw); 
                sw.write(r.getMessage()); 
                sw.write("\n"); 
                thrown.printStackTrace(pw); 
                pw.flush(); 
                return sw.toString(); 
            } else { 
                return r.getMessage(); 
            } 
        } 
    }; 
 
    /** 
     * Constructs a new instance of the Android log handler. 
10
CodeRank
This code example shows how to use the following methods:toString
 
	@Override 
	public boolean onException(Object obj, Throwable e)
	{ 
		// log, but ignore exceptions 
		StringWriter stringWriter = new StringWriter();
		PrintWriter printWriter = new PrintWriter(stringWriter);
		e.printStackTrace(printWriter);
		exceptions.put(obj.toString(), stringWriter.toString());
		return true; 
	} 
} 
10
CodeRank
This code example shows how to use the following methods:toString
	 * @see org.springframework.mail.MailPreparationException 
	 */ 
	public static String processTemplateIntoString(Template template, Object model)
			throws IOException, TemplateException {
 
		StringWriter result = new StringWriter();
		template.process(model, result);
		return result.toString();
	} 
 
} 
9
CodeRank
This code example shows how to use the following methods:toString
        final String original = toString(fileName);
 
        final InputStream is = getResource(fileName);
        final Patch patch = PatchXml.parse(is).resolvePatch(null, null);
 
        final StringWriter writer = new StringWriter();
        PatchXml.marshal(writer, patch);
        final String marshalled = writer.toString();
 
        XMLUtils.compareXml(original, marshalled, false);
    } 
 
    static InputStream getResource(String name) throws IOException {
        final URL resource = PatchXmlUnitTestCase.class.getClassLoader().getResource(name);
        assertNotNull(name, resource);
        return resource.openStream();
    } 
 
    private String toString(String fileName) throws Exception {
        final InputStream is = getResource(fileName);
10
CodeRank
This code example shows how to use the following methods:toString
     * Serialize to XML String 
     * @param document The DOM document 
     * @return The XML String 
     */ 
    public static String serialize(Document document) {
        StringWriter writer = new StringWriter();
        try { 
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            DOMSource domSource = new DOMSource(document);
            StreamResult streamResult = new StreamResult(writer);
            transformer.transform(domSource, streamResult); 
        } catch (TransformerException e) {
            throw new RuntimeException(
                    "Error when serializing XML document.", e);
        } 
        return writer.toString();
    } 
 
    /** 

10
CodeRank
This code example shows how to use the following methods:toString
		inputFactory = XMLOutputFactory.newInstance();
	} 
 
	@Test
	public void streamWriterSource() throws Exception {
		StringWriter stringWriter = new StringWriter();
		XMLStreamWriter streamWriter = inputFactory.createXMLStreamWriter(stringWriter);
		Reader reader = new StringReader(XML);
		Source source = new StreamSource(reader);
		StaxResult result = new StaxResult(streamWriter);
		assertEquals("Invalid streamWriter returned", streamWriter, result.getXMLStreamWriter());
		assertNull("EventWriter returned", result.getXMLEventWriter());
		transformer.transform(source, result);
		assertXMLEqual("Invalid result", XML, stringWriter.toString());
	} 
 
	@Test
	public void eventWriterSource() throws Exception {
		StringWriter stringWriter = new StringWriter();
		XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter);
9
CodeRank
This code example shows how to use the following methods:toString
                sb.append("\"tripleloader\" : ");
                sb.append(JSONObject.quote(stringWriter.toString()));
            } 
             
            { 
                StringWriter stringWriter = new StringWriter();
                MqlwriteLikeTransposedNodeFactory nodeFactory = new MqlwriteLikeTransposedNodeFactory(stringWriter);
                 
                Transposer.transpose(project, filteredRows, protograph, protograph.getRootNode(0), nodeFactory);
                nodeFactory.flush();
                 
                sb.append(", \"mqllike\" : ");
                sb.append(stringWriter.toString());
            } 
 
            sb.append(" }");
             
            respond(response, sb.toString());
        } catch (Exception e) {
            respondException(response, e);
10
CodeRank
This code example shows how to use the following methods:toString
        String s = String.format(format, args);
        errPrintln(s);
    } 
 
    public void exception(Throwable t, String format, Object... args) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        pw.flush();
        error(format + "\n" + sw.toString(), args);
    } 
 
    /** for unit testing */ 
    protected void errPrintln(String msg) {
        System.err.println(msg);
    } 
 
    /** for unit testing */ 
    protected void outPrintln(String msg) {
        System.out.println(msg);
10
CodeRank
This code example shows how to use the following methods:toString
 
	private String storeProperties(Properties props, String header, boolean useWriter) throws IOException {
		DefaultPropertiesPersister persister = new DefaultPropertiesPersister();
		String propCopy = null;
		if (useWriter) {
			StringWriter propWriter = new StringWriter();
			persister.store(props, propWriter, header);
			propCopy = propWriter.toString();
		} 
		else { 
			ByteArrayOutputStream propOut = new ByteArrayOutputStream();
			persister.store(props, propOut, header);
			propCopy = new String(propOut.toByteArray());
		} 
		if (header != null) {
			assertTrue(propCopy.indexOf(header) != -1);
		} 
		assertTrue(propCopy.indexOf("\ncode1=message1") != -1);
		assertTrue(propCopy.indexOf("\ncode2=message2") != -1);
		return propCopy;
9
CodeRank

Related Java 8 StringWriter Questions & Answers:

See Code Examples for Java 8 StringWriter Methods: