package com.wasu.cs.model;

import java.io.ByteArrayInputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.wasu.module.datafetch.DataFetchException;
import com.wasu.module.datafetch.ObjectBase;

/**
 * 广告xml
 */
public class AdXml extends ObjectBase {
	
	/**
     */
    private static final long serialVersionUID = 1L;

    public final static String TAG = "AdXml";
	
	/**
	 * ID
	 */
	private String id;
	/**
	 * VerID
	 */
	private String verId;
	/**
	 * VerSRC
	 */
	private String verSrc;
	/**
	 * Width
	 */
	private int width;
	/**
	 * Height
	 */
	private int height;
	/**
	 * PvURL
	 */
	private String pvUrl;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getVerId() {
		return verId;
	}

	public void setVerId(String verId) {
		this.verId = verId;
	}

	public String getVerSrc() {
		return verSrc;
	}

	public void setVerSrc(String verSrc) {
		this.verSrc = verSrc;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public String getPvUrl() {
		return pvUrl;
	}

	public void setPvUrl(String pvUrl) {
		this.pvUrl = pvUrl;
	}

	@Override
	public void createFromResponse(String response) throws DataFetchException {
		ByteArrayInputStream is = new ByteArrayInputStream(response.getBytes());
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(is, "UTF-8");
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					String name = parser.getName();
					if (name.equalsIgnoreCase("ID")) {
						setId(parser.nextText());
					} else if (name.equalsIgnoreCase("VerID")) {
						setVerId(parser.nextText());
					} else if (name.equalsIgnoreCase("VerSRC")) {
						setVerSrc(parser.nextText());
					} else if (name.equalsIgnoreCase("Width")) {
						try {
							setWidth(Integer.parseInt(parser.nextText()));
						} catch (Exception e) {
							e.printStackTrace();
						}
					} else if (name.equalsIgnoreCase("Height")) {
						try {
							setHeight(Integer.parseInt(parser.nextText()));
						} catch (Exception e) {
							e.printStackTrace();
						}
					} else if (name.equalsIgnoreCase("PvURL")) {
						setPvUrl(parser.nextText());
					}
					break;
				case XmlPullParser.END_TAG:
					break;
				}
				eventType = parser.next();
			}
			if (is != null) {
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
