// $Id: airport.java,v 1.8 2004/07/07 18:35:01 mdean Exp $


class airport
{
    static String base = "http://www.daml.org/cgi-bin/airport?";

    /**
     * convert degrees/minutes/seconds to double
     */
    static double convert(String string)
    {
	string = string.replace('\'', ' ')
	    .replace('"', ' ')
	    .replace('°', ' ');
	java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(string);
	double retval = Double.parseDouble(tokenizer.nextToken())
	    + (Double.parseDouble(tokenizer.nextToken()) / 60.0)
	    + (Double.parseDouble(tokenizer.nextToken()) / 3600.0);
	String direction = tokenizer.nextToken();
	if (direction.equals("S")
	    || (direction.equals("W")))
	    retval *= (-1);
	return retval;
    }

    static com.hp.hpl.mesa.rdf.jena.model.Model airport(String symbol)
	throws Exception
    {
	com.hp.hpl.mesa.rdf.jena.model.Model model = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();
	com.hp.hpl.mesa.rdf.jena.model.Resource airportResource = model.createResource("");

	try {
	    symbol = symbol.toUpperCase();

	    // get page
	    String url = "http://www.airrouting.com/scripts/airportloc.asp?RequestLocation=" + symbol;
	    StringBuffer page = new StringBuffer();
	    java.io.BufferedReader stream = new java.io.BufferedReader(new java.io.InputStreamReader((new java.net.URL(url)).openStream()));
	    String line;
	    while ((line = stream.readLine()) != null)
		{
		    // replace &deg; with " "
		    int deg;
		    while ((deg = line.indexOf("&deg;")) != (-1))
			{
			    line = line.substring(0, deg) + " " + line.substring(deg + 5);
			}
		    
		    page.append(line + "\n");
		}
	    
	    org.daml.html.Tree tree = new org.daml.html.Tree(new java.io.StringBufferInputStream(page.toString()));
	    // tree.dump(System.out);
	    String prefix = "html/body/table/tr[3]/td/table/tr/td/table[2]/tr/td/text()";
	    String name = tree.getString(prefix + "[1]").trim();
	    String iataCode = tree.getString(prefix + "[2]").trim();
	    String icaoCode = tree.getString(prefix + "[3]").trim();
	    String location = tree.getString(prefix + "[4]").trim();
	    double latitude = convert(tree.getString(prefix + "[5]").trim());
	    double longitude = convert(tree.getString(prefix + "[6]").trim());
	    String elevation = tree.getString(prefix + "[7]").trim();
	    
	    model.add(airportResource,
		      com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
		      airport_ont.Airport);
	    model.add(airportResource,
		      airport_ont.name,
		      name);
	    if (! iataCode.equals("N/A"))
		model.add(airportResource,
			  airport_ont.iataCode,
			  iataCode);
	    if (! icaoCode.equals("N/A"))
		model.add(airportResource,
			  airport_ont.icaoCode,
			  icaoCode);
	    switch (symbol.length())
		{
		case 3:
		    if (! icaoCode.equals("N/A"))
			model.add(airportResource,
				  // com.hp.hpl.jena.vocabulary.DAML_OIL.sameIndividualAs,
				  OWL.sameAs,
				  model.createResource(base + icaoCode));
		    break;
		case 4:
		    if (! iataCode.equals("N/A"))
			model.add(airportResource,
				  // com.hp.hpl.jena.vocabulary.DAML_OIL.sameIndividualAs,
				  OWL.sameAs,
				  model.createResource(base + iataCode));
		    break;
		}
	    model.add(airportResource,
		      airport_ont.location,
		      location);
	    model.add(airportResource,
		      airport_ont.latitude,
		      latitude);
	    model.add(airportResource,
		      airport_ont.longitude,
		      longitude);
	    model.add(airportResource,
		      airport_ont.elevation,
		      elevation);
	} catch (Exception e) {
	    e.printStackTrace();
	    // nothing
	}

	return model;
    }

    static void usage()
    {
	System.err.println("Usage:  symbol");
	System.exit(1);
    }

    public static void main(String args[])
	throws Exception
    {
	if (args.length != 1)
	    usage();

	com.hp.hpl.mesa.rdf.jena.model.Model model = airport(args[0]);
	java.io.PrintWriter writer = new java.io.PrintWriter(System.out);
	model.write(writer);
	writer.close();
    }
}
