// $Id: genhtml.java,v 1.69 2002/05/22 21:08:40 kmbarber Exp $


/**
 * generate publications list
 */
class genhtml
{
    static com.hp.hpl.mesa.rdf.jena.model.Model model = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();

    static class Class
    {
	com.hp.hpl.mesa.rdf.jena.model.Resource cl;
	java.util.Vector subclasses = new java.util.Vector();
	java.util.Vector superclasses = new java.util.Vector();

	Class(com.hp.hpl.mesa.rdf.jena.model.Resource cl)
	{
	    this.cl = cl;
	}
		

	/**
	 * map Resource to Class
	 */
	static java.util.Hashtable classes = new java.util.Hashtable();
	
	static Class getClass(com.hp.hpl.mesa.rdf.jena.model.Resource cl)
	{
	    Class retval = (Class) classes.get(cl);
	    if (retval == null)
		{
		    retval = new Class(cl);
		    classes.put(cl, retval);
		}
	    return retval;
	}

	void addSubclass(Class cl)
	{
	    this.subclasses.add(cl);
	    cl.superclasses.add(this);
	}

	/**
	 * sort the array of publications by year
	 */
	

	/**
	 * is the specified resource an instance of this class?
	 */
	boolean isa(com.hp.hpl.mesa.rdf.jena.model.Resource resource)
	    throws Exception
	{
	    com.hp.hpl.mesa.rdf.jena.model.StmtIterator iterator = resource.listProperties(com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type);
	    while (iterator.hasNext())
		{
		    com.hp.hpl.mesa.rdf.jena.model.Statement statement = iterator.next();
		    com.hp.hpl.mesa.rdf.jena.model.Resource type = (com.hp.hpl.mesa.rdf.jena.model.Resource) statement.getObject();
		    Class cl = Class.getClass(type);
		    
		    if (this.isSubClassOf(cl))
			return true;
		}
	    
	    return false;
	}

	/**
	 * return true if cl is this Class or a subclass of this Class.
	 */
	boolean isSubClassOf(Class cl)
	{
	    if (cl == this)
		return true;

	    java.util.Iterator iterator = cl.superclasses.iterator();
	    while (iterator.hasNext())
		{
		    Class superclass = (Class) iterator.next();
		    if (this.isSubClassOf(superclass))
			return true;
		}

	    return false;
	}

	public String toString()
	{
	    return cl.toString();
	}
    }

    /**
     * return the first property of cardinality 1
     */
    static com.hp.hpl.mesa.rdf.jena.model.RDFNode getOptionalLiteral(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 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();
	nodeIterator.close();
	return retval;
    }

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

