Debian Codegraph @

src/test/java/ezvcard/io/JohnDoeVCard.java

raw symbols SCIP
1 package ezvcard.io;
2
3 import java.io.IOException;
4 import java.nio.file.Path;
5 import java.nio.file.Paths;
6 import java.time.LocalDate;
7 import java.time.ZoneOffset;
8
9 import ezvcard.Ezvcard;
10 import ezvcard.VCard;
11 import ezvcard.VCardVersion;
12 import ezvcard.parameter.AddressType;
13 import ezvcard.parameter.EmailType;
14 import ezvcard.parameter.ImageType;
15 import ezvcard.parameter.SoundType;
16 import ezvcard.parameter.TelephoneType;
17 import ezvcard.property.Address;
18 import ezvcard.property.Birthday;
19 import ezvcard.property.Gender;
20 import ezvcard.property.Kind;
21 import ezvcard.property.Photo;
22 import ezvcard.property.Revision;
23 import ezvcard.property.Sound;
24 import ezvcard.property.StructuredName;
25 import ezvcard.property.Timezone;
26 import ezvcard.property.Uid;
27
28 /*
29 Copyright (c) 2012-2023, Michael Angstadt
30 All rights reserved.
31
32 Redistribution and use in source and binary forms, with or without
33 modification, are permitted provided that the following conditions are met:
34
35 1. Redistributions of source code must retain the above copyright notice, this
36 list of conditions and the following disclaimer.
37 2. Redistributions in binary form must reproduce the above copyright notice,
38 this list of conditions and the following disclaimer in the documentation
39 and/or other materials provided with the distribution.
40
41 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
42 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
45 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
52 The views and conclusions contained in the software and documentation are those
53 of the authors and should not be interpreted as representing official policies,
54 either expressed or implied, of the FreeBSD Project.
55 */
56
57 /**
58 * Generates a sample vCard.
59 * @author Michael Angstadt
60 */
61 public class JohnDoeVCard {
62 public static void main(String[] args) throws Throwable {
63 VCard vcard = createVCard();
64
65 //validate vCard for version 2.1
66 System.out.println("Version 2.1 validation warnings:");
67 System.out.println(vcard.validate(VCardVersion.V2_1));
68 System.out.println();
69
70 //validate vCard for version 3.0
71 System.out.println("Version 3.0 validation warnings:");
72 System.out.println(vcard.validate(VCardVersion.V3_0));
73 System.out.println();
74
75 //validate vCard for version 4.0 (xCard and jCard use this version)
76 System.out.println("Version 4.0 validation warnings:");
77 System.out.println(vcard.validate(VCardVersion.V4_0));
78
79 //write vCard
80 Path file = Paths.get("john-doe.21.vcf");
81 System.out.println("Writing " + file + "...");
82 Ezvcard.write(vcard).version(VCardVersion.V2_1).go(file);
83
84 file = Paths.get("john-doe.3.vcf");
85 System.out.println("Writing " + file + "...");
86 Ezvcard.write(vcard).version(VCardVersion.V3_0).go(file);
87
88 file = Paths.get("john-doe.4.vcf");
89 System.out.println("Writing " + file + "...");
90 Ezvcard.write(vcard).version(VCardVersion.V4_0).go(file);
91
92 //write xCard
93 file = Paths.get("john-doe.xml");
94 System.out.println("Writing " + file + "...");
95 Ezvcard.writeXml(vcard).indent(2).go(file);
96
97 //write hCard
98 file = Paths.get("john-doe.html");
99 System.out.println("Writing " + file + "...");
100 Ezvcard.writeHtml(vcard).go(file);
101
102 //write jCard
103 file = Paths.get("john-doe.json");
104 System.out.println("Writing " + file + "...");
105 Ezvcard.writeJson(vcard).prettyPrint(true).go(file);
106 }
107
108 private static VCard createVCard() throws IOException {
109 VCard vcard = new VCard();
110
111 vcard.setKind(Kind.individual());
112
113 vcard.setGender(Gender.male());
114
115 vcard.addLanguage("en-US");
116
117 StructuredName n = new StructuredName();
118 n.setFamily("Doe");
119 n.setGiven("Jonathan");
120 n.getPrefixes().add("Mr");
121 vcard.setStructuredName(n);
122
123 vcard.setFormattedName("Jonathan Doe");
124
125 vcard.setNickname("John", "Jonny");
126
127 vcard.setBirthday(new Birthday(LocalDate.of(1980, 1, 1)));
128
129 vcard.addTitle("Widget Engineer");
130
131 vcard.setOrganization("Acme Co. Ltd.", "Widget Department");
132
133 Address adr = new Address();
134 adr.setStreetAddress("123 Wall St.");
135 adr.setLocality("New York");
136 adr.setRegion("NY");
137 adr.setPostalCode("12345");
138 adr.setCountry("USA");
139 adr.setLabel("123 Wall St.\nNew York, NY 12345\nUSA");
140 adr.getTypes().add(AddressType.WORK);
141 vcard.addAddress(adr);
142
143 adr = new Address();
144 adr.setStreetAddress("123 Main St.");
145 adr.setLocality("Albany");
146 adr.setRegion("NY");
147 adr.setPostalCode("54321");
148 adr.setCountry("USA");
149 adr.setLabel("123 Main St.\nAlbany, NY 54321\nUSA");
150 adr.getTypes().add(AddressType.HOME);
151 vcard.addAddress(adr);
152
153 vcard.addTelephoneNumber("1-555-555-1234", TelephoneType.WORK);
154 vcard.addTelephoneNumber("1-555-555-5678", TelephoneType.WORK, TelephoneType.CELL);
155
156 vcard.addEmail("[email protected]", EmailType.HOME);
157 vcard.addEmail("[email protected]", EmailType.WORK);
158
159 vcard.addUrl("http://www.acme-co.com");
160
161 vcard.setCategories("widgetphile", "biker", "vCard expert");
162
163 vcard.setGeo(37.6, -95.67);
164
165 vcard.setTimezone(new Timezone(ZoneOffset.ofHours(-5), "America/New_York"));
166
167 Path file = Paths.get("portrait.jpg");
168 Photo photo = new Photo(file, ImageType.JPEG);
169 vcard.addPhoto(photo);
170
171 file = Paths.get("pronunciation.ogg");
172 Sound sound = new Sound(file, SoundType.OGG);
173 vcard.addSound(sound);
174
175 vcard.setUid(Uid.random());
176
177 vcard.setRevision(Revision.now());
178
179 return vcard;
180 }
181 }