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:       builder = factory.newDocumentBuilder();
030:    }
031: 
032:    /**
033:       Parses an XML file containing an item list
034:       @param fileName the name of the file
035:       @return an array list containing all items in the XML file
036:    */
037:    public ArrayList parse(String fileName) 
038:       throws SAXException, IOException
039:    {
040:       File f = new File(fileName);
041:       Document doc = builder.parse(f);
042: 
043:       // get the <items> root element
044: 
045:       Element root = doc.getDocumentElement(); 
046:       return getItems(root);
047:    }
048: 
049:    /**
050:       Obtains an array list of items from a DOM element
051:       @param e an <items> element 
052:       @return an array list of all <item> children of e
053:    */
054:    private static ArrayList getItems(Element e)
055:    {
056:       ArrayList items = new ArrayList();
057: 
058:       // get the <item> children
059: 
060:       NodeList children = e.getChildNodes();
061:       for (int i = 0; i < children.getLength(); i++)
062:       {
063:          Node childNode = children.item(i);
064:          if (childNode instanceof Element)
065:          {
066:             Element childElement = (Element)childNode;
067:             if (childElement.getTagName().equals("item"))
068:             {
069:                Item c = getItem(childElement);
070:                items.add(c);
071:             }
072:          }
073:       }
074:       return items;
075:    }
076: 
077:    /**
078:       Obtains an item from a DOM element
079:       @param e an <item> element 
080:       @return the item described by the given element
081:    */
082:    private static Item getItem(Element e)
083:    {
084:       NodeList children = e.getChildNodes();
085:       Product p = null;
086:       int quantity = 0;
087:       for (int j = 0; j < children.getLength(); j++)
088:       {
089:          Node childNode = children.item(j);
090:          if (childNode instanceof Element)
091:          {
092:             Element childElement = (Element)childNode;
093:             String tagName = childElement.getTagName();
094:             if (tagName.equals("product"))
095:                p = getProduct(childElement);
096:             else if (tagName.equals("quantity"))
097:             {
098:                Text textNode = (Text)childElement.getFirstChild();  
099:                String data = textNode.getData();
100:                quantity = Integer.parseInt(data);
101:             }
102:          }
103:       }
104:       return new Item(p, quantity);
105:    }
106: 
107:    /**
108:       Obtains a product from a DOM element
109:       @param e a <product> element 
110:       @return the product described by the given element
111:    */
112:    private static Product getProduct(Element e)
113:    {
114:       NodeList children = e.getChildNodes();
115:       String name = "";
116:       double price = 0;
117:       for (int j = 0; j < children.getLength(); j++)
118:       {
119:          Node childNode = children.item(j);
120:          if (childNode instanceof Element)
121:          {
122:             Element childElement = (Element)childNode;
123:             String tagName = childElement.getTagName();
124:             Text textNode = (Text)childElement.getFirstChild();  
125: 
126:             String data = textNode.getData();
127:             if (tagName.equals("description"))
128:                name = data;
129:             else if (tagName.equals("price"))
130:                price = Double.parseDouble(data);
131:          }
132:       }
133:       return new Product(name, price);
134:    }
135: 
136:    private DocumentBuilder builder;
137: }
138: 
139: 
140: 
141: 
142: 
143: 
144: 
145: 
146: