| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 
 |  
package epu.android.jseduite.core;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
import android.util.Log;
 
public class DataHandler extends DefaultHandler {
 
	private enum Tags { 
		RESULT ("result"),
		INFO ("GetInformationResponse"),
		BREAKINGNEWS ("breakNews"),
		INTERNALNEWS ("internalNews"),
		STUDENTSUMMON ("studentSummon"),
		EVENT ("events"),
		TIMETABLE ("timetable");
 
		private String tagName;
		Tags(String name) {
			tagName = name;
		}
 
		private String getTagName() { return tagName; }
	}
 
	private String itemTag = "item";
	private Tags tag = null;
	private Data data;
	private Item currentItem;
	private WrapInfo currentWrapInfo;
	private boolean innerItem = false;
	private boolean innerInfo = false;
 
	public DataHandler() {
		super();
	}
 
 
	public Data getParsedData() {
		// TODO Auto-generated method stub
		return data;
	}
 
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		data = new Data();
	}
 
	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
	}
 
	/**
         * Gets be called on opening tags like: <tag> Can provide attribute(s), when
         * xml was like: <tag attribute="attributeValue">
         */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
 
		Log.d(this.getClass().getSimpleName(), "element : " + localName);
 
		try {
			tag = Tags.valueOf(Tags.class, localName);
//			tag = Tags.valueOf(localName);
			Log.d(this.getClass().getSimpleName(), "tag retrieving : " + tag.tagName);
		}catch(Exception e){
			//do nothing
			Log.e(this.getClass().getSimpleName(), e.getMessage());
		}
 
		// we get a new item
		if (localName.equals(itemTag)) {
			innerItem = true;
			currentItem = new Item();
			currentItem.setSource(attributes.getValue("source"));
			Log.d(this.getClass().getSimpleName(), "new currentItem : " + currentItem.getSource());
		}
		// we get inner info of an item, other than one of Tags
		else if (tag == null && innerItem) {
			innerInfo = true;
			currentWrapInfo = new WrapInfo();
			currentWrapInfo.setName(localName);
			Log.d(this.getClass().getSimpleName(), "new currentWrapInfo : " + currentWrapInfo.getName());
		}
	}
 
	/**
         * Gets be called on closing tags like: </tag>
         */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
 
		Log.d(this.getClass().getSimpleName(), "end element : " + localName);
 
		try {
			tag = Tags.valueOf(localName);
		}catch(Exception e){
			//do nothing
		}
		if (localName.equals(itemTag)) {
			innerItem = false;
			data.addItem(currentItem);
			Log.d(this.getClass().getSimpleName(), "add currentItem : " + currentItem.toString());
		} else if (tag == null && innerItem && innerInfo) {
			innerInfo = false;
			currentItem.addInfo(currentWrapInfo);
			Log.d(this.getClass().getSimpleName(), "add currentWrapInfo : " + currentWrapInfo.toString());
		}
	}
 
	/**
         * Gets be called on the following structure: <tag>characters</tag>
         */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {		
		if(innerInfo){
			currentWrapInfo.setContent(new String(ch,start,length));
			Log.d(this.getClass().getSimpleName(), "char of " + currentWrapInfo.getName() + " : " + new String(ch,start,length).toString());
		}
	}
} | 
Partager