001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.text.translate;
018
019import java.io.IOException;
020import java.io.Writer;
021
022import org.apache.commons.lang3.CharUtils;
023import org.apache.commons.lang3.StringUtils;
024import org.apache.commons.lang3.Strings;
025
026/**
027 * Holds inner classes for escaping/unescaping Comma Separated Values.
028 * <p>
029 * In general the use a high level API like <a href="https://commons.apache.org/proper/commons-csv/">Apache Commons
030 * CSV</a> should be preferred over these low level classes.
031 * </p>
032 *
033 * @see <a href="https://commons.apache.org/proper/commons-csv/apidocs/index.html">Apache Commons CSV</a>
034 */
035public final class CsvTranslators {
036
037    /**
038     * Translator for escaping Comma Separated Values.
039     */
040    public static class CsvEscaper extends SinglePassTranslator {
041
042        /**
043         * Constructs a new instance.
044         */
045        public CsvEscaper() {
046            // empty
047        }
048
049        @Override
050        void translateWhole(final CharSequence input, final Writer writer) throws IOException {
051            final String inputString = input.toString();
052            if (StringUtils.containsNone(inputString, CSV_SEARCH_CHARS)) {
053                writer.write(inputString);
054            } else {
055                // input needs quoting
056                writer.write(CSV_QUOTE);
057                writer.write(Strings.CS.replace(inputString, CSV_QUOTE_STR, CSV_ESCAPED_QUOTE_STR));
058                writer.write(CSV_QUOTE);
059            }
060        }
061    }
062
063    /**
064     * Translator for unescaping escaped Comma Separated Value entries.
065     */
066    public static class CsvUnescaper extends SinglePassTranslator {
067
068        /**
069         * Constructs a new instance.
070         */
071        public CsvUnescaper() {
072            // empty
073        }
074
075        @Override
076        void translateWhole(final CharSequence input, final Writer writer) throws IOException {
077            // Is input not quoted? A single character cannot be wrapped in a leading and trailing quote.
078            if (input.length() < 2 || input.charAt(0) != CSV_QUOTE || input.charAt(input.length() - 1) != CSV_QUOTE) {
079                writer.write(input.toString());
080                return;
081            }
082            // strip quotes
083            final String quoteless = input.subSequence(1, input.length() - 1).toString();
084            if (StringUtils.containsAny(quoteless, CSV_SEARCH_CHARS)) {
085                // deal with escaped quotes: "" -> "
086                writer.write(Strings.CS.replace(quoteless, CSV_ESCAPED_QUOTE_STR, CSV_QUOTE_STR));
087            } else {
088                writer.write(quoteless);
089            }
090        }
091    }
092
093    /** Comma character. */
094    private static final char CSV_DELIMITER = ',';
095
096    /** Quote character. */
097    private static final char CSV_QUOTE = '"';
098
099    /** Quote character converted to string. */
100    private static final String CSV_QUOTE_STR = String.valueOf(CSV_QUOTE);
101
102    /** Escaped quote string. */
103    private static final String CSV_ESCAPED_QUOTE_STR = CSV_QUOTE_STR + CSV_QUOTE_STR;
104
105    /** CSV key characters in an array. */
106    private static final char[] CSV_SEARCH_CHARS = { CSV_DELIMITER, CSV_QUOTE, CharUtils.CR, CharUtils.LF };
107
108    /** Hidden constructor. */
109    private CsvTranslators() {
110        // empty
111    }
112}