package net;

import java.util.Vector;

/**
 VendorList allows you to save a list of
 vendors to a file. The list comes from
 YellowPages.com
 */
public class VendorList {
// a Typical search URL is:
// http://www.yellowpages.com/asp/search/Searchform.asp?
// CategoryIDs=&CategoryPath=&ResultStart=1&ResultCount=2000&NameOnly=YES&TypeOnly=&SearchRelation=&BName=computer&
// SearchMethod=&SearchMethod=&SearchMethod=&SearchBy=NAME&state=CT&City=
  String url = "http://www.yellowpages.com/yellowpages/scripts/search.dll?ep=0&sic=5545&leaf=y&errp=&SearchRadius=&qzip=&qphone=&qaddress=&query=java&qcity=milford&qstate=CT&orderby=ADSPROMINENCE&ck=&userid=1&userpw=.&uh=1,0,&ccity=milford&cstate=CT&adrVer=-1&ver=5.1";

  public Vector getCsv() {
    Vector raw = net.web.Browser.getUrl(url);
    System.out.println("got url");
    return toAddressList(raw);
  }

  public static Vector toAddressList(Vector in) {
    Vector out = new Vector();
    //search for addresses
    for (int i = 0; i < in.size(); i++) {
      String s = (String) in.elementAt(i);
      if (contains(s, "Phone:"))
        out.addElement(getAddress(in, i));
    }
    return out;
  }

  public static boolean contains(String s1, String s2) {
    return s1.lastIndexOf(s1) != -1;
  }

  public static String getAddress(Vector v, int i) {
    String s = "";
    try {
      for (int j = -3; j < 0; j++) {
        s = s + "," + (String) v.elementAt(j + i);
      }
    } catch (Exception e) {
    }
    return s;
  }

  public static String vector2String(Vector raw) {
    String s = " ";
    for (int i = 0; i < raw.size(); i++)
      s = s + raw.elementAt(i);
    return s;
  }

  public static void main(String args[]) {
    VendorList vl = new VendorList();
    net.web.Browser.print(vl.getCsv());

  }
}