    public static void main(String args[])
	throws Exception
    {
	// parse arguments
	if (args.length == 0)
	    usage();
	for (int i = 0; i < args.length; i++)
	    {
		String arg = args[i];

		if (arg.startsWith("-"))
		    usage();

		try {
		    model.read(arg);
		} catch (Exception e) {
		    System.err.println(arg + ":  " + e);
		}
	    }

	// collect class hierarchy
	com.hp.hpl.mesa.rdf.jena.model.StmtIterator classIterator = model.listStatements();
	while (classIterator.hasNext())
	    {
		com.hp.hpl.mesa.rdf.jena.model.Statement statement = classIterator.next();
		if (statement.getPredicate().equals(com.hp.hpl.mesa.rdf.jena.vocabulary.RDFS.subClassOf))
		    {
			com.hp.hpl.mesa.rdf.jena.model.Resource subclass = statement.getSubject();
			com.hp.hpl.mesa.rdf.jena.model.Resource superclass = (com.hp.hpl.mesa.rdf.jena.model.Resource) statement.getObject();
			Class.getClass(superclass).addSubclass(Class.getClass(subclass));
		    }
	    }

	// get list of publications
	Class cl = Class.getClass(bibtex.BibliographyItem);
	java.util.Vector publications = new java.util.Vector();

	com.hp.hpl.mesa.rdf.jena.model.ResIterator resources = model.listSubjectsWithProperty(com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type);
	while (resources.hasNext())
	    {
		com.hp.hpl.mesa.rdf.jena.model.Resource resource = resources.next();
		if (cl.isa(resource))
		    publications.add(resource);
	    }

	

	// header
	String pageTitle = "DAML Publications";
	System.out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd/\">");
	System.out.println("<html>");
	System.out.println("<head>");
	System.out.println("<title>" + pageTitle + "</title>");
	System.out.println("</head>");
	System.out.println("<body>");
	System.out.println("<table summary=\"Layout Table: First column contains links, second column contains page content\" border=\"0\" width=\"99%\" cellspacing=\"0\" cellpadding=\"5\"><tr><td valign=\"top\" width=\"15%\" bgcolor=\"000099\"><img alt=\"DAML Logo - Link to www.DAML.org\" border=\"0\" align=\"left\" width=\"150\" height=\"73\" src=\"../home/DamlSansText.jpg\"/></td><td valign=\"top\" width=\"69%\" bgcolor=\"000099\"><h1><font color=\"FFFFFF\">DAML Publications</font></h1><a href=\"/index.html\"><font color=\"FFFFFF\">Home</font></a><xsl:text> |  </xsl:text><a href=\"/about.html\"><font color=\"FFFFFF\">About DAML</font></a><xsl:text> | </xsl:text><a href=\"/announcements.html\"><font color=\"FFFFFF\">Announcements</font></a><xsl:text> | </xsl:text><a href=\"/roadmap.html\"><font color=\"FFFFFF\">Roadmap</font></a><xsl:text> | </xsl:text><a href=\"/search/\"><font color=\"FFFFFF\">Site Search</font></a></td><td valign=\"top\" width=\"15%\" bgcolor=\"000099\"><img alt=\"HotDAML Logo\" border=\"0\" align=\"right\" src=\"../logos/HotDAML_Small.jpg\"/></td></tr></table>"); // standard daml banner - hard coded!!! XXX*/
	System.out.println("<h1>" + pageTitle + "</h1>");
	System.out.println("<p>");
	System.out.println("This page contains a list of refereed publications resulting from the DARPA DAML program.  All references are presented in IEEE format.  The IEEE formats can be found by going <a href=\"http://www.ieee.org/organizations/pubs/transactions/information.htm\">here</a> and clicking on 'Information for Authors' (see pages 4-5).  Please be aware that the numbers for each reference may change as new references are added.");
	System.out.println("</p>");
	System.out.println("<p>");
	System.out.println("Please send comments, corrections, and additions to <a href=\"mailto:webmaster@daml.org\">webmaster@daml.org</a>.");
	System.out.println("</p>");
	System.out.println("This page is also available as a <a href=\"http://tiger.isi.edu/webscripter/ws?reportURL=http://www.daml.org/publications/damlpublications.wsrd\">WebScripter report</a>.");
	System.out.println("<hr />");
	
	// XXX - sort by first author (or date?)   
	
    
	
