Example usage for java.io StringWriter toString

List of usage examples for java.io StringWriter toString

Introduction

In this page you can find the example usage for java.io StringWriter toString.

Prototype

public String toString() 

Source Link

Document

Return the buffer's current value as a string.

Usage

From source file:Main.java

public static void main(String[] args) {

    StringWriter sw = new StringWriter();

    // append a char       
    System.out.println(sw.append("a"));
    System.out.println(sw.append("b"));

    System.out.println(sw.toString());
}

From source file:Main.java

public static void main(String[] args) {

    StringWriter sw = new StringWriter();

    CharSequence sq = "tutorial from java2s.com";

    // append sequence
    sw.append(sq, 0, 5);/*from   w  w w  . j  a  v  a2s  .c  o m*/

    System.out.println(sw.toString());

    // append second sequence
    sw.append(sq, 6, 5);

    System.out.println(sw.toString());

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document doc = impl.createDocument(null, null, null);
    Element e1 = doc.createElement("api");
    doc.appendChild(e1);//from  w  w  w.j a v  a2s. c o  m
    Element e2 = doc.createElement("java");
    e1.appendChild(e2);

    e2.setAttribute("url", "http://www.domain.com");
    DOMSource domSource = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    System.out.println(sw.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document doc = impl.createDocument(null, null, null);
    Element e1 = doc.createElement("api");
    doc.appendChild(e1);/*  w  ww  .jav  a 2s.c om*/
    Element e2 = doc.createElement("java");
    e1.appendChild(e2);

    e2.setAttribute("url", "http://www.java2s.com");

    //transform the DOM for showing the result in console
    DOMSource domSource = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    System.out.println(sw.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document doc = impl.createDocument(null, null, null);
    Element e1 = doc.createElement("api");
    doc.appendChild(e1);// ww w.ja v  a 2s  .com
    Element e2 = doc.createElement("java");
    e1.appendChild(e2);

    e2.setAttribute("url", "http://www.java2s.com");

    // transform the DOM for showing the result in console
    DOMSource domSource = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    System.out.println(sw.toString());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader("<root>\r\n" + //
            "<Users>\r\n" + //
            "   <App id=\"test\">\r\n" + //
            "      <Username>ADMIN</Username>\r\n" + //
            "      <Password>ADMIN</Password>\r\n" + //
            "   </App>\r\n" + //
            "</Users>\r\n" + //
            "<Users>\r\n" + //
            "   <App id=\"test2\">\r\n" + //
            "      <Username>ADMIN2</Username>\r\n" + //
            "      <Password>ADMIN2</Password>\r\n" + //
            "   </App>\r\n" + //
            "</Users>\r\n" + //
            "</root>")));
    String inputId = "test2";
    String xpathStr = "//Users/App[@id='" + inputId + "']";
    // retrieve elements and change their content
    XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xpath.compile(xpathStr + "/Username");
    Node username = (Node) expr.evaluate(doc, XPathConstants.NODE);
    username.setTextContent("test-username");
    expr = xpath.compile(xpathStr + "/Password");
    Node password = (Node) expr.evaluate(doc, XPathConstants.NODE);
    password.setTextContent("test-password");
    // output the document
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    System.out.println(writer.toString());

}

From source file:com.enitalk.configs.VelocityConfig.java

public static void main(String[] args) throws IOException {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(VelocityEngine.class);
    VelocityEngine engine = ctx.getBean(VelocityEngine.class);

    long start = System.currentTimeMillis();
    Template t = engine.getTemplate("booking.vm");

    VelocityContext context = new VelocityContext();
    context.put("scheduledDate", new DateTime().toString());
    context.put("link", "http://localhost:8080");

    StringWriter writer = new StringWriter(24 * 1024);
    t.merge(context, writer);//  w w w  . j a  v  a 2 s . c  o m

    FileUtils.write(new File("/home/krash/Desktop/1.html"), writer.toString(), "UTF-8");
    logger.info("Took {}", System.currentTimeMillis() - start);
    //        String templateText = FileUtils.readFileToString(new File("./templates/booking.vm"), "UTF-8");
    //        logger.info("Templated text {}", templateText);

}

From source file:Main.java

public static void main(String[] args) {

    StringWriter sw = new StringWriter();

    // create a new sequence
    String s = "Hello from java2s.com";

    // write a string
    sw.write(s);//from   www  . j  a  v a  2 s.co m

    System.out.println(sw.toString());

    try {
        // close the writer
        sw.close();
    } catch (IOException ex) {
        ex.printStackTrace();
        ;
    }

}

From source file:net.jcreate.e3.templateEngine.webmacro.WebMacroTemplateEngine.java

public static void main(String[] args) {
    WebMacroTemplateEngine a = new WebMacroTemplateEngine();
    Template t = new DefaultTemplate();
    t.setResource("noservlet.wm");
    Context c = new DefaultContext();
    c.put("msg", "hello.");
    StringWriter writer = new StringWriter();
    a.mergeTemplate(t, c, writer);//from   w  w  w  . j  a  v  a2s. c om
    System.out.println(writer.toString());
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);/*from   www .  j a  v  a  2 s . com*/

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager
            .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb");

    ResultSet rs = con.createStatement().executeQuery("select * from product");

    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    while (rs.next()) {
        Element row = doc.createElement("Row");
        results.appendChild(row);
        for (int i = 1; i <= colCount; i++) {
            String columnName = rsmd.getColumnName(i);
            Object value = rs.getObject(i);
            Element node = doc.createElement(columnName);
            node.appendChild(doc.createTextNode(value.toString()));
            row.appendChild(node);
        }
    }
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);

    System.out.println(sw.toString());

    con.close();
    rs.close();
}