org.apache.commons.lang3.StringEscapeUtils.escapeCsv()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(176)

本文整理了Java中org.apache.commons.lang3.StringEscapeUtils.escapeCsv()方法的一些代码示例,展示了StringEscapeUtils.escapeCsv()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StringEscapeUtils.escapeCsv()方法的具体详情如下:
包路径:org.apache.commons.lang3.StringEscapeUtils
类名称:StringEscapeUtils
方法名:escapeCsv

StringEscapeUtils.escapeCsv介绍

[英]Returns a String value for a CSV column enclosed in double quotes, if required.

If the value contains a comma, newline or double quote, then the String value is returned enclosed in double quotes.

Any double quote characters in the value are escaped with another double quote.

If the value does not contain a comma, newline or double quote, then the String value is returned unchanged.
see Wikipedia and RFC 4180.
[中]如果需要,返回用双引号括起来的CSV列的字符串值。
如果该值包含逗号、换行符或双引号,则返回的字符串值用双引号括起来。
值中的任何双引号字符都用另一个双引号转义。
如果该值不包含逗号、换行符或双引号,则返回的字符串值将保持不变。
Wikipedia和{$1$}。

代码示例

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testEscapeCsvString() throws Exception {
  assertEquals("foo.bar",            StringEscapeUtils.escapeCsv("foo.bar"));
  assertEquals("\"foo,bar\"",        StringEscapeUtils.escapeCsv("foo,bar"));
  assertEquals("\"foo\nbar\"",       StringEscapeUtils.escapeCsv("foo\nbar"));
  assertEquals("\"foo\rbar\"",       StringEscapeUtils.escapeCsv("foo\rbar"));
  assertEquals("\"foo\"\"bar\"",     StringEscapeUtils.escapeCsv("foo\"bar"));
  assertEquals("foo\uD84C\uDFB4bar", StringEscapeUtils.escapeCsv("foo\uD84C\uDFB4bar"));
  assertEquals("",   StringEscapeUtils.escapeCsv(""));
  assertNull(StringEscapeUtils.escapeCsv(null));
}

代码示例来源:origin: pmd/pmd

writer.append(String.valueOf(mark.getLineCount())).append(separator);
writer.append(StringEscapeUtils.escapeCsv(mark.getFilename()));
if (marks.hasNext()) {
  writer.append(separator);

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testEscapeSurrogatePairs() throws Exception {
  assertEquals("\uD83D\uDE30", StringEscapeUtils.escapeCsv("\uD83D\uDE30"));
  // Examples from https://en.wikipedia.org/wiki/UTF-16
  assertEquals("\uD800\uDC00", StringEscapeUtils.escapeCsv("\uD800\uDC00"));
  assertEquals("\uD834\uDD1E", StringEscapeUtils.escapeCsv("\uD834\uDD1E"));
  assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeCsv("\uDBFF\uDFFD"));
  assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeHtml3("\uDBFF\uDFFD"));
  assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeHtml4("\uDBFF\uDFFD"));
  assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeXml("\uDBFF\uDFFD"));
}

代码示例来源:origin: org.eclipse.dirigible/dirigible-api-utils

/**
 * Escape CSV.
 *
 * @param input
 *            the input
 * @return the escaped input
 */
public static final String escapeCsv(String input) {
  return StringEscapeUtils.escapeCsv(input);
}

代码示例来源:origin: com.github.dactiv/dactiv-common

/**
 * Csv 转码.
 */
public static String escapeCsv(String csv) {
  return StringEscapeUtils.escapeCsv(csv);
}

代码示例来源:origin: org.eclipse.dirigible/dirigible-api-facade-utils

/**
 * Escape CSV.
 *
 * @param input
 *            the input
 * @return the escaped input
 */
public static final String escapeCsv(String input) {
  return StringEscapeUtils.escapeCsv(input);
}

代码示例来源:origin: osmlab/atlas

@Override
  public String toString()
  {
    return this.tag + "," + this.type + "," + StringEscapeUtils.escapeCsv(this.subType);
  }
}

代码示例来源:origin: Transitime/core

