-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathChainingTextWriter.java
252 lines (229 loc) · 8.16 KB
/
ChainingTextWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package ezvcard.io.chain;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import ezvcard.Ezvcard;
import ezvcard.VCard;
import ezvcard.VCardVersion;
import ezvcard.io.scribe.VCardPropertyScribe;
import ezvcard.io.text.TargetApplication;
import ezvcard.io.text.VCardWriter;
import ezvcard.property.Address;
import ezvcard.property.StructuredName;
import ezvcard.property.VCardProperty;
/*
Copyright (c) 2012-2017, Michael Angstadt
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Chainer class for writing traditional, plain-text vCards.
* @see Ezvcard#write(Collection)
* @see Ezvcard#write(VCard...)
* @author Michael Angstadt
*/
public class ChainingTextWriter extends ChainingWriter<ChainingTextWriter> {
private VCardVersion version;
private boolean caretEncoding = false;
private Boolean includeTrailingSemicolons;
private TargetApplication targetApplication;
/**
* @param vcards the vCards to write
*/
public ChainingTextWriter(Collection<VCard> vcards) {
super(vcards);
}
/**
* <p>
* Sets the version that all the vCards will be marshalled to. The version
* that is attached to each individual {@link VCard} object will be ignored.
* </p>
* <p>
* If no version is passed into this method, the writer will look at the
* version attached to each individual {@link VCard} object and marshal it
* to that version. And if a {@link VCard} object has no version attached to
* it, then it will be marshalled to version 3.0.
* </p>
* @param version the version to marshal the vCards to
* @return this
*/
public ChainingTextWriter version(VCardVersion version) {
this.version = version;
return this;
}
/**
* Sets whether the writer will use circumflex accent encoding for vCard 3.0
* and 4.0 parameter values (disabled by default).
* @param enable true to use circumflex accent encoding, false not to
* @return this
* @see VCardWriter#setCaretEncodingEnabled(boolean)
* @see <a href="http://tools.ietf.org/html/rfc6868">RFC 6868</a>
*/
public ChainingTextWriter caretEncoding(boolean enable) {
this.caretEncoding = enable;
return this;
}
/**
* <p>
* Sets whether to include trailing semicolon delimiters for structured
* property values whose list of values end with null or empty values.
* Examples of properties that use structured values are
* {@link StructuredName} and {@link Address}.
* </p>
* <p>
* This setting exists for compatibility reasons and should not make a
* difference to consumers that correctly implement the vCard grammar.
* </p>
* @param include true to include the trailing semicolons, false not to,
* null to use the default behavior (defaults to false for vCard versions
* 2.1 and 3.0 and true for vCard version 4.0)
* @return this
* @see <a href="https://github.com/mangstadt/ez-vcard/issues/57">Issue
* 57</a>
*/
public ChainingTextWriter includeTrailingSemicolons(Boolean include) {
this.includeTrailingSemicolons = include;
return this;
}
/**
* <p>
* Sets the application that the vCards will be targeted for.
* </p>
* <p>
* Some vCard consumers do not completely adhere to the vCard specifications
* and require their vCards to be formatted in a specific way. See the
* {@link TargetApplication} class for a list of these applications.
* </p>
* @param targetApplication the target application or null if the vCards do
* not require any special processing (defaults to null)
* @return this
* @see VCardWriter#setTargetApplication(TargetApplication)
*/
public ChainingTextWriter targetApplication(TargetApplication targetApplication) {
this.targetApplication = targetApplication;
return this;
}
@Override
public ChainingTextWriter prodId(boolean include) {
return super.prodId(include);
}
@Override
public ChainingTextWriter versionStrict(boolean versionStrict) {
return super.versionStrict(versionStrict);
}
@Override
public ChainingTextWriter register(VCardPropertyScribe<? extends VCardProperty> scribe) {
return super.register(scribe);
}
/**
* Writes the vCards to a string.
* @return the vCard string
*/
public String go() {
StringWriter sw = new StringWriter();
try {
go(sw);
} catch (IOException e) {
//should never be thrown because we're writing to a string
throw new RuntimeException(e);
}
return sw.toString();
}
/**
* Writes the vCards to an output stream.
* @param out the output stream to write to
* @throws IOException if there's a problem writing to the output stream
*/
public void go(OutputStream out) throws IOException {
go(new VCardWriter(out, getVCardWriterConstructorVersion()));
}
/**
* Writes the vCards to a file. If the file exists, it will be overwritten.
* @param file the file to write to
* @throws IOException if there's a problem writing to the file
*/
public void go(File file) throws IOException {
go(file, false);
}
/**
* Writes the vCards to a file.
* @param file the file to write to
* @param append true to append onto the end of the file, false to overwrite
* it
* @throws IOException if there's a problem writing to the file
*/
public void go(File file, boolean append) throws IOException {
VCardWriter writer = new VCardWriter(file, append, getVCardWriterConstructorVersion());
try {
go(writer);
} finally {
writer.close();
}
}
/**
* Writes the vCards to a writer.
* @param writer the writer to write to
* @throws IOException if there's a problem writing to the writer
*/
public void go(Writer writer) throws IOException {
go(new VCardWriter(writer, getVCardWriterConstructorVersion()));
}
private void go(VCardWriter writer) throws IOException {
writer.setAddProdId(prodId);
writer.setCaretEncodingEnabled(caretEncoding);
writer.setVersionStrict(versionStrict);
writer.setIncludeTrailingSemicolons(includeTrailingSemicolons);
writer.setTargetApplication(targetApplication);
if (index != null) {
writer.setScribeIndex(index);
}
for (VCard vcard : vcards) {
if (version == null) {
//use the version that's assigned to each individual vCard
VCardVersion vcardVersion = vcard.getVersion();
if (vcardVersion == null) {
vcardVersion = VCardVersion.V3_0;
}
writer.setTargetVersion(vcardVersion);
}
writer.write(vcard);
writer.flush();
}
}
/**
* <p>
* Gets the {@link VCardVersion} object to pass into the {@link VCardWriter}
* constructor. The constructor does not allow a null version, so this
* method ensures that a non-null version is passed in.
* </p>
* <p>
* If the user hasn't chosen a version, the version that is passed into the
* constructor doesn't matter. This is because the writer's target version
* is reset every time a vCard is written (see the {@link #go(VCardWriter)}
* method).
* </p>
* @return the version to pass into the constructor
*/
private VCardVersion getVCardWriterConstructorVersion() {
return (version == null) ? VCardVersion.V3_0 : version;
}
}