/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlpackage;
import database.Context;
import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import structures.Constants;
import structures.RoadSignEntry;


/* class RoadSignRead
 * Parses the road sign xml file and puts the signs into the road sign hash
 * table using the key: lat;lon;heading;priority
 */
public class RoadSignReader {

    public void ExtractFromFile(File xmlFile) {
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(xmlFile);
            ExtractRoadSigns(doc);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void ExtractRoadSigns(Document doc) {
        try {
            NodeList listRoadSigns = doc.getElementsByTagName(Constants.XML_ROADSIGNS);
            Node sections = listRoadSigns.item(0);
            if (sections.getNodeType() == Node.ELEMENT_NODE) {
                Element singleSection = (Element) sections;
                NodeList listRoadSignSet = singleSection.getElementsByTagName(Constants.XML_ROADSIGNS_SIGN);
                for (int i = 0; i < listRoadSignSet.getLength(); i++) {
                    Node subSections = listRoadSignSet.item(i);
                    if (subSections.getNodeType() == Node.ELEMENT_NODE) {
                        Element singleSubSection = (Element) subSections;
                        NodeList typeList = singleSubSection.getElementsByTagName(Constants.XML_ROADSIGNS_SIGN_TYPE);
                        String type = ExtractRoadSignInfo(typeList);
                        NodeList headingList = singleSubSection.getElementsByTagName(Constants.XML_ROADSIGNS_SIGN_HEADING);
                        String heading = ExtractRoadSignInfo(headingList);
                        NodeList bearingList = singleSubSection.getElementsByTagName(Constants.XML_ROADSIGNS_SIGN_BEARING);
                        String bearing = ExtractRoadSignInfo(bearingList);
                        NodeList latList = singleSubSection.getElementsByTagName(Constants.XML_ROADSIGNS_SIGN_LAT);
                        String strlat = ExtractRoadSignInfo(latList);
                        NodeList lonList = singleSubSection.getElementsByTagName(Constants.XML_ROADSIGNS_SIGN_LON);
                        String strlon = ExtractRoadSignInfo(lonList);
                        NodeList priorityList = singleSubSection.getElementsByTagName(Constants.XML_ROADSIGNS_SIGN_PRIORITY);
                        String priority = ExtractRoadSignInfo(priorityList);
                        NodeList speedLimitList = singleSubSection.getElementsByTagName(Constants.XML_ROADSIGNS_SIGN_SPEEDLIMIT);
                        String speedLimit = ExtractRoadSignInfo(speedLimitList);


                        double lat = Double.parseDouble(strlat);
                        double lon = Double.parseDouble(strlon);

                        //lat = ((double)((int)(lat *10000)))/10000.0;
                        //lon = ((double)((int)(lon *10000)))/10000.0;

                        RoadSignEntry entry = new RoadSignEntry(type, lat, lon, heading, bearing, priority, speedLimit, null);
                        //Put signs in a HASHTABLE
                        
                        //String key = lat+ ";" + lon+";"+heading+";"+priority;
                        //Context.roadSignHash.put(key, entry);

                        //Put signs in a vector
                        Context.staticRoadSignVector.add(entry);
                    }
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    private String ExtractRoadSignInfo(NodeList typeList) {
        if (typeList.getLength() > 0) {
            Element termname = (Element) typeList.item(0);
            NodeList type = termname.getChildNodes();
            return ((Node) type.item(0)).getNodeValue();
        }
        return null;
    }

}
