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