package be.ugent.zeus.resto.client.util;

import android.util.Log;
import be.ugent.zeus.resto.client.data.Activity;
import java.io.StringReader;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 * Parse the activity-xml into a list of activities. The activities are not
 * yet grouped by date!
 * 
 * @author Thomas Meire
 */
public class ActivityXmlParser {

  public List<Activity> parse(String activityXML) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    List<Activity> list = new LinkedList<Activity>();

    try {
      Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(activityXML)));
      doc.getDomConfig().setParameter("cdata-sections", false);

      Node clubNode = doc.getFirstChild();
      NodeList activityList = clubNode.getChildNodes();
      int len = activityList.getLength();

      for (int i = 0; i < len; i++) {
        list.add(parse(activityList.item(i)));
      }
    } catch (Exception e) {
      Log.w("[ActivityXmlParser", "Something went wrong while parsing the activity xml!", e);
    }
    return list;
  }

  private Activity parse(Node node) {
    Activity activity = new Activity();

    NamedNodeMap attributes = node.getAttributes();
    activity.date = attributes.getNamedItem("date").getTextContent().replace("/", "-");
    activity.start = attributes.getNamedItem("from").getTextContent();
    activity.end = attributes.getNamedItem("to").getTextContent();
    activity.association_id = node.getAttributes().getNamedItem("association_id").getTextContent();
    // TODO parse the other attributes (to, from)
    
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
      Node child = children.item(i);

      if ("title".equals(child.getNodeName())) {
        activity.title = child.getTextContent();
        // FIXME: cutoff the CDATA tags
        activity.title = activity.title.substring(9, activity.title.length() - 3);
      } else if ("location".equals(child.getNodeName())) {
        activity.location = child.getTextContent();
        // FIXME: cutoff the CDATA tags
        activity.location = activity.location.substring(9, activity.location.length() - 3);
      } else {
        // unknown tag, ignore
      }
    }

    return activity;
  }
}

@@ -124,28 +124,36 @@
 java.io.
-StringReader
+ByteArrayInputStream
 ;%0Aimport
@@ -366,40 +366,8 @@
 ist;
-%0Aimport org.xml.sax.InputSource;
 %0A%0A/*
@@ -731,109 +731,104 @@
     
-  Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(activityXML)
+ByteArrayInputStream stream = new ByteArrayInputStream(activityXML.getBytes(%22ISO-8859-1%22
 ));%0A    
   do
@@ -827,65 +827,60 @@
     
- 
- doc.getDomConfig().setParameter(%22cdata-sections%22, false
+Document doc = dbf.newDocumentBuilder().parse(stream
 );%0A%0A

Fix possible encoding issue in calendar