X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2FCard.java;h=7939263bfc60b3201458810a735c3690df041077;hb=6a77f2ed5996a89e82edd6552942bf23123c205f;hp=986f81d17c0932cba2081d7586b0ce115ad25547;hpb=e253bd50bb05519f4a16fed4fb95d5b3340128ea;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/Card.java b/src/be/nikiroo/jvcard/Card.java index 986f81d..7939263 100644 --- a/src/be/nikiroo/jvcard/Card.java +++ b/src/be/nikiroo/jvcard/Card.java @@ -1,14 +1,10 @@ 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; @@ -44,12 +40,10 @@ public class Card extends BaseClass { * if format is NULL */ public Card(File file, Format format) throws IOException { - this(load(file, format)); + this(Parser.parseContact(file, format)); - if (file != null) { - if (file.exists()) { - lastModified = file.lastModified(); - } + if (file != null && file.exists()) { + lastModified = file.lastModified(); } this.format = format; @@ -89,7 +83,7 @@ public class Card extends BaseClass { * Save the {@link Card} to the given {@link File} with the given * {@link Format}. * - * @param output + * @param file * the output to save to * @param format * the {@link Format} to use @@ -99,15 +93,17 @@ public class Card extends BaseClass { * @throws IOException * in case of IO errors */ - public boolean saveAs(File output, Format format) throws IOException { - if (output == null) + public boolean saveAs(File file, Format format) throws IOException { + if (file == null) return false; - BufferedWriter writer = new BufferedWriter(new FileWriter(output)); + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.append(toString(format)); writer.close(); - if (output.getCanonicalPath().equals(this.file.getCanonicalPath())) { + if (this.file != null + && file.getCanonicalPath().equals(this.file.getCanonicalPath())) { + lastModified = file.lastModified(); setPristine(); } @@ -138,8 +134,10 @@ public class Card extends BaseClass { if (file == null) return false; - this.replaceListContent(load(file, format)); + this.replaceListContent(Parser.parseContact(file, format)); + lastModified = file.lastModified(); setPristine(); + return true; } @@ -153,7 +151,12 @@ public class Card extends BaseClass { * @return the {@link String} */ public String toString(Format format) { - return Parser.toString(this, format); + StringBuilder builder = new StringBuilder(); + for (String line : Parser.toStrings(this, format)) { + builder.append(line); + builder.append("\r\n"); + } + return builder.toString(); } /** @@ -227,38 +230,4 @@ public class Card extends BaseClass { public String getState() { return "" + name + format; } - - /** - * Load the data from the given {@link File} under the given {@link Format}. - * - * @param file - * the input to load from - * @param format - * the {@link Format} to load as - * - * @return the list of elements - * - * @throws IOException - * in case of IO error - */ - private static List load(File file, Format format) - throws IOException { - List lines = null; - - if (file != null && file.exists()) { - BufferedReader buffer = new BufferedReader(new InputStreamReader( - new FileInputStream(file), "UTF-8")); - lines = new LinkedList(); - for (String line = buffer.readLine(); line != null; line = buffer - .readLine()) { - lines.add(line); - } - buffer.close(); - } - - if (lines == null) - return new LinkedList(); - - return Parser.parse(lines, format); - } }