/*
  Sample code for reading in newsgroup files
*/


import java.io.*;
import java.net.*;

// uses the following data record class to hold information about each group

class Newsgroup {
  String name;
  int first = 0;
  int last = 0;
}


public class NewsParseExample {

  // For illustration only, hold records in an array:
  
  static final int CAP = 5000;
  Newsgroup[] groups_ = new Newsgroup[CAP];
  int count_ = 0;


  void read(InputStream raw) {

    // build a tokenizer around raw input
    StreamTokenizer in = new StreamTokenizer(raw);
    
    in.wordChars('.', '.'); // allow dots as parts of words
    in.wordChars('-', '-'); // allow dashes as parts of words
    

    try {
      for (;;) {
        int tok = in.nextToken();  // grab next token

        if (tok == StreamTokenizer.TT_EOF) 
          return;

        else if (tok == StreamTokenizer.TT_WORD) { // found a group name
          Newsgroup grp = new Newsgroup();
          grp.name = in.sval;

          for (;;) { // find the first number
            tok = in.nextToken();
            if (tok == StreamTokenizer.TT_EOF)  // (incomplete record?)
              return;
            else if (tok == StreamTokenizer.TT_NUMBER) {
              grp.first = (int)(in.nval);
              break;
            }
          }

          for (;;) { // find the second number
            tok = in.nextToken();
            if (tok == StreamTokenizer.TT_EOF) 
              return;
            else if (tok == StreamTokenizer.TT_NUMBER) {
              grp.last = (int)(Math.abs(in.nval));
              break;
            }
          }

          groups_[count_++] = grp; // only add to array if all parts parsed
        }
      }
    }
    catch (IOException ex) { return; }

  }

  // just for testing/demo purposes
  void write() {
    for (int i = 0; i < count_; ++i) {
        System.out.println(groups_[i].name + ": " + groups_[i].first + " " + groups_[i].last);
    }
  }
  
  public static void main(String[] args) {
    NewsParseExample me = new NewsParseExample();

    // open the url stream
    String addr = "http://www.oswego.edu/~dl/newsgroups";
    URL url = null;
    InputStream input = null;
    try {
      url = new URL(addr);
      input = url.openStream();
    }
    catch (MalformedURLException e) {
      System.out.println("Sorry, that is not a valid URL\n");
      return ;
    }
    catch (SecurityException e) {
      System.out.println("Sorry, cannot open foreign URL\n");
      return;
    }
    catch (IOException e) {
      System.out.println("Sorry, cannot open URL.\n");
      return;
    }

    me.read(input);
    me.write();
  }
}


