1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/data/UnfoldingReader.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,239 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.data; 1.36 + 1.37 +import java.io.IOException; 1.38 +import java.io.PushbackReader; 1.39 +import java.io.Reader; 1.40 +import java.util.Arrays; 1.41 + 1.42 +import net.fortuna.ical4j.util.CompatibilityHints; 1.43 + 1.44 +import org.apache.commons.logging.Log; 1.45 +import org.apache.commons.logging.LogFactory; 1.46 + 1.47 +/** 1.48 + * <pre> 1.49 + * $Id$ [06-Apr-2004] 1.50 + * </pre> 1.51 + * 1.52 + * A reader which performs iCalendar unfolding as it reads. Note that unfolding rules may be "relaxed" to allow 1.53 + * unfolding of non-conformant *.ics files. By specifying the system property "ical4j.unfolding.relaxed=true" iCalendar 1.54 + * files created with Mozilla Calendar/Sunbird may be correctly unfolded. 1.55 + * 1.56 + * To wrap this reader with a {@link java.io.BufferedReader} you must ensure you specify an identical buffer size 1.57 + * to that used in the {@link java.io.BufferedReader}. 1.58 + * 1.59 + * @author Ben Fortuna 1.60 + */ 1.61 +public class UnfoldingReader extends PushbackReader { 1.62 + 1.63 + private Log log = LogFactory.getLog(UnfoldingReader.class); 1.64 + 1.65 + /** 1.66 + * The pattern used to identify a fold in an iCalendar data stream. 1.67 + */ 1.68 + private static final char[] DEFAULT_FOLD_PATTERN_1 = { '\r', '\n', ' ' }; 1.69 + 1.70 + /** 1.71 + * The pattern used to identify a fold in Microsoft Outlook 2007. 1.72 + */ 1.73 + private static final char[] DEFAULT_FOLD_PATTERN_2 = { '\r', '\n', '\t' }; 1.74 + 1.75 + /** 1.76 + * The pattern used to identify a fold in Mozilla Calendar/Sunbird and KOrganizer. 1.77 + */ 1.78 + private static final char[] RELAXED_FOLD_PATTERN_1 = { '\n', ' ' }; 1.79 + 1.80 + /** 1.81 + * The pattern used to identify a fold in Microsoft Outlook 2007. 1.82 + */ 1.83 + private static final char[] RELAXED_FOLD_PATTERN_2 = { '\n', '\t' }; 1.84 + 1.85 + private char[][] patterns; 1.86 + 1.87 + private char[][] buffers; 1.88 + 1.89 + private int linesUnfolded; 1.90 + 1.91 + private int maxPatternLength = 0; 1.92 + 1.93 + /** 1.94 + * Creates a new unfolding reader instance. Relaxed unfolding flag is read from system property. 1.95 + * @param in the reader to unfold from 1.96 + */ 1.97 + public UnfoldingReader(final Reader in) { 1.98 + this(in, DEFAULT_FOLD_PATTERN_1.length, CompatibilityHints 1.99 + .isHintEnabled(CompatibilityHints.KEY_RELAXED_UNFOLDING)); 1.100 + } 1.101 + 1.102 + /** 1.103 + * @param in reader source for data 1.104 + * @param size the buffer size 1.105 + */ 1.106 + public UnfoldingReader(final Reader in, int size) { 1.107 + this(in, size, CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_UNFOLDING)); 1.108 + } 1.109 + 1.110 + /** 1.111 + * @param in reader source for data 1.112 + * @param relaxed indicates whether relaxed unfolding is enabled 1.113 + */ 1.114 + public UnfoldingReader(final Reader in, boolean relaxed) { 1.115 + this(in, DEFAULT_FOLD_PATTERN_1.length, relaxed); 1.116 + } 1.117 + 1.118 + /** 1.119 + * Creates a new unfolding reader instance. 1.120 + * @param in a reader to read from 1.121 + * @param size the buffer size 1.122 + * @param relaxed specifies whether unfolding is relaxed 1.123 + */ 1.124 + public UnfoldingReader(final Reader in, int size, final boolean relaxed) { 1.125 + super(in, size); 1.126 + if (relaxed) { 1.127 + patterns = new char[4][]; 1.128 + patterns[0] = DEFAULT_FOLD_PATTERN_1; 1.129 + patterns[1] = DEFAULT_FOLD_PATTERN_2; 1.130 + patterns[2] = RELAXED_FOLD_PATTERN_1; 1.131 + patterns[3] = RELAXED_FOLD_PATTERN_2; 1.132 + } 1.133 + else { 1.134 + patterns = new char[2][]; 1.135 + patterns[0] = DEFAULT_FOLD_PATTERN_1; 1.136 + patterns[1] = DEFAULT_FOLD_PATTERN_2; 1.137 + } 1.138 + buffers = new char[patterns.length][]; 1.139 + for (int i = 0; i < patterns.length; i++) { 1.140 + buffers[i] = new char[patterns[i].length]; 1.141 + maxPatternLength = Math.max(maxPatternLength, patterns[i].length); 1.142 + } 1.143 + } 1.144 + 1.145 + /** 1.146 + * @return number of lines unfolded so far while reading 1.147 + */ 1.148 + public final int getLinesUnfolded() { 1.149 + return linesUnfolded; 1.150 + } 1.151 + 1.152 + /** 1.153 + * {@inheritDoc} 1.154 + */ 1.155 + public final int read() throws IOException { 1.156 + final int c = super.read(); 1.157 + boolean doUnfold = false; 1.158 + for (int i = 0; i < patterns.length; i++) { 1.159 + if (c == patterns[i][0]) { 1.160 + doUnfold = true; 1.161 + break; 1.162 + } 1.163 + } 1.164 + if (!doUnfold) { 1.165 + return c; 1.166 + } 1.167 + else { 1.168 + unread(c); 1.169 + } 1.170 + 1.171 + unfold(); 1.172 + 1.173 + return super.read(); 1.174 + } 1.175 + 1.176 + /** 1.177 + * {@inheritDoc} 1.178 + */ 1.179 + public int read(final char[] cbuf, final int off, final int len) throws IOException { 1.180 + final int read = super.read(cbuf, off, len); 1.181 + boolean doUnfold = false; 1.182 + for (int i = 0; i < patterns.length; i++) { 1.183 + if (read > 0 && cbuf[0] == patterns[i][0]) { 1.184 + doUnfold = true; 1.185 + break; 1.186 + } 1.187 + else { 1.188 + for (int j = 0; j < read; j++) { 1.189 + if (cbuf[j] == patterns[i][0]) { 1.190 + unread(cbuf, j, read - j); 1.191 + return j; 1.192 + } 1.193 + } 1.194 + } 1.195 + } 1.196 + if (!doUnfold) { 1.197 + return read; 1.198 + } 1.199 + else { 1.200 + unread(cbuf, off, read); 1.201 + } 1.202 + 1.203 + unfold(); 1.204 + 1.205 + return super.read(cbuf, off, maxPatternLength); 1.206 + } 1.207 + 1.208 + private void unfold() throws IOException { 1.209 + // need to loop since one line fold might be directly followed by another 1.210 + boolean didUnfold; 1.211 + do { 1.212 + didUnfold = false; 1.213 + 1.214 + for (int i = 0; i < buffers.length; i++) { 1.215 + int read = 0; 1.216 + while (read < buffers[i].length) { 1.217 + final int partialRead = super.read(buffers[i], read, buffers[i].length - read); 1.218 + if (partialRead < 0) { 1.219 + break; 1.220 + } 1.221 + read += partialRead; 1.222 + } 1.223 + if (read > 0) { 1.224 + if (!Arrays.equals(patterns[i], buffers[i])) { 1.225 + unread(buffers[i], 0, read); 1.226 + } 1.227 + else { 1.228 + if (log.isTraceEnabled()) { 1.229 + log.trace("Unfolding..."); 1.230 + } 1.231 + linesUnfolded++; 1.232 + didUnfold = true; 1.233 + } 1.234 + } 1.235 +// else { 1.236 +// return read; 1.237 +// } 1.238 + } 1.239 + } 1.240 + while (didUnfold); 1.241 + } 1.242 +}