Example #1
0
 void setIcon() throws Exception {
   URL url = this.getClass().getResource("/microscope.gif");
   if (url == null) return;
   Image img = createImage((ImageProducer) url.getContent());
   if (img != null) setIconImage(img);
 }
  // This uses the key to get the entire record from the web page
  // For now the fields that compose the record will be hardcoded
  // but these fields need to be checked with the advertisement
  // in future releases. It then uses the parameter passed to it
  // to decide which fields to return
  // Also put's the field values in a Hashtable that can be later accessed
  private synchronized String getRecordFields(String key, String param) {

    fieldsTable = null;

    String city = new String(key);
    displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " + "city = " + city);

    // Add the URL extension corresponding to the city.
    StringBuffer sbuf = new StringBuffer();
    for (int i = 0; i < city.length(); i++) {
      char ch = city.charAt(i);
      if (ch != ' ' && ch != '\t' && ch != '\n') sbuf.append(ch);
    }
    String formURLString = siteURLString + new String(sbuf) + ".html";

    URL infoURL = null;
    try {
      infoURL = new URL(formURLString);
    } catch (MalformedURLException e) {
      displayMessage(
          "ERROR",
          "WCNForecastWeatherQueryFn::getRecordFields " + "Malformed URL " + formURLString);
      return null;
    }
    // displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " +
    //	   "Got URL for site: " + formURLString);
    Object content = null;
    try {
      content = infoURL.getContent();
    } catch (IOException e) {
      displayMessage(
          "ERROR",
          "WCNForecastWeatherQueryFn::getRecordFields "
              + "Can't load content from "
              + formURLString);
      return null;
    }
    InputStreamReader input = new InputStreamReader((InputStream) content);
    BufferedReader reader = new BufferedReader(input);
    sbuf.setLength(0);
    boolean stillReading = true;
    char[] cbuf = new char[100000];
    int ccount = 0;
    long startTime = System.currentTimeMillis();
    while (stillReading) {
      try {
        ccount = reader.read(cbuf);
      } catch (IOException e) {
        displayMessage(
            "ERROR",
            "WCNForecastWeatherQueryFn::getRecordFields "
                + "IO error getting content from "
                + formURLString);
        return null;
      }
      if (ccount > 0) sbuf.append(cbuf, 0, ccount);
      if (ccount < 0) stillReading = false;
    }
    // displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " +
    //	   "Time to read content: " + ((System.currentTimeMillis() - startTime) / 1000.0) +
    //            " seconds");
    String page = sbuf.toString();
    StringBuffer day = new StringBuffer();
    StringBuffer condition = new StringBuffer();

    int pos = page.indexOf("<!-- forecast include");
    int delimit;
    pos = page.indexOf("<TR>", pos);
    pos = page.indexOf("<TR>", pos + 1);
    pos = page.indexOf("<TR>", pos + 1);
    for (int n = 0; n < 10; n++) {
      pos = page.indexOf("<TR>", pos + 1);
      int daypos = page.indexOf("<BR>", pos) + 4;
      delimit = page.indexOf("</B>", daypos);
      day.append("(" + page.substring(daypos, delimit) + (n == 9 ? ")" : ") "));
      pos = page.indexOf("<FONT", daypos);
      int condpos = page.indexOf(">", pos) + 1;
      delimit = page.indexOf("</FONT>", condpos);
      condition.append("(" + page.substring(condpos, delimit) + (n == 9 ? ")" : ") "));
    }

    // Get the parser corresponding to the initial URL
    /*
       Parser infoParser = null;
       try {
         infoParser = new Parser(infoURL,true,display);
       } catch (Exception ex) {
         infoParser = null;
       }

       if (infoParser == null)     {
         displayMessage("ERROR",  "WCNForecastWeatherQueryFn::getRecordFields " +
    	     "Cannot initialize parser for page " + formURLString );
         return null ;
       }


       if (FILE_WRITE) {
         displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " +
    	     "Site URL: " + formURLString +
    	     " Calling writeContent()." + FILE_WRITE_EXT_NUM);
         writeContent(infoParser.getContent());
       }


       displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " +
    	   "ready to return record fields: " + param);

       displayMessage("DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " +
    	   "city is: " + key);



       String ct = null;
       StringBuffer day = new StringBuffer();
       StringBuffer condition = new StringBuffer();
       String tag = null;

       while ((tag = infoParser.nextTag("NONE")) != null) {
         ct = infoParser.getContentBetween ();
         if ((ct == null) || (ct.length() == 0)) continue;
         ct = ct.replace('\n', ' ');
         ct = ct.replace('\r', ' ');
         if (ct.toLowerCase().indexOf("sunrise:") != -1) {
           tag = infoParser.nextTag("NONE");
    while (tag != null && !tag.equalsIgnoreCase("table"))
      tag = infoParser.nextTag("NONE");
    while (tag != null && !tag.equalsIgnoreCase("span"))
      tag = infoParser.nextTag("NONE");
           for ( int n = 0; n < 5; n++ ) {
             if (tag != null) {
               infoParser.removeContentBetween();
               tag = infoParser.nextTag("NONE");
               while (tag != null && !tag.equalsIgnoreCase("/span"))
                 tag = infoParser.nextTag("NONE");
               if (tag != null) {
                 day.append(infoParser.getContentBetween() + (n == 4 ? "" : " "));
                 tag = infoParser.nextTag("IMG");
                 while (tag != null && !tag.equalsIgnoreCase("img"))
                   tag = infoParser.nextTag("IMG");
                 if (tag != null)
                   condition.append(infoParser.getParameterValue("ALT") + (n == 4 ? "" : " "));
               }
             }
             tag = infoParser.nextTag("NONE");
             while (tag != null && !tag.equalsIgnoreCase("img"))
               tag = infoParser.nextTag("NONE");
             while (tag != null && !tag.equalsIgnoreCase("span"))
               tag = infoParser.nextTag("NONE");
             System.out.println("Content after condition = " + infoParser.getContentBetween());
           }
         }
         infoParser.removeContentBetween();
       }
       */

    // If we couldn't get at least a temperature from the page, it must
    // have been an invalid URL, ie.e, a city not in CNN database or
    // incorrectly spelled or designated.
    if (day == null) {
      displayMessage(
          "ERROR",
          "WCNForecastWeatherQueryFn::getRecordFields "
              + "URL "
              + formURLString
              + " is not a valid URL. "
              + "The \"CityCountry\" or \"CityState\" key may be wrong.");
      return null;
    }

    String weather =
        WEATHER_PERF_STRING
            + " "
            + ":"
            + DAY_STRING
            + " ("
            + day.toString()
            + ") "
            + ":"
            + CONDITION_STRING
            + " ("
            + condition.toString()
            + ")";

    if (param.equalsIgnoreCase(ALL_FIELDS)) {

      fieldsTable = new Hashtable();

      if (key != null) {
        fieldsTable.put(CITY_STRING, key);
      }
      if (day != null) {
        fieldsTable.put(DAY_STRING, day);
      }
      if (condition != null) {
        fieldsTable.put(CONDITION_STRING, condition);
      }
      if (formURLString != null) {
        fieldsTable.put(WEATHER_URL_STRING, formURLString);
      }

      if (key == null) {
        key = " ";
      }

      if (formURLString == null) {
        formURLString = " ";
      }

      String reply =
          "(reply :"
              + CITY_STRING
              + " ("
              + key
              + ") "
              + ":"
              + WEATHER_STRING
              + " ("
              + weather
              + ") "
              + ":"
              + WEATHER_URL_STRING
              + " ("
              + formURLString
              + "))";
      displayMessage(
          "DEBUG", "WCNForecastWeatherQueryFn::getRecordFields " + "Weather is: " + reply);
      return reply;
    } else if (param.equalsIgnoreCase(CITY_STRING)) {
      fieldsTable = new Hashtable();
      fieldsTable.put(CITY_STRING, key);
      return key;
    } else if (param.equalsIgnoreCase(WEATHER_STRING)) {
      fieldsTable = new Hashtable();
      fieldsTable.put(WEATHER_STRING, weather);
      return weather;
    } else if (param.equalsIgnoreCase(WEATHER_URL_STRING)) {
      fieldsTable = new Hashtable();
      fieldsTable.put(WEATHER_URL_STRING, formURLString);
      return formURLString;
    } else {
      return null;
    }
  }