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:eu.sisob.uma.restserver.services.internalcvfiles.InternalCVFilesTask.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;
    }//www .  j  a  va 2 s . c  om

    message.getBuffer().setLength(0);
    File code_task_folder_dir = new File(code_task_folder);

    File documents_dir = code_task_folder_dir;

    File input_file = FileSystemManager.getFileIfExists(code_task_folder_dir,
            input_data_source_filename_prefix_csv, input_data_source_filename_ext_csv);
    boolean validate = input_file != null;

    if (!validate) {
        message.write("You have not uploaded any file like this '" + input_data_source_filename_prefix_csv + "*"
                + input_data_source_filename_ext_csv + "' file");
        return false;
    }

    try {
        validate = FileFormatConversor.checkResearchersCSV(input_file, true);
    } catch (Exception ex) {
        eu.sisob.uma.extractors.adhoc.email.ProjectLogger.LOGGER.error(message.toString(), ex);
        validate = false;
    }

    if (!validate) {
        message.write("The format of '" + input_file.getName() + "' does not seems be correct"); //FIXME
        return false;
    }

    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;
    }

    File zip_file = new File(code_task_folder_dir, input_data_documents_in_zip);

    if (zip_file.exists()) {
        documents_dir = new File(code_task_folder_dir, AuthorizationManager.middle_data_dirname);
        if (!ZipUtil.unZipItToSameFolder(zip_file, documents_dir)) {
            message.write(input_data_documents_in_zip + " cannot bet unziped"); //FIXME
            return false;
        }
    }

    String output_filename = input_file.getName().replace(input_data_source_filename_prefix_csv,
            output_data_source_filename_prefix_csv);
    File output_file = new File(results_data_dir, output_filename);

    String out_filename_2 = input_file.getName().replace(input_data_source_filename_prefix_csv,
            output_data_with_files_source_filename_prefix_csv);
    File output_file_2 = new File(results_data_dir, out_filename_2);

    InternalCVFilesExtractorTaskInRest task = new InternalCVFilesExtractorTaskInRest(user, task_code,
            code_task_folder, email, input_file, documents_dir, output_file,
            new File(results_data_dir, results_dirname), new File(results_data_dir, zip_output_filename),
            output_file_2);

    try {
        EmailExtractorService.getInstance().addExecution((new CallbackableTaskExecution(task)));

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

    return true;
}

From source file:eu.scidipes.toolkits.palibrary.utils.XMLUtils.java

