package biometric;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReader {

 String xmlFailas;
  DocumentBuilderFactory dbf;
  DocumentBuilder db;
  Document doc;

 public XMLReader(String xmlFailas) {
  this.xmlFailas = xmlFailas;
  try {
  File file = new File( xmlFailas );
  dbf = DocumentBuilderFactory.newInstance();
  db = dbf.newDocumentBuilder();
  doc = db.parse(file);
  doc.getDocumentElement().normalize();
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
 public java.util.HashMap<String, Biometric> getBiometricsByTagNames(String valueTagName, String keyTagName )
 {
  boolean test = true;
  java.util.HashMap<String, Biometric> v = new java.util.HashMap<String, Biometric>();
  NodeList nodeLst = doc.getElementsByTagName(valueTagName);
  System.out.println("Information of all " + valueTagName + "\tlength = " + nodeLst.getLength() );

  for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);
    
    if (fstNode.getNodeType() == Node.ELEMENT_NODE){  
	Element fstElmnt = (Element) fstNode;
	NodeList secElmntLst = fstElmnt.getElementsByTagName(keyTagName);
	String	key = ((Element) secElmntLst.item(0)).getAttribute("name"),
		value = fstElmnt.getAttribute("name"),
		modality = fstElmnt.getAttribute("modality");
	if ( test ) System.out.println( key  + "\t" + value );
	if ( v.containsKey( key ) ) System.err.println("Pakartotinis raktas:\t" + key + "\tvalue:\t" + value );
  	v.put( key, new Biometric( key, value, modality ) );
    }

  }
  return v;
 }

 public static void main(String argv[])
 {
  XMLReader xml = new XMLReader( (argv.length < 1 ) ? "FVC2002_DB1_A.xml" : argv[0] );
  java.util.HashMap biometricMap = xml.getBiometricsByTagNames( "complex-biometric-signature", "complex-presentation");
 }
} 