001    import java.net.*;
002    import java.io.*;
003    import java.util.*;
004    
005    /** 
006     * <h2>Inet</h2>
007     *
008     * @author Matthew Hough
009     * @version .01, 6/2/07
010     * @assignment.number A19015
011     * @prgm.usage This class connects to the internet to download weather data
012     *
013     */
014    
015    public class Inet implements InetTemplate {
016    
017            // Variables
018            String strContent = "";
019    
020            String strPreData = "";
021    
022            // Functions
023            /** 
024             * This method will accept a string URL, download the web page, store the web page in a string variable and return the entire web page as a string. 
025             * @param strURL The URL of the web page as a string 
026             * @return strWebPage The entire web page as a string 
027             */
028            public String getURL(String strURL) {
029                    try {
030                            URL myWebAddress = new URL(strURL);
031                            URLConnection myConn = myWebAddress.openConnection();
032                            InputStream myStrIn = myConn.getInputStream();
033                            BufferedReader fromRemoteServer = new BufferedReader(
034                                            new InputStreamReader(myStrIn));
035                            strContent = "";
036                            for (String myLine = ""; (myLine = fromRemoteServer.readLine()) != null;) {
037                                    strContent += myLine + "\r\n";
038                            }
039                    } catch (MalformedURLException errnum) {
040                            // display error if URL is messed up
041                    } catch (IOException errnum) {
042                            // display error if Internet connection is flaky
043                    }
044                    return strContent;
045            }
046    
047            /** 
048             * This method will accept a large string and return all of the data in that string between the first pair of PRE tags.  Remind the programmer that PRE tags can be coded in the page as upper or lower case tags (i.e. PRE or pre). 
049             * @param strData A large string 
050             * @return strPreData All of the data in that string between the first pair of PRE tags 
051             */
052            public String getData(String strData) {
053                    StringReader temp = new StringReader(strData);
054                    BufferedReader result = new BufferedReader(temp);
055                    try {
056                            boolean record = false;
057                            for (String tempLine = ""; (tempLine = result.readLine()) != null;) {
058                                    if (tempLine.equalsIgnoreCase("<pre>")) {
059                                            record = true;
060                                    }
061                                    if (tempLine.equalsIgnoreCase("</pre>")) {
062                                            record = false;
063                                    }
064                                    if (record == true && !tempLine.equalsIgnoreCase("</pre>")
065                                                    && !tempLine.equalsIgnoreCase("<pre>")
066                                                    && !tempLine.equalsIgnoreCase("Starting DemoMain09")) {
067                                            strPreData += tempLine + "\r\n";
068                                    }
069                            }
070                    } catch (IOException e) {
071                            // throw exception
072                    }
073                    return strPreData;
074            }
075    
076            /** 
077             * Accepts two parameters; the file name and the data (both string variables).  The method will open a text file for output and save the data to it. Typical usage would be that the user uses getURL to download the web page, getData to extract only the needed data and saveData to save that data to disk.  Remind the programmer to close the output file at the end of the method. 
078             * @param strFileName The file name<br>strFileData The file data 
079             * @return VOID This function writes the data to a text file; returns nothing 
080             */
081            public void saveData(String strFileName, String strFileData) {
082                    try {
083                            BufferedWriter out = new BufferedWriter(new FileWriter(strFileName));
084                            out.write(strFileData);
085                            out.close();
086                    } catch (IOException e) {
087                            // throw exception
088                    }
089            }
090    
091            /** 
092             * Convert a date variable to a string using the format: MM/dd/yyyy kk:mm:ss. 
093             * @param dteDate 
094             */
095            public String convertDate(Date dteDate) {
096                    return "";
097            }
098    
099            /** 
100             * Download the current time from NIST (which uses Daytime Protocol RFC-867) and convert it into a Date variable. Use getURL() and parseString().  
101             */
102            public Date getNISTTime() {
103                    Date empty = new Date();
104                    return empty;
105            }
106    
107            /** 
108             * Download the current time from NIST (which uses Daytime Protocol RFC-867) and convert it into a Date variable. Use getURL() and parseString(). Adjust the time for Pacific Time (Americas/Los Angeles) and take into account Daylight Saving Time (minus 7 hours in summer and minus 8 hours in winter). I used TimeZone and Calendar variables but you are free to use any methodology as long as it works!  
109             */
110            public Date getPSTTime() {
111                    Date empty = new Date();
112                    return empty;
113            }
114    
115            /** 
116             * Parse the "DATA BASED ON" line from a Winds Aloft report (third line) and return a date variable with the proper Year, Month, Day, Hour and Minutes. 
117             * @param strDataBasedOn - a string from the NWS FD report (3rd line)  
118             */
119            public Date getRPTTime() {
120                    Date empty = new Date();
121                    return empty;
122            }
123    
124            /** 
125             * Parse a string using the parse() method of SimpleDateFormat and the pattern supplied in strPattern and return a Date variable. 
126             * @param strDate - A string date from the NIST or the NWS. 
127             * @param strPattern - A string showing where data is located within the string. 
128             */
129            public Date parseString(String strDate, String strPattern) {
130                    Date empty = new Date();
131                    return empty;
132            }
133    
134    }