home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-01-09 | 6.9 KB | 301 lines |
- ///////////////////////////////////////////////////////////////////////////////////
- //
- // GetWData.java : base-class for weather-data reader classes
- //
- // Author: F. Salazar
- // Created: June, 1997
- //
- // Copyright (c)1992-1997 Lotus Development Corp. All Rights Reserved.
- //
- ///////////////////////////////////////////////////////////////////////////////////
- /*******************************************************************************
- Change History:
-
- $Log: //reebok/xyzL/JavaComp/webpack/samples/weatherchart/GetWData.java
-
- Rev 1.7.1.1 09 Jan 1998 10:52:06 jdonohue
- Change package name to samples.weatherchart
-
- Rev 1.7 11 Dec 1997 15:19:14 jdonohue
- Don't limit temperature values (for winter)
-
- Rev 1.6 13 Oct 1997 11:19:24 fsalazar
- Correct precision on floating pt values.
-
- Rev 1.5 03 Oct 1997 11:19:26 fsalazar
- Correct for no-proxy read.
-
- Rev 1.4 19 Aug 1997 06:54:38 fsalazar
- Correcthumidity-clip code.
-
- Rev 1.3 18 Aug 1997 09:22:10 fsalazar
- Clip retrieved weather values to appropriate ranges.
-
- Rev 1.2 05 Aug 1997 12:06:30 fsalazar
- Correct deprecated readline().
-
- Rev 1.1 05 Aug 1997 10:15:40 fsalazar
- Use webpack.samples.weatherchart package name.
-
- *******************************************************************************/
-
-
- package samples.weatherchart;
-
- import java.io.*;
- import java.net.*;
- import java.util.StringTokenizer;
-
-
- ///////////////////////////////////////////////////////////////////////////////////
-
- public abstract class GetWData
- {
- static final String TEXT_TEMP = new String( "Temperature" );
- static final String TEXT_PRESSURE = new String( "Pressure" );
- static final String TEXT_HUMID = new String( "Humidity" );
-
- private Integer m_Temperature;
- private Float m_Humidity;
- private Float m_Pressure;
-
-
- GetWData()
- {
- m_Temperature = new Integer( 0 );
- m_Humidity = new Float( 0.0 );
- m_Pressure = new Float( 0.0 );
- }
-
- protected void setTemperature( int iTemp )
- {
- m_Temperature = new Integer( iTemp );
- }
-
- protected void setHumidity( double fHumid )
- {
- m_Humidity = new Float( fHumid );
- }
-
- protected void setPressure( double fPressure )
- {
- m_Pressure = new Float( fPressure );
- }
-
- protected int getTemperature()
- {
- return m_Temperature.intValue();
- }
-
- protected double getHumidity()
- {
- return m_Humidity.doubleValue();
- }
-
- protected double getPressure()
- {
- return m_Pressure.doubleValue();
- }
-
-
- protected String getTemperatureString()
- {
- return m_Temperature.toString();
- }
-
- protected String getHumidityString()
- {
- return m_Humidity.toString();
- }
-
- protected String getPressureString()
- {
- return m_Pressure.toString();
- }
-
- public abstract void read() throws IOException, MalformedURLException ;
-
-
- // trim a string so it contains only an integer value
- private String trimInt( String s )
- {
- int i, max, start, end;
- char c;
-
- try
- {
- start = 0;
- max = s.length();
-
- // scan to 1st number char
- for ( i = 0; i < max; i++ )
- {
- c = s.charAt( i );
- if ( Character.isDigit( c ) )
- break;
- start++;
- }
-
- // scan to non-digit
- for ( i = start, end = start; i < max; i++ )
- {
- c = s.charAt( i );
- if ( !Character.isDigit( c ) )
- break;
- end++;
- }
-
- return s.substring( start, end );
- }
- catch( Exception e )
- {
- return s;
- }
- } // trimInt
-
- // trim a string so it contains only a floating pt. value.
- private String trimFloat( String s )
- {
- int i, max, start, end;
- char c;
-
- try
- {
- start = 0;
- max = s.length();
-
- // scan to 1st number char
- for ( i = 0; i < max; i++ )
- {
- c = s.charAt( i );
- if ( Character.isDigit( c ) )
- break;
- start++;
- }
-
- // scan to non-digit, non-decimal
- for ( i = start, end = start; i < max; i++ )
- {
- c = s.charAt( i );
- if ( !Character.isDigit( c ) && c != '.' )
- break;
- end++;
- }
-
- return s.substring( start, end );
- }
- catch( Exception e )
- {
- return s;
- }
- } // trimFloat
-
-
- /////////////////////////////////////////////////////////////////////////
- //
- // parse weather data from stream. We do this as follows:
- //
- // - skip HTTP header lines
- // - read weather data file, in the format:
- //
- // line # data
- // ------ -----------------------------------------------
- // 0 Text description
- // 1 temp;humidity;wind;weather;barometric pressure
- //
- // Example file:
- //
- // : Observations for 8am(12z) 14-jul-97
- // : 79;61;w 13;9;fair;29.82
-
- protected boolean parseStream( BufferedInputStream in, boolean bSkipHdr )
- {
- String s, v, hdr = new String();
- StringTokenizer tokenizer;
- DataInputStream di = new DataInputStream( in );
- BufferedReader bdi = new BufferedReader( new InputStreamReader( di ) );
-
-
- try
- {
- // Skip HTML header. The first blank line
- // marks end of the header.
- while ( (bSkipHdr) && (true) )
- {
- s = bdi.readLine();
- if ( null == s )
- return false; // got to EOF before end of header
-
- if ( 0 == s.length() )
- break;
-
- hdr = hdr + "\n" + s; // debug - save header
- }
-
- // read weather data
- // skip 1st line
- s = bdi.readLine();
- if ( null == s )
- return false; // EOF; we were expecting data
-
- // read the data line
- s = bdi.readLine();
- if ( null == s )
- return false; // EOF; we were expecting data
-
- // read the items from the data line
- tokenizer = new StringTokenizer( s, ";\n\r" );
-
- // temperature
- v = tokenizer.nextToken();
- v = trimInt( v );
- m_Temperature = new Integer( v );
-
- // clip returned value to a reasonable range...
- // JMD: This won't work in the winter
- // if ( m_Temperature.intValue() < 40 ) m_Temperature = new Integer( 40 );
- // if ( m_Temperature.intValue() > 100 ) m_Temperature = new Integer( 100 );
-
- // humidity
- v = tokenizer.nextToken();
- s = trimFloat( s );
- m_Humidity = new Float( v );
-
- // clip returned value to a reasonable range...
- if ( m_Humidity.floatValue() < 0.0 ) m_Humidity = new Float( 0.0 );
- if ( m_Humidity.floatValue() > 100.0 ) m_Humidity = new Float( 100.0 );
-
-
- // skip 3 tokens; wind, weather, visibility
- v = tokenizer.nextToken();
- v = tokenizer.nextToken();
- v = tokenizer.nextToken();
-
- // pressure
- v = tokenizer.nextToken();
- v = trimFloat( v );
- m_Pressure = new Float( v );
-
- // clip returned value to a reasonable range...
- if ( m_Pressure.floatValue() < 29.0 ) m_Pressure = new Float( 29.0 );
- if ( m_Pressure.floatValue() > 31.5 ) m_Pressure = new Float( 31.5 );
- }
- catch( Exception e )
- {
- System.out.print( "Exception in parseStream() : " + e.getMessage() + "\r\n");
- return false;
- }
-
- // success
- return true;
- } // parseStream
-
-
-
- }
-
-
-
- // end of GetWData.java
- /////////////////////////
-