001: import java.io.File;
002: import java.io.IOException;
003: import java.util.ArrayList;
004: import javax.xml.parsers.DocumentBuilder;
005: import javax.xml.parsers.DocumentBuilderFactory;
006: import javax.xml.parsers.ParserConfigurationException;
007: import org.w3c.dom.Attr;
008: import org.w3c.dom.Document;
009: import org.w3c.dom.Element;
010: import org.w3c.dom.NamedNodeMap;
011: import org.w3c.dom.Node;
012: import org.w3c.dom.NodeList;
013: import org.w3c.dom.Text;
014: import org.xml.sax.SAXException;
015: 
016: /**
017:    An XML parser for item lists
018: */
019: public class ItemListParser
020: {
021:    /**
022:       Constructs a parser that can parse item lists
023:    */
024:    public ItemListParser() 
025:       throws ParserConfigurationException
026:    {
027:       DocumentBuilderFactory factory 
028:          = DocumentBuilderFactory.newInstance();
029:       factory.setValidating(true);
030:       factory.setIgnoringElementContentWhitespace(true);
031:       builder = factory.newDocumentBuilder();
032:    }
033: 
034:    /**
035:       Parses an XML file containing an item list
036:       @param fileName the name of the file
037:       @return an array list containing all items in the XML file
038:    */
039:    public ArrayList parse(String fileName) 
040:       throws SAXException, IOException
041:    {
042:       File f = new File(fileName);
043:       Document doc = builder.parse(f);
044: 
045:       // get the <items> root element
046: 
047:       Element root = doc.getDocumentElement(); 
048:       return getItems(root);
049:    }
050: 
051:    /**
052:       Obtains an array list of items from a DOM element
053:       @param e an <items> element 
054:       @return an array list of all <item> children of e
055:    */
056:    private static ArrayList getItems(Element e)
057:    {
058:       ArrayList items = new ArrayList();
059: 
060:       // get the <item> children
061: 
062:       NodeList children = e.getChildNodes();
063:       for (int i = 0; i < children.getLength(); i++)
064:       {
065:          Element childElement = (Element)children.item(i);
066:          Item c = getItem(childElement);
067:          items.add(c);
068:       }
069:       return items;
070:    }
071: 
072:    /**
073:       Obtains an item from a DOM element
074:       @param e an <item> element 
075:       @return the item described by the given element
076:    */
077:    private static Item getItem(Element e)
078:    {
079:       NodeList children = e.getChildNodes();
080: 
081:       Product p = getProduct((Element)children.item(0));
082: 
083:       Element quantityElement = (Element)children.item(1);
084:       Text quantityText 
085:          = (Text)quantityElement.getFirstChild();  
086:       int quantity = Integer.parseInt(quantityText.getData());
087: 
088:       return new Item(p, quantity);
089:    }
090: 
091:    /**
092:       Obtains a product from a DOM element
093:       @param e a <product> element 
094:       @return the product described by the given element
095:    */
096:    private static Product getProduct(Element e)
097:    {
098:       NodeList children = e.getChildNodes();
099: 
100:       Element descriptionElement = (Element)children.item(1);
101:       Text descriptionText 
102:          = (Text)descriptionElement.getFirstChild();  
103:       String description = descriptionText.getData();
104: 
105:       Element priceElement = (Element)children.item(1);
106:       Text priceText 
107:          = (Text)priceElement.getFirstChild();  
108:       double price = Double.parseDouble(priceText.getData());
109: 
110:       return new Product(description, price);
111:    }
112: 
113:    private DocumentBuilder builder;
114: }