View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.csv;
18  
19  import java.io.Serializable;
20  
21  /**
22   * CSVStrategy
23   * 
24   * Represents the strategy for a CSV.
25   */
26  public class CSVStrategy implements Cloneable, Serializable {
27  
28      private char delimiter;
29      private char encapsulator;
30      private char commentStart;
31      private char escape;
32      private boolean ignoreLeadingWhitespaces;
33      private boolean ignoreTrailingWhitespaces;
34      private boolean interpretUnicodeEscapes;
35      private boolean ignoreEmptyLines;
36  
37      // -2 is used to signal disabled, because it won't be confused with
38      // an EOF signal (-1), and because \ufffe in UTF-16 would be
39      // encoded as two chars (using surrogates) and thus there should never
40      // be a collision with a real text char.
41      public static char COMMENTS_DISABLED       = (char)-2;
42      public static char ESCAPE_DISABLED         = (char)-2;
43  
44      public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, 
45                                                                   true, false, true);
46      public static CSVStrategy EXCEL_STRATEGY   = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false, 
47                                                                   false, false, false);
48      public static CSVStrategy TDF_STRATEGY     = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, 
49                                                                   true, false, true);
50  
51  
52      public CSVStrategy(char delimiter, char encapsulator, char commentStart) {
53          this(delimiter, encapsulator, commentStart, true, false, true);
54      }
55    
56      /**
57       * Customized CSV strategy setter.
58       * 
59       * @param delimiter a Char used for value separation
60       * @param encapsulator a Char used as value encapsulation marker
61       * @param commentStart a Char used for comment identification
62       * @param ignoreLeadingWhitespace TRUE when leading whitespaces should be
63       *                                ignored
64       * @param interpretUnicodeEscapes TRUE when unicode escapes should be 
65       *                                interpreted
66       * @param ignoreEmptyLines TRUE when the parser should skip emtpy lines
67       */
68      public CSVStrategy(
69          char delimiter, 
70          char encapsulator, 
71          char commentStart,
72          char escape,
73          boolean ignoreLeadingWhitespace, 
74          boolean ignoreTrailingWhitespace, 
75          boolean interpretUnicodeEscapes,
76          boolean ignoreEmptyLines) 
77      {
78          setDelimiter(delimiter);
79          setEncapsulator(encapsulator);
80          setCommentStart(commentStart);
81          setEscape(escape);
82          setIgnoreLeadingWhitespaces(ignoreLeadingWhitespace);
83          setIgnoreTrailingWhitespaces(ignoreTrailingWhitespace);
84          setUnicodeEscapeInterpretation(interpretUnicodeEscapes);
85          setIgnoreEmptyLines(ignoreEmptyLines);
86      }
87  
88      /** @deprecated */
89      public CSVStrategy(
90          char delimiter,
91          char encapsulator,
92          char commentStart,
93          boolean ignoreLeadingWhitespace,
94          boolean interpretUnicodeEscapes,
95          boolean ignoreEmptyLines)
96      {
97          this(delimiter, encapsulator, commentStart, CSVStrategy.ESCAPE_DISABLED, ignoreLeadingWhitespace, 
98               true, interpretUnicodeEscapes, ignoreEmptyLines);
99      }
100 
101 
102     public void setDelimiter(char delimiter) { this.delimiter = delimiter; }
103     public char getDelimiter() { return this.delimiter; }
104 
105     public void setEncapsulator(char encapsulator) { this.encapsulator = encapsulator; }
106     public char getEncapsulator() { return this.encapsulator; }
107 
108     public void setCommentStart(char commentStart) { this.commentStart = commentStart; }
109     public char getCommentStart() { return this.commentStart; }
110     public boolean isCommentingDisabled() { return this.commentStart == COMMENTS_DISABLED; }
111 
112     public void setEscape(char escape) { this.escape = escape; }
113     public char getEscape() { return this.escape; }
114 
115     public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) { 
116         this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces; 
117     }
118     public boolean getIgnoreLeadingWhitespaces() { return this.ignoreLeadingWhitespaces; }
119 
120     public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) { 
121         this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces; 
122     }
123     public boolean getIgnoreTrailingWhitespaces() { return this.ignoreTrailingWhitespaces; }
124 
125     public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) { 
126         this.interpretUnicodeEscapes = interpretUnicodeEscapes; 
127     }
128     public boolean getUnicodeEscapeInterpretation() { return this.interpretUnicodeEscapes; }
129 
130     public void setIgnoreEmptyLines(boolean ignoreEmptyLines) { this.ignoreEmptyLines = ignoreEmptyLines; }
131     public boolean getIgnoreEmptyLines() { return this.ignoreEmptyLines; }
132 
133     public Object clone() {
134       try {
135         return super.clone();
136       } catch (CloneNotSupportedException e) {
137         throw new RuntimeException(e);  // impossible
138       }
139     }
140 }