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 writer
.append(toString(format
));
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 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 StringBuilder builder
= new StringBuilder();
154 for (String line
: Parser
.toStrings(this, format
)) {
155 builder
.append(line
);
156 builder
.append("\r\n");
158 return builder
.toString();
162 * Return the name of this card (the name of the {@link File} which it was
167 public String
getName() {
172 * Return the original {@link Format} of the {@link Card}.
174 * @return the {@link Format}
176 public Format
getFormat() {
181 * Return the {@link File} which was used to open this {@link Card}.
185 public File
getFile() {
190 * Return the date of the last modification for this {@link Card} (or -1 if
193 * @return the last modified date
195 public long getLastModified() {
200 public String
toString() {
201 return toString(Format
.VCard21
);
205 public String
getId() {
210 public String
getState() {
211 return ("" + name
+ format
).replace(' ', '_');