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
.parseContact(file
, format
));
45 if (file
!= null && file
.exists()) {
46 lastModified
= file
.lastModified();
55 this.name
= file
.getName().replaceAll(".[vV][cC][fF]$", "");
59 this.name
= file
.getName();
66 * Create a new {@link Card} from the given {@link Contact}s.
73 * @throws InvalidParameterException
76 public Card(List
<Contact
> contacts
) throws IOException
{
83 * Save the {@link Card} to the given {@link File} with the given
87 * the output to save to
89 * the {@link Format} to use
91 * @return TRUE if it was saved
94 * in case of IO errors
96 public boolean saveAs(File file
, Format format
) throws IOException
{
100 BufferedWriter writer
= new BufferedWriter(new FileWriter(file
));
101 writer
.append(toString(format
));
104 if (this.file
!= null
105 && file
.getCanonicalPath().equals(this.file
.getCanonicalPath())) {
106 lastModified
= file
.lastModified();
114 * Save the {@link Card} to the original {@link File} it was open from.
116 * @return TRUE if it was saved
118 * @throws IOException
119 * in case of IO errors
121 public boolean save() throws IOException
{
122 return saveAs(file
, format
);
126 * Reload the data from the input.
128 * @return TRUE if it was done
130 * @throws IOException
131 * in case of IO error
133 public boolean reload() throws IOException
{
137 this.replaceListContent(Parser
.parseContact(file
, format
));
138 lastModified
= file
.lastModified();
145 * Return a {@link String} representation of this {@link Card} in the given
149 * the {@link Format} to use
151 * @return the {@link String}
153 public String
toString(Format format
) {
154 StringBuilder builder
= new StringBuilder();
155 for (String line
: Parser
.toStrings(this, format
)) {
156 builder
.append(line
);
157 builder
.append("\r\n");
159 return builder
.toString();
163 * Return the name of this card (the name of the {@link File} which it was
168 public String
getName() {
173 * Return the original {@link Format} of the {@link Card}.
175 * @return the {@link Format}
177 public Format
getFormat() {
182 * Return the {@link File} which was used to open this {@link Card}.
186 public File
getFile() {
191 * Return the date of the last modification for this {@link Card} (or -1 if
194 * @return the last modified date
196 public long getLastModified() {
201 * Check if this {@link Card} is remote.
203 * @return TRUE if this {@link Card} is remote
205 public boolean isRemote() {
210 * Set the remote option on this {@link Card}.
213 * TRUE if this {@link Card} is remote
215 public void setRemote(boolean remote
) {
216 this.remote
= remote
;
220 public String
toString() {
221 return toString(Format
.VCard21
);
225 public String
getId() {
230 public String
getState() {
231 return "" + name
+ format
;