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
.Arrays
;
12 import java
.util
.LinkedList
;
13 import java
.util
.List
;
15 import be
.nikiroo
.jvcard
.parsers
.Format
;
16 import be
.nikiroo
.jvcard
.parsers
.Parser
;
19 * A card is a contact information card. It contains data about one or more
26 private List
<Contact
> contacts
;
28 private boolean dirty
;
30 private Format format
;
33 * Create a new {@link Card} from the given {@link File} and {@link Format}.
36 * the file containing the {@link Card} data, must not be NULL
38 * the {@link Format} to use to parse it
42 * @throws NullPointerException
44 * @throws InvalidParameterException
47 public Card(File file
, Format format
) throws IOException
{
50 this.name
= file
.getName();
52 BufferedReader buffer
= new BufferedReader(new InputStreamReader(
53 new FileInputStream(file
), "UTF-8"));
54 List
<String
> lines
= new LinkedList
<String
>();
55 for (String line
= buffer
.readLine(); line
!= null; line
= buffer
62 dirty
= false; // initial load, so no change yet, so no need to call
67 * Return the number of {@link Contact} present in this {@link Card}.
69 * @return the number of {@link Contact}s
72 return contacts
.size();
76 * Return the {@link Contact} at index <i>index</i>.
79 * the index of the {@link Contact} to find
81 * @return the {@link Contact}
83 * @throws IndexOutOfBoundsException
84 * if the index is < 0 or >= {@link Card#size()}
86 public Contact
get(int index
) {
87 return contacts
.get(index
);
91 * Save the {@link Card} to the given {@link File} with the given
95 * the {@link File} to save to
97 * the {@link Format} to use
99 * @return TRUE if it was saved
101 * @throws IOException
102 * in case of IO errors
104 public boolean saveAs(File file
, Format format
) throws IOException
{
108 BufferedWriter writer
= new BufferedWriter(new FileWriter(file
));
109 writer
.append(toString(format
));
112 if (file
.equals(this.file
)) {
120 * Save the {@link Card} to the original {@link File} it was open from.
122 * @return TRUE if it was saved
124 * @throws IOException
125 * in case of IO errors
127 public boolean save() throws IOException
{
128 return saveAs(file
, format
);
132 * Return a {@link String} representation of this {@link Card} in the given
136 * the {@link Format} to use
138 * @return the {@link String}
140 public String
toString(Format format
) {
141 return Parser
.toString(this, format
);
145 * Check if this {@link Card} has unsaved changes.
147 * @return TRUE if it has
149 public boolean isDirty() {
154 * Return the name of this card (the name of the {@link File} which it was
159 public String
getName() {
164 public String
toString() {
165 return toString(Format
.VCard21
);
169 * Load the given data from the given {@link Format} in this {@link Card}.
171 * @param serializedContent
176 protected void load(String serializedContent
, Format format
) {
177 // note: fixed size array
178 List
<String
> lines
= Arrays
.asList(serializedContent
.split("\n"));
183 * Load the given data from the given {@link Format} in this {@link Card}.
190 protected void load(List
<String
> lines
, Format format
) {
191 this.contacts
= Parser
.parse(lines
, format
);
194 for (Contact contact
: contacts
) {
195 contact
.setParent(this);
200 * Return the full list of {@link Contact}s. Please use responsibly (this is
201 * the original list).
203 * @return the list of {@link Contact}s
205 List
<Contact
> getContactsList() {
210 * Notify that this element has unsaved changes.
217 * Notify this element <i>and all its descendants</i> that it is in pristine
218 * state (as opposed to dirty).
222 for (Contact contact
: contacts
) {
223 contact
.setPristine();