1
2
3
4
5
6
7
8
9
10
11
12 package ca.spaz.cron.datasource.www;
13
14 import java.io.IOException;
15 import java.util.*;
16 import java.util.regex.*;
17
18 import javax.swing.text.BadLocationException;
19
20 import org.apache.log4j.Logger;
21 import org.xml.sax.SAXException;
22
23 import ca.spaz.cron.database.*;
24 import ca.spaz.cron.datasource.*;
25
26 import com.meterware.httpunit.*;
27
28 /***
29 * A Food implementation for NutritionDataDatasource
30 * @author Chris Rose
31 */
32 public class NutritionDataFood implements Food {
33 /***
34 * Logger for this class
35 */
36 private static final Logger logger = Logger
37 .getLogger(NutritionDataFood.class);
38
39 private static final Pattern MEASURE = Pattern.compile("(.+?)//(([^)]+)g//)");
40 private NutritionDataDatasource source;
41
42 private String description;
43
44 private List measures;
45
46 public NutritionDataFood(NutritionDataDatasource source, WebResponse response) {
47 super();
48 this.source = source;
49 try {
50 loadWebData(response);
51 } catch (IOException e) {
52 throw new FoodDatasourceException("Error reading web data for food", e);
53 } catch (BadLocationException e) {
54 throw new FoodDatasourceException("Error reading web data for food", e);
55 } catch (SAXException e) {
56 throw new FoodDatasourceException("Error reading web data for food", e);
57 }
58 }
59
60 /***
61 * @todo Load measures
62 * @todo Load food group
63 * @todo load nutrients
64 * @todo set baseline values.
65 * @todo Parse the table layout of this page in order to get the info we need.
66 * @param response
67 * @throws IOException
68 * @throws BadLocationException
69 * @throws SAXException
70 */
71 private void loadWebData(WebResponse response) throws IOException, BadLocationException, SAXException {
72 WebTable mainTable = getTable(response);
73 if (null == mainTable) {
74 throw new FoodDatasourceException("Error in food construction, web table structure has changed.");
75 }
76 loadName(mainTable);
77 loadMeasures(mainTable);
78
79 }
80
81 private void loadMeasures(WebTable mainTable) {
82
83 measures = new ArrayList();
84
85 TableCell topcell = mainTable.getTableCell(0,0);
86 WebTable tbl = topcell.getTables()[0];
87 TableCell cell = tbl.getTableCell(1,0);
88 tbl = cell.getTables()[0];
89 cell = tbl.getTableCell(0,1);
90
91 String cellText = cell.getText();
92 Matcher m = MEASURE.matcher(cellText);
93 int idx = 0;
94 while (m.find(idx)) {
95 Measure meas = new Measure();
96 meas.setAmount(1);
97 meas.setDescription(m.group(1));
98 meas.setGrams(Double.parseDouble(m.group(2)));
99 measures.add(meas);
100 idx = m.end();
101 }
102 }
103
104 private void loadName(WebTable mainTable) {
105 TableCell cell = mainTable.getTableCell(0,0);
106 WebTable tbl = cell.getTables()[0];
107 TableCell tcell = tbl.getTableCell(0,0);
108 setDescription(tcell.getText());
109 }
110
111 private WebTable getTable(WebResponse response) {
112
113
114 WebTable tbl = null;
115 try {
116 tbl = response.getTables()[0].getTableCell(1,0).getTables()[0].getTableCell(0,0).getTables()[0];
117 } catch (SAXException e) {
118 logger.error("getTable(WebResponse)", e);
119 } catch (NullPointerException e) {
120 logger.error("getTable(WebResponse)", e);
121 } catch (ArrayIndexOutOfBoundsException e) {
122 logger.error("getTable(WebResponse)", e);
123 }
124 return tbl;
125 }
126
127
128
129
130 public FoodGroup getFoodGroup() {
131 return null;
132 }
133
134
135
136
137 public String getSource() {
138 return null;
139 }
140
141
142
143
144 public List getMeasures() {
145 return Collections.unmodifiableList(measures);
146 }
147
148
149
150
151 public void setMeasures(List measures) {
152 this.measures.clear();
153 this.measures.addAll(measures);
154 }
155
156
157
158
159 public double getCalories() {
160 return 0;
161 }
162
163
164
165
166 public MacroNutrients getMacroNutrients() {
167 return null;
168 }
169
170
171
172
173 public String getDescription() {
174 return this.description;
175 }
176
177
178
179
180 public Minerals getMinerals() {
181 return null;
182 }
183
184
185
186
187 public Vitamins getVitamins() {
188 return null;
189 }
190
191
192
193
194 public AminoAcids getAminoAcids() {
195 return null;
196 }
197
198
199
200
201 public Lipids getLipids() {
202 return null;
203 }
204
205
206
207
208 public double getNutrientAmount(NutrientInfo ni) {
209 return 0;
210 }
211
212
213
214
215 public void setNutrientAmount(NutrientInfo ni, double val) {
216
217 }
218
219
220
221
222 public void setDescription(String text) {
223 this.description = text;
224 }
225
226
227
228
229 public void setFoodGroup(FoodGroup foodGroup) {
230
231 }
232
233
234
235
236 public int getNumTimesConsumed() {
237 return 0;
238 }
239
240
241
242
243 public void setSource(String string) {
244
245 }
246
247
248
249
250 public IFoodDatasource getDataSource() {
251 return source;
252 }
253
254
255
256
257 public String toString() {
258 return getDescription();
259 }
260
261
262
263
264 public String getSourceUID() {
265 return null;
266 }
267
268 public void setModified() {
269
270
271 }
272
273 public void setSourceUID(String uid) {
274
275
276 }
277
278 }