michael@0: /** michael@0: * Copyright (c) 2012, Ben Fortuna michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * o Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * o Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: package net.fortuna.ical4j.data; michael@0: michael@0: import java.io.IOException; michael@0: import java.io.PushbackReader; michael@0: import java.io.Reader; michael@0: import java.util.Arrays; michael@0: michael@0: import net.fortuna.ical4j.util.CompatibilityHints; michael@0: michael@0: import org.apache.commons.logging.Log; michael@0: import org.apache.commons.logging.LogFactory; michael@0: michael@0: /** michael@0: *
michael@0:  * $Id$ [06-Apr-2004]
michael@0:  * 
michael@0: * michael@0: * A reader which performs iCalendar unfolding as it reads. Note that unfolding rules may be "relaxed" to allow michael@0: * unfolding of non-conformant *.ics files. By specifying the system property "ical4j.unfolding.relaxed=true" iCalendar michael@0: * files created with Mozilla Calendar/Sunbird may be correctly unfolded. michael@0: * michael@0: * To wrap this reader with a {@link java.io.BufferedReader} you must ensure you specify an identical buffer size michael@0: * to that used in the {@link java.io.BufferedReader}. michael@0: * michael@0: * @author Ben Fortuna michael@0: */ michael@0: public class UnfoldingReader extends PushbackReader { michael@0: michael@0: private Log log = LogFactory.getLog(UnfoldingReader.class); michael@0: michael@0: /** michael@0: * The pattern used to identify a fold in an iCalendar data stream. michael@0: */ michael@0: private static final char[] DEFAULT_FOLD_PATTERN_1 = { '\r', '\n', ' ' }; michael@0: michael@0: /** michael@0: * The pattern used to identify a fold in Microsoft Outlook 2007. michael@0: */ michael@0: private static final char[] DEFAULT_FOLD_PATTERN_2 = { '\r', '\n', '\t' }; michael@0: michael@0: /** michael@0: * The pattern used to identify a fold in Mozilla Calendar/Sunbird and KOrganizer. michael@0: */ michael@0: private static final char[] RELAXED_FOLD_PATTERN_1 = { '\n', ' ' }; michael@0: michael@0: /** michael@0: * The pattern used to identify a fold in Microsoft Outlook 2007. michael@0: */ michael@0: private static final char[] RELAXED_FOLD_PATTERN_2 = { '\n', '\t' }; michael@0: michael@0: private char[][] patterns; michael@0: michael@0: private char[][] buffers; michael@0: michael@0: private int linesUnfolded; michael@0: michael@0: private int maxPatternLength = 0; michael@0: michael@0: /** michael@0: * Creates a new unfolding reader instance. Relaxed unfolding flag is read from system property. michael@0: * @param in the reader to unfold from michael@0: */ michael@0: public UnfoldingReader(final Reader in) { michael@0: this(in, DEFAULT_FOLD_PATTERN_1.length, CompatibilityHints michael@0: .isHintEnabled(CompatibilityHints.KEY_RELAXED_UNFOLDING)); michael@0: } michael@0: michael@0: /** michael@0: * @param in reader source for data michael@0: * @param size the buffer size michael@0: */ michael@0: public UnfoldingReader(final Reader in, int size) { michael@0: this(in, size, CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_UNFOLDING)); michael@0: } michael@0: michael@0: /** michael@0: * @param in reader source for data michael@0: * @param relaxed indicates whether relaxed unfolding is enabled michael@0: */ michael@0: public UnfoldingReader(final Reader in, boolean relaxed) { michael@0: this(in, DEFAULT_FOLD_PATTERN_1.length, relaxed); michael@0: } michael@0: michael@0: /** michael@0: * Creates a new unfolding reader instance. michael@0: * @param in a reader to read from michael@0: * @param size the buffer size michael@0: * @param relaxed specifies whether unfolding is relaxed michael@0: */ michael@0: public UnfoldingReader(final Reader in, int size, final boolean relaxed) { michael@0: super(in, size); michael@0: if (relaxed) { michael@0: patterns = new char[4][]; michael@0: patterns[0] = DEFAULT_FOLD_PATTERN_1; michael@0: patterns[1] = DEFAULT_FOLD_PATTERN_2; michael@0: patterns[2] = RELAXED_FOLD_PATTERN_1; michael@0: patterns[3] = RELAXED_FOLD_PATTERN_2; michael@0: } michael@0: else { michael@0: patterns = new char[2][]; michael@0: patterns[0] = DEFAULT_FOLD_PATTERN_1; michael@0: patterns[1] = DEFAULT_FOLD_PATTERN_2; michael@0: } michael@0: buffers = new char[patterns.length][]; michael@0: for (int i = 0; i < patterns.length; i++) { michael@0: buffers[i] = new char[patterns[i].length]; michael@0: maxPatternLength = Math.max(maxPatternLength, patterns[i].length); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * @return number of lines unfolded so far while reading michael@0: */ michael@0: public final int getLinesUnfolded() { michael@0: return linesUnfolded; michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final int read() throws IOException { michael@0: final int c = super.read(); michael@0: boolean doUnfold = false; michael@0: for (int i = 0; i < patterns.length; i++) { michael@0: if (c == patterns[i][0]) { michael@0: doUnfold = true; michael@0: break; michael@0: } michael@0: } michael@0: if (!doUnfold) { michael@0: return c; michael@0: } michael@0: else { michael@0: unread(c); michael@0: } michael@0: michael@0: unfold(); michael@0: michael@0: return super.read(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public int read(final char[] cbuf, final int off, final int len) throws IOException { michael@0: final int read = super.read(cbuf, off, len); michael@0: boolean doUnfold = false; michael@0: for (int i = 0; i < patterns.length; i++) { michael@0: if (read > 0 && cbuf[0] == patterns[i][0]) { michael@0: doUnfold = true; michael@0: break; michael@0: } michael@0: else { michael@0: for (int j = 0; j < read; j++) { michael@0: if (cbuf[j] == patterns[i][0]) { michael@0: unread(cbuf, j, read - j); michael@0: return j; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: if (!doUnfold) { michael@0: return read; michael@0: } michael@0: else { michael@0: unread(cbuf, off, read); michael@0: } michael@0: michael@0: unfold(); michael@0: michael@0: return super.read(cbuf, off, maxPatternLength); michael@0: } michael@0: michael@0: private void unfold() throws IOException { michael@0: // need to loop since one line fold might be directly followed by another michael@0: boolean didUnfold; michael@0: do { michael@0: didUnfold = false; michael@0: michael@0: for (int i = 0; i < buffers.length; i++) { michael@0: int read = 0; michael@0: while (read < buffers[i].length) { michael@0: final int partialRead = super.read(buffers[i], read, buffers[i].length - read); michael@0: if (partialRead < 0) { michael@0: break; michael@0: } michael@0: read += partialRead; michael@0: } michael@0: if (read > 0) { michael@0: if (!Arrays.equals(patterns[i], buffers[i])) { michael@0: unread(buffers[i], 0, read); michael@0: } michael@0: else { michael@0: if (log.isTraceEnabled()) { michael@0: log.trace("Unfolding..."); michael@0: } michael@0: linesUnfolded++; michael@0: didUnfold = true; michael@0: } michael@0: } michael@0: // else { michael@0: // return read; michael@0: // } michael@0: } michael@0: } michael@0: while (didUnfold); michael@0: } michael@0: }