001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.item.file.separator;
018
019
020/**
021 * A {@link RecordSeparatorPolicy} that looks for an exact match for a String at
022 * the end of a line (e.g. a semicolon).
023 * 
024 * @author Dave Syer
025 * 
026 */
027public class SuffixRecordSeparatorPolicy extends DefaultRecordSeparatorPolicy {
028
029        /**
030         * Default value for record terminator suffix.
031         */
032        public static final String DEFAULT_SUFFIX = ";";
033
034        private String suffix = DEFAULT_SUFFIX;
035
036        private boolean ignoreWhitespace = true;
037
038        /**
039         * Lines ending in this terminator String signal the end of a record.
040         * 
041         * @param suffix suffix to indicate the end of a record
042         */
043        public void setSuffix(String suffix) {
044                this.suffix = suffix;
045        }
046
047        /**
048         * Flag to indicate that the decision to terminate a record should ignore
049         * whitespace at the end of the line.
050         * 
051         * @param ignoreWhitespace indicator
052         */
053        public void setIgnoreWhitespace(boolean ignoreWhitespace) {
054                this.ignoreWhitespace = ignoreWhitespace;
055        }
056
057        /**
058         * Return true if the line ends with the specified substring. By default
059         * whitespace is trimmed before the comparison. Also returns true if the
060         * line is null, but not if it is empty.
061         * 
062         * @see org.springframework.batch.item.file.separator.RecordSeparatorPolicy#isEndOfRecord(java.lang.String)
063         */
064    @Override
065        public boolean isEndOfRecord(String line) {
066                if (line == null) {
067                        return true;
068                }
069                String trimmed = ignoreWhitespace ? line.trim() : line;
070                return trimmed.endsWith(suffix);
071        }
072        
073        /**
074         * Remove the suffix from the end of the record.
075         * 
076         * @see org.springframework.batch.item.file.separator.SimpleRecordSeparatorPolicy#postProcess(java.lang.String)
077         */
078    @Override
079        public String postProcess(String record) {
080                if (record==null) {
081                        return null;
082                }
083                return record.substring(0, record.lastIndexOf(suffix));
084        }
085
086}