X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fparsers%2FParser.java;h=5cdd25fee995dfe10aef8c6d4a5d00dcc18aa5ab;hb=1c03abafc3987d93fa682e7b8758e51bed8a4faf;hp=5ecad4a42abcb9fe1c8c34307b53270aa70e1f0c;hpb=e253bd50bb05519f4a16fed4fb95d5b3340128ea;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/parsers/Parser.java b/src/be/nikiroo/jvcard/parsers/Parser.java index 5ecad4a..5cdd25f 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,6 +15,50 @@ import be.nikiroo.jvcard.Data; public class Parser { + /** + * 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 parse(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 parse(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 parse(List lines, Format format) { switch (format) { case VCard21: