// $Id: gendaml.java,v 1.3 2003/01/30 18:33:31 mdean Exp $


/**
 * generate DAML units from .txt files derived from .pdf files.
 * A complete entry starts with a number, and ends with a (UIC),
 * possibly spanning multiple lines. 
 */
class gendaml
{
    static com.hp.hpl.mesa.rdf.jena.model.Model model = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();

    static java.util.Stack stack = new java.util.Stack();

    static boolean isNumber(String string)
    {
	for (int i = 0; i < string.length(); i++)
	    {
		char ch = string.charAt(i);
		switch (ch)
		    {
		    case '0':
		    case '1':
		    case '2':
		    case '3':
		    case '4':
		    case '5':
		    case '6':
		    case '7':
		    case '8':
		    case '9':
			break;
		    default:
			return false;
		    }

	    }
	return true;
    }

    static String getUIC(String line)
    {
	int lastSpace = line.lastIndexOf(' ');
	String lastWord = (lastSpace == (-1)) ? line : line.substring(lastSpace + 1);
	if (lastWord.startsWith("(")
	    && lastWord.endsWith(")"))
	    {
		lastWord = lastWord.substring(1, lastWord.length() - 1);
		if ((lastWord.length() == 5)
		    && (isNumber(lastWord.substring(0, 4)))) // last char alphanumeric
		    {
			System.out.println("UIC " + lastWord); // XXX
			return "N" + lastWord;
		    }
	    }
	return null;
    }

    static void finishLine(String line)
	throws com.hp.hpl.mesa.rdf.jena.model.RDFException
    {
	if (line == null)
	    return;
	int firstSpace = line.indexOf(' ');
	String firstWord = line.substring(0, firstSpace);
	boolean gotDepth = isNumber(firstWord);
	int lastSpace = line.lastIndexOf(' ');
	String lastWord = line.substring(lastSpace + 1);
	String uic = getUIC(line);
	String comment = null;
	if (uic == null)
	    {
		if (line.endsWith(")"))
		    {
			int left = line.lastIndexOf('(');
			comment = line.substring(left + 1, line.length() - 1);
			if (left == 0)
			    return;
			line = line.substring(0, left - 2);
			uic = getUIC(line);
		    }
	    }
	if (uic != null)
	    {
		String label = line.substring(gotDepth ? (firstSpace + 1) : 0,
					      lastSpace);
		com.hp.hpl.mesa.rdf.jena.model.Property parentProperty;
		if (label.startsWith("("))
		    {
			label = label.substring(1, label.length() - 1);
			parentProperty = unit_ont.detachmentOf;
		    }
		else
		    parentProperty = unit_ont.reportsTo;

		com.hp.hpl.mesa.rdf.jena.model.Resource unit = model.createResource("#" + uic);
		model.add(unit,
			  com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
			  unit_ont.Unit);
		model.add(unit,
			  unit_ont.uic,
			  uic);
		model.add(unit,
			  com.hp.hpl.mesa.rdf.jena.vocabulary.RDFS.label,
			  label);
		if (gotDepth)
		    {
			int depth = Integer.parseInt(firstWord);
			while (stack.size() > (depth - 1))
			    stack.pop();
		    }
		if (stack.size() > 0)
		    model.add(unit,
			      parentProperty,
			      stack.peek());
		if (gotDepth)
		    stack.push(unit);
	    }
    }

    static void process(String path)
	throws Exception
    {
	String currentLine = null;
	java.io.BufferedReader stream = new java.io.BufferedReader(new java.io.FileReader(path));
	String line;
	while ((line = stream.readLine()) != null)
	    {
		int firstSpace = line.indexOf(' ');
		System.out.println(line);
		String firstWord = (firstSpace == (-1)) ? line : line.substring(0, firstSpace);
		if (isNumber(firstWord))
		    {
			int depth = Integer.parseInt(firstWord);
			finishLine(currentLine);
			currentLine = line;
		    }
		else
		    {
			// append to currentLine
			if (currentLine == null)
			    currentLine = line;
			else
			    currentLine += (" " + line);
		    }

		if (getUIC(currentLine) != null)
		    {
			finishLine(currentLine);
			currentLine = null;
		    }
	    }
	finishLine(currentLine);
    }

    public static void main(String args[])
	throws Exception
    {
	for (int i = 0; i < args.length; i++)
	    process(args[i]);

	java.io.PrintWriter output = new java.io.PrintWriter(new java.io.FileOutputStream("usnavy.daml"));
	model.write(output);
	output.close();
    }
}
