// $Id: genhtml.java,v 1.7 2002/01/08 06:18:24 mdean Exp $


/**
 * generate HTML table showing issues by report
 */
class genhtml
{
    static class Issue
	implements Comparable
    {
	com.hp.hpl.mesa.rdf.jena.model.Resource resource;
	String label;
	
	Issue(com.hp.hpl.mesa.rdf.jena.model.Resource resource,
	      String label)
	    {
		this.resource = resource;
		this.label = label;
	    }

	public int compareTo(Object object)
	{
	    return label.compareTo(((Issue) object).label);
	}
    }

    static class BadCardinality
	extends Exception
    {
	com.hp.hpl.mesa.rdf.jena.model.Resource subject;
	com.hp.hpl.mesa.rdf.jena.model.Property property;
	int count;

	BadCardinality(com.hp.hpl.mesa.rdf.jena.model.Resource subject,
		       com.hp.hpl.mesa.rdf.jena.model.Property property,
		       int count)
	{
	    this.subject = subject;
	    this.property = property;
	    this.count = count;
	}

	public String toString()
	{
	    return "BadCardinality " + count + " for " + property + " of " + subject;
	}
    }

    /**
     * return the single property of cardinality 1
     */
    static com.hp.hpl.mesa.rdf.jena.model.RDFNode getValue(com.hp.hpl.mesa.rdf.jena.model.Model model,
							   com.hp.hpl.mesa.rdf.jena.model.Resource subject,
							   com.hp.hpl.mesa.rdf.jena.model.Property property)
	throws BadCardinality,
	       Exception
    {
	com.hp.hpl.mesa.rdf.jena.model.RDFNode retval;
	com.hp.hpl.mesa.rdf.jena.model.NodeIterator nodeIterator = model.listObjectsOfProperty(subject, property);
	if (! nodeIterator.hasNext())
	    throw new BadCardinality(subject, property, 0);
	retval = nodeIterator.next();
	if (nodeIterator.hasNext())
	    {
		int cardinality = 1;
		while (nodeIterator.hasNext())
		    {
			nodeIterator.next();
			cardinality++;
		    }
		throw new BadCardinality(subject, property, cardinality);
	    }
	nodeIterator.close();
	return retval;
    }

    /**
     * return the single property of cardinality 0 or 1
     */
    static com.hp.hpl.mesa.rdf.jena.model.RDFNode getOptionalValue(com.hp.hpl.mesa.rdf.jena.model.Model model,
								   com.hp.hpl.mesa.rdf.jena.model.Resource subject,
								   com.hp.hpl.mesa.rdf.jena.model.Property property)
	throws BadCardinality,
	       Exception
    {
	com.hp.hpl.mesa.rdf.jena.model.RDFNode retval;
	com.hp.hpl.mesa.rdf.jena.model.NodeIterator nodeIterator = model.listObjectsOfProperty(subject, property);
	if (! nodeIterator.hasNext())
	    return null;
	retval = nodeIterator.next();
	if (nodeIterator.hasNext())
	    {
		int cardinality = 1;
		while (nodeIterator.hasNext())
		    {
			nodeIterator.next();
			cardinality++;
		    }
		throw new BadCardinality(subject, property, cardinality);
	    }
	nodeIterator.close();
	return retval;
    }

    static void usage()
    {
	System.err.println("Usage:  ");
	System.err.println("(no arguments are expected)");
	System.exit(1);
    }
    
