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

/**
 * An address book Model - MVC pattern
 *    
 * Open an XML File and read entire file into a DOM
 * Retrieve all <entry>'s
 * Retrieve each element within <entry>
 * For every element, retrieve the text content
 * Compare partial lastname, ignoring case 
 * If match, store in AddrBookEntry array  
 */

public class AddrBookModelXML implements AddrBookModel
{
    private AddrBookControl control;		    	//A reference to the control object
    private AddrBookEntry[] found;                 	//An array of address book entry objects matching search

    private Element   root, entry, element;
    private Text      text;
    private NodeList  entries, nodeList;

    private String    lastname, firstname, street, city, state, zip, phone;
    private int j = 0;


    AddrBookModelXML(AddrBookControl c)			  //constructor
    {
    	this.control = c;
    }

    public AddrBookEntry[] search(String searchLastname)         //search method                           
    {                                                             //returns array of matching entries        
	Document doc = null;		

	try
	{
	    InputSource input = new InputSource("zAddrBook.xml");

            found = new AddrBookEntry[100];
            int j = 0;

	    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();			
	    DocumentBuilder DOM         = fact.newDocumentBuilder();
	    doc                         = DOM.parse(input);

	    root  = doc.getDocumentElement();

	    entries = root.getElementsByTagName("entry");

	    for (int i=0; i < entries.getLength(); i++)
	    {			
	        entry = (Element) entries.item(i);			//get ref to an entry

	        nodeList  = entry.getElementsByTagName("lastname");	//get lastname nodes, returns a nodeList
	        element   = (Element) nodeList.item(0);	                //get lastname node
//              lastname  = element.getTextContent();			//get text value of this element and children
	        text      = (Text) element.getFirstChild(); 		//get text node
	        lastname  = text.getNodeValue();			//get the text value 

	        nodeList  = entry.getElementsByTagName("firstname");	
	        element   = (Element) nodeList.item(0);	                        
	        text      = (Text) element.getFirstChild(); 		
	        firstname = text.getNodeValue();				

	        nodeList  = entry.getElementsByTagName("street");	
	        element   = (Element) nodeList.item(0);	                        
	        text      = (Text) element.getFirstChild(); 		
	        street    = text.getNodeValue();				

	        nodeList  = entry.getElementsByTagName("city");	
	        element   = (Element) nodeList.item(0);	                        
	        text      = (Text) element.getFirstChild(); 		
	        city      = text.getNodeValue();				

	        nodeList  = entry.getElementsByTagName("state");	
	        element   = (Element) nodeList.item(0);	                        
	        text      = (Text) element.getFirstChild(); 		
	        state     = text.getNodeValue();				

	        nodeList  = entry.getElementsByTagName("zip");	
	        element   = (Element) nodeList.item(0);	                        
	        text      = (Text) element.getFirstChild(); 		
	        zip       = text.getNodeValue();				

	        nodeList  = entry.getElementsByTagName("phone");	
	        element   = (Element) nodeList.item(0);	                        
	        text      = (Text) element.getFirstChild(); 		
	        phone     = text.getNodeValue();				

                String XMLlastname = lastname.toLowerCase();            //convert to lowercase
                searchLastname     = searchLastname.toLowerCase();

                if (XMLlastname.indexOf(searchLastname) == 0)           //if match
                {
                    AddrBookEntry e = new AddrBookEntry(lastname,firstname,street,city,state,zip,phone);  //create an address book entry object

                    found[j++] = e;                     		//save object in the array
                }
	    }
	}		
	catch(Exception e)	//catch ParserConfigurationException, SAXException, IOException
	{
	    System.err.println(e);
            e.printStackTrace();
	}
	finally				//in either case (success or failure)
	{
	    return (found);
	}
    }
}