| 1 | package be.nikiroo.jvcard; |
| 2 | |
| 3 | import java.io.BufferedReader; |
| 4 | import java.io.BufferedWriter; |
| 5 | import java.io.File; |
| 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; |
| 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 | */ |
| 24 | public class Card extends BaseClass<Contact> { |
| 25 | private File file; |
| 26 | private String name; |
| 27 | private Format format; |
| 28 | |
| 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 | */ |
| 44 | public Card(File file, Format format) throws IOException { |
| 45 | super(load(file, format)); |
| 46 | |
| 47 | this.file = file; |
| 48 | this.format = format; |
| 49 | this.name = file.getName(); |
| 50 | } |
| 51 | |
| 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 | */ |
| 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)) { |
| 75 | setPristine(); |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 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 | */ |
| 89 | public boolean save() throws IOException { |
| 90 | return saveAs(file, format); |
| 91 | } |
| 92 | |
| 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 | */ |
| 102 | public String toString(Format format) { |
| 103 | return Parser.toString(this, format); |
| 104 | } |
| 105 | |
| 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 |
| 117 | public String toString() { |
| 118 | return toString(Format.VCard21); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Load the data from the given {@link File} under the given {@link Format}. |
| 123 | * |
| 124 | * @param file |
| 125 | * the {@link File} to load from |
| 126 | * @param format |
| 127 | * the {@link Format} to load as |
| 128 | * |
| 129 | * @return the list of elements |
| 130 | * @throws IOException |
| 131 | * in case of IO error |
| 132 | */ |
| 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); |
| 141 | } |
| 142 | buffer.close(); |
| 143 | |
| 144 | return Parser.parse(lines, format); |
| 145 | } |
| 146 | } |