View Javadoc

1   /*
2    * $Id: UnfoldingReader.java [06-Apr-2004]
3    *
4    * Copyright (c) 2004, Ben Fortuna
5    * All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 	o Redistributions of source code must retain the above copyright
12   * notice, this list of conditions and the following disclaimer.
13   *
14   * 	o Redistributions in binary form must reproduce the above copyright
15   * notice, this list of conditions and the following disclaimer in the
16   * documentation and/or other materials provided with the distribution.
17   *
18   * 	o Neither the name of Ben Fortuna nor the names of any other contributors
19   * may be used to endorse or promote products derived from this software
20   * without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   */
34  package net.fortuna.ical4j.data;
35  
36  import java.io.IOException;
37  import java.io.PushbackReader;
38  import java.io.Reader;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /***
44   * A reader which performs iCalendar unfolding as it reads.
45   *
46   * @author benfortuna
47   */
48  public class UnfoldingReader extends PushbackReader {
49  
50      private static Log log = LogFactory.getLog(UnfoldingReader.class);
51  
52      /***
53       * The pattern used to identify a fold in the iCalendar stream.
54       * The unfolder may use a relaxed version of the pattern which does
55       * not require the carriage return by specified the system property:
56       * <code>ical4j.unfolding.relaxed=true</code>
57       */
58      private static final String FOLD_PATTERN = ("true".equals(System
59              .getProperty("ical4j.unfolding.relaxed"))) ? "\n " : "\r\n ";
60  
61      private static final int BUFFER_SIZE = FOLD_PATTERN.length();
62  
63      private char[] buffer = new char[BUFFER_SIZE];
64  
65      /***
66       * @param in
67       *            a reader to read from
68       */
69      public UnfoldingReader(final Reader in) {
70  
71          super(in, BUFFER_SIZE);
72      }
73  
74      /***
75       * @see java.io.PushbackReader#read()
76       */
77      public final int read() throws IOException {
78  
79          super.read(buffer);
80  
81          if (!FOLD_PATTERN.equals(new String(buffer))) {
82              unread(buffer);
83          }
84          else {
85              log.debug("Unfolding..");
86          }
87  
88          return super.read();
89      }
90  
91      /*
92       * (non-Javadoc)
93       *
94       * @see java.io.Reader#read(char[])
95       *
96       * public int read(char[] arg0) throws IOException {
97       *
98       * super.read(buffer);
99       *
100      * if (!FOLD_PATTERN.equals(new String(buffer))) { unread(buffer); }
101      *
102      * return super.read(arg0); } /* (non-Javadoc)
103      * @see java.io.PushbackReader#read(char[], int, int)
104      *
105      * public int read(char[] arg0, int arg1, int arg2) throws IOException {
106      *
107      * super.read(buffer);
108      *
109      * if (!FOLD_PATTERN.equals(new String(buffer))) { unread(buffer); }
110      *
111      * return super.read(arg0, arg1, arg2); }
112      */
113 }