f3e07733528f50d4ce121b67eecdba8243205ac4
1 package be
.nikiroo
.jvcard
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.BufferedWriter
;
6 import java
.io
.FileInputStream
;
7 import java
.io
.FileReader
;
8 import java
.io
.FileWriter
;
9 import java
.io
.IOException
;
10 import java
.io
.InputStreamReader
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.LinkedList
;
14 import java
.util
.List
;
16 import be
.nikiroo
.jvcard
.parsers
.Format
;
17 import be
.nikiroo
.jvcard
.parsers
.Parser
;
20 * A card is a contact information card. It contains data about one or more
27 private List
<Contact
> contacts
;
29 private boolean dirty
;
31 private Format format
;
33 public Card(File file
, Format format
) throws IOException
{
38 name
= file
.getName();
41 BufferedReader buffer
= new BufferedReader(new InputStreamReader(
42 new FileInputStream(file
), "UTF-8"));
43 List
<String
> lines
= new LinkedList
<String
>();
44 for (String line
= buffer
.readLine(); line
!= null; line
= buffer
51 dirty
= false; // initial load, so no change yet, so no need to call
56 * Return the full list of {@link Contact}s. Please use responsibly (this is
57 * the original list, do not modify the list itself).
59 * @return the list of {@link Contact}s
61 public List
<Contact
> getContactsList() {
66 * Return the list of {@link Contact}s. Note that this list is a copy.
68 * @return the list of {@link Contact}s
70 public List
<Contact
> getContacts() {
71 ArrayList
<Contact
> list
= new ArrayList
<Contact
>(size());
72 list
.addAll(contacts
);
77 return contacts
.size();
80 public Contact
get(int index
) {
81 return contacts
.get(index
);
84 public boolean saveAs(File file
, Format format
) throws IOException
{
88 BufferedWriter writer
= new BufferedWriter(new FileWriter(file
));
89 writer
.append(toString(format
));
92 if (file
.equals(this.file
)) {
99 public boolean save() throws IOException
{
100 return saveAs(file
, format
);
103 public String
toString(Format format
) {
104 return Parser
.toString(this, format
);
107 public String
toString() {
108 return toString(Format
.VCard21
);
111 protected void load(String serializedContent
, Format format
) {
112 // note: fixed size array
113 List
<String
> lines
= Arrays
.asList(serializedContent
.split("\n"));
117 protected void load(List
<String
> lines
, Format format
) {
118 this.contacts
= Parser
.parse(lines
, format
);
121 for (Contact contact
: contacts
) {
122 contact
.setParent(this);
126 public boolean isDirty() {
131 * Return the name of this card.
135 public String
getName() {
140 * Notify that this element has unsaved changes.
147 * Notify this element <i>and all its descendants</i> that it is in pristine
148 * state (as opposed to dirty).
152 for (Contact contact
: contacts
) {
153 contact
.setPristine();