/**
 * Copyright 2009 Joe LaPenna
 */

package com.joelapenna.foursquare.parsers;

import com.joelapenna.foursquare.Foursquare;
import com.joelapenna.foursquare.error.FoursquareError;
import com.joelapenna.foursquare.types.Checkin;

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

import android.net.Uri;
import android.util.Log;

import java.io.IOException;

/**
 * @author Joe LaPenna (joe@joelapenna.com)
 */
public class CheckinResponseParser extends AbstractParser<Checkin> {
    private static final String TAG = "CheckinResponseParser";
    private static final boolean DEBUG = Foursquare.PARSER_DEBUG;

    @Override
    public Checkin parseInner(XmlPullParser parser) throws XmlPullParserException,
            IOException, FoursquareError {
        Checkin checkin = new Checkin();
        int eventType = parser.nextToken();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (DEBUG) Log.d(TAG, "Tag Name: " + String.valueOf(parser.getName()));
                    String name = parser.getName();

                    if ("error".equals(name)) {
                        throw new FoursquareError(parser.getText());
                    } else if ("checkin".equals(name)) {
                        parseCheckinTag(parser, checkin);
                        return checkin;
                    }

                default:
                    if (DEBUG) Log.d(TAG, "Unhandled Event");
            }
            eventType = parser.nextToken();
        }
        return checkin;
    }

    public void parseCheckinTag(XmlPullParser parser, Checkin checkin)
            throws XmlPullParserException, IOException {
        assert parser.getName() == "checkin";
        if (DEBUG) Log.d(TAG, "parse()ing checkin stanza");

        while (parser.nextTag() != XmlPullParser.END_TAG) {
            String name = parser.getName();
            if ("userid".equals(name)) {
                checkin.setUserid(parser.nextText());
            } else if ("message".equals(name)) {
                checkin.setMessage(parser.nextText());
            } else if ("status".equals(name)) {
                checkin.setStatus(parser.nextText().equals("1") ? true : false);
            } else if ("url".equals(name)) {
                // Looks like an escapld string, with & = &amp;
                // http://playfoursquare.com/incoming/breakdown?cid=67889&uid=9232&client=iphone
                String urlString = parser.nextText();
                checkin.setUrl(urlString);
                checkin.setCheckinid(Uri.parse(urlString).getQueryParameter("cid"));
            }
        }
    }
}

@@ -170,21 +170,30 @@
 ursquare
-Error
+ParseException
 ;%0Aimport
@@ -745,28 +745,16 @@
 ception,
-%0A           
  IOExcep
@@ -762,592 +762,123 @@
 ion,
- FoursquareError %7B%0A        Checkin checkin = new Checkin();%0A        int eventType = parser.nextToken();%0A        while (eventType != XmlPullParser.END_DOCUMENT) %7B%0A            switch (eventType) %7B%0A                case XmlPullParser.START_TAG:%0A                    if (DEBUG) Log.d(TAG, %22Tag Name: %22 + String.valueOf(parser.getName()));%0A                    String name = parser.getName();%0A%0A                    if (%22error%22.equals(name)) %7B%0A                        throw new FoursquareError(parser.getText());%0A                    %7D else if (%22checkin%22.equals(name)) %7B%0A                
+%0A            FoursquareParseException %7B%0A        parser.require(XmlPullParser.START_TAG, null, %22checkin%22);%0A%0A
         
 pars
@@ -877,585 +877,188 @@
     
-parse
 Checkin
-Tag(parser, checkin);%0A                        return checkin;%0A                    %7D%0A%0A                default:%0A                    if (DEBUG) Log.d(TAG, %22Unhandled Event%22);%0A            %7D%0A            eventType = parser.nextToken();%0A        %7D%0A        return checkin;%0A    %7D%0A%0A    public void parseCheckinTag(XmlPullParser parser, Checkin checkin)%0A            throws XmlPullParserException, IOException %7B%0A        assert parser.getName() == %22checkin%22;%0A        if (DEBUG) Log.d(TAG, %22parse()ing checkin stanza%22);%0A%0A        while (parser.nextTag() != XmlPullParser.END_TAG) %7B
+ checkin = new Checkin();%0A%0A        while (parser.nextTag() == XmlPullParser.START_TAG) %7B%0A            if (DEBUG) Log.d(TAG, %22Tag Name: %22 + String.valueOf(parser.getName()));%0A
 %0A   
@@ -1185,32 +1185,33 @@
 er.nextText());%0A
+%0A
             %7D el
@@ -1294,24 +1294,25 @@
 extText());%0A
+%0A
             
@@ -1428,16 +1428,17 @@
 false);%0A
+%0A
         
@@ -1821,31 +1821,257 @@
 ));%0A
-            %7D%0A        %7D
+%0A            %7D else %7B%0A                // Consume something we don't understand.%0A                if (DEBUG) Log.d(TAG, %22Found tag that we don't recognize: %22 + name);%0A                skipSubTree(parser);%0A            %7D%0A        %7D%0A        return checkin;
 %0A   

Improve checkin response parser to crash less.