// ==========================================================================
// Author:  Yee Hsu
// Date:    3/10/2007
// File:    xmlparser.java
//
// Desc:    Demonstrates parsing of XML file in Java
// ==========================================================================
package com.company.xmlparser;

import com.company.util. * ;
import java.io. * ;
import java.util. * ;

public class XmlParserImpl implements CXmlParser {

    private String sLastUpdateTime;
    private String sTimeStamp;

    private Map < Integer,
    CUpdate > m_mapUpdate;
    //private Map<String, Map<Integer, CUpdate>> m_mapEndPoint;  
    public int Init(final String sLicense) {
        return 1;
    }

    public boolean ParseXMLFile(final String sFile) {

        try {

            this.m_mapUpdate = new HashMap < Integer,
            CUpdate > ();
            //this.m_mapEndPoint  = new HashMap<String, Map<Integer, CUpdate>>();
            // Setup XML Parsing
            InputStream xmlInputStream = new FileInputStream(sFile);
            XmlTree xmlDirectivesTree = new XmlTree(xmlInputStream, true);

            // Get Root info
            //Attempt to find the name attribute of the application
            XmlNode nLastUpdateTime = (XmlNode) xmlDirectivesTree.getRoot().getFirstChild().getFirstChild(new XmlNode("LastUpdateTime", XmlNode.Type.ATTRIBUTE, ""));
            XmlNode nTimeStamp = (XmlNode) xmlDirectivesTree.getRoot().getFirstChild().getFirstChild(new XmlNode("TimeStamp", XmlNode.Type.ATTRIBUTE, ""));

            sLastUpdateTime = nLastUpdateTime.getValue();
            sTimeStamp = nTimeStamp.getValue();

            XmlNode Vendors = (XmlNode) xmlDirectivesTree.getRoot().getFirstChild().getFirstChild(new XmlNode("Vendor", XmlNode.Type.ELEMENT, null));

            while (Vendors != null) {

                XmlNode VendorNode = this.GetAttriInfo(Vendors, "Name");
                String sVendorName = VendorNode.getValue();

                if (!this.GetEPUpdate(Vendors, sVendorName, "Node")) return false;

                Vendors = (XmlNode) xmlDirectivesTree.getRoot().getFirstChild().getNextChild(new XmlNode("Vendor", XmlNode.Type.ELEMENT, null), Vendors);
            }

        } catch(Exception e) {
            //System.err.println("Exception caught: " + e.getMessage());
            //e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean GetEPUpdate(XmlNode nodeVendor, final String sVendorName, final String sElementName) {

        try {

            XmlNode nodeEndPoint = this.GetChildInfo(nodeVendor, sElementName);
            XmlNode nodeUpdate = this.GetChildInfo(nodeEndPoint, "Next");

            while (nodeUpdate != null) {

                CUpdate objUpdate = new CUpdate();
                objUpdate.sVendorName = sVendorName;
                objUpdate.mUpdateHistory = new HashMap < Integer,
                DefinitionUpdate > ();

                XmlNode nodeOS = this.GetChildInfo(nodeUpdate, "OS");
                XmlNode nodeHist = (XmlNode) nodeUpdate.getNextChild(nodeOS);
                XmlNode nextHist = this.GetChildInfo(nodeHist, "History");

                objUpdate.sUpdateType = this.GetAttriInfo(nodeUpdate, "Type").getValue();
                objUpdate.nUpdateID = Integer.parseInt(this.GetAttriInfo(nodeUpdate, "ID").getValue());
                objUpdate.nOSType = Integer.parseInt(this.GetAttriInfo(nodeOS, "Type").getValue());

                while (nextHist != null) {

                    DefinitionUpdate objDefHistory = new DefinitionUpdate();

                    objDefHistory.nHistoryId = Integer.parseInt(this.GetAttriInfo(nextHist, "ID").getValue());
                    objDefHistory.sTimeStamp = this.GetAttriInfo(nextHist, "TimeStamp").getValue();
                    objDefHistory.sDefDate = this.GetChildInfo(nextHist, "Date").getValue();
                    objDefHistory.sDefVersion = this.GetChildInfo(nextHist, "Version").getValue();

                    objUpdate.mUpdateHistory.put(objDefHistory.nHistoryId, objDefHistory);

                    nextHist = (XmlNode) nodeHist.getNextChild(new XmlNode("History", XmlNode.Type.ELEMENT, null), nextHist);
                }
                this.m_mapUpdate.put(objUpdate.nUpdateID, objUpdate);
                //this.m_mapEndPoint.put(objUpdate.sVendorName, this.m_mapUpdate);
                nodeUpdate = (XmlNode) nodeEndPoint.getNextChild(new XmlNode("Next", XmlNode.Type.ELEMENT, null), nodeUpdate);
            }

        } catch(Exception e) {
            //System.err.println("Exception caught: " + e.getMessage());
            //e.printStackTrace();
            return false;
        }
        return true;
    }

    public XmlNode GetChildInfo(XmlNode node, final String sChildName) {
        return (XmlNode) node.getFirstChild(new XmlNode(sChildName, XmlNode.Type.ELEMENT, null));
    }

    public XmlNode GetAttriInfo(XmlNode node, final String sAttribName) {
        return (XmlNode) node.getFirstChild(new XmlNode(sAttribName, XmlNode.Type.ATTRIBUTE, ""));
    }

    public Calendar GetGregorianCalendar(final String sDefDate) {

        String[] sDate = sDefDate.split("/");

        int nMon = Integer.parseInt(sDate[0]) - 1;
        int nDay = Integer.parseInt(sDate[1]);
        int nYear = Integer.parseInt(sDate[2]);

        return new GregorianCalendar(nYear, nMon, nDay);
    }

}