View Javadoc

1   /*
2    *******************************************************************************
3    * Copyright (c) 2005 Chris Rose and AIMedia
4    * All rights reserved. NutritionDataFood and the accompanying materials
5    * are made available under the terms of the Common Public License v1.0
6    * which accompanies this distribution, and is available at
7    * http://www.eclipse.org/legal/cpl-v10.html
8    * 
9    * Contributors:
10   *     Chris Rose
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        // Nutrients start at getTables(4)
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        // At this point, we should have the whole shebang. Groups 2 and 3 of the expression
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); // This is the title table cell
106       WebTable tbl = cell.getTables()[0]; // This is the table the title is in
107       TableCell tcell = tbl.getTableCell(0,0); // This is the title cell;
108       setDescription(tcell.getText());
109    }
110 
111    private WebTable getTable(WebResponse response) {
112       // This is quite fragile.  Even a small change to the page layout will BORK this.
113       // This is quite fragile.  Even a small change to the page layout will BORK this.
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    /* (non-Javadoc)
128     * @see ca.spaz.cron.database.Food#getFoodGroup()
129     */
130    public FoodGroup getFoodGroup() {
131       return null;
132    }
133 
134    /* (non-Javadoc)
135     * @see ca.spaz.cron.database.Food#getSource()
136     */
137    public String getSource() {
138       return null;
139    }
140 
141    /* (non-Javadoc)
142     * @see ca.spaz.cron.database.Food#getMeasures()
143     */
144    public List getMeasures() {
145       return Collections.unmodifiableList(measures);
146    }
147 
148    /* (non-Javadoc)
149     * @see ca.spaz.cron.database.Food#setMeasures(java.util.List)
150     */
151    public void setMeasures(List measures) {
152       this.measures.clear();
153       this.measures.addAll(measures);
154    }
155 
156    /* (non-Javadoc)
157     * @see ca.spaz.cron.database.Food#getCalories()
158     */
159    public double getCalories() {
160       return 0;
161    }
162 
163    /* (non-Javadoc)
164     * @see ca.spaz.cron.database.Food#getMacroNutrients()
165     */
166    public MacroNutrients getMacroNutrients() {
167       return null;
168    }
169 
170    /* (non-Javadoc)
171     * @see ca.spaz.cron.database.Food#getDescription()
172     */
173    public String getDescription() {
174       return this.description;
175    }
176 
177    /* (non-Javadoc)
178     * @see ca.spaz.cron.database.Food#getMinerals()
179     */
180    public Minerals getMinerals() {
181       return null;
182    }
183 
184    /* (non-Javadoc)
185     * @see ca.spaz.cron.database.Food#getVitamins()
186     */
187    public Vitamins getVitamins() {
188       return null;
189    }
190 
191    /* (non-Javadoc)
192     * @see ca.spaz.cron.database.Food#getAminoAcids()
193     */
194    public AminoAcids getAminoAcids() {
195       return null;
196    }
197 
198    /* (non-Javadoc)
199     * @see ca.spaz.cron.database.Food#getLipids()
200     */
201    public Lipids getLipids() {
202       return null;
203    }
204 
205    /* (non-Javadoc)
206     * @see ca.spaz.cron.database.Food#getNutrientAmount(ca.spaz.cron.database.NutrientInfo)
207     */
208    public double getNutrientAmount(NutrientInfo ni) {
209       return 0;
210    }
211 
212    /* (non-Javadoc)
213     * @see ca.spaz.cron.database.Food#setNutrientAmount(ca.spaz.cron.database.NutrientInfo, double)
214     */
215    public void setNutrientAmount(NutrientInfo ni, double val) {
216 
217    }
218 
219    /* (non-Javadoc)
220     * @see ca.spaz.cron.database.Food#setDescription(java.lang.String)
221     */
222    public void setDescription(String text) {
223       this.description = text;
224    }
225 
226    /* (non-Javadoc)
227     * @see ca.spaz.cron.database.Food#setFoodGroup(ca.spaz.cron.database.FoodGroup)
228     */
229    public void setFoodGroup(FoodGroup foodGroup) {
230 
231    }
232 
233    /* (non-Javadoc)
234     * @see ca.spaz.cron.database.Food#getNumTimesConsumed()
235     */
236    public int getNumTimesConsumed() {
237       return 0;
238    }
239 
240    /* (non-Javadoc)
241     * @see ca.spaz.cron.database.Food#setSource(java.lang.String)
242     */
243    public void setSource(String string) {
244 
245    }
246 
247    /* (non-Javadoc)
248     * @see ca.spaz.cron.database.Food#getDataSource()
249     */
250    public IFoodDatasource getDataSource() {
251       return source;
252    }
253 
254    /* (non-Javadoc)
255     * @see java.lang.Object#toString()
256     */
257    public String toString() {
258       return getDescription();
259    }
260 
261    /* (non-Javadoc)
262     * @see ca.spaz.cron.database.Food#getSourceUID()
263     */
264    public String getSourceUID() {
265       return null;
266    }
267 
268    public void setModified() {
269       // TODO Auto-generated method stub
270       
271    }
272 
273    public void setSourceUID(String uid) {
274       // TODO Auto-generated method stub
275       
276    }
277 
278 }