Example usage for org.apache.commons.lang3 StringEscapeUtils escapeCsv

List of usage examples for org.apache.commons.lang3 StringEscapeUtils escapeCsv

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringEscapeUtils escapeCsv.

Prototype

public static final String escapeCsv(final String input) 

Source Link

Document

<p>Returns a String value for a CSV column enclosed in double quotes, if required.</p> <p>If the value contains a comma, newline or double quote, then the String value is returned enclosed in double quotes.</p> <p>Any double quote characters in the value are escaped with another double quote.</p> <p>If the value does not contain a comma, newline or double quote, then the String value is returned unchanged.</p> see <a href="http://en.wikipedia.org/wiki/Comma-separated_values">Wikipedia</a> and <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>.

Usage

From source file:org.mfcrawler.model.export.csv.ExportPagesCsvData.java

/**
 * Exports data of pages with a minimum score in a CSV file
 * @param file the file used to export// w w w  . j  a  v  a2  s  .  c  o  m
 * @param minScore minimum score for a page
 */
public static void export(File file, Double minScore) {
    try {
        FileWriter csvFileWriter = new FileWriter(file, false);
        csvFileWriter.write(CSV_HEADER);

        ExportPageDAO exportPageDao = new ExportPageDAO();
        PageDbIterator pageIterator = exportPageDao.getPageListToExport(minScore);

        while (pageIterator.hasNext()) {
            Page page = pageIterator.next();
            csvFileWriter.write(StringEscapeUtils.escapeCsv(page.getLink().getUrl()));
            csvFileWriter.write(CSV_COLUMN_SEPARATOR);
            csvFileWriter.write(StringEscapeUtils.escapeCsv(page.getTitle()));
            csvFileWriter.write(CSV_COLUMN_SEPARATOR);
            csvFileWriter.write(StringEscapeUtils.escapeCsv(ConversionUtils.toString(page.getCrawlTime())));
            csvFileWriter.write(CSV_COLUMN_SEPARATOR);
            csvFileWriter.write(ConversionUtils.toString(page.getInnerDeep()));
            csvFileWriter.write(CSV_COLUMN_SEPARATOR);
            csvFileWriter.write(ConversionUtils.toString(page.getOuterDeep()));
            csvFileWriter.write(CSV_COLUMN_SEPARATOR);
            csvFileWriter.write(ConversionUtils.toString(page.getScore()));
            csvFileWriter.write(CSV_LINE_SEPARATOR);
        }

        csvFileWriter.flush();
        csvFileWriter.close();
    } catch (IOException ie) {
        Logger.getLogger(ExportSitesGexf.class.getName()).log(Level.SEVERE, "Error to write CSV data", ie);
    }
}

From source file:org.mfcrawler.model.export.csv.ExportPagesCsvLinks.java

/**
 * Exports links of pages with a minimum score in a CSV file
 * @param file the file used to export//from w w  w  . ja  v a  2s .com
 * @param minScore minimum score for a page
 */
public static void export(File file, Double minScore) {
    try {
        FileWriter csvFileWriter = new FileWriter(file, false);
        csvFileWriter.write(CSV_HEADER);

        ExportPageDAO exportPageDao = new ExportPageDAO();
        LinkDbIterator linkIterator = exportPageDao.getSourceLinkList(minScore);

        while (linkIterator.hasNext()) {
            Link sourceLink = linkIterator.next();
            List<Link> targetLinkList = exportPageDao.getTargetLinkList(sourceLink, minScore);

            for (Link targetLink : targetLinkList) {
                csvFileWriter.write(StringEscapeUtils.escapeCsv(sourceLink.getUrl()));
                csvFileWriter.write(CSV_COLUMN_SEPARATOR);
                csvFileWriter.write(StringEscapeUtils.escapeCsv(targetLink.getUrl()));
                csvFileWriter.write(CSV_LINE_SEPARATOR);
            }
        }

        csvFileWriter.flush();
        csvFileWriter.close();
    } catch (IOException ie) {
        Logger.getLogger(ExportSitesGexf.class.getName()).log(Level.SEVERE, "Error to write CSV links", ie);
    }
}

From source file:org.mfcrawler.model.export.csv.ExportSitesCsvData.java

/**
 * Exports data of sites with a minimum score in a CSV file
 * @param file the file used to export// w  ww .j  a v  a2s  .  c  om
 * @param minTotalScore minimum total score for a site
 */