private static String nodeToString(final Node doc, final Properties outputProperties) {
    try {//from   www  .  j  a va  2s.  c o  m
        final Transformer transformer = transformerFactory.newTransformer();
        outputProperties.put(OutputKeys.METHOD, "xml");
        outputProperties.put(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperties(outputProperties);
        final StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (final TransformerException e) {
        LOG.error(e.getMessage(), e);
    }
    return "";
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerException {
    Source source = new DOMSource(node);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(source, result);
    return stringWriter.getBuffer().toString();
}

From source file:org.apache.cxf.management.web.logging.LogRecord.java

/**
 * Creates this object from JUL LogRecord. Most attributes are copied, others are converted as follows:
 * raw {@link java.util.logging.LogRecord#getMessage() message} is formatted with
 * {@link java.util.logging.LogRecord#getParameters() parameters} using {@link MessageFormat}, attached
 * {@link java.util.logging.LogRecord#getThrown() throwable} has full stack trace dumped, and log levels
 * are mapped as specified in {@link LogRecord}.
 * /*  w  w  w  .ja v a  2  s .co  m*/
 * @param julRecord log record to convert.
 * @return conversion result.
 */
public static LogRecord fromJUL(java.util.logging.LogRecord julRecord) {
    Validate.notNull(julRecord, "julRecord is null");
    LogRecord record = new LogRecord();
    record.setDate(new Date(julRecord.getMillis()));
    record.setLevel(LogLevel.fromJUL(julRecord.getLevel()));
    record.setLoggerName(julRecord.getLoggerName());
    if (julRecord.getThrown() != null) {
        StringWriter sw = new StringWriter();
        julRecord.getThrown().printStackTrace(new PrintWriter(sw));
        record.setThrowable(sw.getBuffer().toString());
    }
    if (julRecord.getParameters() != null) {
        record.setMessage(MessageFormat.format(julRecord.getMessage(), julRecord.getParameters()));
    } else {
        record.setMessage(julRecord.getMessage());
    }
    record.setThreadName(Integer.toString(julRecord.getThreadID()));
    return record;
}

From source file:Main.java

public static String documentToString(Document document) throws TransformerException {
    Node rootNode = (Node) document.getDocumentElement();
    Source source = new DOMSource(rootNode);

    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().replace("\n", "");
}

From source file:Main.java

/**
 * convert org.w3c.dom.Document to xml-String
 * //from   ww w  .  j a  v  a  2s  .  c  o m
 * @param document
 * @return String xml
 * @throws Exception
 */
private static String toString(Document document) throws Exception {
    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();
}

From source file:eu.sisob.uma.restserver.services.email.EmailTask.java

public static boolean launch(String user, String pass, String task_code, String code_task_folder, String email,
        List<String> filters, StringWriter message) {
    if (message == null) {
        return false;
    }//from   www .j  ava2s .co  m

    message.getBuffer().setLength(0);
    File code_task_folder_dir = new File(code_task_folder);

    File documents_dir = code_task_folder_dir;

    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) {
        message.write("You have not uploaded any file like this '" + input_data_source_filename_prefix_csv + "*"
                + input_data_source_filename_ext_csv + "' file");
        return false;
    }

    try {
        validate = FileFormatConversor.checkResearchersCSV(csv_data_source_file, true);
    } catch (Exception ex) {
        eu.sisob.uma.extractors.adhoc.email.ProjectLogger.LOGGER.error(message.toString(), ex);
        validate = false;
    }

    if (!validate) {
        message.write("The format of '" + csv_data_source_file.getName() + "' does not seems be correct"); //FIXME
        return false;
    }

    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;
    }

    File zip_file = new File(code_task_folder_dir, input_data_documents_in_zip);

    if (zip_file.exists()) {
        documents_dir = new File(code_task_folder_dir, AuthorizationManager.middle_data_dirname);
        if (!ZipUtil.unZipItToSameFolder(zip_file, documents_dir)) {
            message.write(input_data_documents_in_zip + " cannot bet unziped"); //FIXME
            return false;
        }
    }

    String out_filename = csv_data_source_file.getName().replace(input_data_source_filename_prefix_csv,
            output_data_source_filename_prefix_csv);
    File csv_data_output_file = new File(results_data_dir, out_filename);

    String out_filename_unfounded = csv_data_source_file.getName()
            .replace(input_data_source_filename_prefix_csv, output_data_source_nofound_filename_prefix_csv);
    File csv_data_output_file_unfounded = new File(results_data_dir, out_filename_unfounded);

    String out_filename_wor = csv_data_source_file.getName().replace(input_data_source_filename_prefix_csv,
            output_data_source_norepeat_filename_prefix_csv);
    File csv_data_output_file_wor = new File(results_data_dir, out_filename_wor);

    String out_filename_wor_notfound = csv_data_source_file.getName().replace(
            input_data_source_filename_prefix_csv, output_data_source_nofound_norepeat_filename_prefix_csv);
    File csv_data_output_file_notfound_wor = new File(results_data_dir, out_filename_wor_notfound);

    EmailExtractorTaskInRest task = new EmailExtractorTaskInRest(user, task_code, code_task_folder, email,
            csv_data_source_file, documents_dir, csv_data_output_file, csv_data_output_file_wor,
            csv_data_output_file_unfounded, csv_data_output_file_notfound_wor, filters);

    try {
        EmailExtractorService.getInstance().addExecution((new CallbackableTaskExecution(task)));

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

    return true;
}

From source file:Main.java

public static String getXMLStringFromDocument(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/* w  w w  .  ja v  a  2 s. co  m*/
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
    } catch (TransformerException e) {
        throw new RuntimeException("Error while transforming XML document to String : ", e);
    }
    String output = writer.getBuffer().toString();
    return output;
}

From source file:mrcg.utils.Utils.java

public static String execute(String template, Map<String, Object> map) throws Exception {
    VelocityContext context = new VelocityContext(map);
    StringWriter writer = new StringWriter();
    if (Velocity.evaluate(context, writer, "", template)) {
        return writer.getBuffer().toString();
    } else {// ww w . j  av a2  s  .  c o  m
        throw new RuntimeException("Un unspecified error occured while transforming template");
    }
}

From source file:libcore.tzdata.update_test_app.installupdatetestapp.MainActivity.java

private static String exceptionToString(Exception e) {
    StringWriter writer = new StringWriter();
    e.printStackTrace(new PrintWriter(writer));
    return writer.getBuffer().toString();
}