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
;
30 * Create a new {@link Card} from the given {@link File} and {@link Format}.
33 * the file containing the {@link Card} data, must not be NULL
35 * the {@link Format} to use to parse it
39 * @throws NullPointerException
41 * @throws InvalidParameterException
44 public Card(File file
, Format format
) throws IOException
{
45 super(load(file
, format
));
49 this.name
= file
.getName();
53 * Save the {@link Card} to the given {@link File} with the given
57 * the {@link File} to save to
59 * the {@link Format} to use
61 * @return TRUE if it was saved
64 * in case of IO errors
66 public boolean saveAs(File file
, Format format
) throws IOException
{
70 BufferedWriter writer
= new BufferedWriter(new FileWriter(file
));
71 writer
.append(toString(format
));
74 if (file
.equals(this.file
)) {
82 * Save the {@link Card} to the original {@link File} it was open from.
84 * @return TRUE if it was saved
87 * in case of IO errors
89 public boolean save() throws IOException
{
90 return saveAs(file
, format
);
94 * Return a {@link String} representation of this {@link Card} in the given
98 * the {@link Format} to use
100 * @return the {@link String}
102 public String
toString(Format format
) {
103 return Parser
.toString(this, format
);
107 * Return the name of this card (the name of the {@link File} which it was
112 public String
getName() {
117 public String
toString() {
118 return toString(Format
.VCard21
);
122 * Load the data from the given {@link File} under the given {@link Format}.
125 * the {@link File} to load from
127 * the {@link Format} to load as
129 * @return the list of elements
130 * @throws IOException
131 * in case of IO error
133 static private List
<Contact
> load(File file
, Format format
)
135 BufferedReader buffer
= new BufferedReader(new InputStreamReader(
136 new FileInputStream(file
), "UTF-8"));
137 List
<String
> lines
= new LinkedList
<String
>();
138 for (String line
= buffer
.readLine(); line
!= null; line
= buffer
144 return Parser
.parse(lines
, format
);