1 /*
2 * $Id: CalendarOutputter.java [Apr 5, 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.OutputStream;
38 import java.io.OutputStreamWriter;
39 import java.io.Writer;
40
41 import net.fortuna.ical4j.model.Calendar;
42 import net.fortuna.ical4j.model.ValidationException;
43
44 /***
45 * Writes an iCalendar model to an output stream.
46 *
47 * @author benf
48 */
49 public class CalendarOutputter {
50
51 private boolean validating;
52
53 /***
54 * Default constructor.
55 */
56 public CalendarOutputter() {
57 this(true);
58 }
59
60 /***
61 * @param validating indicates whether to validate
62 * calendar when outputting to stream
63 */
64 public CalendarOutputter(final boolean validating) {
65 this.validating = validating;
66 }
67
68 /***
69 * Outputs an iCalender string to the specified output stream.
70 *
71 * @param calendar
72 * calendar to write to ouput stream
73 * @param out
74 * an output stream
75 * @throws IOException
76 * thrown when unable to write to output stream
77 */
78 public final void output(final Calendar calendar, final OutputStream out)
79 throws IOException, ValidationException {
80
81 output(calendar, new OutputStreamWriter(out));
82 }
83
84 /***
85 * Outputs an iCalender string to the specified writer.
86 *
87 * @param calendar
88 * calendar to write to writer
89 * @param out
90 * a writer
91 * @throws IOException
92 * thrown when unable to write to writer
93 */
94 public final void output(final Calendar calendar, final Writer out)
95 throws IOException, ValidationException {
96
97 if (isValidating()) {
98 calendar.validate();
99 }
100
101 FoldingWriter writer = new FoldingWriter(out);
102
103 try {
104
105 writer.write(calendar.toString());
106 }
107 finally {
108
109 writer.close();
110 }
111 }
112
113 /***
114 * @return Returns the validating.
115 */
116 public boolean isValidating() {
117 return validating;
118 }
119
120 /***
121 * @param validating The validating to set.
122 */
123 public void setValidating(boolean validating) {
124 this.validating = validating;
125 }
126 }