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 | public boolean isEndOfRecord(String line) { |
89 | return !isQuoteUnterminated(line) && !isContinued(line); |
90 | } |
91 | |
92 | /** |
93 | * If we are in an unterminated quote, add a line separator. Otherwise |
94 | * remove the continuation marker (plus whitespace at the end) if it is |
95 | * there. |
96 | * |
97 | * @see org.springframework.batch.item.file.separator.SimpleRecordSeparatorPolicy#preProcess(java.lang.String) |
98 | */ |
99 | public String preProcess(String line) { |
100 | if (isQuoteUnterminated(line)) { |
101 | return line + "\n"; |
102 | } |
103 | if (isContinued(line)) { |
104 | return line.substring(0, line.lastIndexOf(continuation)); |
105 | } |
106 | return line; |
107 | } |
108 | |
109 | /** |
110 | * Determine if the current line (or buffered concatenation of lines) |
111 | * contains an unterminated quote, indicating that the record is continuing |
112 | * onto the next line. |
113 | * |
114 | * @param result |
115 | * @return |
116 | */ |
117 | private boolean isQuoteUnterminated(String line) { |
118 | return StringUtils.countOccurrencesOf(line, quoteCharacter) % 2 != 0; |
119 | } |
120 | |
121 | /** |
122 | * Determine if the current line (or buffered concatenation of lines) |
123 | * contains an unterminated quote, indicating that the record is continuing |
124 | * onto the next line. |
125 | * |
126 | * @param result |
127 | * @return |
128 | */ |
129 | private boolean isContinued(String line) { |
130 | if (line == null) { |
131 | return false; |
132 | } |
133 | return line.trim().endsWith(continuation); |
134 | } |
135 | } |