1 package be
.nikiroo
.jvcard
;
3 import java
.io
.BufferedWriter
;
5 import java
.io
.FileWriter
;
6 import java
.io
.IOException
;
7 import java
.security
.InvalidParameterException
;
10 import be
.nikiroo
.jvcard
.parsers
.Format
;
11 import be
.nikiroo
.jvcard
.parsers
.Parser
;
14 * A card is a contact information card. It contains data about one or more
20 public class Card
extends BaseClass
<Contact
> {
23 private Format format
;
24 private long lastModified
;
25 private boolean remote
;
28 * Create a new {@link Card} from the given {@link File} and {@link Format}.
31 * the input {@link File} containing the {@link Card} data or
32 * NULL for an empty card (usually a {@link File} name or a
35 * the {@link Format} to use to parse it
39 * @throws InvalidParameterException
42 public Card(File file
, Format format
) throws IOException
{
43 this(Parser
.parse(file
, format
));
47 lastModified
= file
.lastModified();
57 this.name
= file
.getName().replaceAll(".[vV][cC][fF]$", "");
61 this.name
= file
.getName();
68 * Create a new {@link Card} from the given {@link Contact}s.
75 * @throws InvalidParameterException
78 public Card(List
<Contact
> contacts
) throws IOException
{
85 * Save the {@link Card} to the given {@link File} with the given
89 * the output to save to
91 * the {@link Format} to use
93 * @return TRUE if it was saved
96 * in case of IO errors
98 public boolean saveAs(File file
, Format format
) throws IOException
{
102 BufferedWriter writer
= new BufferedWriter(new FileWriter(file
));
103 writer
.append(toString(format
));
106 if (this.file
!= null
107 && file
.getCanonicalPath().equals(this.file
.getCanonicalPath())) {
115 * Save the {@link Card} to the original {@link File} it was open from.
117 * @return TRUE if it was saved
119 * @throws IOException
120 * in case of IO errors
122 public boolean save() throws IOException
{
123 return saveAs(file
, format
);
127 * Reload the data from the input.
129 * @return TRUE if it was done
131 * @throws IOException
132 * in case of IO error
134 public boolean reload() throws IOException
{
138 this.replaceListContent(Parser
.parse(file
, format
));
144 * Return a {@link String} representation of this {@link Card} in the given
148 * the {@link Format} to use
150 * @return the {@link String}
152 public String
toString(Format format
) {
153 return Parser
.toString(this, format
);
157 * Return the name of this card (the name of the {@link File} which it was
162 public String
getName() {
167 * Return the original {@link Format} of the {@link Card}.
169 * @return the {@link Format}
171 public Format
getFormat() {
176 * Return the {@link File} which was used to open this {@link Card}.
180 public File
getFile() {
185 * Return the date of the last modification for this {@link Card} (or -1 if
188 * @return the last modified date
190 public long getLastModified() {
195 * Check if this {@link Card} is remote.
197 * @return TRUE if this {@link Card} is remote
199 public boolean isRemote() {
204 * Set the remote option on this {@link Card}.
207 * TRUE if this {@link Card} is remote
209 public void setRemote(boolean remote
) {
210 this.remote
= remote
;
214 public String
toString() {
215 return toString(Format
.VCard21
);
219 public String
getId() {
224 public String
getState() {
225 return "" + name
+ format
;