-
Notifications
You must be signed in to change notification settings - Fork 0
/
MealMasterImporter.java
200 lines (182 loc) · 5.23 KB
/
MealMasterImporter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.niceprograms.recipe.data;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Imports recipes in the Meal Master format.
*/
public class MealMasterImporter implements IRecipeImporter {
private static final String INVALID_TEXT = "Invalid Meal Master recipe file.";
private enum ReadStates {
/** Ready state. */
READY,
/** Title state. */
TITLE,
/** Categories state. */
CATEGORIES,
/** Yield state. */
YIELD,
/** Ingredients state. */
INGREDIENTS,
/** Directions state. */
DIRECTIONS
}
private ReadStates state;
/**
* Creates a new MealMasterImporter.
*/
public MealMasterImporter() {
super();
state = ReadStates.READY;
}
/**
* {@inheritDoc}
*/
public void importRecipes(IRecipeBookModel recipeBook, String filePath)
throws RecipeImportException {
String title = null;
String cuisine = null;
String[] categories = null;
String ingredients = null;
String directions = null;
File file = new File(filePath);
if (!file.exists()) {
throw new RecipeImportException("Specified file does not exist: "
+ filePath);
}
try {
FileInputStream fis = new FileInputStream(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(
fis));
String line = null;
while ((line = reader.readLine()) != null) {
switch (state) {
case READY:
if (line.startsWith("MMMMM----- ")
|| line.startsWith("----------")) {
line = reader.readLine();
if (line.trim().equals("")) {
title = null;
cuisine = "Default Cuisine";
categories = null;
ingredients = null;
directions = null;
state = ReadStates.TITLE;
} else {
throw new RecipeImportException(INVALID_TEXT);
}
} else {
throw new RecipeImportException(INVALID_TEXT);
}
break;
case TITLE:
if (line.trim().startsWith("Title:")) {
title = line.trim().replaceFirst("Title:", "").trim();
state = ReadStates.CATEGORIES;
} else {
throw new RecipeImportException(INVALID_TEXT);
}
break;
case CATEGORIES:
if (line.trim().startsWith("Categories:")) {
categories = line.trim()
.replaceFirst("Categories:", "").split(",");
int numCategories = categories.length;
for (int i = 0; i < numCategories; i++) {
categories[i] = categories[i].trim();
}
state = ReadStates.YIELD;
} else {
throw new RecipeImportException(INVALID_TEXT);
}
break;
case YIELD:
if (line.trim().startsWith("Yield:")) {
@SuppressWarnings("unused")
String yield = line.trim().replaceFirst("Yield:", "");
// Yield unused
line = reader.readLine();
if (line.trim().equals("")) {
state = ReadStates.INGREDIENTS;
} else {
throw new RecipeImportException(INVALID_TEXT);
}
} else {
throw new RecipeImportException(INVALID_TEXT);
}
break;
case INGREDIENTS:
if (line.trim().equals("")) {
state = ReadStates.DIRECTIONS;
} else {
String currentIngredients = ingredients;
if (currentIngredients == null) {
currentIngredients = line
.replace("MMMMM------", "")
.replace("-", "").trim();
ingredients = currentIngredients;
} else {
currentIngredients += "\n";
currentIngredients += line.replace("MMMMM------",
"").replace("-", "").trim();
ingredients = currentIngredients;
}
}
break;
case DIRECTIONS:
if (line.startsWith("MMMMM------")
|| line.startsWith("------")) {
String currentIngredients = ingredients;
if (currentIngredients == null) {
currentIngredients = line
.replace("MMMMM------", "")
.replace("-", "").trim();
ingredients = currentIngredients;
} else {
currentIngredients += "\n\n";
currentIngredients += line.replace("MMMMM------",
"").replace("-", "").trim();
ingredients = currentIngredients;
}
state = ReadStates.INGREDIENTS;
} else if (line.trim().equals("MMMMM")
|| line.trim().equals("-----")) {
if (recipeBook instanceof RecipeBookModel) {
((RecipeBookModel) recipeBook).createRecipeSilent(
title, cuisine, categories, ingredients,
directions, null);
} else {
recipeBook.createRecipe(title, cuisine, categories,
ingredients, directions, null);
}
line = reader.readLine();
state = ReadStates.READY;
} else if (line.trim().startsWith("From: ")) {
// do no include from line
} else {
String currentDirections = directions;
if (currentDirections == null) {
currentDirections = line.trim();
directions = currentDirections;
} else {
currentDirections += "\n";
currentDirections += line.trim();
directions = currentDirections;
}
}
break;
default:
throw new RecipeImportException("Invalid state: " + state);
}
}
recipeBook.save();
} catch (FileNotFoundException e) {
throw new RecipeImportException(e);
} catch (IOException e) {
throw new RecipeImportException(e);
}
}
}