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 | |
20 | /** |
21 | * A {@link RecordSeparatorPolicy} that looks for an exact match for a String at |
22 | * the end of a line (e.g. a semicolon). |
23 | * |
24 | * @author Dave Syer |
25 | * |
26 | */ |
27 | public class SuffixRecordSeparatorPolicy extends DefaultRecordSeparatorPolicy { |
28 | |
29 | /** |
30 | * Default value for record terminator suffix. |
31 | */ |
32 | public static final String DEFAULT_SUFFIX = ";"; |
33 | |
34 | private String suffix = DEFAULT_SUFFIX; |
35 | |
36 | private boolean ignoreWhitespace = true; |
37 | |
38 | /** |
39 | * Lines ending in this terminator String signal the end of a record. |
40 | * |
41 | * @param suffix |
42 | */ |
43 | public void setSuffix(String suffix) { |
44 | this.suffix = suffix; |
45 | } |
46 | |
47 | /** |
48 | * Flag to indicate that the decision to terminate a record should ignore |
49 | * whitespace at the end of the line. |
50 | * |
51 | * @param ignoreWhitespace |
52 | */ |
53 | public void setIgnoreWhitespace(boolean ignoreWhitespace) { |
54 | this.ignoreWhitespace = ignoreWhitespace; |
55 | } |
56 | |
57 | /** |
58 | * Return true if the line ends with the specified substring. By default |
59 | * whitespace is trimmed before the comparison. Also returns true if the |
60 | * line is null, but not if it is empty. |
61 | * |
62 | * @see org.springframework.batch.item.file.separator.RecordSeparatorPolicy#isEndOfRecord(java.lang.String) |
63 | */ |
64 | public boolean isEndOfRecord(String line) { |
65 | if (line == null) { |
66 | return true; |
67 | } |
68 | String trimmed = ignoreWhitespace ? line.trim() : line; |
69 | return trimmed.endsWith(suffix); |
70 | } |
71 | |
72 | /** |
73 | * Remove the suffix from the end of the record. |
74 | * |
75 | * @see org.springframework.batch.item.file.separator.SimpleRecordSeparatorPolicy#postProcess(java.lang.String) |
76 | */ |
77 | public String postProcess(String record) { |
78 | if (record==null) { |
79 | return null; |
80 | } |
81 | return record.substring(0, record.lastIndexOf(suffix)); |
82 | } |
83 | |
84 | } |