001package ezvcard;
002
003import java.io.File;
004import java.io.IOException;
005import java.io.InputStream;
006import java.io.Reader;
007import java.net.URL;
008import java.util.Arrays;
009import java.util.Collection;
010import java.util.Properties;
011
012import org.w3c.dom.Document;
013
014import ezvcard.io.chain.ChainingHtmlParser;
015import ezvcard.io.chain.ChainingHtmlStringParser;
016import ezvcard.io.chain.ChainingHtmlWriter;
017import ezvcard.io.chain.ChainingJsonParser;
018import ezvcard.io.chain.ChainingJsonStringParser;
019import ezvcard.io.chain.ChainingJsonWriter;
020import ezvcard.io.chain.ChainingTextParser;
021import ezvcard.io.chain.ChainingTextStringParser;
022import ezvcard.io.chain.ChainingTextWriter;
023import ezvcard.io.chain.ChainingXmlMemoryParser;
024import ezvcard.io.chain.ChainingXmlParser;
025import ezvcard.io.chain.ChainingXmlWriter;
026import ezvcard.io.html.HCardPage;
027import ezvcard.io.html.HCardParser;
028import ezvcard.io.json.JCardReader;
029import ezvcard.io.json.JCardWriter;
030import ezvcard.io.text.VCardReader;
031import ezvcard.io.text.VCardWriter;
032import ezvcard.io.xml.XCardDocument;
033import ezvcard.io.xml.XCardReader;
034import ezvcard.io.xml.XCardWriter;
035import ezvcard.util.IOUtils;
036
037/*
038 Copyright (c) 2012-2018, Michael Angstadt
039 All rights reserved.
040
041 Redistribution and use in source and binary forms, with or without
042 modification, are permitted provided that the following conditions are met: 
043
044 1. Redistributions of source code must retain the above copyright notice, this
045 list of conditions and the following disclaimer. 
046 2. Redistributions in binary form must reproduce the above copyright notice,
047 this list of conditions and the following disclaimer in the documentation
048 and/or other materials provided with the distribution. 
049
050 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
051 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
052 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
053 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
054 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
055 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
056 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
057 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
058 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
059 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
060
061 The views and conclusions contained in the software and documentation are those
062 of the authors and should not be interpreted as representing official policies, 
063 either expressed or implied, of the FreeBSD Project.
064 */
065
066/**
067 * <p>
068 * Contains chaining factory methods for parsing/writing vCards. They are
069 * convenience methods that make use of the following classes:
070 * </p>
071 * <table class="simpleTable">
072 * <caption>Classes used by this class</caption>
073 * <tr>
074 * <th></th>
075 * <th>Reading</th>
076 * <th>Writing</th>
077 * </tr>
078 * <tr>
079 * <th>Plain text</th>
080 * <td>{@link VCardReader}</td>
081 * <td>{@link VCardWriter}</td>
082 * </tr>
083 * <tr>
084 * <th>XML</th>
085 * <td>{@link XCardDocument}, {@link XCardReader}</td>
086 * <td>{@link XCardDocument}, {@link XCardWriter}</td>
087 * </tr>
088 * <tr>
089 * <th>HTML</th>
090 * <td>{@link HCardParser}</td>
091 * <td>{@link HCardPage}</td>
092 * </tr>
093 * <tr>
094 * <th>JSON</th>
095 * <td>{@link JCardReader}</td>
096 * <td>{@link JCardWriter}</td>
097 * </tr>
098 * </table>
099 * @author Michael Angstadt
100 */
101public final class Ezvcard {
102        /**
103         * The version of the library.
104         */
105        public static final String VERSION;
106
107        /**
108         * The Maven group ID.
109         */
110        public static final String GROUP_ID;
111
112        /**
113         * The Maven artifact ID.
114         */
115        public static final String ARTIFACT_ID;
116
117        /**
118         * The project webpage.
119         */
120        public static final String URL;
121
122        static {
123                InputStream in = null;
124                Properties props = new Properties();
125                try {
126                        in = Ezvcard.class.getResourceAsStream("ez-vcard.properties");
127                        props.load(in);
128                } catch (IOException e) {
129                        throw new RuntimeException(e);
130                } finally {
131                        IOUtils.closeQuietly(in);
132                }
133
134                VERSION = props.getProperty("version");
135                GROUP_ID = props.getProperty("groupId");
136                ARTIFACT_ID = props.getProperty("artifactId");
137                URL = props.getProperty("url");
138        }
139
140        /**
141         * <p>
142         * Parses plain text vCards.
143         * </p>
144         * <p>
145         * Use {@link VCardReader} for more control over the parsing.
146         * </p>
147         * @param str the vCard string
148         * @return chainer object for completing the parse operation
149         * @see VCardReader
150         * @see <a href="http://www.imc.org/pdi/vcard-21.rtf">vCard 2.1</a>
151         * @see <a href="http://tools.ietf.org/html/rfc2426">RFC 2426 (3.0)</a>
152         * @see <a href="http://tools.ietf.org/html/rfc6350">RFC 6350 (4.0)</a>
153         */
154        public static ChainingTextStringParser parse(String str) {
155                return new ChainingTextStringParser(str);
156        }
157
158        /**
159         * <p>
160         * Parses plain text vCards.
161         * </p>
162         * <p>
163         * Use {@link VCardReader} for more control over the parsing.
164         * </p>
165         * @param file the vCard file
166         * @return chainer object for completing the parse operation
167         * @see VCardReader
168         * @see <a href="http://www.imc.org/pdi/vcard-21.rtf">vCard 2.1</a>
169         * @see <a href="http://tools.ietf.org/html/rfc2426">RFC 2426 (3.0)</a>
170         * @see <a href="http://tools.ietf.org/html/rfc6350">RFC 6350 (4.0)</a>
171         */
172        public static ChainingTextParser<ChainingTextParser<?>> parse(File file) {
173                return new ChainingTextParser<ChainingTextParser<?>>(file);
174        }
175
176        /**
177         * <p>
178         * Parses plain text vCards.
179         * </p>
180         * <p>
181         * Use {@link VCardReader} for more control over the parsing.
182         * </p>
183         * @param in the input stream
184         * @return chainer object for completing the parse operation
185         * @see VCardReader
186         * @see <a href="http://www.imc.org/pdi/vcard-21.rtf">vCard 2.1</a>
187         * @see <a href="http://tools.ietf.org/html/rfc2426">RFC 2426 (3.0)</a>
188         * @see <a href="http://tools.ietf.org/html/rfc6350">RFC 6350 (4.0)</a>
189         */
190        public static ChainingTextParser<ChainingTextParser<?>> parse(InputStream in) {
191                return new ChainingTextParser<ChainingTextParser<?>>(in);
192        }
193
194        /**
195         * <p>
196         * Parses plain text vCards.
197         * </p>
198         * <p>
199         * Use {@link VCardReader} for more control over the parsing.
200         * </p>
201         * @param reader the reader
202         * @return chainer object for completing the parse operation
203         * @see VCardReader
204         * @see <a href="http://www.imc.org/pdi/vcard-21.rtf">vCard 2.1</a>
205         * @see <a href="http://tools.ietf.org/html/rfc2426">RFC 2426 (3.0)</a>
206         * @see <a href="http://tools.ietf.org/html/rfc6350">RFC 6350 (4.0)</a>
207         */
208        public static ChainingTextParser<ChainingTextParser<?>> parse(Reader reader) {
209                return new ChainingTextParser<ChainingTextParser<?>>(reader);
210        }
211
212        /**
213         * <p>
214         * Parses XML-encoded vCards (xCard) from a string.
215         * </p>
216         * <p>
217         * Use {@link XCardDocument} or {@link XCardReader} for more control over
218         * the parsing.
219         * </p>
220         * @param xml the XML document
221         * @return chainer object for completing the parse operation
222         * @see XCardDocument
223         * @see XCardReader
224         * @see <a href="http://tools.ietf.org/html/rfc6351">RFC 6351</a>
225         */
226        public static ChainingXmlMemoryParser parseXml(String xml) {
227                return new ChainingXmlMemoryParser(xml);
228        }
229
230        /**
231         * <p>
232         * Parses XML-encoded vCards (xCard) from a file.
233         * </p>
234         * <p>
235         * Use {@link XCardDocument} or {@link XCardReader} for more control over
236         * the parsing.
237         * </p>
238         * @param file the XML file
239         * @return chainer object for completing the parse operation
240         * @see XCardDocument
241         * @see XCardReader
242         * @see <a href="http://tools.ietf.org/html/rfc6351">RFC 6351</a>
243         */
244        public static ChainingXmlParser<ChainingXmlParser<?>> parseXml(File file) {
245                return new ChainingXmlParser<ChainingXmlParser<?>>(file);
246        }
247
248        /**
249         * <p>
250         * Parses XML-encoded vCards (xCard) from an input stream.
251         * </p>
252         * <p>
253         * Use {@link XCardDocument} or {@link XCardReader} for more control over
254         * the parsing.
255         * </p>
256         * @param in the input stream to the XML document
257         * @return chainer object for completing the parse operation
258         * @see XCardDocument
259         * @see XCardReader
260         * @see <a href="http://tools.ietf.org/html/rfc6351">RFC 6351</a>
261         */
262        public static ChainingXmlParser<ChainingXmlParser<?>> parseXml(InputStream in) {
263                return new ChainingXmlParser<ChainingXmlParser<?>>(in);
264        }
265
266        /**
267         * <p>
268         * Parses XML-encoded vCards (xCard) from a reader.
269         * </p>
270         * <p>
271         * Note that use of this method is discouraged. It ignores the character
272         * encoding that is defined within the XML document itself, and should only
273         * be used if the encoding is undefined or if the encoding needs to be
274         * ignored for some reason. The {@link #parseXml(InputStream)} method should
275         * be used instead, since it takes the XML document's character encoding
276         * into account when parsing.
277         * </p>
278         * <p>
279         * Use {@link XCardDocument} or {@link XCardReader} for more control over
280         * the parsing.
281         * </p>
282         * @param reader the reader to the XML document
283         * @return chainer object for completing the parse operation
284         * @see XCardDocument
285         * @see XCardReader
286         * @see <a href="http://tools.ietf.org/html/rfc6351">RFC 6351</a>
287         */
288        public static ChainingXmlParser<ChainingXmlParser<?>> parseXml(Reader reader) {
289                return new ChainingXmlParser<ChainingXmlParser<?>>(reader);
290        }
291
292        /**
293         * <p>
294         * Parses XML-encoded vCards (xCard).
295         * </p>
296         * <p>
297         * Use {@link XCardDocument} or {@link XCardReader} for more control over
298         * the parsing.
299         * </p>
300         * @param document the XML document
301         * @return chainer object for completing the parse operation
302         * @see XCardDocument
303         * @see XCardReader
304         * @see <a href="http://tools.ietf.org/html/rfc6351">RFC 6351</a>
305         */
306        public static ChainingXmlMemoryParser parseXml(Document document) {
307                return new ChainingXmlMemoryParser(document);
308        }
309
310        /**
311         * <p>
312         * Parses HTML-encoded vCards (hCard).
313         * </p>
314         * <p>
315         * Use {@link HCardParser} for more control over the parsing.
316         * </p>
317         * @param html the HTML page
318         * @return chainer object for completing the parse operation
319         * @see HCardParser
320         * @see <a href="http://microformats.org/wiki/hcard">hCard 1.0</a>
321         */
322        public static ChainingHtmlStringParser parseHtml(String html) {
323                return new ChainingHtmlStringParser(html);
324        }
325
326        /**
327         * <p>
328         * Parses HTML-encoded vCards (hCard).
329         * </p>
330         * <p>
331         * Use {@link HCardParser} for more control over the parsing.
332         * </p>
333         * @param file the HTML file
334         * @return chainer object for completing the parse operation
335         * @see HCardParser
336         * @see <a href="http://microformats.org/wiki/hcard">hCard 1.0</a>
337         */
338        public static ChainingHtmlParser<ChainingHtmlParser<?>> parseHtml(File file) {
339                return new ChainingHtmlParser<ChainingHtmlParser<?>>(file);
340        }
341
342        /**
343         * <p>
344         * Parses HTML-encoded vCards (hCard).
345         * </p>
346         * <p>
347         * Use {@link HCardParser} for more control over the parsing.
348         * </p>
349         * @param in the input stream to the HTML page
350         * @return chainer object for completing the parse operation
351         * @see HCardParser
352         * @see <a href="http://microformats.org/wiki/hcard">hCard 1.0</a>
353         */
354        public static ChainingHtmlParser<ChainingHtmlParser<?>> parseHtml(InputStream in) {
355                return new ChainingHtmlParser<ChainingHtmlParser<?>>(in);
356        }
357
358        /**
359         * <p>
360         * Parses HTML-encoded vCards (hCard).
361         * </p>
362         * <p>
363         * Use {@link HCardParser} for more control over the parsing.
364         * </p>
365         * @param reader the reader to the HTML page
366         * @return chainer object for completing the parse operation
367         * @see HCardParser
368         * @see <a href="http://microformats.org/wiki/hcard">hCard 1.0</a>
369         */
370        public static ChainingHtmlParser<ChainingHtmlParser<?>> parseHtml(Reader reader) {
371                return new ChainingHtmlParser<ChainingHtmlParser<?>>(reader);
372        }
373
374        /**
375         * <p>
376         * Parses HTML-encoded vCards (hCard).
377         * </p>
378         * <p>
379         * Use {@link HCardParser} for more control over the parsing.
380         * </p>
381         * @param url the URL of the webpage
382         * @return chainer object for completing the parse operation
383         * @see HCardParser
384         * @see <a href="http://microformats.org/wiki/hcard">hCard 1.0</a>
385         */
386        public static ChainingHtmlParser<ChainingHtmlParser<?>> parseHtml(URL url) {
387                return new ChainingHtmlParser<ChainingHtmlParser<?>>(url);
388        }
389
390        /**
391         * <p>
392         * Parses JSON-encoded vCards (jCard).
393         * </p>
394         * <p>
395         * Use {@link JCardReader} for more control over the parsing.
396         * </p>
397         * @param json the JSON string
398         * @return chainer object for completing the parse operation
399         * @see JCardReader
400         * @see <a href="http://tools.ietf.org/html/rfc7095">RFC 7095</a>
401         */
402        public static ChainingJsonStringParser parseJson(String json) {
403                return new ChainingJsonStringParser(json);
404        }
405
406        /**
407         * <p>
408         * Parses JSON-encoded vCards (jCard).
409         * </p>
410         * <p>
411         * Use {@link JCardReader} for more control over the parsing.
412         * </p>
413         * @param file the JSON file
414         * @return chainer object for completing the parse operation
415         * @see JCardReader
416         * @see <a href="http://tools.ietf.org/html/rfc7095">RFC 7095</a>
417         */
418        public static ChainingJsonParser<ChainingJsonParser<?>> parseJson(File file) {
419                return new ChainingJsonParser<ChainingJsonParser<?>>(file);
420        }
421
422        /**
423         * <p>
424         * Parses JSON-encoded vCards (jCard).
425         * </p>
426         * <p>
427         * Use {@link JCardReader} for more control over the parsing.
428         * </p>
429         * @param in the input stream
430         * @return chainer object for completing the parse operation
431         * @see JCardReader
432         * @see <a href="http://tools.ietf.org/html/rfc7095">RFC 7095</a>
433         */
434        public static ChainingJsonParser<ChainingJsonParser<?>> parseJson(InputStream in) {
435                return new ChainingJsonParser<ChainingJsonParser<?>>(in);
436        }
437
438        /**
439         * <p>
440         * Parses JSON-encoded vCards (jCard).
441         * </p>
442         * <p>
443         * Use {@link JCardReader} for more control over the parsing.
444         * </p>
445         * @param reader the reader
446         * @return chainer object for completing the parse operation
447         * @see JCardReader
448         * @see <a href="http://tools.ietf.org/html/rfc7095">RFC 7095</a>
449         */
450        public static ChainingJsonParser<ChainingJsonParser<?>> parseJson(Reader reader) {
451                return new ChainingJsonParser<ChainingJsonParser<?>>(reader);
452        }
453
454        /**
455         * <p>
456         * Marshals one or more vCards to their traditional, plain-text
457         * representation.
458         * </p>
459         * <p>
460         * Use {@link VCardWriter} for more control over how the vCards are written.
461         * </p>
462         * @param vcards the vCards to marshal
463         * @return chainer object for completing the write operation
464         * @see VCardWriter
465         * @see <a href="http://www.imc.org/pdi/vcard-21.rtf">vCard 2.1</a>
466         * @see <a href="http://tools.ietf.org/html/rfc2426">RFC 2426 (3.0)</a>
467         * @see <a href="http://tools.ietf.org/html/rfc6350">RFC 6350 (4.0)</a>
468         */
469        public static ChainingTextWriter write(VCard... vcards) {
470                return write(Arrays.asList(vcards));
471        }
472
473        /**
474         * <p>
475         * Marshals one or more vCards to their traditional, plain-text
476         * representation.
477         * </p>
478         * <p>
479         * Use {@link VCardWriter} for more control over how the vCards are written.
480         * </p>
481         * @param vcards the vCards to marshal
482         * @return chainer object for completing the write operation
483         * @see VCardWriter
484         * @see <a href="http://www.imc.org/pdi/vcard-21.rtf">vCard 2.1</a>
485         * @see <a href="http://tools.ietf.org/html/rfc2426">RFC 2426 (3.0)</a>
486         * @see <a href="http://tools.ietf.org/html/rfc6350">RFC 6350 (4.0)</a>
487         */
488        public static ChainingTextWriter write(Collection<VCard> vcards) {
489                return new ChainingTextWriter(vcards);
490        }
491
492        /**
493         * <p>
494         * Marshals one or more vCards to their XML representation (xCard).
495         * </p>
496         * <p>
497         * Use {@link XCardDocument} or {@link XCardWriter} for more control over
498         * how the vCards are written.
499         * </p>
500         * @param vcards the vCards to marshal
501         * @return chainer object for completing the write operation
502         * @see XCardDocument
503         * @see XCardWriter
504         * @see <a href="http://tools.ietf.org/html/rfc6351">RFC 6351</a>
505         */
506        public static ChainingXmlWriter writeXml(VCard... vcards) {
507                return writeXml(Arrays.asList(vcards));
508        }
509
510        /**
511         * <p>
512         * Marshals one or more vCards to their XML representation (xCard).
513         * </p>
514         * <p>
515         * Use {@link XCardDocument} or {@link XCardWriter} for more control over
516         * how the vCards are written.
517         * </p>
518         * @param vcards the vCard to marshal
519         * @return chainer object for completing the write operation
520         * @see XCardDocument
521         * @see XCardWriter
522         * @see <a href="http://tools.ietf.org/html/rfc6351">RFC 6351</a>
523         */
524        public static ChainingXmlWriter writeXml(Collection<VCard> vcards) {
525                return new ChainingXmlWriter(vcards);
526        }
527
528        /**
529         * <p>
530         * Marshals one or more vCards their HTML representation (hCard).
531         * </p>
532         * <p>
533         * Use {@link HCardPage} for more control over how the vCards are written.
534         * </p>
535         * @param vcards the vCard(s) to marshal
536         * @return chainer object for completing the write operation
537         * @see HCardPage
538         * @see <a href="http://microformats.org/wiki/hcard">hCard 1.0</a>
539         */
540        public static ChainingHtmlWriter writeHtml(VCard... vcards) {
541                return writeHtml(Arrays.asList(vcards));
542        }
543
544        /**
545         * <p>
546         * Marshals one or more vCards their HTML representation (hCard).
547         * </p>
548         * <p>
549         * Use {@link HCardPage} for more control over how the vCards are written.
550         * </p>
551         * @param vcards the vCard(s) to marshal
552         * @return chainer object for completing the write operation
553         * @see HCardPage
554         * @see <a href="http://microformats.org/wiki/hcard">hCard 1.0</a>
555         */
556        public static ChainingHtmlWriter writeHtml(Collection<VCard> vcards) {
557                return new ChainingHtmlWriter(vcards);
558        }
559
560        /**
561         * <p>
562         * Marshals one or more vCards to their JSON representation (jCard).
563         * </p>
564         * <p>
565         * Use {@link JCardWriter} for more control over how the vCards are written.
566         * </p>
567         * @param vcards the vCards to marshal
568         * @return chainer object for completing the write operation
569         * @see JCardWriter
570         * @see <a href="http://tools.ietf.org/html/rfc7095">RFC 7095</a>
571         */
572        public static ChainingJsonWriter writeJson(VCard... vcards) {
573                return writeJson(Arrays.asList(vcards));
574        }
575
576        /**
577         * <p>
578         * Marshals one or more vCards to their JSON representation (jCard).
579         * </p>
580         * <p>
581         * Use {@link JCardWriter} for more control over how the vCards are written.
582         * </p>
583         * @param vcards the vCards to marshal
584         * @return chainer object for completing the write operation
585         * @see JCardWriter
586         * @see <a href="http://tools.ietf.org/html/rfc7095">RFC 7095</a>
587         */
588        public static ChainingJsonWriter writeJson(Collection<VCard> vcards) {
589                return new ChainingJsonWriter(vcards);
590        }
591
592        private Ezvcard() {
593                //hide
594        }
595}