Coverage report

  %line %branch
ca.spaz.cron.datasource.www.NutritionDataFood
0% 
0% 

 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  0
    private static final Logger logger = Logger
 37  0
          .getLogger(NutritionDataFood.class);
 38  
    
 39  0
    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  0
       super();
 48  0
       this.source = source;
 49  
       try {
 50  0
          loadWebData(response);
 51  0
       } catch (IOException e) {
 52  0
          throw new FoodDatasourceException("Error reading web data for food", e);
 53  0
       } catch (BadLocationException e) {
 54  0
          throw new FoodDatasourceException("Error reading web data for food", e);
 55  0
       } catch (SAXException e) {
 56  0
          throw new FoodDatasourceException("Error reading web data for food", e);
 57  0
       }
 58  0
    }
 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  0
       WebTable mainTable = getTable(response);
 73  0
       if (null == mainTable) {
 74  0
          throw new FoodDatasourceException("Error in food construction, web table structure has changed.");
 75  
       }
 76  0
       loadName(mainTable);
 77  0
       loadMeasures(mainTable);
 78  
       // Nutrients start at getTables(4)
 79  0
    }
 80  
    
 81  
    private void loadMeasures(WebTable mainTable) {
 82  
       
 83  0
       measures = new ArrayList();
 84  
       
 85  0
       TableCell topcell = mainTable.getTableCell(0,0);
 86  0
       WebTable tbl = topcell.getTables()[0];
 87  0
       TableCell cell = tbl.getTableCell(1,0);
 88  0
       tbl = cell.getTables()[0];
 89  0
       cell = tbl.getTableCell(0,1);
 90  
       // At this point, we should have the whole shebang. Groups 2 and 3 of the expression
 91  0
       String cellText = cell.getText();
 92  0
       Matcher m = MEASURE.matcher(cellText);
 93  0
       int idx = 0;
 94  0
       while (m.find(idx)) {
 95  0
          Measure meas = new Measure();
 96  0
          meas.setAmount(1);
 97  0
          meas.setDescription(m.group(1));
 98  0
          meas.setGrams(Double.parseDouble(m.group(2)));
 99  0
          measures.add(meas);
 100  0
          idx = m.end();
 101  0
       }
 102  0
    }
 103  
 
 104  
    private void loadName(WebTable mainTable) {
 105  0
       TableCell cell = mainTable.getTableCell(0,0); // This is the title table cell
 106  0
       WebTable tbl = cell.getTables()[0]; // This is the table the title is in
 107  0
       TableCell tcell = tbl.getTableCell(0,0); // This is the title cell;
 108  0
       setDescription(tcell.getText());
 109  0
    }
 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  0
       WebTable tbl = null;
 115  
       try {
 116  0
          tbl = response.getTables()[0].getTableCell(1,0).getTables()[0].getTableCell(0,0).getTables()[0];
 117  0
       } catch (SAXException e) {
 118  0
          logger.error("getTable(WebResponse)", e);
 119  0
       } catch (NullPointerException e) {
 120  0
          logger.error("getTable(WebResponse)", e);
 121  0
       } catch (ArrayIndexOutOfBoundsException e) {
 122  0
          logger.error("getTable(WebResponse)", e);
 123  0
       }
 124  0
       return tbl;
 125  
    }
 126  
 
 127  
    /* (non-Javadoc)
 128  
     * @see ca.spaz.cron.database.Food#getFoodGroup()
 129  
     */
 130  
    public FoodGroup getFoodGroup() {
 131  0
       return null;
 132  
    }
 133  
 
 134  
    /* (non-Javadoc)
 135  
     * @see ca.spaz.cron.database.Food#getSource()
 136  
     */
 137  
    public String getSource() {
 138  0
       return null;
 139  
    }
 140  
 
 141  
    /* (non-Javadoc)
 142  
     * @see ca.spaz.cron.database.Food#getMeasures()
 143  
     */
 144  
    public List getMeasures() {
 145  0
       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  0
       this.measures.clear();
 153  0
       this.measures.addAll(measures);
 154  0
    }
 155  
 
 156  
    /* (non-Javadoc)
 157  
     * @see ca.spaz.cron.database.Food#getCalories()
 158  
     */
 159  
    public double getCalories() {
 160  0
       return 0;
 161  
    }
 162  
 
 163  
    /* (non-Javadoc)
 164  
     * @see ca.spaz.cron.database.Food#getMacroNutrients()
 165  
     */
 166  
    public MacroNutrients getMacroNutrients() {
 167  0
       return null;
 168  
    }
 169  
 
 170  
    /* (non-Javadoc)
 171  
     * @see ca.spaz.cron.database.Food#getDescription()
 172  
     */
 173  
    public String getDescription() {
 174  0
       return this.description;
 175  
    }
 176  
 
 177  
    /* (non-Javadoc)
 178  
     * @see ca.spaz.cron.database.Food#getMinerals()
 179  
     */
 180  
    public Minerals getMinerals() {
 181  0
       return null;
 182  
    }
 183  
 
 184  
    /* (non-Javadoc)
 185  
     * @see ca.spaz.cron.database.Food#getVitamins()
 186  
     */
 187  
    public Vitamins getVitamins() {
 188  0
       return null;
 189  
    }
 190  
 
 191  
    /* (non-Javadoc)
 192  
     * @see ca.spaz.cron.database.Food#getAminoAcids()
 193  
     */
 194  
    public AminoAcids getAminoAcids() {
 195  0
       return null;
 196  
    }
 197  
 
 198  
    /* (non-Javadoc)
 199  
     * @see ca.spaz.cron.database.Food#getLipids()
 200  
     */
 201  
    public Lipids getLipids() {
 202  0
       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  0
       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  0
    }
 218  
 
 219  
    /* (non-Javadoc)
 220  
     * @see ca.spaz.cron.database.Food#setDescription(java.lang.String)
 221  
     */
 222  
    public void setDescription(String text) {
 223  0
       this.description = text;
 224  0
    }
 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  0
    }
 232  
 
 233  
    /* (non-Javadoc)
 234  
     * @see ca.spaz.cron.database.Food#getNumTimesConsumed()
 235  
     */
 236  
    public int getNumTimesConsumed() {
 237  0
       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  0
    }
 246  
 
 247  
    /* (non-Javadoc)
 248  
     * @see ca.spaz.cron.database.Food#getDataSource()
 249  
     */
 250  
    public IFoodDatasource getDataSource() {
 251  0
       return source;
 252  0
    }
 253  
 
 254  
    /* (non-Javadoc)
 255  
     * @see java.lang.Object#toString()
 256  
     */
 257  
    public String toString() {
 258  0
       return getDescription();
 259  
    }
 260  
 
 261  
    /* (non-Javadoc)
 262  
     * @see ca.spaz.cron.database.Food#getSourceUID()
 263  
     */
 264  
    public String getSourceUID() {
 265  0
       return null;
 266  
    }
 267  
 
 268  
    public void setModified() {
 269  
       // TODO Auto-generated method stub
 270  
       
 271  0
    }
 272  
 
 273  
    public void setSourceUID(String uid) {
 274  
       // TODO Auto-generated method stub
 275  
       
 276  0
    }
 277  0
 
 278  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.