EMMA Coverage Report (generated Fri Jan 30 13:20:29 EST 2009)
[all classes][org.springframework.batch.item.file.separator]

COVERAGE SUMMARY FOR SOURCE FILE [DefaultRecordSeparatorPolicy.java]

nameclass, %method, %block, %line, %
DefaultRecordSeparatorPolicy.java100% (1/1)89%  (8/9)95%  (88/93)92%  (22/24)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DefaultRecordSeparatorPolicy100% (1/1)89%  (8/9)95%  (88/93)92%  (22/24)
DefaultRecordSeparatorPolicy (String): void 0%   (0/1)0%   (0/5)0%   (0/2)
DefaultRecordSeparatorPolicy (): void 100% (1/1)100% (5/5)100% (2/2)
DefaultRecordSeparatorPolicy (String, String): void 100% (1/1)100% (15/15)100% (6/6)
isContinued (String): boolean 100% (1/1)100% (10/10)100% (3/3)
isEndOfRecord (String): boolean 100% (1/1)100% (12/12)100% (1/1)
isQuoteUnterminated (String): boolean 100% (1/1)100% (11/11)100% (1/1)
preProcess (String): String 100% (1/1)100% (27/27)100% (5/5)
setContinuation (String): void 100% (1/1)100% (4/4)100% (2/2)
setQuoteCharacter (String): void 100% (1/1)100% (4/4)100% (2/2)

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 
17package org.springframework.batch.item.file.separator;
18 
19import 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 */
29public 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}

[all classes][org.springframework.batch.item.file.separator]
EMMA 2.0.5312 (C) Vladimir Roubtsov