/**
 * Class Sentence
 * The class in charge of converting a property name or an attribute name and
 * value into a short English sentence, which is eventually printed.
 * Basically, an instance of this class parses the RDF schema when being
 * created, and is then able to give the english sentence corresponding to
 * any given property name.
 * Thus, new properties should be added in the RDF Schema file itself.
 * (axsvg-schema.rdf)
 * while new attributes should be added directly in the ListOfAttributeNames
 * class code, for the moment. (Should be read from the RDF Schema in the
 * future, as well as for properties).
 **/

package axsvg;

import java.io.*;
import fr.dyade.koala.dom.*;
import fr.dyade.koala.dom.util.*;
import org.w3c.dom.*;
import java.util.*;


public class Sentence {

    private final Hashtable translator;
    
    /**
     * Method Sentence
     * The constructor of the Class.
     * @param the name of the file which contains the RDF Schema
     * corresponding to the axsvg namespace.
     * It parses this file and explores the resulting DOM to build
     * the hashtable translator. This table admits the property names
     * of the axsvg vocabulary as keys, and the corresponding English
     * short sentence as values (the string within the <rdfs:comment> tag in
     * the RDF Schema file).
     **/
    public Sentence(String schema) throws Exception {
	translator = new Hashtable();
	Node node;

	// Sets the sax parser property to use the xerces parser
       System.setProperty("org.xml.sax.driver",
			  "org.apache.xerces.parsers.SAXParser");
 
       // Opens a 'doc.xml' file located in the current directory
       String uri = new File(".").toURL().toString() + schema;
       DOMImplementation impl=GenericDOMImplementation.getDOMImplementation();
       Document doc = new DocumentFactory(impl).createDocument(
		"http://www.w3.org/1999/02/22-rdf-syntax-ns#","rdf:RDF",uri);
 
       // The document can now be used through the DOM API.
       // ...

       node = doc.getDocumentElement();
       node = node.getFirstChild();
       while(node != null) {
	   if(node.getNodeName().equalsIgnoreCase("rdf:Property")) {
	       String id = ((Element)node).getAttribute("ID");
	       String comment="-no associated sentence-";
	       Node child = node.getFirstChild();
	       while(child != null) {
		   if(child.getNodeName().equalsIgnoreCase("rdfs:comment")) {
		       comment = ((Text)child.getFirstChild()).getData();
		   }
		   child = child.getNextSibling();
	       }
	       translator.put(id, comment);
	   }
	   node = node.getNextSibling();
       }
    }
	

    /**
     * Method getTranslator
     * @return the translator attribute of the calling object.
     **/
    public Hashtable getTranslator() {
	return(translator);
    }
    
       


    /**
     * Method print
     * @param the name of the property to be turned into English
     **/
    public boolean print(String word) {
	word = word.substring(word.indexOf(":")+1);
	String meaning = (String)translator.get(word);
	if(meaning.endsWith("(by value)")) {
	    meaning = meaning.substring(0, meaning.indexOf("(by value)")-1);
	    System.out.print(meaning);
	    return(false);
	}
	else {
	   System.out.print(meaning);
	   return(true);
	}
    }


    /**
     * method print
     * @param the property node whom the attributes will be
     * presented in English (names and meanings of such attributes
     * to be checked are fetched in the class ListOfAttributeNames)
     **/
    public void print(Element elt) {
	String attrValue;
	ListOfAttributeNames attributeNames = new ListOfAttributeNames();
	String name = attributeNames.getNextAttributeName();

	while(name != null) {
	    attrValue = elt.getAttribute(name);
	    if(!attrValue.equalsIgnoreCase("")) {
		System.out.print(", "+attributeNames.getMeaning(name)+" "
				 +attrValue);
	    }
	    name = attributeNames.getNextAttributeName();
	}

	System.out.println(".");
	return;
    }
	
}
	
