package com.dbs.consent.demo.consent;

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

import java.io.IOException;
import java.io.StringReader;

public class SavedConsent {

  private String dataXml;


  public void parse(String xml) {
    this.dataXml = xml;
    try {
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      XmlPullParser parser = factory.newPullParser();
      parser.setInput(new StringReader(xml));
      int eventType = parser.getEventType();
      while(eventType != XmlPullParser.END_DOCUMENT) {
        switch(eventType) {
          case XmlPullParser.START_TAG:
            // 태그명 가져오기
            String startTag = parser.getName();
            Class clz = this.getClass();
            try {
              // 태그명 필드 찾아서 값 설정
              clz.getField(startTag).set(this, parser.nextText());
            } catch (NoSuchFieldException e) {
              // 없는필드 무시
            } catch (IllegalAccessException e) {
              e.printStackTrace();
              // 발생할일이 없음
            }
            break;
          case XmlPullParser.END_TAG:
            String endTag = parser.getName();
            break;
        }
        // 다음 이벤트 가져옴
        eventType = parser.next();
      }
    } catch (XmlPullParserException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public String getDataXml() { return this.dataXml; }
}