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
19 public class Card
extends BaseClass
<Contact
> {
22 private Format format
;
23 private long lastModified
;
26 * Create a new {@link Card} from the given {@link File} and {@link Format}.
29 * the input {@link File} containing the {@link Card} data or
30 * NULL for an empty card (usually a {@link File} name or a
33 * the {@link Format} to use to parse it
37 * @throws InvalidParameterException
40 public Card(File file
, Format format
) throws IOException
{
41 this(Parser
.parseContact(file
, format
));
43 if (file
!= null && file
.exists()) {
44 lastModified
= file
.lastModified();
53 this.name
= file
.getName().replaceAll(".[vV][cC][fF]$", "");
57 this.name
= file
.getName();
64 * Create a new {@link Card} from the given {@link Contact}s.
71 * @throws InvalidParameterException
74 public Card(List
<Contact
> contacts
) {
81 * Save the {@link Card} to the given {@link File} with the given
85 * the output to save to
87 * the {@link Format} to use
89 * @return TRUE if it was saved
92 * in case of IO errors
94 public boolean saveAs(File file
, Format format
) throws IOException
{
98 BufferedWriter writer
= new BufferedWriter(new FileWriter(file
));
100 Parser
.write(writer
, format
, this);
105 if (this.file
!= null
106 && file
.getCanonicalPath().equals(this.file
.getCanonicalPath())) {
107 lastModified
= file
.lastModified();
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
.parseContact(file
, format
));
139 lastModified
= file
.lastModified();
146 * Return the name of this card (the name of the {@link File} which it was
151 public String
getName() {
156 * Return the original {@link Format} of the {@link Card}.
158 * @return the {@link Format}
160 public Format
getFormat() {
165 * Return the {@link File} which was used to open this {@link Card}.
169 public File
getFile() {
174 * Break the link between this {@link Card} and he {@link File} which was
175 * used to open it if any.
177 public void unlink() {
183 * Return the date of the last modification for this {@link Card} (or -1 if
186 * @return the last modified date
188 public long getLastModified() {
193 public String
toString() {
194 return "[Card: " + name
+ "]";
198 public String
getId() {
203 public String
getState() {
204 return ("" + name
+ format
).replace(' ', '_');