public static void export(File file, Double minTotalScore) {
    try {
        FileWriter csvFileWriter = new FileWriter(file, false);
        csvFileWriter.write(CSV_HEADER);

        ExportSiteDAO exportSiteDao = new ExportSiteDAO();
        List<Site> siteList = exportSiteDao.getSiteListToExport(minTotalScore);

        for (Site site : siteList) {
            csvFileWriter.write(StringEscapeUtils.escapeCsv(site.getDomain().getName()));
            csvFileWriter.write(CSV_COLUMN_SEPARATOR);
            csvFileWriter.write(StringEscapeUtils.escapeCsv(ConversionUtils.toString(site.getCrawlTime())));
            csvFileWriter.write(CSV_COLUMN_SEPARATOR);
            csvFileWriter.write(ConversionUtils.toString(site.getCrawledPagesNumber()));
            csvFileWriter.write(CSV_COLUMN_SEPARATOR);
            csvFileWriter.write(ConversionUtils.toString(site.getTotalScore()));
            csvFileWriter.write(CSV_LINE_SEPARATOR);
        }

        csvFileWriter.flush();
        csvFileWriter.close();
    } catch (IOException ie) {
        Logger.getLogger(ExportSitesGexf.class.getName()).log(Level.SEVERE, "Error to write CSV data", ie);
    }
}

From source file:org.mfcrawler.model.export.csv.ExportSitesCsvLinks.java

/**
 * Exports links of sites with a minimum score in a CSV file
 * @param file the file used to export/*from  w w  w .j a  va 2  s  .com*/
 * @param minTotalScore minimum total score for a site
 */
public static void export(File file, Double minTotalScore) {
    try {
        FileWriter csvFileWriter = new FileWriter(file, false);
        csvFileWriter.write(CSV_HEADER);

        ExportSiteDAO exportSiteDao = new ExportSiteDAO();
        List<Domain> domainList = exportSiteDao.getDomainListToExport(minTotalScore);

        for (Domain sourceDomain : domainList) {
            List<Domain> targetDomainList = exportSiteDao.getTargetDomainList(sourceDomain, domainList);
            for (Domain targetDomain : targetDomainList) {
                csvFileWriter.write(StringEscapeUtils.escapeCsv(sourceDomain.getName()));
                csvFileWriter.write(CSV_COLUMN_SEPARATOR);
                csvFileWriter.write(StringEscapeUtils.escapeCsv(targetDomain.getName()));
                csvFileWriter.write(CSV_LINE_SEPARATOR);
            }
        }

        csvFileWriter.flush();
        csvFileWriter.close();
    } catch (IOException ie) {
        Logger.getLogger(ExportSitesGexf.class.getName()).log(Level.SEVERE, "Error to write CSV links", ie);
    }
}

From source file:org.moeaframework.analysis.collector.Accumulator.java

/**
 * Returns the contents of this accumulator as a string in CSV format.
 * //from w  w  w  .j a va 2s .c  om
 * @return the contents of this accumulator as a string in CSV format
 */
public String toCSV() {
    StringBuilder sb = new StringBuilder();
    boolean firstValue = true;

    // determine the ordering of the fields
    Set<String> fields = new LinkedHashSet<String>();
    fields.add("NFE");

    if (data.containsKey("Elapsed Time")) {
        fields.add("Elapsed Time");
    }

    fields.addAll(keySet());

    // create the header
    for (String field : fields) {
        if (!firstValue) {
            sb.append(", ");
        }

        sb.append(StringEscapeUtils.escapeCsv(field));
        firstValue = false;
    }

    // create the data
    for (int i = 0; i < size("NFE"); i++) {
        sb.append(Settings.NEW_LINE);
        firstValue = true;

        for (String field : fields) {
            if (!firstValue) {
                sb.append(", ");
            }

            sb.append(StringEscapeUtils.escapeCsv(get(field, i).toString()));
            firstValue = false;
        }
    }

    return sb.toString();
}

From source file:org.repodriller.persistence.csv.CSVFile.java

@Override
public synchronized void write(Object... line) throws CSVFileFormatException {
    if (header != null && header.length != line.length)
        throw new CSVFileFormatException("CSV Header Columns Number Differs From Writer Columns Number.");

    boolean first = true;
    for (Object o : line) {
        if (!first)
            ps.print(",");

        if (o == null)
            ps.print("null");
        else {//ww w .  j a v  a 2  s  . co m
            String field = o.toString();
            field = StringEscapeUtils.escapeCsv(field);
            ps.print(field);
        }
        first = false;
    }

    ps.println();
    ps.flush();

}

From source file:org.transitime.db.GenericCsvQuery.java

@Override
protected void addRow(List<Object> values) {
    int column = 0;
    for (Object o : values) {
        // Comma separate the cells
        if (column++ > 0)
            sb.append(',');

        // Output value as long as it is not null
        if (o != null) {
            // Strings should be escaped but numbers can be output directly
            if (o instanceof String)
                sb.append(StringEscapeUtils.escapeCsv((String) o));
            else//from w  ww  . j  a v a  2 s  . co  m
                sb.append(o);
        }
    }
    sb.append('\n');

}