    public static void main(String args[])
	throws Exception
    {
	com.hp.hpl.mesa.rdf.jena.model.Model model = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();
	String experiencesURI = "file:experiences.daml";
	model.read(experiencesURI);

	// identify reports
	com.hp.hpl.mesa.rdf.jena.model.Resource reports = (com.hp.hpl.mesa.rdf.jena.model.Resource) getValue(model, experiences.Report, DAML.oneOf);

	// identify issues
	java.util.TreeSet issues = new java.util.TreeSet();
	com.hp.hpl.mesa.rdf.jena.model.ResIterator issueIterator = model.listSubjectsWithProperty(com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
												  experiences.Issue);
	while (issueIterator.hasNext())
	    {
		com.hp.hpl.mesa.rdf.jena.model.Resource issue = issueIterator.next();
		issues.add(new Issue(issue,
				     "" + getValue(model, issue, com.hp.hpl.mesa.rdf.jena.vocabulary.RDFS.label)));
	    }
	
	String title = "DAML+OIL Issues and Experiences";
	System.out.println("<title>" + title + "</title>");
	System.out.println("<h1>" + title + "</h1>");

	System.out.println("In preparation for the <a href=\"http://www.w3.org/2001/sw/WebOnt/ftf1.html\">first WebOnt Face-to-Face meeting</a>, <a href=\"http://www.cs.vu.nl/~frankh/\">Frank van Harmelen</a> <a href=\"http://lists.w3.org/Archives/Public/www-webont-wg/2001Dec/0086.html\">suggested</a> collecting feedback from current users of <a href=\"http://www.daml.org/2001/03/daml+oil-index\">DAML+OIL</a>.");
	System.out.println("<a href=\"http://www.daml.org/people/mdean/\">Mike Dean</a> <a href=\"http://lists.w3.org/Archives/Public/www-webont-wg/2001Dec/0138.html\">collected</a> the results and <a href=\"#colophon\">prepared</a> <a href=\"http://www.daml.org/2002/01/experiences/\">this summary</a>.");
	System.out.println("<p>");
	System.out.println("The table below identifies the issues addressed in each of the submitted reports, and attempts to draw some consensus conclusions.");
	System.out.println("<p>");

	com.hp.hpl.mesa.rdf.jena.model.Resource list; 
	System.out.println("<table border=\"1\">");
	
	list = reports;
	int index = 1;
	System.out.println("  <tr>");
	System.out.println("    <th>Issue</th>");
	while (! list.equals(DAML.nil))
	    {
		com.hp.hpl.mesa.rdf.jena.model.Resource report = (com.hp.hpl.mesa.rdf.jena.model.Resource) getValue(model, list, DAML.first);
		System.out.println("    <th><a href=\"" + getValue(model, report, experiences.uri) + "\">[" + index + "]</a></th>");
		index++;
		list = (com.hp.hpl.mesa.rdf.jena.model.Resource) getValue(model, list, DAML.rest);
	    }
	System.out.println("    <th>Conclusions</th>");
	System.out.println("  </tr>");

	java.util.Iterator iterator = issues.iterator();
	while (iterator.hasNext())
	    {
		Issue issue = (Issue) iterator.next();

		System.out.println("  <tr>");
		System.out.println("    <th align=\"left\">" + issue.label + "</th>");
		list = reports;
		while (! list.equals(DAML.nil))
		    {
			com.hp.hpl.mesa.rdf.jena.model.Resource report = (com.hp.hpl.mesa.rdf.jena.model.Resource) getValue(model, list, DAML.first);
			System.out.println("      <td>");
			com.hp.hpl.mesa.rdf.jena.model.NodeIterator nodeIterator = model.listObjectsOfProperty(report, experiences.issue);
			while (nodeIterator.hasNext())
			    {
				com.hp.hpl.mesa.rdf.jena.model.Resource thisIssue = (com.hp.hpl.mesa.rdf.jena.model.Resource) nodeIterator.next();
				if (issue.resource.equals(thisIssue))
				    System.out.println("X");
			    }
			System.out.println("      </td>");
			list = (com.hp.hpl.mesa.rdf.jena.model.Resource) getValue(model, list, DAML.rest);
		    }
		System.out.println("    <td>");
		Object conclusion = getOptionalValue(model, issue.resource, experiences.conclusion);
		if (conclusion != null)
		    System.out.println(conclusion);
		System.out.println("    </td>"); 
		System.out.println("  </tr>");
	    }

	System.out.println("</table>");

	System.out.println("<a name=\"colophon\"/>");
	System.out.println("<h2>Colophon</h2>");
	System.out.println("This page was generated from experience information (<a href=\"experiences.n3\">n3</a> and generated <a href=\"experiences.daml\">DAML</a>) using a custom <a href=\"genhtml.java\">Java program</a>.");
	System.out.println("<p>");
	System.out.println("<hr>");
	System.out.println("<address>" + getValue(model, new com.hp.hpl.mesa.rdf.jena.common.ResourceImpl(experiencesURI), DAML.versionInfo) + "</address>");
    }
}
