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