// $Id: gendaml.java,v 1.2 2003/08/04 18:53:01 drager Exp $


class gendaml
{
    static String fipsBase = "http://www.daml.org/2001/09/countries/fips#";
    static String geonamesBase = "http://www.daml.org/2002/04/geonames/";
    static String featureTypesBase = geonamesBase + "featureTypes#";

    public static String FILE_EXTENSION = ".owl";

    /**
     * java.util.StringTokenizer skips multiple consecutive separators
     */
    static class MyTokenizer
    {
	String line;
	char separator;
	int start = 0;

	MyTokenizer(String line,
		    char separator)
	{
	    this.line = line;
	    this.separator = separator;
	}

	String nextToken()
	{
	    String retval;

	    int end = line.indexOf(separator, start);
	    if (end == (-1))
		{
		    retval = line.substring(start);
		    start = line.length();
		}
	    else
		{
		    retval = line.substring(start, end);
		    start = end + 1;
		}

	    return retval;
	}
    }

    /**
     * write model to the specified path
     */
    static void writeModel(com.hp.hpl.mesa.rdf.jena.model.Model model,
			   String name)
	throws Exception
    {
	java.io.PrintWriter stream = new java.io.PrintWriter(new java.io.FileOutputStream(name + FILE_EXTENSION));
	stream.println("<?xml version='1.0' encoding='ISO-8859-1'?>");
	model.write(stream);
	stream.close();
    }

    /**
     * generate feature types
     */
    static void processFeatureTypes(String uri)
	throws Exception
    {
	com.hp.hpl.mesa.rdf.jena.model.Model model = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();

	java.net.URL url = new java.net.URL(uri);
	java.io.BufferedReader stream = new java.io.BufferedReader(new java.io.InputStreamReader(url.openStream()));
	String line;
	while ((line = stream.readLine()) != null)
	    {
		if ((line.length() == 0)
		    || (line.charAt(0) != ' ')
		    || (line.startsWith("  href"))
		    || (line.startsWith("  <")))
		    continue;
		int firstIndex = line.indexOf("  ", 2);
		String first = line.substring(2, firstIndex).trim();
		firstIndex += first.length();
		while (line.charAt(firstIndex) == ' ')
		    firstIndex++;
		int secondIndex = line.indexOf("  ", firstIndex);
		String second;
		if (secondIndex == (-1))
		    {
			second = line.substring(firstIndex).trim();
			com.hp.hpl.mesa.rdf.jena.model.Resource resource = model.createResource("#" + first);
			model.add(resource,
				  com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
				  com.hp.hpl.mesa.rdf.jena.vocabulary.RDFS.Class);
			model.add(resource,
				  com.hp.hpl.mesa.rdf.jena.vocabulary.RDFS.subClassOf,
				  geonames_ont.Feature);
			model.add(resource,
				  com.hp.hpl.mesa.rdf.jena.vocabulary.RDFS.label,
				  second);
		    }
		else
		    {
			second = line.substring(firstIndex, secondIndex);
			while (line.charAt(secondIndex) == ' ')
			    secondIndex++;
			String third = line.substring(secondIndex).trim();
			com.hp.hpl.mesa.rdf.jena.model.Resource resource = model.createResource("#" + first);
			model.add(resource,
				  com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
				  com.hp.hpl.mesa.rdf.jena.vocabulary.RDFS.Class);
			model.add(resource,
				  com.hp.hpl.mesa.rdf.jena.vocabulary.RDFS.subClassOf,
				  model.createResource("#" + second));
			model.add(resource,
				  com.hp.hpl.mesa.rdf.jena.vocabulary.RDFS.label,
				  third);
		    }
	    }
	writeModel(model, "featureTypes");

    }

    static void processCountry(String path)
	throws Exception
    {
	String countryCode = path.substring(0, path.indexOf('.'));
	
	com.hp.hpl.mesa.rdf.jena.model.Model countryModel = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();
	com.hp.hpl.mesa.rdf.jena.model.Resource country = countryModel.createResource(fipsBase + countryCode);

	java.io.BufferedReader stream = new java.io.BufferedReader(new java.io.FileReader(path));
	String line;
	// skip first line
	stream.readLine();
	while ((line = stream.readLine()) != null)
	    {
		MyTokenizer tokenizer = new MyTokenizer(line, '\t');
		String regionCode = tokenizer.nextToken();
		String uniqueFeatureIdentifier = tokenizer.nextToken();
		String uniqueNameIdentifier = tokenizer.nextToken();
		String latitude = tokenizer.nextToken();
		String longitude = tokenizer.nextToken();
		tokenizer.nextToken();	// DMS_LAT
		tokenizer.nextToken();	// DMS_LONG
		String utmGrid = tokenizer.nextToken();
		String jog = tokenizer.nextToken();
		tokenizer.nextToken(); // Feature Classification
		String featureDesignation = tokenizer.nextToken();
		String populatedPlaceClassification = tokenizer.nextToken();
		String primaryCountry = tokenizer.nextToken();
		String adm1 = tokenizer.nextToken();
		String adm2 = tokenizer.nextToken();
		String dimension = tokenizer.nextToken();
		String secondaryCountry = tokenizer.nextToken();
		String nameType = tokenizer.nextToken();
		String languageCode = tokenizer.nextToken();
		String shortName = tokenizer.nextToken();
		String generic = tokenizer.nextToken();
		String sortName = tokenizer.nextToken();
		String fullName = tokenizer.nextToken();
		tokenizer.nextToken(); // Full Name ND
		String modifyDate = tokenizer.nextToken();

		com.hp.hpl.mesa.rdf.jena.model.Model featureModel = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();
		com.hp.hpl.mesa.rdf.jena.model.Resource featureResource = featureModel.createResource("#" + uniqueFeatureIdentifier);
		featureModel.add(featureResource,
				 com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
				 featureModel.createResource(featureTypesBase + featureDesignation));
		featureModel.add(featureResource,
				 geonames_ont.uniqueIdentifier,
				 uniqueFeatureIdentifier);
		featureModel.add(featureResource,
				 geonames_ont.latitude,
				 latitude);
		featureModel.add(featureResource,
				 geonames_ont.longitude,
				 longitude);
		if (! dimension.equals(""))
		    featureModel.add(featureResource,
				     geonames_ont.dimension,
				     dimension);
		featureModel.add(featureResource,
				 geonames_ont.modifyDate,
				 modifyDate);

		writeModel(featureModel,
			   "f" + uniqueFeatureIdentifier);

		countryModel.add(country,
				 geonames_ont.feature,
				 countryModel.createResource(geonamesBase + "f" + uniqueFeatureIdentifier));
	    }
	
	writeModel(countryModel, countryCode);
    }

    public static void main(String args[])
	throws Exception
    {

	int i = 0;
	if (args[i].equals("-daml")) {
	    FILE_EXTENSION=".daml";
	    i++;
	} else if (args[i].equals("-owl")) {
	    FILE_EXTENSION=".owl";
	    i++;
	}

	processFeatureTypes("file:fd_cross_ref.html");

	// process inputs
	while (i < args.length) {
		processCountry(args[i]);
		i++;
	    }
    }
}