	// print publications
	int counter = 1; // to keep track for the purpose of numbering the references
     	java.util.Iterator publicationIterator = publications.iterator();
	while (publicationIterator.hasNext())
	    {
		com.hp.hpl.mesa.rdf.jena.model.Resource publication = (com.hp.hpl.mesa.rdf.jena.model.Resource) publicationIterator.next();

		System.out.println("<br />[" + counter +"]");
		counter++;
		Object type = getOptionalLiteral(model, publication, 
						 com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type);
		
		if (type != null)
		    {
			if (type.toString().endsWith("#Article"))
			    {
				// Article in a journal
				Object url = getOptionalLiteral(model, publication, 
								bibtex.url);

				if (url != null)
				    {
				        // online format - article in a journal
					Object author = getOptionalLiteral(model, publication,
									   bibtex.author);
					if (author != null)
					    System.out.println(author + ".");
					
					Object month = getOptionalLiteral(model, publication,
									  bibtex.month);
					
					Object year = getOptionalLiteral(model, publication,
									 bibtex.year);
					
					if (month != null && year != null)
					    System.out.println("(" + year + ", " + month + ").");
					else if (year != null)
					    System.out.println("(" + year + ").");
					
					Object title = getOptionalLiteral(model, publication,
									  bibtex.title);
					if (title != null)
					    System.out.println(title + ".");
					
					Object journal = getOptionalLiteral(model, publication,
									    bibtex.journal);
					
					if (journal != null)
					    System.out.println("<i>" + journal + "</i>.");
					
					System.out.println("[Online].");
					
					Object volume = getOptionalLiteral(model, publication,
									   bibtex.volume);
					
					Object issue = getOptionalLiteral(model, publication,
									  bibtex.number);
					
					if (volume != null)
					    {
						System.out.println("<i>" + volume);
						if (issue != null)
						    System.out.println(" (" + issue + ")");
						System.out.println(",</i>");
					    }
					
					Object pages = getOptionalLiteral(model, publication,
									  bibtex.pages);
					
					if (pages != null)
					    System.out.println("pp. " + pages + ".");
					
					if (url != null)
					    System.out.println("Available: <a href=\"" + url + "\">" + url + "</a>");
					
				    }
				else
				    {
					// offline format - article in a journal
					Object author = getOptionalLiteral(model, publication,
									   bibtex.author);
					if (author != null)
					    System.out.println(author + ", ");

					Object title = getOptionalLiteral(model, publication,
									  bibtex.title);
					if (title != null)
					    System.out.println("\"" + title + ",\" ");

					Object journal = getOptionalLiteral(model, publication,
									    bibtex.journal);
					
					if (journal != null)
					    System.out.println("<i>" + journal + "</i>,");

					Object volume = getOptionalLiteral(model, publication,
									   bibtex.volume);

					if (volume != null)
					    System.out.println(volume + ", ");
					
					Object issue = getOptionalLiteral(model, publication,
									  bibtex.number);
					
					if (issue != null)
					    System.out.println(issue + ", ");

					Object pages = getOptionalLiteral(model, publication,
									  bibtex.pages);
					
					if (pages != null)
					    System.out.println("pp. " + pages + ", ");
						
					Object month = getOptionalLiteral(model, publication,
									  bibtex.month);
					
					if (month != null)
					    System.out.println(month + ". ");
					
					Object year = getOptionalLiteral(model, publication,
									 bibtex.year);
					
					if (year != null)
					    System.out.println(year + ".");
				    }
			    }
			if (type.toString().endsWith("#InProceedings"))
			    {
				// Paper at a conference
				Object url = getOptionalLiteral(model, publication, 
								bibtex.url);

				if (url != null)
				    {
					// online format - paper at a conference
					Object author = getOptionalLiteral(model, publication,
									   bibtex.author);
					
					if (author != null)
					    System.out.println(author + ".");
					
					Object month = getOptionalLiteral(model, publication,
									  bibtex.month);
					
					Object year = getOptionalLiteral(model, publication,
									 bibtex.year);
					
					if (month != null && year != null)
					    System.out.println("(" + year + ", " + month + ").");
					else if (year != null)
					    System.out.println("(" + year + ").");
					
					Object title = getOptionalLiteral(model, publication,
									  bibtex.title);
					if (title != null)
					    System.out.println(title + ".");
					
					Object conference = getOptionalLiteral(model, publication,
									       bibtex.booktitle);
					
					if (conference != null) // assumes only a title, no proc. of, etc.
					    System.out.println("Presented at " + conference + ".");
					
					System.out.println("[Online].");
					
					if (url != null)
					    System.out.println("Available: <a href=\"" + url + "\">" + url + "</a>");
				    }
				else
				    {
					// offline format - paper at a conference 
					// assume that the conference proceedings are published

					Object author = getOptionalLiteral(model, publication,
									   bibtex.author);
					
					if (author != null)
					    System.out.println(author + ", ");

					Object title = getOptionalLiteral(model, publication,
									  bibtex.title);
					if (title != null)
					    System.out.println("\"" + title + ",\" ");

					Object conference = getOptionalLiteral(model, publication,
									       bibtex.booktitle);
					
					if (conference != null) 
					    System.out.println("in <i>Proc. " +  conference + "</i>, ");

					Object year = getOptionalLiteral(model, publication,
									 bibtex.year);
					
					if (year != null)
					    System.out.println(year + ",");

					Object pages = getOptionalLiteral(model, publication, 
									  bibtex.pages);
					
					if (pages != null)
					    System.out.println(pages + ".");
				    }
					
			    }
			if (type.toString().endsWith("#Book"))
			    {
				// books
				Object url = getOptionalLiteral(model, publication, 
								bibtex.url);

				if (url != null)
				    {
					// online format - books
					
					Object author = getOptionalLiteral(model, publication,
									   bibtex.author);
					
					if (author != null)
					    System.out.println(author + ".");
					
					Object month = getOptionalLiteral(model, publication,
									  bibtex.month);
					
					Object year = getOptionalLiteral(model, publication,
									 bibtex.year);
					
					if (month != null && year != null)
					    System.out.println("(" + year + ", " + month + ").");
					else if (year != null)
					    System.out.println("(" + year + ").");
					
					Object booktitle = getOptionalLiteral(model, publication,
									      bibtex.booktitle);
					
					if (booktitle != null)
					    System.out.println("<i>" + booktitle + "</i>.");
					
					Object edition = getOptionalLiteral(model, publication, 
									    bibtex.edition);
					
					if (edition != null)
					    System.out.println("(" + edition + ")");		
					
					System.out.println("[Online].");
					
					Object volume = getOptionalLiteral(model, publication,
									   bibtex.volume);
					
					Object issue = getOptionalLiteral(model, publication,
									  bibtex.number);
					
					if (volume != null)
					    {
						System.out.println("<i>" + volume);
						if (issue != null)
						    System.out.println(" (" + issue + ")");
						System.out.println(",</i>");
					    }
					
					if (url != null)
					    System.out.println("Available: <a href=\"" + url + "\">" + url + "</a>");
				    }
				else
				    {
					// offline format - books
					
					Object author = getOptionalLiteral(model, publication,
									   bibtex.author);
					
					if (author != null)
					    System.out.println(author + ",");

					Object booktitle = getOptionalLiteral(model, publication,
									      bibtex.booktitle);
					
					if (booktitle != null)
					    System.out.println("<i>" + booktitle + "</i>,");

					Object edition = getOptionalLiteral(model, publication, 
									    bibtex.edition);
					
					if (edition != null)
					    System.out.println(edition + ",");	

					Object volume = getOptionalLiteral(model, publication,
									   bibtex.volume);
					
					if (volume != null)
					    System.out.println(volume + ",");
					
					Object editor = getOptionalLiteral(model, publication, 
									   bibtex.editor);
					
					if (editor != null)
					    System.out.println(editor + ", Ed.");
					
					Object location = getOptionalLiteral(model, publication, 
									     bibtex.location);
					
					if (location != null)
					    System.out.println(location + ":");
					
					Object publisher = getOptionalLiteral(model, publication,
									      bibtex.publisher);
					
					if (publisher != null)
					    System.out.println(publisher + ",");

					Object year = getOptionalLiteral(model, publication,
									 bibtex.year);
					
					if (year != null)
					    System.out.println(year + ",");
				    }
			    }
			if (type.toString().endsWith("#InBook"))
			    {
				// chapter in a book

				Object url = getOptionalLiteral(model, publication, 
								bibtex.url);
				
				if (url != null)
				    {
					//online format - chapter in a book
					
					Object author = getOptionalLiteral(model, publication,
									   bibtex.author);
					
					if (author != null)
					    System.out.println(author + ".");
					
					Object month = getOptionalLiteral(model, publication,
									  bibtex.month);
					
					Object year = getOptionalLiteral(model, publication,
									 bibtex.year);
					
					if (month != null && year != null)
					    System.out.println("(" + year + ", " + month + ").");
					else if (year != null)
					    System.out.println("(" + year + ").");
					
					Object chapter = getOptionalLiteral(model, publication,
									    bibtex.chapter);
					Object booktitle = getOptionalLiteral(model, publication,
									      bibtex.booktitle);
					
					if (chapter != null)
					    {
						System.out.println("\"" + chapter + ",\"");
						if (booktitle != null)
						    {
							System.out.println("in <i>" + booktitle + "</i>.");  
						    }
					    }
					else if (booktitle != null)
					    System.out.println("<i>" + booktitle + "</i>.");
					
					Object edition = getOptionalLiteral(model, publication, 
									    bibtex.edition);
					
					if (edition != null)
					    System.out.println("(" + edition + ")");		
					
					System.out.println("[Online].");
					
					Object volume = getOptionalLiteral(model, publication,
									   bibtex.volume);
					
					Object issue = getOptionalLiteral(model, publication,
									  bibtex.number);
					
					if (volume != null)
					    {
						System.out.println("<i>" + volume);
						if (issue != null)
						    System.out.println(" (" + issue + ")");
						System.out.println(",</i>");
					    }
					
					if (url != null)
					    System.out.println("Available: <a href=\"" + url + "\">" + url + "</a>");
				    }
				else
				    {
					// offline format - books
					
					Object author = getOptionalLiteral(model, publication,
									   bibtex.author);
										
					if (author != null)
					    System.out.println(author + ",");

					Object title = getOptionalLiteral(model, publication, 
									  bibtex.title);
					
					if (title != null)
					    System.out.println("\"" + title + ",\" in ");

					Object booktitle = getOptionalLiteral(model, publication,
									      bibtex.booktitle);
					
					if (booktitle != null)
					    System.out.println("<i>" + booktitle + "</i>,");

					Object edition = getOptionalLiteral(model, publication, 
									    bibtex.edition);
					
					if (edition != null)
					    System.out.println(edition + ",");	

					Object volume = getOptionalLiteral(model, publication,
									   bibtex.volume);
					
					if (volume != null)
					    System.out.println(volume + ",");
					
					Object editor = getOptionalLiteral(model, publication, 
									   bibtex.editor);
					
					if (editor != null)
					    System.out.println(editor + ", Ed.");
					
					Object location = getOptionalLiteral(model, publication, 
									     bibtex.location);
					
					if (location != null)
					    System.out.println(location + ":");
					
					Object publisher = getOptionalLiteral(model, publication,
									      bibtex.publisher);
					
					if (publisher != null)
					    System.out.println(publisher + ",");

					Object year = getOptionalLiteral(model, publication,
									 bibtex.year);
					
					if (year != null)
					    System.out.println(year + ",");
					
					Object pages = getOptionalLiteral(model, publication, 
									  bibtex.pages);
					
					if (pages != null)
					    System.out.println(pages);
				    }
			    }
			if (type.toString().endsWith("#TechReport"))
			    {
				// technical reports

				Object url = getOptionalLiteral(model, publication, 
								bibtex.url);
				
				if (url != null)
				    {
					//online format - technical reports
					
					Object author = getOptionalLiteral(model, publication,
									   bibtex.author);
					
					if (author != null)
					    System.out.println(author + ".");
					
					Object month = getOptionalLiteral(model, publication,
									  bibtex.month);
					
					Object year = getOptionalLiteral(model, publication,
									 bibtex.year);
					
					if (month != null && year != null)
					    System.out.println("(" + year + ", " + month + ").");
					else if (year != null)
					    System.out.println("(" + year + ").");
					
					Object title = getOptionalLiteral(model, publication,
									  bibtex.title);
					if (title != null)
					    System.out.println(title + ".");
					
					Object organization = getOptionalLiteral(model, publication, 
										 bibtex.organization);
					
					if (organization != null)
					    System.out.println(organization + ".");
					
					Object location = getOptionalLiteral(model, publication,
									     bibtex.location);
					
					if (location != null)
					    System.out.println(location + ".");
					
					System.out.println("[Online].");
					
					if (url != null)
					    System.out.println("Available: <a href=\"" + url + "\">" + url + "</a>");
				    }
				else
				    {
					//offline format - technical reports
					
					Object author = getOptionalLiteral(model, publication,
									   bibtex.author);
					
					if (author != null)
					    System.out.println(author + ",");

					Object title = getOptionalLiteral(model, publication,
									  bibtex.title);
					if (title != null)
					    System.out.println("\"" + title + ",\"");

					Object organization = getOptionalLiteral(model, publication, 
										 bibtex.organization);
					
					if (organization != null)
					    System.out.println(organization + ",");

					Object location = getOptionalLiteral(model, publication,
									     bibtex.location);
					
					if (location != null)
					    System.out.println(location + ",");

					Object reportTitle = getOptionalLiteral(model, publication,
									  bibtex.booktitle);
					if (title != null)
					    System.out.println(reportTitle + ".");

					Object month = getOptionalLiteral(model, publication,
									  bibtex.month);
					
					Object year = getOptionalLiteral(model, publication,
									 bibtex.year);
					
					if (month != null && year != null)
					    System.out.println("(" + year + ", " + month + ").");
					else if (year != null)
					    System.out.println("(" + year + ").");
				    }
			    }
		    }
	    }
	
	System.out.println("<hr />");
	System.out.println("<h2>Colophon</h2>");
	System.out.println("<p>");
	System.out.println("This page was generated from the following DAML files using <a href=\"genhtml.java\">genhtml.java</a>:");
	System.out.println("<ul>");
	for (int i = 0; i < args.length; i++)
	    {
		String arg = args[i];
		System.out.println("<li><a href=\"" + arg + "\">" + arg + "</a>");
	    }
	System.out.println("</ul>");
	System.out.println("</p>");
    }
}

