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 org.apache.commons.lang3.CharUtils;
020import org.apache.commons.lang3.StringUtils;
021
022import java.io.IOException;
023import java.io.Writer;
024
025/**
026 * This class holds inner classes for escaping/unescaping Comma Separated Values.
027 */
028public final class CsvTranslators {
029
030    /** Comma character. */
031    private static final char CSV_DELIMITER = ',';
032    /** Quote character. */
033    private static final char CSV_QUOTE = '"';
034    /** Quote character converted to string. */
035    private static final String CSV_QUOTE_STR = String.valueOf(CSV_QUOTE);
036    /** Escaped quote string. */
037    private static final String CSV_ESCAPED_QUOTE_STR = CSV_QUOTE_STR + CSV_QUOTE_STR;
038    /** CSV key characters in an array. */
039    private static final char[] CSV_SEARCH_CHARS =
040            new char[] {CSV_DELIMITER, CSV_QUOTE, CharUtils.CR, CharUtils.LF};
041
042    /** Hidden constructor. */
043    private CsvTranslators() { }
044
045    /**
046     * Translator for escaping Comma Separated Values.
047     */
048    public static class CsvEscaper extends SinglePassTranslator {
049
050        @Override
051        void translateWhole(final CharSequence input, final Writer out) throws IOException {
052            final String inputSting = input.toString();
053            if (StringUtils.containsNone(inputSting, CSV_SEARCH_CHARS)) {
054                out.write(inputSting);
055            } else {
056                // input needs quoting
057                out.write(CSV_QUOTE);
058                out.write(StringUtils.replace(inputSting, CSV_QUOTE_STR, CSV_ESCAPED_QUOTE_STR));
059                out.write(CSV_QUOTE);
060            }
061        }
062    }
063
064    /**
065     * Translator for unescaping escaped Comma Separated Value entries.
066     */
067    public static class CsvUnescaper extends SinglePassTranslator {
068
069        @Override
070        void translateWhole(final CharSequence input, final Writer out) throws IOException {
071            // is input not quoted?
072            if (input.charAt(0) != CSV_QUOTE || input.charAt(input.length() - 1) != CSV_QUOTE) {
073                out.write(input.toString());
074                return;
075            }
076
077            // strip quotes
078            final String quoteless = input.subSequence(1, input.length() - 1).toString();
079
080            if (StringUtils.containsAny(quoteless, CSV_SEARCH_CHARS)) {
081                // deal with escaped quotes; ie) ""
082                out.write(StringUtils.replace(quoteless, CSV_ESCAPED_QUOTE_STR, CSV_QUOTE_STR));
083            } else {
084                out.write(input.toString());
085            }
086        }
087    }
088}