1 package be
.nikiroo
.jvcard
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.BufferedWriter
;
6 import java
.io
.FileReader
;
7 import java
.io
.FileWriter
;
8 import java
.io
.IOException
;
9 import java
.util
.ArrayList
;
10 import java
.util
.Arrays
;
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
25 private List
<Contact
> contacts
;
27 private boolean dirty
;
29 private Format format
;
31 public Card(File file
, Format format
) throws IOException
{
36 name
= file
.getName();
39 BufferedReader buffer
= new BufferedReader(new FileReader(file
));
40 List
<String
> lines
= new LinkedList
<String
>();
41 for (String line
= buffer
.readLine(); line
!= null; line
= buffer
48 dirty
= false; // initial load, so no change yet, so no need to call
53 * Return the full list of {@link Contact}s. Please use responsibly (this is
54 * the original list, do not modify the list itself).
56 * @return the list of {@link Contact}s
58 public List
<Contact
> getContactsList() {
63 * Return the list of {@link Contact}s. Note that this list is a copy.
65 * @return the list of {@link Contact}s
67 public List
<Contact
> getContacts() {
68 ArrayList
<Contact
> list
= new ArrayList
<Contact
>(size());
69 list
.addAll(contacts
);
74 return contacts
.size();
77 public Contact
get(int index
) {
78 return contacts
.get(index
);
81 public boolean saveAs(File file
, Format format
) throws IOException
{
85 BufferedWriter writer
= new BufferedWriter(new FileWriter(file
));
86 writer
.append(toString(format
));
89 if (file
.equals(this.file
)) {
96 public boolean save() throws IOException
{
97 return saveAs(file
, format
);
100 public String
toString(Format format
) {
101 return Parser
.toString(this, format
);
104 public String
toString() {
105 return toString(Format
.VCard21
);
108 protected void load(String serializedContent
, Format format
) {
109 // note: fixed size array
110 List
<String
> lines
= Arrays
.asList(serializedContent
.split("\n"));
114 protected void load(List
<String
> lines
, Format format
) {
115 this.contacts
= Parser
.parse(lines
, format
);
118 for (Contact contact
: contacts
) {
119 contact
.setParent(this);
123 public boolean isDirty() {
128 * Return the name of this card.
132 public String
getName() {
137 * Notify that this element has unsaved changes.
144 * Notify this element <i>and all its descendants</i> that it is in pristine
145 * state (as opposed to dirty).
149 for (Contact contact
: contacts
) {
150 contact
.setPristine();