@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
        sb.append(o);
    }
  }
  sb.append('\n');
}

代码示例来源:origin: in-the-keyhole/khs-sherpa

private String applyEncoding(String value,String format) {
  String result = value;
  if (format != null) {
    if (format.equals(Encode.XML)) {
      result = StringEscapeUtils.escapeXml(value);        
    } else if (format.equals(Encode.HTML)) {
      result = StringEscapeUtils.escapeHtml4(value);        
    } else if (format.equals(Encode.CSV)) {
      result = StringEscapeUtils.escapeCsv(value);
    }
  }
   
  return result;
}

代码示例来源:origin: opendedup/sdfs

public static byte[] serializeHashMap(HashMap<String, String> map) {
  StringBuffer keys = new StringBuffer();
  Iterator<String> iter = map.keySet().iterator();
  while (iter.hasNext()) {
    String key = iter.next();
      keys.append(StringEscapeUtils.escapeCsv(key));
      if (iter.hasNext())
        keys.append(",");
  }
  StringBuffer values = new StringBuffer();
  iter = map.values().iterator();
  while (iter.hasNext()) {
    String key = iter.next();
      values.append(StringEscapeUtils.escapeCsv(key));
    if (iter.hasNext())
      values.append(",");
  }
  byte[] kb = keys.toString().getBytes();
  byte[] vb = values.toString().getBytes();
  byte[] out = new byte[kb.length + vb.length + 8];
  ByteBuffer buf = ByteBuffer.wrap(out);
  buf.putInt(kb.length);
  buf.put(kb);
  buf.putInt(vb.length);
  buf.put(vb);
  return buf.array();
}

代码示例来源:origin: org.lenskit/lenskit-core

@Override
public synchronized void writeRow(List<?> row) throws IOException {
  Preconditions.checkState(writer != null, "writer has been closed");
  if (layout != null) {
    checkRowWidth(row.size());
  }
  boolean first = true;
  for (Object val: row) {
    if (!first) {
      writer.write(',');
    }
    first = false;
    if (val != null) {
      writer.write(escapeCsv(val.toString()));
    }
  }
  writer.newLine();
}

代码示例来源:origin: org.grouplens.lenskit/lenskit-eval

@Override
public synchronized void writeRow(List<?> row) throws IOException {
  Preconditions.checkState(writer != null, "writer has been closed");
  if (layout != null) {
    checkRowWidth(row.size());
  }
  boolean first = true;
  for (Object val: row) {
    if (!first) {
      writer.write(',');
    }
    first = false;
    if (val != null) {
      writer.write(escapeCsv(val.toString()));
    }
  }
  writer.newLine();
  writer.flush();
}

代码示例来源:origin: org.jboss.windup.utils/windup-utils

/**
 * Serializes the timing data to a "~" delimited file at outputPath.
 */
public void serializeTimingData(Path outputPath)
{
  //merge subThreads instances into the main instance
  merge();
  try (FileWriter fw = new FileWriter(outputPath.toFile()))
  {
    fw.write("Number Of Executions, Total Milliseconds,  Milliseconds per execution, Type\n");
    for (Map.Entry<String, TimingData> timing : executionInfo.entrySet())
    {
      TimingData data = timing.getValue();
      long totalMillis = (data.totalNanos / 1000000);
      double millisPerExecution = (double) totalMillis / (double) data.numberOfExecutions;
      fw.write(String.format("%6d, %6d, %8.2f, %s\n",
        data.numberOfExecutions, totalMillis, millisPerExecution,
        StringEscapeUtils.escapeCsv(timing.getKey())
      ));
    }
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }
}

代码示例来源:origin: windup/windup

/**
 * Serializes the timing data to a "~" delimited file at outputPath.
 */
