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:Main.java

/**
 * Convert XML Document to a String.//w  ww. j  a  v a2s  .c  o m
 * 
 * @param node
 * @return
 */
public static String xmlToString(Node node) {
    try {
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static final String obj2xml(final Object obj, final boolean formatted) throws JAXBException {
    StringWriter writer = new StringWriter();
    JAXBContext contextOut = JAXBContext.newInstance(obj.getClass());
    Marshaller marshallerOut = contextOut.createMarshaller();
    marshallerOut.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
    marshallerOut.marshal(obj, writer);//ww  w.j  a  v a2  s  .  c o m

    return new String(writer.getBuffer());
}

From source file:Main.java

/** Write a DOM document to a string */
public static String xmlDocumentToString(Document document) {
    try {/*from   ww w. j  a  va 2 s  .  c o m*/
        Source source = new DOMSource(document);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static String xmlToString(Node node) {
    try {/*w ww  .ja v  a2  s.c om*/
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

static String getXMLString(Document xmlDoc) throws Exception {
    StringWriter writer = null;
    DOMSource source = new DOMSource(xmlDoc.getDocumentElement());
    writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.transform(source, result);
    StringBuffer strBuf = writer.getBuffer();
    return strBuf.toString();
}

From source file:eu.sisob.uma.restserver.services.crawler.CrawlerTask.java

public static boolean launch(String user, String pass, String task_code, String code_task_folder, String email,
        StringWriter message) {
    if (message == null) {
        return false;
    }//  w ww.  j a  v  a  2  s.c o m
    boolean success = false;
    message.getBuffer().setLength(0);
    File code_task_folder_dir = new File(code_task_folder);

    File csv_data_source_file = FileSystemManager.getFileIfExists(code_task_folder_dir,
            input_data_source_filename_prefix_csv, input_data_source_filename_ext_csv);

    boolean validate = csv_data_source_file != null;

    if (!validate) {
        success = false;
        message.write("You have not uploaded 'data-researchers-urls.csv' file"); //FIXME
    }

    org.dom4j.Document document = null;

    String middle_data_folder = code_task_folder + File.separator + AuthorizationManager.middle_data_dirname;
    File middle_data_dir = null;
    try {
        middle_data_dir = FileSystemManager.createFileAndIfExistsDelete(middle_data_folder);
    } catch (Exception ex) {
        ProjectLogger.LOGGER.error(ex.toString(), ex);
        message.append("The file couldn't be created " + middle_data_dir.getName() + "\r\n");
        return false;
    }

    String results_data_folder = code_task_folder + File.separator + AuthorizationManager.results_dirname;
    File results_data_dir = null;
    try {
        results_data_dir = FileSystemManager.createFileAndIfExistsDelete(results_data_folder);
    } catch (Exception ex) {
        ProjectLogger.LOGGER.error(ex.toString(), ex);
        message.append("The file couldn't be created " + results_data_dir.getName() + "\r\n");
        return false;
    }

    try {
        File middle_data_source_file = new File(middle_data_dir, middle_data_source_filename_xml);
        validate = FileFormatConversor.createResearchersXMLFileFromCSV(csv_data_source_file,
                middle_data_source_file);
    } catch (Exception ex) {
        ProjectLogger.LOGGER.error(ex.getMessage(), ex);
        validate = false;
    }

    if (!validate) {
        message.append("The format of '" + csv_data_source_file.getName()
                + "' does not seems be correct. Please check the headers of the csv file (read in the instructions which are optionals) and be sure that the field separators are ';'. Please read the intructions of the task."
                + "\r\n");
        return false;
    }

    try {
        File xmlFile = new File(middle_data_folder, middle_data_source_filename_xml);
        org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader();
        document = reader.read(xmlFile);
    } catch (Exception ex) {
        message.write("The format of '" + csv_data_source_file.getName() + "' does not seems be correct"); //FIXME
        ProjectLogger.LOGGER.error(message.toString(), ex);
        return false;
    }

    String out_filename = csv_data_source_file.getName().replace(input_data_source_filename_prefix_csv,
            output_data_source_filename_csv);
    File csv_data_output_file = new File(results_data_dir, out_filename);
    ResearchersCrawlerTaskInRest task = new ResearchersCrawlerTaskInRest(document,
            new File(ResearchersCrawlerService.CRAWLER_DATA_PATH), middle_data_dir, results_data_dir,
            csv_data_output_file, user, pass, task_code, code_task_folder, email);
    try {

        ResearchersCrawlerService.getInstance().addExecution(new CallbackableTaskExecution(task));
        success = true;
        message.write(TheResourceBundle.getString("Jsp Task Executed Msg"));

    } catch (InterruptedException ex) {

        message.write(TheResourceBundle.getString("Jsp Task Executed Error Msg"));
        ProjectLogger.LOGGER.error(message.toString(), ex);
        return false;
    }

    return true;
}

From source file:org.apache.axis2.jaxws.WebServiceExceptionLogger.java

/**
 * Get a string containing the stack of the specified exception
 *
 * @param e/*from   ww w.ja v  a2  s .  co  m*/
 * @return
 */
private static String stackToString(Throwable e) {
    java.io.StringWriter sw = new java.io.StringWriter();
    java.io.BufferedWriter bw = new java.io.BufferedWriter(sw);
    java.io.PrintWriter pw = new java.io.PrintWriter(bw);
    e.printStackTrace(pw);
    pw.close();
    String text = sw.getBuffer().toString();
    // Jump past the throwable
    text = text.substring(text.indexOf("at"));
    return text;
}

From source file:com.netflix.hystrix.serial.SerialHystrixUtilization.java

public static String toJsonString(HystrixUtilization utilization) {
    StringWriter jsonString = new StringWriter();

    try {//from w  ww  .j a v a  2 s. c o  m
        JsonGenerator json = jsonFactory.createGenerator(jsonString);

        serializeUtilization(utilization, json);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

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

From source file:com.netflix.hystrix.serial.SerialHystrixRequestEvents.java

public static String toJsonString(HystrixRequestEvents requestEvents) {
    StringWriter jsonString = new StringWriter();

    try {//from  w  w w .  j a  v  a 2s  .c om
        JsonGenerator json = jsonFactory.createGenerator(jsonString);

        serializeRequestEvents(requestEvents, json);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

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

From source file:Main.java

public static String xmlToString(Node node) {
    try {/*from w w  w.  j  a  v  a  2s.  c om*/
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}