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 *      http://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;
024
025/**
026 * This class holds inner classes for escaping/unescaping Comma Separated Values.
027 * <p>
028 * In general the use a high level API like <a href="https://commons.apache.org/proper/commons-csv/">Apache Commons
029 * CSV</a> should be preferred over these low level classes.
030 * </p>
031 *
032 * @see <a href="https://commons.apache.org/proper/commons-csv/apidocs/index.html">Apache Commons CSV</a>
033 */
034public final class CsvTranslators {
035
036    /**
037     * Translator for escaping Comma Separated Values.
038     */
039    public static class CsvEscaper extends SinglePassTranslator {
040
041        /**
042         * Construct a new instance.
043         */
044        public CsvEscaper() {
045            // empty
046        }
047
048        @Override
049        void translateWhole(final CharSequence input, final Writer writer) throws IOException {
050            final String inputSting = input.toString();
051            if (StringUtils.containsNone(inputSting, CSV_SEARCH_CHARS)) {
052                writer.write(inputSting);
053            } else {
054                // input needs quoting
055                writer.write(CSV_QUOTE);
056                writer.write(StringUtils.replace(inputSting, CSV_QUOTE_STR, CSV_ESCAPED_QUOTE_STR));
057                writer.write(CSV_QUOTE);
058            }
059        }
060    }
061    /**
062     * Translator for unescaping escaped Comma Separated Value entries.
063     */
064    public static class CsvUnescaper extends SinglePassTranslator {
065
066        /**
067         * Construct a new instance.
068         */
069        public CsvUnescaper() {
070            // empty
071        }
072
073        @Override
074        void translateWhole(final CharSequence input, final Writer writer) throws IOException {
075            // is input not quoted?
076            if (input.charAt(0) != CSV_QUOTE || input.charAt(input.length() - 1) != CSV_QUOTE) {
077                writer.write(input.toString());
078                return;
079            }
080
081            // strip quotes
082            final String quoteless = input.subSequence(1, input.length() - 1).toString();
083
084            if (StringUtils.containsAny(quoteless, CSV_SEARCH_CHARS)) {
085                // deal with escaped quotes; ie) ""
086                writer.write(StringUtils.replace(quoteless, CSV_ESCAPED_QUOTE_STR, CSV_QUOTE_STR));
087            } else {
088                writer.write(quoteless);
089            }
090        }
091    }
092    /** Comma character. */
093    private static final char CSV_DELIMITER = ',';
094    /** Quote character. */
095    private static final char CSV_QUOTE = '"';
096    /** Quote character converted to string. */
097    private static final String CSV_QUOTE_STR = String.valueOf(CSV_QUOTE);
098
099    /** Escaped quote string. */
100    private static final String CSV_ESCAPED_QUOTE_STR = CSV_QUOTE_STR + CSV_QUOTE_STR;
101
102    /** CSV key characters in an array. */
103    private static final char[] CSV_SEARCH_CHARS = { CSV_DELIMITER, CSV_QUOTE, CharUtils.CR, CharUtils.LF };
104
105    /** Hidden constructor. */
106    private CsvTranslators() {
107        // empty
108    }
109}