package de.vogella.xml.stax.read;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import de.vogella.xml.stax.model.Item;

public class StaXParser {
	static final String DATE = "date";
	static final String ITEM = "item";
	static final String MODE = "mode";
	static final String UNIT = "unit";
	static final String CURRENT = "current";
	static final String INTERACTIVE = "interactive";

	@SuppressWarnings({ "unchecked", "null" })
	public List<Item>  readConfig(String configFile) {
		List<Item> items = new ArrayList<Item>();
		try {
			// First create a new XMLInputFactory
			XMLInputFactory inputFactory = XMLInputFactory.newInstance();
			// Setup a new eventReader
			InputStream in = new FileInputStream(configFile);
			XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
			// Read the XML document
			Item item = null;
			
			while (eventReader.hasNext()) {
				XMLEvent event = eventReader.nextEvent();

				if (event.isStartElement()) {
					StartElement startElement = event.asStartElement();
					// If we have a item element we create a new item
					if (startElement.getName().getLocalPart() == (ITEM)) {
						item = new Item();
						// We read the attributes from this tag and add the date attribute to our object
						Iterator<Attribute> attributes = startElement
								.getAttributes();
						while (attributes.hasNext()) {
							Attribute attribute = attributes.next();
							if (attribute.getName().toString().equals(DATE));
							item.setDate(attribute.getValue());
						}
					}

					if (event.isStartElement()) {
						if (event.asStartElement().getName().getLocalPart()
								.equals(MODE)) {
							event = eventReader.nextEvent();
							item.setMode(event.asCharacters().getData());
							continue;
						}
					}
					if (event.asStartElement().getName().getLocalPart().equals(UNIT)) {
						event = eventReader.nextEvent();
						item.setUnit(event.asCharacters().getData());
						continue;
					}

					if (event.asStartElement().getName().getLocalPart().equals(CURRENT)) {
						event = eventReader.nextEvent();
						item.setCurrent(event.asCharacters().getData());
						continue;
					}

					if (event.asStartElement().getName().getLocalPart().equals(INTERACTIVE)) {
						event = eventReader.nextEvent();
						item.setInteractive(event.asCharacters().getData());
						continue;
					}
				}
				// If we reach the end of an item element we add it to the list
				if (event.isEndElement()){
					EndElement endElement = event.asEndElement();
					if (endElement.getName().getLocalPart() ==(ITEM)){
						items.add(item);
					}
				}

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (XMLStreamException e) {
			e.printStackTrace();
		}
		return items; 
	}

}

@@ -895,17 +895,16 @@
 st%3CItem%3E
- 
  readCon
@@ -929,16 +929,16 @@
 ile) %7B%0D%0A
+
 %09%09List%3CI
@@ -1303,13 +1303,10 @@
 l;%0D%0A
-%09%09%09
 %0D%0A
+
 %09%09%09w
@@ -1684,16 +1684,26 @@
 the date
+%0D%0A%09%09%09%09%09%09//
  attribu
@@ -1942,19 +1942,21 @@
 s(DATE))
-;
+ %7B
 %0D%0A
+%09
 %09%09%09%09%09%09%09i
@@ -1987,24 +1987,36 @@
 tValue());%0D%0A
+%09%09%09%09%09%09%09%7D%0D%0A%0D%0A
 %09%09%09%09%09%09%7D%0D%0A%09%09%09
@@ -2321,32 +2321,41 @@
 ).getLocalPart()
+%0D%0A%09%09%09%09%09%09%09
 .equals(UNIT)) %7B
@@ -2524,32 +2524,41 @@
 ).getLocalPart()
+%0D%0A%09%09%09%09%09%09%09
 .equals(CURRENT)
@@ -2741,16 +2741,25 @@
 alPart()
+%0D%0A%09%09%09%09%09%09%09
 .equals(
@@ -3005,16 +3005,17 @@
 ement())
+ 
 %7B%0D%0A%09%09%09%09%09
@@ -3108,23 +3108,25 @@
 art() ==
+ 
 (ITEM))
+ 
 %7B%0D%0A%09%09%09%09%09
@@ -3297,16 +3297,16 @@
 ;%0D%0A%09%09%7D%0D%0A
+
 %09%09return
@@ -3312,17 +3312,16 @@
 n items;
- 
 %0D%0A%09%7D%0D%0A%0D%0A

update examples