Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractParser |
|
| 3.25;3.25 |
1 | package org.codeforamerica.open311.internals.parsing; | |
2 | ||
3 | import java.net.URL; | |
4 | ||
5 | import org.codeforamerica.open311.facade.exceptions.DataParsingException; | |
6 | ||
7 | /** | |
8 | * Contains common methods of the different {@link DataParser} implementations. | |
9 | * | |
10 | * @author Santiago MunĂn <santimunin@gmail.com> | |
11 | * | |
12 | */ | |
13 | 24 | public abstract class AbstractParser implements DataParser { |
14 | ||
15 | /** | |
16 | * Parses a comma separated list of keywords. | |
17 | * | |
18 | * @param rawKeywords | |
19 | * A comma separated list of keywords. | |
20 | * @return An array of strings (keywords). | |
21 | */ | |
22 | protected String[] getKeywords(String rawKeywords) { | |
23 | 10 | String[] result = rawKeywords.split(KEYWORDS_SEPARATOR); |
24 | 40 | for (int i = 0; i < result.length; i++) { |
25 | 30 | result[i] = result[i].trim(); |
26 | } | |
27 | 10 | return result; |
28 | } | |
29 | ||
30 | /** | |
31 | * Tries to build a URL. | |
32 | * | |
33 | * @param url | |
34 | * Raw url. | |
35 | * @return <code>null</code> if it was any problem. | |
36 | */ | |
37 | protected URL buildUrl(String url) { | |
38 | try { | |
39 | 12 | return new URL(url); |
40 | 0 | } catch (Exception e) { |
41 | 0 | return null; |
42 | } | |
43 | } | |
44 | ||
45 | /** | |
46 | * Check if all the given strings are empty. | |
47 | * | |
48 | * @param strings | |
49 | * List of strings. | |
50 | * @return <code>true</code> is all are empty. | |
51 | */ | |
52 | protected boolean allStringsAreEmpty(String... strings) { | |
53 | 9 | for (int i = 0; i < strings.length; i++) { |
54 | 9 | if (strings[i] != null && strings[i].length() > 0) { |
55 | 9 | return false; |
56 | } | |
57 | } | |
58 | 0 | return true; |
59 | } | |
60 | ||
61 | /** | |
62 | * Checks if any parameter is valid. | |
63 | * | |
64 | * @param strings | |
65 | * List of parameters. | |
66 | * @throws DataParsingException | |
67 | * If all parameters are missing. | |
68 | */ | |
69 | protected void checkParameters(String... strings) | |
70 | throws DataParsingException { | |
71 | 9 | if (allStringsAreEmpty(strings)) { |
72 | 0 | throw new DataParsingException( |
73 | "Invalid data, required fields wasn't received."); | |
74 | } | |
75 | 9 | } |
76 | } |