X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fparsers%2FParser.java;h=040cd3e113ab9862b15ace9c3967bfca4610878b;hb=59597d59aa262e31c2e1b7f66b4cb299f88ebd1b;hp=cec4c1ae9cd8ea404f931b597474fd7eea566701;hpb=a3b510ab4bf89a7a2a05f3851ffe0f030b8a78f4;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/parsers/Parser.java b/src/be/nikiroo/jvcard/parsers/Parser.java index cec4c1a..040cd3e 100644 --- a/src/be/nikiroo/jvcard/parsers/Parser.java +++ b/src/be/nikiroo/jvcard/parsers/Parser.java @@ -1,6 +1,12 @@ package be.nikiroo.jvcard.parsers; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; import java.security.InvalidParameterException; +import java.util.LinkedList; import java.util.List; import be.nikiroo.jvcard.Card; @@ -9,12 +15,56 @@ import be.nikiroo.jvcard.Data; public class Parser { - public static List parse(List lines, Format 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 + */ + public static List parseContact(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 parseContact(lines, format); + } + + /** + * Load the given data from under the given {@link Format}. + * + * @param lines + * the input to load from + * @param format + * the {@link Format} to load as + * + * @return the list of elements + */ + public static List parseContact(List lines, Format format) { switch (format) { case VCard21: - return Vcard21Parser.parse(lines); + return Vcard21Parser.parseContact(lines); case Abook: - return AbookParser.parse(lines); + return AbookParser.parseContact(lines); default: throw new InvalidParameterException("Unknown format: " @@ -22,28 +72,59 @@ public class Parser { } } - // -1 = no bkeys - public static String toString(Card card, Format format) { + /** + * Write the given {@link Card} in the {@link Appendable}. + * + * @param writer + * the {@link Appendable} + * @param card + * the {@link Card} to write + * @param format + * the {@link Format} to export to + * + * @throws IOException + * in case of IO error + */ + public static void write(Appendable writer, Format format, Card card) + throws IOException { switch (format) { case VCard21: - return Vcard21Parser.toString(card); + Vcard21Parser.write(writer, card); + break; case Abook: - return AbookParser.toString(card); - + AbookParser.write(writer, card); + break; default: throw new InvalidParameterException("Unknown format: " + format.toString()); } } - // -1 = no bkeys - public static String toString(Contact contact, Format format, int startingBKey) { + /** + * Write the given {@link Contact} in the {@link Appendable}. + * + * @param writer + * the {@link Appendable} + * @param contact + * the {@link Contact} to write + * @param startingBKey + * the starting BKey number (all the other will follow) or -1 for + * no BKey + * @param format + * the {@link Format} to export to + * + * @throws IOException + * in case of IO error + */ + public static void write(Appendable writer, Contact contact, Format format, + int startingBKey) throws IOException { switch (format) { case VCard21: - return Vcard21Parser.toString(contact, startingBKey); + Vcard21Parser.write(writer, contact, startingBKey); + break; case Abook: - return AbookParser.toString(contact, startingBKey); - + AbookParser.write(writer, contact, startingBKey); + break; default: throw new InvalidParameterException("Unknown format: " + format.toString()); @@ -54,8 +135,8 @@ public class Parser { public static int getBKey(Data data) { if (data.isBinary() && data.getValue().startsWith("", "")); + int bkey = Integer.parseInt(data.getValue() + .replace("", "")); if (bkey < 0) throw new InvalidParameterException( "All bkeys MUST be positive");