001package biweekly.io.text;
002
003import java.io.BufferedReader;
004import java.io.IOException;
005import java.io.Reader;
006import java.io.StringReader;
007
008/*
009 Copyright (c) 2013, Michael Angstadt
010 All rights reserved.
011
012 Redistribution and use in source and binary forms, with or without
013 modification, are permitted provided that the following conditions are met: 
014
015 1. Redistributions of source code must retain the above copyright notice, this
016 list of conditions and the following disclaimer. 
017 2. Redistributions in binary form must reproduce the above copyright notice,
018 this list of conditions and the following disclaimer in the documentation
019 and/or other materials provided with the distribution. 
020
021 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
022 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
023 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
024 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
025 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
026 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
027 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
028 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
029 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 */
032
033/**
034 * Automatically unfolds lines of text as they are read.
035 * @author Michael Angstadt
036 */
037public class FoldedLineReader extends BufferedReader {
038        private String lastLine;
039        private boolean singleSpaceFolding = true;
040        private int lastLineNum = 0, lineCount = 0;
041
042        /**
043         * Creates a new folded line reader.
044         * @param reader the reader object to wrap
045         */
046        public FoldedLineReader(Reader reader) {
047                super(reader);
048        }
049
050        /**
051         * Creates a new folded line reader.
052         * @param text the text to read
053         */
054        public FoldedLineReader(String text) {
055                this(new StringReader(text));
056        }
057
058        /**
059         * Sets whether the reader will only ignore the first whitespace character
060         * it encounters at the beginning of a folded line. This setting is enabled
061         * by default in order to support iCalendar files generated by Outlook.
062         * @param enabled true to enable (default), false to disable
063         */
064        public void setSingleSpaceFoldingEnabled(boolean enabled) {
065                singleSpaceFolding = enabled;
066        }
067
068        /**
069         * Gets whether the reader will only ignore the first whitespace character
070         * it encounters at the beginning of a folded line. This setting is enabled
071         * by default in order to support iCalendar files generated by Outlook.
072         * @return true if enabled (default), false if disabled
073         */
074        public boolean isSingleSpaceFoldingEnabled() {
075                return singleSpaceFolding;
076        }
077
078        /**
079         * Gets the starting line number of the last unfolded line that was read.
080         * @return the line number
081         */
082        public int getLineNum() {
083                return lastLineNum;
084        }
085
086        /**
087         * Reads the next non-empty line.
088         * @return the next non-empty line or null of EOF
089         * @throws IOException
090         */
091        private String readNonEmptyLine() throws IOException {
092                String line;
093                do {
094                        line = super.readLine();
095                        if (line != null) {
096                                lineCount++;
097                        }
098                } while (line != null && line.length() == 0);
099                return line;
100        }
101
102        /**
103         * Reads the next line, unfolding it if necessary.
104         * @return the next line or null if EOF
105         * @throws IOException if there's a problem reading from the reader
106         */
107        @Override
108        public String readLine() throws IOException {
109                String wholeLine = (lastLine == null) ? readNonEmptyLine() : lastLine;
110                lastLine = null;
111                if (wholeLine == null) {
112                        return null;
113                }
114
115                //long lines are folded
116                lastLineNum = lineCount;
117                StringBuilder wholeLineSb = new StringBuilder(wholeLine);
118                while (true) {
119                        String line = readNonEmptyLine();
120                        if (line == null) {
121                                break;
122                        }
123
124                        if (!Character.isWhitespace(line.charAt(0))) {
125                                lastLine = line;
126                                break;
127                        }
128
129                        //the line was folded
130                        int lastWhitespace = 1;
131                        if (!singleSpaceFolding) {
132                                while (lastWhitespace < line.length() && Character.isWhitespace(line.charAt(lastWhitespace))) {
133                                        lastWhitespace++;
134                                }
135                        }
136                        wholeLineSb.append(line.substring(lastWhitespace));
137                }
138                return wholeLineSb.toString();
139        }
140}