View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.batch.item.file.separator;
18  
19  import org.springframework.util.StringUtils;
20  
21  /**
22   * A {@link RecordSeparatorPolicy} that treats all lines as record endings, as
23   * long as they do not have unterminated quotes, and do not end in a
24   * continuation marker.
25   * 
26   * @author Dave Syer
27   * 
28   */
29  public class DefaultRecordSeparatorPolicy extends SimpleRecordSeparatorPolicy {
30  
31  	private static final String QUOTE = "\"";
32  
33  	private static final String CONTINUATION = "\\";
34  
35  	private String quoteCharacter = QUOTE;
36  
37  	private String continuation = CONTINUATION;
38  
39  	/**
40  	 * Default constructor.
41  	 */
42  	public DefaultRecordSeparatorPolicy() {
43  		this(QUOTE, CONTINUATION);
44  	}
45  
46  	/**
47  	 * Convenient constructor with quote character as parameter.
48  	 */
49  	public DefaultRecordSeparatorPolicy(String quoteCharacter) {
50  		this(quoteCharacter, CONTINUATION);
51  	}
52  
53  	/**
54  	 * Convenient constructor with quote character and continuation marker as
55  	 * parameters.
56  	 */
57  	public DefaultRecordSeparatorPolicy(String quoteCharacter, String continuation) {
58  		super();
59  		this.continuation = continuation;
60  		this.quoteCharacter = quoteCharacter;
61  	}
62  
63  	/**
64  	 * Public setter for the quoteCharacter. Defaults to double quote mark.
65  	 * 
66  	 * @param quoteCharacter the quoteCharacter to set
67  	 */
68  	public void setQuoteCharacter(String quoteCharacter) {
69  		this.quoteCharacter = quoteCharacter;
70  	}
71  
72  	/**
73  	 * Public setter for the continuation. Defaults to back slash.
74  	 * 
75  	 * @param continuation the continuation to set
76  	 */
77  	public void setContinuation(String continuation) {
78  		this.continuation = continuation;
79  	}
80  
81  	/**
82  	 * Return true if the line does not have unterminated quotes (delimited by
83  	 * "), and does not end with a continuation marker ('\'). The test for the
84  	 * continuation marker ignores whitespace at the end of the line.
85  	 * 
86  	 * @see org.springframework.batch.item.file.separator.RecordSeparatorPolicy#isEndOfRecord(java.lang.String)
87  	 */
88      @Override
89  	public boolean isEndOfRecord(String line) {
90  		return !isQuoteUnterminated(line) && !isContinued(line);
91  	}
92  
93  	/**
94  	 * If we are in an unterminated quote, add a line separator. Otherwise
95  	 * remove the continuation marker (plus whitespace at the end) if it is
96  	 * there.
97  	 * 
98  	 * @see org.springframework.batch.item.file.separator.SimpleRecordSeparatorPolicy#preProcess(java.lang.String)
99  	 */
100     @Override
101 	public String preProcess(String line) {
102 		if (isQuoteUnterminated(line)) {
103 			return line + "\n";
104 		}
105 		if (isContinued(line)) {
106 			return line.substring(0, line.lastIndexOf(continuation));
107 		}
108 		return line;
109 	}
110 
111 	/**
112 	 * Determine if the current line (or buffered concatenation of lines)
113 	 * contains an unterminated quote, indicating that the record is continuing
114 	 * onto the next line.
115 	 * 
116 	 * @param result
117 	 * @return
118 	 */
119 	private boolean isQuoteUnterminated(String line) {
120 		return StringUtils.countOccurrencesOf(line, quoteCharacter) % 2 != 0;
121 	}
122 
123 	/**
124 	 * Determine if the current line (or buffered concatenation of lines) ends
125 	 * with the continuation marker, indicating that the record is continuing
126 	 * onto the next line.
127 	 * 
128 	 * @param result
129 	 * @return
130 	 */
131 	private boolean isContinued(String line) {
132 		if (line == null) {
133 			return false;
134 		}
135 		return line.trim().endsWith(continuation);
136 	}
137 }