com.oopitis.weather
Class URLDataSource<T>

java.lang.Object
  extended by com.oopitis.weather.URLDataSource<T>
Type Parameters:
T - the type of the weather data
Direct Known Subclasses:
FioData, OwmData

public abstract class URLDataSource<T>
extends java.lang.Object

A helper class for retrieving weather data from URL-based providers. The only abstract method in this class to implement is parse(BufferedReader), which parses the data streamed from the URL. Notice that this class does not have public methods because it is not meant to be used with an arbitrary URL. Typically subclasses provide URL construction methods and data access interfaces to the public, and use fetch and setProxy only internally.


Nested Class Summary
static class URLDataSource.FetchException
          Thrown when an error occurs during fetching data from the a data source.
 
Field Summary
protected  java.lang.String licenseKey
          The license key from the constructor.
 
Constructor Summary
protected URLDataSource(java.lang.String licenseKey)
          Constructs a URL data source with a license key.
 
Method Summary
protected  T fetch(java.lang.String urlStr)
          Fetches raw data from a URL, parses it and returns the parsed data.
protected abstract  T parse(java.io.BufferedReader reader)
          Parses the data from a reader and returns the parsed data.
protected  void setProxy(java.net.Proxy p)
          Sets the proxy to be used with fetch(String).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

licenseKey

protected final java.lang.String licenseKey
The license key from the constructor.

Constructor Detail

URLDataSource

protected URLDataSource(java.lang.String licenseKey)
Constructs a URL data source with a license key.

Parameters:
licenseKey - the license key to be used to access the URL; null if not applicable
Method Detail

setProxy

protected void setProxy(java.net.Proxy p)
Sets the proxy to be used with fetch(String).

Parameters:
p - the proxy to be used with fetch(String); null if a direct connection should be used

fetch

protected T fetch(java.lang.String urlStr)
Fetches raw data from a URL, parses it and returns the parsed data. This method opens an input stream from the given URL and passes it to parse(BufferedReader) to read and parse. The proxy, if not null, is used to open the connection.

Parameters:
urlStr - the URL
Returns:
the parsed data
Throws:
URLDataSource.FetchException - if the given URL is malformed or an error occurs during an I/O operation
See Also:
parse(BufferedReader)

parse

protected abstract T parse(java.io.BufferedReader reader)
Parses the data from a reader and returns the parsed data.

Parameters:
reader - the reader that supplies the raw data
Returns:
the parsed data
See Also:
fetch(String)