1 package be
.nikiroo
.jvcard
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.BufferedWriter
;
6 import java
.io
.FileInputStream
;
7 import java
.io
.FileWriter
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStreamReader
;
10 import java
.security
.InvalidParameterException
;
11 import java
.util
.LinkedList
;
12 import java
.util
.List
;
14 import be
.nikiroo
.jvcard
.parsers
.Format
;
15 import be
.nikiroo
.jvcard
.parsers
.Parser
;
18 * A card is a contact information card. It contains data about one or more
24 public class Card
extends BaseClass
<Contact
> {
27 private Format format
;
28 private long lastModified
;
29 private boolean remote
;
32 * Create a new {@link Card} from the given {@link File} and {@link Format}.
35 * the input {@link File} containing the {@link Card} data or
36 * NULL for an empty card (usually a {@link File} name or a
39 * the {@link Format} to use to parse it
43 * @throws InvalidParameterException
46 public Card(File file
, Format format
) throws IOException
{
47 this(load(file
, format
));
51 lastModified
= file
.lastModified();
61 this.name
= file
.getName().replaceAll(
62 ".[vV][cC][fF]$", "");
66 this.name
= file
.getName();
73 * Create a new {@link Card} from the given {@link Contact}s.
80 * @throws InvalidParameterException
83 public Card(List
<Contact
> contacts
) throws IOException
{
90 * Save the {@link Card} to the given {@link File} with the given
94 * the output to save to
96 * the {@link Format} to use
98 * @return TRUE if it was saved
100 * @throws IOException
101 * in case of IO errors
103 public boolean saveAs(File output
, Format format
) throws IOException
{
107 BufferedWriter writer
= new BufferedWriter(new FileWriter(output
));
108 writer
.append(toString(format
));
111 if (output
.getCanonicalPath().equals(this.file
.getCanonicalPath())) {
119 * Save the {@link Card} to the original {@link File} it was open from.
121 * @return TRUE if it was saved
123 * @throws IOException
124 * in case of IO errors
126 public boolean save() throws IOException
{
127 return saveAs(file
, format
);
131 * Reload the data from the input.
133 * @return TRUE if it was done
135 * @throws IOException
136 * in case of IO error
138 public boolean reload() throws IOException
{
142 this.replaceListContent(load(file
, format
));
148 * Return a {@link String} representation of this {@link Card} in the given
152 * the {@link Format} to use
154 * @return the {@link String}
156 public String
toString(Format format
) {
157 return Parser
.toString(this, format
);
161 * Return the name of this card (the name of the {@link File} which it was
166 public String
getName() {
171 * Return the original {@link Format} of the {@link Card}.
173 * @return the {@link Format}
175 public Format
getFormat() {
180 * Return the input which was used to open this {@link Card}.
184 public File
getInput() {
189 * Return the date of the last modification for this {@link Card} (or -1 if
192 * @return the last modified date
194 public long getLastModified() {
199 * Check if this {@link Card} is remote.
201 * @return TRUE if this {@link Card} is remote
203 public boolean isRemote() {
208 * Set the remote option on this {@link Card}.
211 * TRUE if this {@link Card} is remote
213 public void setRemote(boolean remote
) {
214 this.remote
= remote
;
218 public String
toString() {
219 return toString(Format
.VCard21
);
223 * Load the data from the given {@link File} under the given {@link Format}.
226 * the input to load from
228 * the {@link Format} to load as
230 * @return the list of elements
232 * @throws IOException
233 * in case of IO error
235 private static List
<Contact
> load(File file
, Format format
)
237 List
<String
> lines
= null;
239 if (file
!= null && file
.exists()) {
240 BufferedReader buffer
= new BufferedReader(new InputStreamReader(
241 new FileInputStream(file
), "UTF-8"));
242 lines
= new LinkedList
<String
>();
243 for (String line
= buffer
.readLine(); line
!= null; line
= buffer
251 return new LinkedList
<Contact
>();
253 return Parser
.parse(lines
, format
);