CsvTranslators.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.text.translate;

  18. import java.io.IOException;
  19. import java.io.Writer;

  20. import org.apache.commons.lang3.CharUtils;
  21. import org.apache.commons.lang3.StringUtils;

  22. /**
  23.  * This class holds inner classes for escaping/unescaping Comma Separated Values.
  24.  * <p>
  25.  * In general the use a high level API like <a href="https://commons.apache.org/proper/commons-csv/">Apache Commons
  26.  * CSV</a> should be preferred over these low level classes.
  27.  * </p>
  28.  *
  29.  * @see <a href="https://commons.apache.org/proper/commons-csv/apidocs/index.html">Apache Commons CSV</a>
  30.  */
  31. public final class CsvTranslators {

  32.     /**
  33.      * Translator for escaping Comma Separated Values.
  34.      */
  35.     public static class CsvEscaper extends SinglePassTranslator {

  36.         /**
  37.          * Construct a new instance.
  38.          */
  39.         public CsvEscaper() {
  40.             // empty
  41.         }

  42.         @Override
  43.         void translateWhole(final CharSequence input, final Writer writer) throws IOException {
  44.             final String inputSting = input.toString();
  45.             if (StringUtils.containsNone(inputSting, CSV_SEARCH_CHARS)) {
  46.                 writer.write(inputSting);
  47.             } else {
  48.                 // input needs quoting
  49.                 writer.write(CSV_QUOTE);
  50.                 writer.write(StringUtils.replace(inputSting, CSV_QUOTE_STR, CSV_ESCAPED_QUOTE_STR));
  51.                 writer.write(CSV_QUOTE);
  52.             }
  53.         }
  54.     }
  55.     /**
  56.      * Translator for unescaping escaped Comma Separated Value entries.
  57.      */
  58.     public static class CsvUnescaper extends SinglePassTranslator {

  59.         /**
  60.          * Construct a new instance.
  61.          */
  62.         public CsvUnescaper() {
  63.             // empty
  64.         }

  65.         @Override
  66.         void translateWhole(final CharSequence input, final Writer writer) throws IOException {
  67.             // is input not quoted?
  68.             if (input.charAt(0) != CSV_QUOTE || input.charAt(input.length() - 1) != CSV_QUOTE) {
  69.                 writer.write(input.toString());
  70.                 return;
  71.             }

  72.             // strip quotes
  73.             final String quoteless = input.subSequence(1, input.length() - 1).toString();

  74.             if (StringUtils.containsAny(quoteless, CSV_SEARCH_CHARS)) {
  75.                 // deal with escaped quotes; ie) ""
  76.                 writer.write(StringUtils.replace(quoteless, CSV_ESCAPED_QUOTE_STR, CSV_QUOTE_STR));
  77.             } else {
  78.                 writer.write(quoteless);
  79.             }
  80.         }
  81.     }
  82.     /** Comma character. */
  83.     private static final char CSV_DELIMITER = ',';
  84.     /** Quote character. */
  85.     private static final char CSV_QUOTE = '"';
  86.     /** Quote character converted to string. */
  87.     private static final String CSV_QUOTE_STR = String.valueOf(CSV_QUOTE);

  88.     /** Escaped quote string. */
  89.     private static final String CSV_ESCAPED_QUOTE_STR = CSV_QUOTE_STR + CSV_QUOTE_STR;

  90.     /** CSV key characters in an array. */
  91.     private static final char[] CSV_SEARCH_CHARS = { CSV_DELIMITER, CSV_QUOTE, CharUtils.CR, CharUtils.LF };

  92.     /** Hidden constructor. */
  93.     private CsvTranslators() {
  94.         // empty
  95.     }
  96. }