1 package be
.nikiroo
.jvcard
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.BufferedWriter
;
6 import java
.io
.FileInputStream
;
7 import java
.io
.FileWriter
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStreamReader
;
10 import java
.security
.InvalidParameterException
;
11 import java
.util
.LinkedList
;
12 import java
.util
.List
;
14 import be
.nikiroo
.jvcard
.parsers
.Format
;
15 import be
.nikiroo
.jvcard
.parsers
.Parser
;
18 * A card is a contact information card. It contains data about one or more
24 public class Card
extends BaseClass
<Contact
> {
27 private Format format
;
28 private long lastModified
;
29 private boolean remote
;
32 * Create a new {@link Card} from the given {@link File} and {@link Format}.
35 * the input {@link File} containing the {@link Card} data or
36 * NULL for an empty card (usually a {@link File} name or a
39 * the {@link Format} to use to parse it
43 * @throws InvalidParameterException
46 public Card(File file
, Format format
) throws IOException
{
47 this(load(file
, format
));
51 lastModified
= file
.lastModified();
61 this.name
= file
.getName().replaceAll(".[vV][cC][fF]$", "");
65 this.name
= file
.getName();
72 * Create a new {@link Card} from the given {@link Contact}s.
79 * @throws InvalidParameterException
82 public Card(List
<Contact
> contacts
) throws IOException
{
89 * Save the {@link Card} to the given {@link File} with the given
93 * the output to save to
95 * the {@link Format} to use
97 * @return TRUE if it was saved
100 * in case of IO errors
102 public boolean saveAs(File output
, Format format
) throws IOException
{
106 BufferedWriter writer
= new BufferedWriter(new FileWriter(output
));
107 writer
.append(toString(format
));
110 if (output
.getCanonicalPath().equals(this.file
.getCanonicalPath())) {
118 * Save the {@link Card} to the original {@link File} it was open from.
120 * @return TRUE if it was saved
122 * @throws IOException
123 * in case of IO errors
125 public boolean save() throws IOException
{
126 return saveAs(file
, format
);
130 * Reload the data from the input.
132 * @return TRUE if it was done
134 * @throws IOException
135 * in case of IO error
137 public boolean reload() throws IOException
{
141 this.replaceListContent(load(file
, format
));
147 * Return a {@link String} representation of this {@link Card} in the given
151 * the {@link Format} to use
153 * @return the {@link String}
155 public String
toString(Format format
) {
156 return Parser
.toString(this, format
);
160 * Return the name of this card (the name of the {@link File} which it was
165 public String
getName() {
170 * Return the original {@link Format} of the {@link Card}.
172 * @return the {@link Format}
174 public Format
getFormat() {
179 * Return the {@link File} which was used to open this {@link Card}.
183 public File
getFile() {
188 * Return the date of the last modification for this {@link Card} (or -1 if
191 * @return the last modified date
193 public long getLastModified() {
198 * Check if this {@link Card} is remote.
200 * @return TRUE if this {@link Card} is remote
202 public boolean isRemote() {
207 * Set the remote option on this {@link Card}.
210 * TRUE if this {@link Card} is remote
212 public void setRemote(boolean remote
) {
213 this.remote
= remote
;
217 public String
toString() {
218 return toString(Format
.VCard21
);
222 public String
getId() {
227 public String
getState() {
228 return "" + name
+ format
;
232 * Load the data from the given {@link File} under the given {@link Format}.
235 * the input to load from
237 * the {@link Format} to load as
239 * @return the list of elements
241 * @throws IOException
242 * in case of IO error
244 private static List
<Contact
> load(File file
, Format format
)
246 List
<String
> lines
= null;
248 if (file
!= null && file
.exists()) {
249 BufferedReader buffer
= new BufferedReader(new InputStreamReader(
250 new FileInputStream(file
), "UTF-8"));
251 lines
= new LinkedList
<String
>();
252 for (String line
= buffer
.readLine(); line
!= null; line
= buffer
260 return new LinkedList
<Contact
>();
262 return Parser
.parse(lines
, format
);