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

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

Introduction

In this page you can find the example usage for org.apache.commons.text 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:com.seleniumtests.util.StringUtility.java

/**
 * Encode string according to provided format
 * @param message      message to encode
 * @param format      'xml', 'csv', 'html', 'json', 'text'. the later does not change anything
 * @return//from  w  w w.  j  av  a2  s.c  o  m
 */
public static String encodeString(String message, String format) {
    String newMessage;
    switch (format) {
    case "xml":
        newMessage = StringEscapeUtils.escapeXml11(message);
        break;
    case "csv":
        newMessage = StringEscapeUtils.escapeCsv(message);
        break;
    case "html":
        newMessage = StringEscapeUtils.escapeHtml4(message);
        break;
    case "json":
        newMessage = StringEscapeUtils.escapeJson(message);
        break;
    case "text":
        newMessage = message;
        break;
    default:
        throw new CustomSeleniumTestsException("only escaping of 'xml', 'html', 'csv', 'json' is allowed");
    }
    return newMessage;
}

From source file:org.apache.nifi.processors.standard.AttributesToCSV.java

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    final FlowFile original = session.get();
    if (original == null) {
        return;/* w  w w . ja  va 2s  .c om*/
    }
    if (context.getProperty(ATTRIBUTES_REGEX).isSet()) {
        pattern = Pattern.compile(
                context.getProperty(ATTRIBUTES_REGEX).evaluateAttributeExpressions(original).getValue());
    }

    final Set<String> attributeList = attributeListStringToSet(
            context.getProperty(ATTRIBUTES_LIST).evaluateAttributeExpressions(original).getValue());
    final Map<String, String> atrList = buildAttributesMapForFlowFile(original, attributeList, pattern);

    //escape attribute values
    int index = 0;
    final int atrListSize = atrList.values().size() - 1;
    final StringBuilder sbValues = new StringBuilder();
    for (final Map.Entry<String, String> attr : atrList.entrySet()) {
        sbValues.append(StringEscapeUtils.escapeCsv(attr.getValue()));
        sbValues.append(index++ < atrListSize ? OUTPUT_SEPARATOR : "");
    }

    //build the csv header if needed
    final StringBuilder sbNames = new StringBuilder();
    if (includeSchema) {
        index = 0;
        for (final Map.Entry<String, String> attr : atrList.entrySet()) {
            sbNames.append(StringEscapeUtils.escapeCsv(attr.getKey()));
            sbNames.append(index++ < atrListSize ? OUTPUT_SEPARATOR : "");
        }
    }

    try {
        if (destinationContent) {
            FlowFile conFlowfile = session.write(original, (in, out) -> {
                if (includeSchema) {
                    sbNames.append(System.getProperty("line.separator"));
                    out.write(sbNames.toString().getBytes());
                }
                out.write(sbValues.toString().getBytes());
            });
            conFlowfile = session.putAttribute(conFlowfile, CoreAttributes.MIME_TYPE.key(), OUTPUT_MIME_TYPE);
            session.transfer(conFlowfile, REL_SUCCESS);
        } else {
            FlowFile atFlowfile = session.putAttribute(original, DATA_ATTRIBUTE_NAME, sbValues.toString());
            if (includeSchema) {
                session.putAttribute(original, SCHEMA_ATTRIBUTE_NAME, sbNames.toString());
            }
            session.transfer(atFlowfile, REL_SUCCESS);
        }
    } catch (Exception e) {
        getLogger().error(e.getMessage());
        session.transfer(original, REL_FAILURE);
    }
}