jDoc, possible leak on crash, new depth option
[jvcard.git] / src / be / nikiroo / jvcard / Card.java
1 package be.nikiroo.jvcard;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.security.InvalidParameterException;
8 import java.util.List;
9
10 import be.nikiroo.jvcard.parsers.Format;
11 import be.nikiroo.jvcard.parsers.Parser;
12
13 /**
14 * A card is a contact information card. It contains data about one or more
15 * contacts.
16 *
17 * @author niki
18 */
19 public class Card extends BaseClass<Contact> {
20 private File file;
21 private String name;
22 private Format format;
23 private long lastModified;
24
25 /**
26 * Create a new {@link Card} from the given {@link File} and {@link Format}.
27 *
28 * @param file
29 * the input {@link File} containing the {@link Card} data or
30 * NULL for an empty card (usually a {@link File} name or a
31 * network path)
32 * @param format
33 * the {@link Format} to use to parse it
34 *
35 * @throws IOException
36 * in case of IO error
37 * @throws InvalidParameterException
38 * if format is NULL
39 */
40 public Card(File file, Format format) throws IOException {
41 this(Parser.parseContact(file, format));
42
43 if (file != null && file.exists()) {
44 lastModified = file.lastModified();
45 }
46
47 this.format = format;
48
49 if (file != null) {
50 this.file = file;
51 switch (format) {
52 case VCard21:
53 this.name = file.getName().replaceAll(".[vV][cC][fF]$", "");
54 break;
55 case Abook:
56 default:
57 this.name = file.getName();
58 break;
59 }
60 }
61 }
62
63 /**
64 * Create a new {@link Card} from the given {@link Contact}s.
65 *
66 * @param contacts
67 * the input contacts
68 *
69 * @throws IOException
70 * in case of IO error
71 * @throws InvalidParameterException
72 * if format is NULL
73 */
74 public Card(List<Contact> contacts) {
75 super(contacts);
76
77 lastModified = -1;
78 }
79
80 /**
81 * Save the {@link Card} to the given {@link File} with the given
82 * {@link Format}.
83 *
84 * @param file
85 * the output to save to
86 * @param format
87 * the {@link Format} to use
88 *
89 * @return TRUE if it was saved
90 *
91 * @throws IOException
92 * in case of IO errors
93 */
94 public boolean saveAs(File file, Format format) throws IOException {
95 if (file == null)
96 return false;
97
98 BufferedWriter writer = new BufferedWriter(new FileWriter(file));
99 try {
100 Parser.write(writer, format, this);
101 } finally {
102 writer.close();
103 }
104
105 if (this.file != null
106 && file.getCanonicalPath().equals(this.file.getCanonicalPath())) {
107 lastModified = file.lastModified();
108 setPristine();
109 }
110
111 return true;
112 }
113
114 /**
115 * Save the {@link Card} to the original {@link File} it was open from.
116 *
117 * @return TRUE if it was saved
118 *
119 * @throws IOException
120 * in case of IO errors
121 */
122 public boolean save() throws IOException {
123 return saveAs(file, format);
124 }
125
126 /**
127 * Reload the data from the input.
128 *
129 * @return TRUE if it was done
130 *
131 * @throws IOException
132 * in case of IO error
133 */
134 public boolean reload() throws IOException {
135 if (file == null)
136 return false;
137
138 this.replaceListContent(Parser.parseContact(file, format));
139 lastModified = file.lastModified();
140 setPristine();
141
142 return true;
143 }
144
145 /**
146 * Return the name of this card (the name of the {@link File} which it was
147 * opened from).
148 *
149 * @return the name
150 */
151 public String getName() {
152 return name;
153 }
154
155 /**
156 * Return the original {@link Format} of the {@link Card}.
157 *
158 * @return the {@link Format}
159 */
160 public Format getFormat() {
161 return format;
162 }
163
164 /**
165 * Return the {@link File} which was used to open this {@link Card}.
166 *
167 * @return the input
168 */
169 public File getFile() {
170 return file;
171 }
172
173 /**
174 * Break the link between this {@link Card} and he {@link File} which was
175 * used to open it if any.
176 */
177 public void unlink() {
178 file = null;
179 lastModified = -1;
180 }
181
182 /**
183 * Return the date of the last modification for this {@link Card} (or -1 if
184 * unknown/new).
185 *
186 * @return the last modified date
187 */
188 public long getLastModified() {
189 return lastModified;
190 }
191
192 @Override
193 public String toString() {
194 return "[Card: " + name + "]";
195 }
196
197 @Override
198 public String getId() {
199 return "" + name;
200 }
201
202 @Override
203 public String getState() {
204 return ("" + name + format).replace(' ', '_');
205 }
206 }