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