Example usage for java.io StringWriter getBuffer

List of usage examples for java.io StringWriter getBuffer

Introduction

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

Prototype

public StringBuffer getBuffer() 

Source Link

Document

Return the string buffer itself.

Usage

From source file:de.uni.bremen.monty.moco.Main.java

public static void main(String[] args) throws IOException, InterruptedException {
    Namespace ns = parseArgs(args);

    if (ns == null) {
        return;//from   ww  w .  j ava 2  s.com
    }

    String inputFileName = ns.get("file");
    String outputFileName = ns.get("outputFile");
    boolean emitAssembly = ns.get("emitAssembly");
    boolean compileOnly = ns.get("compileOnly");
    boolean stopOnFirstError = ns.get("stopOnFirstError");
    boolean printAST = ns.get("printAST");
    boolean debugParseTree = ns.get("debugParseTree");

    if (debugParseTree) {
        debugParseTree(inputFileName);
        return;
    }

    StringWriter writer = new StringWriter();
    IOUtils.copy(Main.class.getResourceAsStream("/std_llvm_include.ll"), writer);

    Package ast = buildPackage(inputFileName);

    if (!visitVisitors(ast, stopOnFirstError, writer.getBuffer())) {
        return;
    }

    if (printAST) {
        (new PrintVisitor()).visitDoubleDispatched(ast);
    }

    if (emitAssembly) {
        writeAssembly(outputFileName, inputFileName, writer.toString());
        return;
    }

    File executable = buildExecutable(outputFileName, inputFileName, compileOnly, writer.toString());
    if (!compileOnly) {
        runExecutable(executable);
    }
}

From source file:net.sf.jabref.logic.xmp.XMPUtil.java

/**
 * Command-line tool for working with XMP-data.
 *
 * Read or write XMP-metadata from or to pdf file.
 *
 * Usage:// www.j  a  v a 2s  . com
 * <dl>
 * <dd>Read from PDF and print as bibtex:</dd>
 * <dt>xmpUtil PDF</dt>
 * <dd>Read from PDF and print raw XMP:</dd>
 * <dt>xmpUtil -x PDF</dt>
 * <dd>Write the entry in BIB given by KEY to the PDF:</dd>
 * <dt>xmpUtil KEY BIB PDF</dt>
 * <dd>Write all entries in BIB to the PDF:</dd>
 * <dt>xmpUtil BIB PDF</dt>
 * </dl>
 *
 * @param args
 *            Command line strings passed to utility.
 * @throws IOException
 *             If any of the given files could not be read or written.
 * @throws TransformerException
 *             If the given BibEntry is malformed.
 */
public static void main(String[] args) throws IOException, TransformerException {

    // Don't forget to initialize the preferences
    if (Globals.prefs == null) {
        Globals.prefs = JabRefPreferences.getInstance();
    }

    switch (args.length) {
    case 0:
        XMPUtil.usage();
        break;
    case 1:

        if (args[0].endsWith(".pdf")) {
            // Read from pdf and write as BibTex
            List<BibEntry> l = XMPUtil.readXMP(new File(args[0]), Globals.prefs);

            BibEntryWriter bibtexEntryWriter = new BibEntryWriter(
                    new LatexFieldFormatter(LatexFieldFormatterPreferences.fromPreferences(Globals.prefs)),
                    false);

            for (BibEntry entry : l) {
                StringWriter sw = new StringWriter();
                bibtexEntryWriter.write(entry, sw, BibDatabaseMode.BIBTEX);
                System.out.println(sw.getBuffer());
            }

        } else if (args[0].endsWith(".bib")) {
            // Read from bib and write as XMP
            try (FileReader fr = new FileReader(args[0])) {
                ParserResult result = BibtexParser.parse(fr);
                Collection<BibEntry> entries = result.getDatabase().getEntries();

                if (entries.isEmpty()) {
                    System.err.println("Could not find BibEntry in " + args[0]);
                } else {
                    System.out.println(XMPUtil.toXMP(entries, result.getDatabase()));
                }
            }
        } else {
            XMPUtil.usage();
        }
        break;
    case 2:
        if ("-x".equals(args[0]) && args[1].endsWith(".pdf")) {
            // Read from pdf and write as BibTex
            Optional<XMPMetadata> meta = XMPUtil.readRawXMP(new File(args[1]));

            if (meta.isPresent()) {
                XMLUtil.save(meta.get().getXMPDocument(), System.out, StandardCharsets.UTF_8.name());
            } else {
                System.err.println("The given pdf does not contain any XMP-metadata.");
            }
            break;
        }

        if (args[0].endsWith(".bib") && args[1].endsWith(".pdf")) {
            ParserResult result = BibtexParser.parse(new FileReader(args[0]));

            Collection<BibEntry> entries = result.getDatabase().getEntries();

            if (entries.isEmpty()) {
                System.err.println("Could not find BibEntry in " + args[0]);
            } else {
                XMPUtil.writeXMP(new File(args[1]), entries, result.getDatabase(), false);
                System.out.println("XMP written.");
            }
            break;
        }

        XMPUtil.usage();
        break;
    case 3:
        if (!args[1].endsWith(".bib") && !args[2].endsWith(".pdf")) {
            XMPUtil.usage();
            break;
        }

        ParserResult result = BibtexParser.parse(new FileReader(args[1]));

        Optional<BibEntry> bibEntry = result.getDatabase().getEntryByKey(args[0]);

        if (bibEntry.isPresent()) {
            XMPUtil.writeXMP(new File(args[2]), bibEntry.get(), result.getDatabase());

            System.out.println("XMP written.");
        } else {
            System.err.println("Could not find BibEntry " + args[0] + " in " + args[0]);
        }
        break;

    default:
        XMPUtil.usage();
    }
}

From source file:Main.java

public static void error(Component parent, Exception e, String title) {
    StringWriter out = new StringWriter();
    e.printStackTrace(new PrintWriter(out));
    out.flush();// w ww.j  ava 2 s  .co  m
    error(parent, out.getBuffer().toString(), title);
}

From source file:Main.java

public static void nodeToString(Node node, StringBuffer buf) throws TransformerException {

    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    DOMSource dSource = new DOMSource(node);
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(dSource, sr);//from  w  ww.  j a v a 2s.com
    StringWriter anotherSW = (StringWriter) sr.getWriter();

    buf.append(anotherSW.getBuffer());

}

From source file:Main.java

private static String stringForm(Throwable e) {
    StringWriter sw = new StringWriter(256);
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);//from  w w w.  j  a va  2 s.c  o m
    return sw.getBuffer().toString();
}

From source file:Main.java

public static String newStringFromDocument(Document doc)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}

From source file:Main.java

public static String getStringFromXmlDoc(org.w3c.dom.Node node) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.getBuffer().toString().replaceAll("\n|\r", "");
}

From source file:Main.java

public static String asString(Document doc) {
    try {//from  w  w w.  j ava 2  s .c o m
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        throw Throwables.propagate(e);
    }
}

From source file:Main.java

public static String getStringFromXmlDoc(org.w3c.dom.Node node) throws Exception {
    TransformerFactory tf = TransformerFactory
            .newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", null);
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.getBuffer().toString().replaceAll("\n|\r", "");
}

From source file:Main.java

public static String saveXmlToStreamWriter(Document doc) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));

    return writer.getBuffer().toString();
}