/*
 * 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.ChargePointEntry;
import structures.Constants;


/* 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 ChargePointReader {

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

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

    private void ExtractChargePoints(Document doc) {
        try {
            NodeList listChargePoints = doc.getElementsByTagName(Constants.XML_CHARGEPOINTS);
            Node sections = listChargePoints.item(0);
            if (sections.getNodeType() == Node.ELEMENT_NODE) {
                Element singleSection = (Element) sections;
                NodeList listChargePointSet = singleSection.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT);
                for (int i = 0; i < listChargePointSet.getLength(); i++) {
                    Node subSections = listChargePointSet.item(i);
                    if (subSections.getNodeType() == Node.ELEMENT_NODE) {
                        Element singleSubSection = (Element) subSections;
                        NodeList idList = singleSubSection.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_ID);
                        String id = ExtractChargePointInfo(idList);
                        NodeList typeList = singleSubSection.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_TYPE);
                        String type = ExtractChargePointInfo(typeList);
                        NodeList latList = singleSubSection.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_LAT);
                        String strlat = ExtractChargePointInfo(latList);
                        NodeList lonList = singleSubSection.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_LON);
                        String strlon = ExtractChargePointInfo(lonList);
                        NodeList priceList = singleSubSection.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_PRICE);
                        String price = ExtractChargePointInfo(priceList);
                        NodeList seqList = singleSubSection.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_SEQUENCE);
                        String seq = ExtractChargePointInfo(seqList);

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

                        ChargePointEntry entry = new ChargePointEntry(id, type, lat, lon, price, seq);

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

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

}
