package be.nikiroo.jvcard;
-import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
-import java.io.InputStreamReader;
import java.security.InvalidParameterException;
-import java.util.LinkedList;
import java.util.List;
import be.nikiroo.jvcard.parsers.Format;
private File file;
private String name;
private Format format;
+ private long lastModified;
/**
* Create a new {@link Card} from the given {@link File} and {@link Format}.
*
* @param file
- * the file containing the {@link Card} data, must not be NULL
+ * the input {@link File} containing the {@link Card} data or
+ * NULL for an empty card (usually a {@link File} name or a
+ * network path)
* @param format
* the {@link Format} to use to parse it
*
* @throws IOException
* in case of IO error
- * @throws NullPointerException
- * if file is NULL
* @throws InvalidParameterException
* if format is NULL
*/
public Card(File file, Format format) throws IOException {
- super(load(file, format));
+ this(Parser.parseContact(file, format));
+
+ if (file != null && file.exists()) {
+ lastModified = file.lastModified();
+ }
- this.file = file;
this.format = format;
- this.name = file.getName();
+
+ if (file != null) {
+ this.file = file;
+ switch (format) {
+ case VCard21:
+ this.name = file.getName().replaceAll(".[vV][cC][fF]$", "");
+ break;
+ case Abook:
+ default:
+ this.name = file.getName();
+ break;
+ }
+ }
+ }
+
+ /**
+ * Create a new {@link Card} from the given {@link Contact}s.
+ *
+ * @param contacts
+ * the input contacts
+ *
+ * @throws IOException
+ * in case of IO error
+ * @throws InvalidParameterException
+ * if format is NULL
+ */
+ public Card(List<Contact> contacts) {
+ super(contacts);
+
+ lastModified = -1;
}
/**
* {@link Format}.
*
* @param file
- * the {@link File} to save to
+ * the output to save to
* @param format
* the {@link Format} to use
*
return false;
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
- writer.append(toString(format));
+ Parser.write(writer, format, this);
writer.close();
- if (file.equals(this.file)) {
+ if (this.file != null
+ && file.getCanonicalPath().equals(this.file.getCanonicalPath())) {
+ lastModified = file.lastModified();
setPristine();
}
}
/**
- * Return a {@link String} representation of this {@link Card} in the given
- * {@link Format}.
+ * Reload the data from the input.
*
- * @param format
- * the {@link Format} to use
+ * @return TRUE if it was done
*
- * @return the {@link String}
+ * @throws IOException
+ * in case of IO error
*/
- public String toString(Format format) {
- return Parser.toString(this, format);
+ public boolean reload() throws IOException {
+ if (file == null)
+ return false;
+
+ this.replaceListContent(Parser.parseContact(file, format));
+ lastModified = file.lastModified();
+ setPristine();
+
+ return true;
}
/**
return name;
}
- @Override
- public String toString() {
- return toString(Format.VCard21);
+ /**
+ * Return the original {@link Format} of the {@link Card}.
+ *
+ * @return the {@link Format}
+ */
+ public Format getFormat() {
+ return format;
}
/**
- * Load the data from the given {@link File} under the given {@link Format}.
+ * Return the {@link File} which was used to open this {@link Card}.
*
- * @param file
- * the {@link File} to load from
- * @param format
- * the {@link Format} to load as
+ * @return the input
+ */
+ public File getFile() {
+ return file;
+ }
+
+ /**
+ * Break the link between this {@link Card} and he {@link File} which was
+ * used to open it if any.
+ */
+ public void unlink() {
+ file = null;
+ lastModified = -1;
+ }
+
+ /**
+ * Return the date of the last modification for this {@link Card} (or -1 if
+ * unknown/new).
*
- * @return the list of elements
- * @throws IOException
- * in case of IO error
+ * @return the last modified date
*/
- static private List<Contact> load(File file, Format format)
- throws IOException {
- BufferedReader buffer = new BufferedReader(new InputStreamReader(
- new FileInputStream(file), "UTF-8"));
- List<String> lines = new LinkedList<String>();
- for (String line = buffer.readLine(); line != null; line = buffer
- .readLine()) {
- lines.add(line);
- }
- buffer.close();
+ public long getLastModified() {
+ return lastModified;
+ }
- return Parser.parse(lines, format);
+ @Override
+ public String toString() {
+ return "[Card: " + name + "]";
+ }
+
+ @Override
+ public String getId() {
+ return "" + name;
+ }
+
+ @Override
+ public String getState() {
+ return ("" + name + format).replace(' ', '_');
}
}