From source file:password.pwm.util.java.StringUtil.java

public static String escapeCsv(final String input) {
    return StringEscapeUtils.escapeCsv(input);
}

From source file:serposcope.controllers.google.GoogleGroupController.java

@FilterWith({ XSRFFilter.class, AdminFilter.class })
public Result exportSearches(Context context, @Params("id[]") String[] ids) {
    FlashScope flash = context.getFlashScope();
    Group group = context.getAttribute("group", Group.class);

    if (ids == null || ids.length == 0) {
        flash.error("error.noSearchSelected");
        return Results.redirect(
                router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }// w  w w . j  a va 2s  .c  o m

    List<GoogleSearch> searches = new ArrayList<>();
    for (String id : ids) {
        GoogleSearch search = null;
        try {
            search = getSearch(context, Integer.parseInt(id));
        } catch (Exception ex) {
            search = null;
        }

        if (search == null) {
            flash.error("error.invalidSearch");
            return Results.redirect(
                    router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
        }

        searches.add(search);
    }

    StringBuilder builder = new StringBuilder();
    for (GoogleSearch search : searches) {
        builder.append(StringEscapeUtils.escapeCsv(search.getKeyword())).append(",");
        builder.append(search.getTld() != null ? search.getTld() : "com").append(",");
        builder.append(search.getDatacenter() != null ? search.getDatacenter() : "").append(",");
        builder.append(
                search.getDevice() != null ? (search.getDevice() == GoogleDevice.DESKTOP ? "desktop" : "mobile")
                        : "")
                .append(",");
        builder.append(StringEscapeUtils.escapeCsv(search.getLocal() != null ? search.getLocal() : ""))
                .append(",");
        builder.append(StringEscapeUtils
                .escapeCsv(search.getCustomParameters() != null ? search.getCustomParameters() : ""))
                .append("\n");
    }

    return Results.ok().text().render(builder.toString());
}

From source file:serposcope.controllers.google.GoogleSearchController.java

public Result exportSerp(Context context, @PathParam("searchId") Integer searchId,
        @Param("date") String pdate) {
    GoogleSerp serp = null;//from  w w w.j av a  2 s .  co m
    LocalDate date = null;
    try {
        date = LocalDate.parse(pdate);
    } catch (Exception ex) {
    }
    if (date != null) {
        List<Run> runs = baseDB.run.findByDay(Module.GOOGLE, date);
        if (!runs.isEmpty()) {
            GoogleSearch search = getSearch(context, searchId);
            if (search != null) {
                serp = googleDB.serp.get(runs.get(0).getId(), search.getId());
            }
        }
    }

    if (serp == null) {
        return Results.ok().text().renderRaw("SERP not found");
    }

    boolean exportRank = context.getParameter("rank") != null;
    boolean exportD1 = context.getParameter("d1") != null;
    boolean exportD7 = context.getParameter("d7") != null;
    boolean exportD30 = context.getParameter("d30") != null;
    boolean exportD90 = context.getParameter("d90") != null;

    int position = 0;
    StringBuilder builder = new StringBuilder();
    for (GoogleSerpEntry entry : serp.getEntries()) {
        ++position;
        if (exportRank) {
            builder.append(position).append(",");
        }
        builder.append(StringEscapeUtils.escapeCsv(entry.getUrl())).append(",");
        if (exportD1) {
            Short rank = entry.getMap().getOrDefault((short) 1, (short) GoogleRank.UNRANKED);
            builder.append(rank != GoogleRank.UNRANKED ? rank.intValue() : "").append(",");
        }
        if (exportD7) {
            Short rank = entry.getMap().getOrDefault((short) 7, (short) GoogleRank.UNRANKED);
            builder.append(rank != GoogleRank.UNRANKED ? rank.intValue() : "").append(",");
        }
        if (exportD30) {
            Short rank = entry.getMap().getOrDefault((short) 30, (short) GoogleRank.UNRANKED);
            builder.append(rank != GoogleRank.UNRANKED ? rank.intValue() : "").append(",");
        }
        if (exportD90) {
            Short rank = entry.getMap().getOrDefault((short) 90, (short) GoogleRank.UNRANKED);
            builder.append(rank != GoogleRank.UNRANKED ? rank.intValue() : "").append(",");
        }
        if (builder.length() > 0) {
            builder.setCharAt(builder.length() - 1, '\n');
        }
    }

    return Results.text()
            .addHeader("Content-Disposition",
                    "attachment; filename=\"" + serp.getRunDay().toLocalDate() + ".csv\"")
            .renderRaw(builder.toString());
}