ca.spaz.cron.datasource.www
Class AbstractWWWDatasource

java.lang.Object
  extended by ca.spaz.cron.datasource.AbstractFoodDataSource
      extended by ca.spaz.cron.datasource.www.AbstractWWWDatasource
All Implemented Interfaces:
IFoodDatasource
Direct Known Subclasses:
NutritionDataDatasource

public abstract class AbstractWWWDatasource
extends AbstractFoodDataSource

The base class of all WWW food datasources. Provides convenience methods for some general functions, as well as some assertions about capabilities.

Author:
Chris Rose

Constructor Summary
protected AbstractWWWDatasource(java.lang.String name)
           
 
Method Summary
 void close()
          Close this connection.
protected abstract  void doClose()
           
protected abstract  java.util.List doFindAllFoods()
          Retrieve a list of all foods in this datasource.
protected  java.util.List doFindFoods(java.lang.String[] keys)
          Retrieve a List of all foods in this particular Datasource.
protected  java.util.List doFindFoods(java.lang.String[] keys, java.lang.String foodGroup, java.lang.String source)
          Retrieve a List of all foods in this particular Datasource having the given food group and source.
protected abstract  java.util.List doGetFoodGroups()
          Retrieve a list of food groups for this data source.
protected  java.util.List doGetMeasuresFor(Food food)
          Return a List of Measure objects for the provided food.
protected  void doGetNutrientsFor(Food food, NutrientTable nutrients)
          Fill the provided NutrientTable with the nutrients associated with the Food.
protected abstract  java.util.List doGetSources()
          Retrieve a list of Sources available from this data source.
 java.util.List getFoodGroups()
          Get a list of all food groups in this datasource.
protected  java.util.List getFoodGroups(java.net.URL sourceUrl, java.util.regex.Pattern startGroups, java.util.regex.Pattern endGroups, java.util.regex.Pattern groupID, int keyGroup, int nameGroup)
          Utility method to retrieve food groups from a URL's data, using regular expressions to mark the beginning and end of the list, as well as a regex to get the food group itself.
 java.util.List getSources()
          Get a list of all sources in this datasource.
 void initialize()
          Initialize the datasource to a working state.
 boolean isListable()
          Implementors that can provide listing services should return true here, false otherwise.
 boolean isSearchable()
          Implementors that can provide search services should return true here, false otherwise.
protected static java.lang.String readURLAsString(java.net.URL source)
           
 
Methods inherited from class ca.spaz.cron.datasource.AbstractFoodDataSource
diffSource, findAllFoods, findFoods, findFoods, getLastError, getMeasuresFor, getName, getNutrientsFor, notNull, registerError, sameSource, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface ca.spaz.cron.datasource.IFoodDatasource
containsFood, isAvailable
 

Constructor Detail

AbstractWWWDatasource

protected AbstractWWWDatasource(java.lang.String name)
Parameters:
name - The unique ID of this data source.
Method Detail

doFindAllFoods

protected abstract java.util.List doFindAllFoods()
Description copied from class: AbstractFoodDataSource
Retrieve a list of all foods in this datasource.

Specified by:
doFindAllFoods in class AbstractFoodDataSource
Returns:
a List of Food objects consisting of every food in the datasource.

getSources

public final java.util.List getSources()
Description copied from interface: IFoodDatasource
Get a list of all sources in this datasource. What these are is subimplementation- dependent, and will most often be only one item.

Returns:
a List of String objects naming the sources for this Datasource.

doGetSources

protected abstract java.util.List doGetSources()
Retrieve a list of Sources available from this data source.

Returns:
a List of sources for this datasource.

getFoodGroups

public final java.util.List getFoodGroups()
Description copied from interface: IFoodDatasource
Get a list of all food groups in this datasource.

Returns:
a List of FoodGroup objects naming the food groups for this Datasource.

doGetFoodGroups

protected abstract java.util.List doGetFoodGroups()
Retrieve a list of food groups for this data source.

Returns:
a List of FoodGroup implementations containing all of the food groups in this data source.

isSearchable

public boolean isSearchable()
Description copied from interface: IFoodDatasource
Implementors that can provide search services should return true here, false otherwise.

Returns:
true if this Datasource supports search, false otherwise.

isListable

public boolean isListable()
Description copied from interface: IFoodDatasource
Implementors that can provide listing services should return true here, false otherwise.

Returns:
true if this Datasource supports listing, false otherwise.

close

public final void close()
Description copied from interface: IFoodDatasource
Close this connection. May or may not be required, but this method should ensure that there is nothing remaining of the connection.


doClose

protected abstract void doClose()

getFoodGroups

protected java.util.List getFoodGroups(java.net.URL sourceUrl,
                                       java.util.regex.Pattern startGroups,
                                       java.util.regex.Pattern endGroups,
                                       java.util.regex.Pattern groupID,
                                       int keyGroup,
                                       int nameGroup)
                                throws java.io.IOException
Utility method to retrieve food groups from a URL's data, using regular expressions to mark the beginning and end of the list, as well as a regex to get the food group itself.

Parameters:
sourceUrl - This is the URL whose data will be read.
startGroups - This pattern should mark the beginning of the list of food groups.
endGroups - This pattern should mark the end of the list of food groups.
groupID - This pattern should mark an individual food group. More specifically, it must provide at least two groupings, one of which will be the group key for searching, and the other of which will be the group name for display.
keyGroup - This is the group number in the groupID pattern that marks the search key for the food group.
nameGroup - This is the group number in the groupID pattern that marks the name of the food group.
Returns:
A List of FoodGroup implementations that contains all food groups available at the URL.
Throws:
java.io.IOException - if there is an error reading from the URL.

readURLAsString

protected static java.lang.String readURLAsString(java.net.URL source)

doFindFoods

protected java.util.List doFindFoods(java.lang.String[] keys,
                                     java.lang.String foodGroup,
                                     java.lang.String source)
Description copied from class: AbstractFoodDataSource
Retrieve a List of all foods in this particular Datasource having the given food group and source.

Specified by:
doFindFoods in class AbstractFoodDataSource
Parameters:
keys - the keys to search on. This searches with an AND relation.
foodGroup - the food group to search for. null to ignore.
source - the source of this food item. null to ignore.
Returns:
a List of Food objects matching the criteria.

doFindFoods

protected java.util.List doFindFoods(java.lang.String[] keys)
Description copied from class: AbstractFoodDataSource
Retrieve a List of all foods in this particular Datasource.

Specified by:
doFindFoods in class AbstractFoodDataSource
Parameters:
keys - the keys to search on. This searches with an AND relation.
Returns:
a List of Food objects matching the criteria.

doGetMeasuresFor

protected java.util.List doGetMeasuresFor(Food food)
Description copied from class: AbstractFoodDataSource
Return a List of Measure objects for the provided food.

Specified by:
doGetMeasuresFor in class AbstractFoodDataSource
Parameters:
food - a Food object to find the measures for
Returns:
a List that is guaranteed to contain only Measure objects.

doGetNutrientsFor

protected void doGetNutrientsFor(Food food,
                                 NutrientTable nutrients)
Description copied from class: AbstractFoodDataSource
Fill the provided NutrientTable with the nutrients associated with the Food.

Specified by:
doGetNutrientsFor in class AbstractFoodDataSource
Parameters:
food - The food item to load from
nutrients - the NutrientTable to fill.

initialize

public void initialize()
Description copied from interface: IFoodDatasource
Initialize the datasource to a working state. At the time that this method completes normally (without throwing an exception), isAvailable must return true for this data source. If this source is already available, this method will do nothing.



Copyright © 2005 Spaz. All Rights Reserved.