Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard; |
2 | ||
3 | import java.io.BufferedReader; | |
4 | import java.io.BufferedWriter; | |
5 | import java.io.File; | |
296a0b75 | 6 | import java.io.FileInputStream; |
a3b510ab NR |
7 | import java.io.FileWriter; |
8 | import java.io.IOException; | |
296a0b75 | 9 | import java.io.InputStreamReader; |
78e4af97 | 10 | import java.security.InvalidParameterException; |
a3b510ab NR |
11 | import java.util.LinkedList; |
12 | import java.util.List; | |
13 | ||
14 | import be.nikiroo.jvcard.parsers.Format; | |
15 | import be.nikiroo.jvcard.parsers.Parser; | |
16 | ||
17 | /** | |
18 | * A card is a contact information card. It contains data about one or more | |
19 | * contacts. | |
20 | * | |
21 | * @author niki | |
22 | * | |
23 | */ | |
26d2bd05 | 24 | public class Card extends BaseClass<Contact> { |
a3b510ab | 25 | private File file; |
0b0b2b0f | 26 | private String name; |
bcb54330 | 27 | private Format format; |
a3b510ab | 28 | |
78e4af97 NR |
29 | /** |
30 | * Create a new {@link Card} from the given {@link File} and {@link Format}. | |
31 | * | |
32 | * @param file | |
33 | * the file containing the {@link Card} data, must not be NULL | |
34 | * @param format | |
35 | * the {@link Format} to use to parse it | |
36 | * | |
37 | * @throws IOException | |
38 | * in case of IO error | |
39 | * @throws NullPointerException | |
40 | * if file is NULL | |
41 | * @throws InvalidParameterException | |
42 | * if format is NULL | |
43 | */ | |
a3b510ab | 44 | public Card(File file, Format format) throws IOException { |
26d2bd05 NR |
45 | super(load(file, format)); |
46 | ||
a3b510ab | 47 | this.file = file; |
bcb54330 | 48 | this.format = format; |
78e4af97 | 49 | this.name = file.getName(); |
176a8327 | 50 | } |
bcb54330 | 51 | |
78e4af97 NR |
52 | /** |
53 | * Save the {@link Card} to the given {@link File} with the given | |
54 | * {@link Format}. | |
55 | * | |
56 | * @param file | |
57 | * the {@link File} to save to | |
58 | * @param format | |
59 | * the {@link Format} to use | |
60 | * | |
61 | * @return TRUE if it was saved | |
62 | * | |
63 | * @throws IOException | |
64 | * in case of IO errors | |
65 | */ | |
a3b510ab NR |
66 | public boolean saveAs(File file, Format format) throws IOException { |
67 | if (file == null) | |
68 | return false; | |
69 | ||
70 | BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | |
71 | writer.append(toString(format)); | |
72 | writer.close(); | |
73 | ||
74 | if (file.equals(this.file)) { | |
bcb54330 | 75 | setPristine(); |
a3b510ab NR |
76 | } |
77 | ||
78 | return true; | |
79 | } | |
80 | ||
78e4af97 NR |
81 | /** |
82 | * Save the {@link Card} to the original {@link File} it was open from. | |
83 | * | |
84 | * @return TRUE if it was saved | |
85 | * | |
86 | * @throws IOException | |
87 | * in case of IO errors | |
88 | */ | |
bcb54330 | 89 | public boolean save() throws IOException { |
a3b510ab NR |
90 | return saveAs(file, format); |
91 | } | |
92 | ||
78e4af97 NR |
93 | /** |
94 | * Return a {@link String} representation of this {@link Card} in the given | |
95 | * {@link Format}. | |
96 | * | |
97 | * @param format | |
98 | * the {@link Format} to use | |
99 | * | |
100 | * @return the {@link String} | |
101 | */ | |
a3b510ab NR |
102 | public String toString(Format format) { |
103 | return Parser.toString(this, format); | |
104 | } | |
105 | ||
78e4af97 NR |
106 | /** |
107 | * Return the name of this card (the name of the {@link File} which it was | |
108 | * opened from). | |
109 | * | |
110 | * @return the name | |
111 | */ | |
112 | public String getName() { | |
113 | return name; | |
114 | } | |
115 | ||
116 | @Override | |
a3b510ab NR |
117 | public String toString() { |
118 | return toString(Format.VCard21); | |
119 | } | |
120 | ||
78e4af97 | 121 | /** |
26d2bd05 | 122 | * Load the data from the given {@link File} under the given {@link Format}. |
78e4af97 | 123 | * |
26d2bd05 NR |
124 | * @param file |
125 | * the {@link File} to load from | |
78e4af97 | 126 | * @param format |
26d2bd05 | 127 | * the {@link Format} to load as |
78e4af97 | 128 | * |
26d2bd05 NR |
129 | * @return the list of elements |
130 | * @throws IOException | |
131 | * in case of IO error | |
78e4af97 | 132 | */ |
26d2bd05 NR |
133 | static private List<Contact> load(File file, Format format) |
134 | throws IOException { | |
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 | |
139 | .readLine()) { | |
140 | lines.add(line); | |
a3b510ab | 141 | } |
26d2bd05 | 142 | buffer.close(); |
bcb54330 | 143 | |
26d2bd05 | 144 | return Parser.parse(lines, format); |
bcb54330 | 145 | } |
a3b510ab | 146 | } |