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 */ 017 package org.apache.commons.csv; 018 019 import java.io.Serializable; 020 021 /** 022 * CSVStrategy 023 * 024 * Represents the strategy for a CSV. 025 */ 026 public class CSVStrategy implements Cloneable, Serializable { 027 028 private char delimiter; 029 private char encapsulator; 030 private char commentStart; 031 private char escape; 032 private boolean ignoreLeadingWhitespaces; 033 private boolean ignoreTrailingWhitespaces; 034 private boolean interpretUnicodeEscapes; 035 private boolean ignoreEmptyLines; 036 037 // controls for output 038 private String printerNewline = "\n"; 039 040 // -2 is used to signal disabled, because it won't be confused with 041 // an EOF signal (-1), and because \ufffe in UTF-16 would be 042 // encoded as two chars (using surrogates) and thus there should never 043 // be a collision with a real text char. 044 public static char COMMENTS_DISABLED = (char) -2; 045 public static char ESCAPE_DISABLED = (char) -2; 046 public static char ENCAPSULATOR_DISABLED = (char) -2; 047 048 public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, 049 true, false, true); 050 public static CSVStrategy EXCEL_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false, 051 false, false, false); 052 public static CSVStrategy TDF_STRATEGY = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, 053 true, false, true); 054 055 056 public CSVStrategy(char delimiter, char encapsulator, char commentStart) { 057 this(delimiter, encapsulator, commentStart, true, false, true); 058 } 059 060 /** 061 * Customized CSV strategy setter. 062 * 063 * @param delimiter a Char used for value separation 064 * @param encapsulator a Char used as value encapsulation marker 065 * @param commentStart a Char used for comment identification 066 * @param escape a Char used to escape special characters in values 067 * @param ignoreLeadingWhitespace TRUE when leading whitespaces should be 068 * ignored 069 * @param ignoreTrailingWhitespace TRUE when trailing whitespaces should be 070 * ignored 071 * @param interpretUnicodeEscapes TRUE when unicode escapes should be 072 * interpreted 073 * @param ignoreEmptyLines TRUE when the parser should skip emtpy lines 074 */ 075 public CSVStrategy( 076 char delimiter, 077 char encapsulator, 078 char commentStart, 079 char escape, 080 boolean ignoreLeadingWhitespace, 081 boolean ignoreTrailingWhitespace, 082 boolean interpretUnicodeEscapes, 083 boolean ignoreEmptyLines) { 084 setDelimiter(delimiter); 085 setEncapsulator(encapsulator); 086 setCommentStart(commentStart); 087 setEscape(escape); 088 setIgnoreLeadingWhitespaces(ignoreLeadingWhitespace); 089 setIgnoreTrailingWhitespaces(ignoreTrailingWhitespace); 090 setUnicodeEscapeInterpretation(interpretUnicodeEscapes); 091 setIgnoreEmptyLines(ignoreEmptyLines); 092 } 093 094 /** 095 * @deprecated 096 */ 097 public CSVStrategy( 098 char delimiter, 099 char encapsulator, 100 char commentStart, 101 boolean ignoreLeadingWhitespace, 102 boolean interpretUnicodeEscapes, 103 boolean ignoreEmptyLines) { 104 this(delimiter, encapsulator, commentStart, CSVStrategy.ESCAPE_DISABLED, ignoreLeadingWhitespace, 105 true, interpretUnicodeEscapes, ignoreEmptyLines); 106 } 107 108 public void setDelimiter(char delimiter) { 109 this.delimiter = delimiter; 110 } 111 112 public char getDelimiter() { 113 return this.delimiter; 114 } 115 116 public void setEncapsulator(char encapsulator) { 117 this.encapsulator = encapsulator; 118 } 119 120 public char getEncapsulator() { 121 return this.encapsulator; 122 } 123 124 public void setCommentStart(char commentStart) { 125 this.commentStart = commentStart; 126 } 127 128 public char getCommentStart() { 129 return this.commentStart; 130 } 131 132 public boolean isCommentingDisabled() { 133 return this.commentStart == COMMENTS_DISABLED; 134 } 135 136 public void setEscape(char escape) { 137 this.escape = escape; 138 } 139 140 public char getEscape() { 141 return this.escape; 142 } 143 144 public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) { 145 this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces; 146 } 147 148 public boolean getIgnoreLeadingWhitespaces() { 149 return this.ignoreLeadingWhitespaces; 150 } 151 152 public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) { 153 this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces; 154 } 155 156 public boolean getIgnoreTrailingWhitespaces() { 157 return this.ignoreTrailingWhitespaces; 158 } 159 160 public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) { 161 this.interpretUnicodeEscapes = interpretUnicodeEscapes; 162 } 163 164 public boolean getUnicodeEscapeInterpretation() { 165 return this.interpretUnicodeEscapes; 166 } 167 168 public void setIgnoreEmptyLines(boolean ignoreEmptyLines) { 169 this.ignoreEmptyLines = ignoreEmptyLines; 170 } 171 172 public boolean getIgnoreEmptyLines() { 173 return this.ignoreEmptyLines; 174 } 175 176 public void setPrinterNewline(String newline) { 177 this.printerNewline = newline; 178 } 179 180 public String getPrinterNewline() { 181 return this.printerNewline; 182 } 183 184 public Object clone() { 185 try { 186 return super.clone(); 187 } catch (CloneNotSupportedException e) { 188 throw new RuntimeException(e); // impossible 189 } 190 } 191 }