public void serializeTimingData(Path outputPath)
{
  //merge subThreads instances into the main instance
  merge();
  try (FileWriter fw = new FileWriter(outputPath.toFile()))
  {
    fw.write("Number Of Executions, Total Milliseconds,  Milliseconds per execution, Type\n");
    for (Map.Entry<String, TimingData> timing : executionInfo.entrySet())
    {
      TimingData data = timing.getValue();
      long totalMillis = (data.totalNanos / 1000000);
      double millisPerExecution = (double) totalMillis / (double) data.numberOfExecutions;
      fw.write(String.format("%6d, %6d, %8.2f, %s\n",
        data.numberOfExecutions, totalMillis, millisPerExecution,
        StringEscapeUtils.escapeCsv(timing.getKey())
      ));
    }
  }
  catch (Exception e)
  {
    e.printStackTrace();
  }
}

代码示例来源:origin: org.xworker/xworker_core

public static String escapeCsv(ActionContext actionContext){
  Thing self = actionContext.getObject("self");
  String str  = (String) self.doAction("getStr", actionContext);
  return StringEscapeUtils.escapeCsv(str);
}

代码示例来源:origin: i38/kaixin

public void write(OutputStream output) throws WebApplicationException {
    try {
      //表头
      String csvEncode = PropsUtil.get(PropsKeys.CSV_EXPORT_ENCODE);
      for(Field field : fieldList) {
        output.write(field.getLabel().getBytes(csvEncode));
        output.write(",".getBytes());
      }
      output.write("\r\n".getBytes());
      //记录
      for (Map<String,Object> line : results) {
        for(Field field : fieldList) {
          String value = StringEscapeUtils.escapeCsv(line.get(field.getName()) == null ? "" : line.get(field.getName()).toString()) +",";
          output.write(value.getBytes(csvEncode));
        }
        output.write("\r\n".getBytes());
      }
    } catch (Exception e) {
      throw new WebApplicationException(e);
    }
  }
};

代码示例来源:origin: uk.q3c.krail/krail

/**
 * {@inheritDoc}
 */
public String convertToString(@Nonnull OptionList<E> value) {
  checkNotNull(value);
  if (value.isEmpty()) {
    return "";
  }
  StringBuilder buf = new StringBuilder();
  boolean first = true;
  for (E e : value.getList()) {
    if (!first) {
      buf.append(separator);
    } else {
      first = false;
    }
    String s = converter.convertValueToString(e);
    buf.append(StringEscapeUtils.escapeCsv(s));
  }
  return buf.toString();
}

代码示例来源:origin: net.sourceforge.pmd/pmd-core

writer.append(String.valueOf(mark.getLineCount())).append(separator);
writer.append(StringEscapeUtils.escapeCsv(mark.getFilename()));
if (marks.hasNext()) {
  writer.append(separator);

代码示例来源:origin: uk.q3c.krail/krail

/**
 * Converts {@code model} to String
 *
 * @param model the model to convert
 * @return Comma separated String representation of the list contents
 */
public String convertToString(@Nonnull AnnotationOptionList model) {
  checkNotNull(model);
  if (model.isEmpty()) {
    return "";
  }
  StringBuilder buf = new StringBuilder();
  ImmutableList<Class<? extends Annotation>> modelList = model.getList();
  ClassConverter classConverter = new ClassConverter();
  boolean first = true;
  for (Class<? extends Annotation> e : modelList) {
    if (!first) {
      buf.append(separator);
    } else {
      first = false;
    }
    buf.append(StringEscapeUtils.escapeCsv(classConverter.convertToString(e)));
  }
  return buf.toString();
}

代码示例来源:origin: uk.q3c.util/q3c-util

/**
 * Converts {@code model} to String
 *
 * @param model the model to convert
 * @return Comma separated String representation of the list contents
 */
public String convertToString(AnnotationList model) {
  checkNotNull(model);
  if (model.isEmpty()) {
    return "";
  }
  StringBuilder buf = new StringBuilder();
  ImmutableList<Class<? extends Annotation>> modelList = model.getList();
  ClassConverter classConverter = new ClassConverter();
  boolean first = true;
  for (Class<? extends Annotation> e : modelList) {
    if (!first) {
      buf.append(separator);
    } else {
      first = false;
    }
    buf.append(StringEscapeUtils.escapeCsv(classConverter.convertToString(e)));
  }
  return buf.toString();
}

相关文章