
package DOM;
import java.io.*;
import java.security.AllPermission;

import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;

/**
 * @author Administrateur
 *
 */


public class Parser {

	public PrintWriter out = null;

	//Rem changer Package par ModelElement
	public void parse(String _fichier)
		throws SAXException, ParserConfigurationException, IOException {

		// Charger le document
		FileInputStream _xml_input_file = new FileInputStream(_fichier);

		parse(_xml_input_file);
	}

	public void parse(InputStream _xml_input_file)
		throws SAXException, ParserConfigurationException, IOException {
		
		//creation du fichier output
		out = new PrintWriter( 
		new FileOutputStream("./output.html"));
		out.println("<html xmlns:fo=\"http://www.w3.org/1999/XSL/Format\"><head />");
		out.println("<body>");
		out.println("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><h1 align=\"left\">Domaines </h1>");

		//instancier le contructeur de parseurs
		DocumentBuilderFactory _factory = DocumentBuilderFactory.newInstance();

		//ignorer les commentaires dans les fichiers XML parsees
		_factory.setIgnoringComments(true);
		
		// creer un parseur
		DocumentBuilder _builder = _factory.newDocumentBuilder();

		// Charger le document
		Document doc = _builder.parse(_xml_input_file);

		// Parser le document
		Element library = doc.getDocumentElement();
		NodeList allChilds = library.getChildNodes();
		
		for (int i = 0; i < allChilds.getLength() ; i++) {
			Node node = allChilds.item(i);
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				Element elt = (Element) node;
				Element title = (Element) elt.getElementsByTagName("title").item(0);
				Node text = title.getFirstChild();
				String titre = text.getNodeValue();
				out.print("<a href=#" + titre + ">" + titre + "</a> <br/>");
			}
		}
		
		for (int i = 0; i < allChilds.getLength() ; i++) {
			Node node = allChilds.item(i);
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				Element elt = (Element) node;
				Element title = (Element) elt.getElementsByTagName("title").item(0) ;
				Node text = title.getFirstChild() ;
				String titre = text.getNodeValue();
				DomaineTemplate(elt, titre);
			}
		}
		out.close();
		out.flush();
	}

	public void DomaineTemplate(Element domaine, String name) {
		out.println("<table width=\"100%\" border=\"1\">");
		out.println("	<tr>");
		out.println("   	<td width=\"100%\" bgcolor=\"#C0C0C0\">");	
		out.println("			<h2><a name=" + name + ">" + name + "</a></h2>");
		out.println("		</td>");
		out.println("	</tr>");
		out.println("</table>");
		
		NodeList filsDomaine = domaine.getChildNodes();
		for(int i = 0; i <filsDomaine.getLength() ; i++) {
			Node node =filsDomaine .item(i);
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				Element elt = (Element) node;
				if (elt.getNodeName().equals("bib_ref")){
					out.println("<table width=\"100%\" border=\"1\">");
					out.println("	<tr>");
					out.println(" Annee : " + elt.getElementsByTagName("year").item(0).getTextContent());
					out.println("<br/>");
					out.println("<h3> Titre : " + elt.getElementsByTagName("title").item(0).getTextContent() + "</h3>");
					out.println(" Auteur(s) : "+ elt.getElementsByTagName("author").item(0).getTextContent());
					out.println("<br/>");
					out.println(" Lien : "+ elt.getElementsByTagName("weblink").item(0).getTextContent());
					out.println("	</tr>");
					out.println("</table>");
				}
			}
		}

	}
}
