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
;
27 * Create a new {@link Card} from the given {@link File} and {@link Format}.
30 * the input {@link File} containing the {@link Card} data or
31 * NULL for an empty card (usually a {@link File} name or a
34 * the {@link Format} to use to parse it
38 * @throws InvalidParameterException
41 public Card(File file
, Format format
) throws IOException
{
42 this(Parser
.parseContact(file
, format
));
44 if (file
!= null && file
.exists()) {
45 lastModified
= file
.lastModified();
54 this.name
= file
.getName().replaceAll(".[vV][cC][fF]$", "");
58 this.name
= file
.getName();
65 * Create a new {@link Card} from the given {@link Contact}s.
72 * @throws InvalidParameterException
75 public Card(List
<Contact
> contacts
) {
82 * Save the {@link Card} to the given {@link File} with the given
86 * the output to save to
88 * the {@link Format} to use
90 * @return TRUE if it was saved
93 * in case of IO errors
95 public boolean saveAs(File file
, Format format
) throws IOException
{
99 BufferedWriter writer
= new BufferedWriter(new FileWriter(file
));
100 Parser
.write(writer
, format
, this);
103 if (this.file
!= null
104 && file
.getCanonicalPath().equals(this.file
.getCanonicalPath())) {
105 lastModified
= file
.lastModified();
113 * Save the {@link Card} to the original {@link File} it was open from.
115 * @return TRUE if it was saved
117 * @throws IOException
118 * in case of IO errors
120 public boolean save() throws IOException
{
121 return saveAs(file
, format
);
125 * Reload the data from the input.
127 * @return TRUE if it was done
129 * @throws IOException
130 * in case of IO error
132 public boolean reload() throws IOException
{
136 this.replaceListContent(Parser
.parseContact(file
, format
));
137 lastModified
= file
.lastModified();
144 * Return the name of this card (the name of the {@link File} which it was
149 public String
getName() {
154 * Return the original {@link Format} of the {@link Card}.
156 * @return the {@link Format}
158 public Format
getFormat() {
163 * Return the {@link File} which was used to open this {@link Card}.
167 public File
getFile() {
172 * Break the link between this {@link Card} and he {@link File} which was
173 * used to open it if any.
175 public void unlink() {
181 * Return the date of the last modification for this {@link Card} (or -1 if
184 * @return the last modified date
186 public long getLastModified() {
191 public String
toString() {
192 return "[Card: " + name
+ "]";
196 public String
getId() {
201 public String
getState() {
202 return ("" + name
+ format
).replace(' ', '_');