<commit_before>/**
 * Copyright 2014, barter.li
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package li.barter.http;

import com.google.android.gms.drive.internal.n;

import org.apache.http.HttpStatus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.ContentValues;
import android.os.Bundle;
import android.text.TextUtils;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import li.barter.data.DBInterface;
import li.barter.data.DatabaseColumns;
import li.barter.data.SQLConstants;
import li.barter.data.TableLocations;
import li.barter.data.TableMyBooks;
import li.barter.data.TableSearchBooks;
import li.barter.data.TableUserBooks;
import li.barter.data.TableUsers;
import li.barter.http.HttpConstants.RequestId;
import li.barter.http.JsonUtils.FieldType;
import li.barter.models.Hangout;
import li.barter.models.Team;
import li.barter.utils.AppConstants;
import li.barter.utils.AppConstants.Keys;
import li.barter.utils.Logger;
import li.barter.widgets.autocomplete.Suggestion;

/**
 * Class that reads an API response and parses it and stores it in the database
 * 
 * @author <NAME>
 */
public class HttpResponseParser {

    private static final String         TAG  = "HttpResponseParser";
    private static final Object         LOCK = new Object();

    private static XmlPullParserFactory sPullParserFactory;

    /**
     * Parses the string response(when the request was successful -
     * {@linkplain HttpStatus#SC_OK} was returned) for a particular
     * {@linkplain RequestId} and returns the response data
     * 
     * @param requestId
     * @param response
     * @return
     * @throws JSONException
     * @throws XmlPullParserException
     * @throws IOException
     */
    public ResponseInfo getSuccessResponse(final int requestId,
                    final String response) throws JSONException,
                    XmlPullParserException, IOException {

        Logger.d(TAG, "Request Id %d\nResponse %s", requestId, response);
        switch (requestId) {

            case RequestId.CREATE_BOOK: {
                return parseCreateBookResponse(response);
            }

            case RequestId.GET_BOOK_INFO: {
                return parseGetBookInfoResponse(response);
            }

            case RequestId.SEARCH_BOOKS: {
                return parseSearchBooksResponse(response);
            }

            case RequestId.CREATE_USER: {
                return parseCreateUserResponse(response);
            }

            case RequestId.SAVE_USER_PROFILE: {
                return parseCreateUserResponse(response);
            }

            case RequestId.HANGOUTS: {
                return parseHangoutsResponse(response);
            }

            case RequestId.REPORT_BUG: {
                return parseReportBugOrSuggestFeatureResponse(response);
            }

            case RequestId.SUGGEST_FEATURE: {
                return parseReportBugOrSuggestFeatureResponse(response);
            }

            case RequestId.COLLABORATE_REQUEST: {
                return parseReportBugOrSuggestFeatureResponse(response);
            }

            case RequestId.SET_USER_PREFERRED_LOCATION: {
                return parseSetUserPreferredLocationResponse(response);
            }

            case RequestId.AMPQ: {
                return parseAmpqResponse(response);
            }

            case RequestId.TRIBUTE: {
                return parseTributeResponse(response);
            }

            case RequestId.TEAM: {
                return parseTeamResponse(response);
            }

            case RequestId.GET_USER_PROFILE: {
                return parseUserProfileResponse(response);
            }

            case RequestId.BOOK_SUGGESTIONS: {
                return parseBookSuggestionsResponse(response);
            }

            case RequestId.UPDATE_BOOK: {
                return parseUpdateBookResponse(response);
            }

            case RequestId.DELETE_BOOK: {
                return parseUpdateBookResponse(response);
            }

            case RequestId.GOODREADS_SHOW_BOOK: {
                return parseGoodreadsBookResponse(response);
            }

            default: {
                throw new IllegalArgumentException("Unknown request Id:"
                                + requestId);
            }
        }
    }

    /**
     * Parses the good reads API response for fetching a book
     * 
     * @param response
     * @return
     * @throws XmlPullParserException
     * @throws IOException
     */
    private ResponseInfo parseGoodreadsBookResponse(String response)
                    throws XmlPullParserException, IOException {

        final ResponseInfo responseInfo = new ResponseInfo();
        final XmlPullParser xmlParser = getPullParserInstance(false, false);
        xmlParser.setInput(new StringReader(response));

        for (int eventType = xmlParser.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = xmlParser
                        .next()) {

            String name = null;

            if (eventType == XmlPullParser.START_TAG) {
                name = xmlParser.getName();

                if ((name != null) && name.equals(HttpConstants.BOOK)) {

                    responseInfo.responseBundle = parseGoodreadsBookInfo(xmlParser);
                    break;

                }
            }
        }
        return responseInfo;
    }

    /**
     * Parse the response for goodreads book info
     * 
     * @param xmlParser An {@link XmlPullParser} instance, forwarded to an event
     *            before the book item begins
     * @return A {@link Bundle} containing the parsed info
     * @throws IOException
     * @throws XmlPullParserException
     */
    private Bundle parseGoodreadsBookInfo(XmlPullParser xmlParser)
                    throws XmlPullParserException, IOException {

        final Bundle bundle = new Bundle(7);
        String name;
        int authorEventType;
        final ArrayList<String> authorsList = new ArrayList<String>();
        for (int eventType = xmlParser.next(); eventType != XmlPullParser.END_DOCUMENT; eventType = xmlParser
                        .next()) {

            if (eventType == XmlPullParser.START_TAG) {
                name = xmlParser.getName();

                if (name.equals(HttpConstants.TITLE)) {
                    bundle.putString(HttpConstants.TITLE, xmlParser.nextText());
                } else if (name.equals(HttpConstants.ISBN)) {
                    bundle.putString(HttpConstants.ISBN_10, xmlParser
                                    .nextText());
                } else if (name.equals(HttpConstants.ISBN13)) {
                    bundle.putString(HttpConstants.ISBN_13, xmlParser
                                    .nextText());
                } else if (name.equals(HttpConstants.DESCRIPTION)) {
                    bundle.putString(HttpConstants.DESCRIPTION, xmlParser
                                    .nextText());
                } else if (name.equals(HttpConstants.PUBLICATION_YEAR)) {
                    bundle.putString(HttpConstants.PUBLICATION_YEAR, xmlParser
                                    .nextText());
                } else if (name.equals(HttpConstants.AUTHORS)) {
                    //We need to handle multiple authors here
                    while (true) {
                        authorEventType = xmlParser.next();

                        if (authorEventType == XmlPullParser.END_DOCUMENT) {
                            break;
                        } else if (authorEventType == XmlPullParser.END_TAG) {
                            name = xmlParser.getName();

                            if (name.equals(HttpConstants.AUTHORS)) {
                                //We have parsed out all authors

                                if (authorsList.size() == 1) {
                                    //There is only 1 author
                                    bundle.putString(HttpConstants.AUTHOR, authorsList
                                                    .get(0));
                                } else if (authorsList.size() > 1) {
                                    //Multiple authors
                                    bundle.putString(HttpConstants.AUTHOR, TextUtils
                                                    .join(", ", authorsList));
                                }
                            }

                            authorsList.clear();
                            break;

                        } else if (authorEventType == XmlPullParser.START_TAG) {
                            name = xmlParser.getName();

                            if (name.equals(HttpConstants.NAME)) {
                                authorsList.add(xmlParser.nextText());
                            }
                        }

                    }
                    
                    break;
                }
            }
        }
        return bundle;
    }
<commit_msg>Fixed suggestion parsing4<commit_after>/**
 * Copyright 2014, barter.li
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package li.barter.http;

import com.google.android.gms.drive.internal.n;

import org.apache.http.HttpStatus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.ContentValues;
import android.os.Bundle;
import android.text.TextUtils;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import li.barter.data.DBInterface;
import li.barter.data.DatabaseColumns;
import li.barter.data.SQLConstants;
import li.barter.data.TableLocations;
import li.barter.data.TableMyBooks;
import li.barter.data.TableSearchBooks;
import li.barter.data.TableUserBooks;
import li.barter.data.TableUsers;
import li.barter.http.HttpConstants.RequestId;
import li.barter.http.JsonUtils.FieldType;
import li.barter.models.Hangout;
import li.barter.models.Team;
import li.barter.utils.AppConstants;
import li.barter.utils.AppConstants.Keys;
import li.barter.utils.Logger;
import li.barter.widgets.autocomplete.Suggestion;

/**
 * Class that reads an API response and parses it and stores it in the database
 * 
 * @author <NAME>
 */
public class HttpResponseParser {

    private static final String         TAG  = "HttpResponseParser";
    private static final Object         LOCK = new Object();

    private static XmlPullParserFactory sPullParserFactory;

    /**
     * Parses the string response(when the request was successful -
     * {@linkplain HttpStatus#SC_OK} was returned) for a particular
     * {@linkplain RequestId} and returns the response data
     * 
     * @param requestId
     * @param response
     * @return
     * @throws JSONException
     * @throws XmlPullParserException
     * @throws IOException
     */
    public ResponseInfo getSuccessResponse(final int requestId,
                    final String response) throws JSONException,
                    XmlPullParserException, IOException {

        Logger.d(TAG, "Request Id %d\nResponse %s", requestId, response);
        switch (requestId) {

            case RequestId.CREATE_BOOK: {
                return parseCreateBookResponse(response);
            }

            case RequestId.GET_BOOK_INFO: {
                return parseGetBookInfoResponse(response);
            }

            case RequestId.SEARCH_BOOKS: {
                return parseSearchBooksResponse(response);
            }

            case RequestId.CREATE_USER: {
                return parseCreateUserResponse(response);
            }

            case RequestId.SAVE_USER_PROFILE: {
                return parseCreateUserResponse(response);
            }

            case RequestId.HANGOUTS: {
                return parseHangoutsResponse(response);
            }

            case RequestId.REPORT_BUG: {
                return parseReportBugOrSuggestFeatureResponse(response);
            }

            case RequestId.SUGGEST_FEATURE: {
                return parseReportBugOrSuggestFeatureResponse(response);
            }

            case RequestId.COLLABORATE_REQUEST: {
                return parseReportBugOrSuggestFeatureResponse(response);
            }

            case RequestId.SET_USER_PREFERRED_LOCATION: {
                return parseSetUserPreferredLocationResponse(response);
            }

            case RequestId.AMPQ: {
                return parseAmpqResponse(response);
            }

            case RequestId.TRIBUTE: {
                return parseTributeResponse(response);
            }

            case RequestId.TEAM: {
                return parseTeamResponse(response);
            }

            case RequestId.GET_USER_PROFILE: {
                return parseUserProfileResponse(response);
            }

            case RequestId.BOOK_SUGGESTIONS: {
                return parseBookSuggestionsResponse(response);
            }

            case RequestId.UPDATE_BOOK: {
                return parseUpdateBookResponse(response);
            }

            case RequestId.DELETE_BOOK: {
                return parseUpdateBookResponse(response);
            }

            case RequestId.GOODREADS_SHOW_BOOK: {
                return parseGoodreadsBookResponse(response);
            }

            default: {
                throw new IllegalArgumentException("Unknown request Id:"
                                + requestId);
            }
        }
    }

    /**
     * Parses the good reads API response for fetching a book
     * 
     * @param response
     * @return
     * @throws XmlPullParserException
     * @throws IOException
     */
    private ResponseInfo parseGoodreadsBookResponse(String response)
                    throws XmlPullParserException, IOException {

        final ResponseInfo responseInfo = new ResponseInfo();
        final XmlPullParser xmlParser = getPullParserInstance(false, false);
        xmlParser.setInput(new StringReader(response));

        for (int eventType = xmlParser.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = xmlParser
                        .next()) {

            String name = null;

            if (eventType == XmlPullParser.START_TAG) {
                name = xmlParser.getName();

                if ((name != null) && name.equals(HttpConstants.BOOK)) {

                    responseInfo.responseBundle = parseGoodreadsBookInfo(xmlParser);
                    break;

                }
            }
        }
        return responseInfo;
    }

    /**
     * Parse the response for goodreads book info
     * 
     * @param xmlParser An {@link XmlPullParser} instance, forwarded to an event
     *            before the book item begins
     * @return A {@link Bundle} containing the parsed info
     * @throws IOException
     * @throws XmlPullParserException
     */
    private Bundle parseGoodreadsBookInfo(XmlPullParser xmlParser)
                    throws XmlPullParserException, IOException {

        final Bundle bundle = new Bundle(7);
        String name;
        int authorEventType;
        final ArrayList<String> authorsList = new ArrayList<String>();
        for (int eventType = xmlParser.next(); eventType != XmlPullParser.END_DOCUMENT; eventType = xmlParser
                        .next()) {

            if (eventType == XmlPullParser.START_TAG) {
                name = xmlParser.getName();

                if (name.equals(HttpConstants.TITLE)) {
                    bundle.putString(HttpConstants.TITLE, xmlParser.nextText());
                } else if (name.equals(HttpConstants.ISBN)) {
                    bundle.putString(HttpConstants.ISBN_10, xmlParser
                                    .nextText());
                } else if (name.equals(HttpConstants.ISBN13)) {
                    bundle.putString(HttpConstants.ISBN_13, xmlParser
                                    .nextText());
                } else if (name.equals(HttpConstants.DESCRIPTION)) {
                    bundle.putString(HttpConstants.DESCRIPTION, xmlParser
                                    .nextText());
                } else if (name.equals(HttpConstants.PUBLICATION_YEAR)) {
                    bundle.putString(HttpConstants.PUBLICATION_YEAR, xmlParser
                                    .nextText());
                } else if (name.equals(HttpConstants.AUTHORS)) {
                    //We need to handle multiple authors here
                    while (true) {
                        authorEventType = xmlParser.next();

                        if (authorEventType == XmlPullParser.END_DOCUMENT) {
                            break;
                        } else if (authorEventType == XmlPullParser.END_TAG) {
                            name = xmlParser.getName();

                            if (name.equals(HttpConstants.AUTHORS)) {
                                //We have parsed out all authors

                                if (authorsList.size() == 1) {
                                    //There is only 1 author
                                    bundle.putString(HttpConstants.AUTHOR, authorsList
                                                    .get(0));
                                } else if (authorsList.size() > 1) {
                                    //Multiple authors
                                    bundle.putString(HttpConstants.AUTHOR, TextUtils
                                                    .join(", ", authorsList));
                                }

                                authorsList.clear();
                                break;
                            }

                        } else if (authorEventType == XmlPullParser.START_TAG) {
                            name = xmlParser.getName();

                            if (name.equals(HttpConstants.NAME)) {
                                authorsList.add(xmlParser.nextText());
                            }
                        }

                    }

                    break;
                }
            }
        }
        return bundle;
    }

{'fraction_non_alphanumeric': 0.0771760353509403, 'fraction_numerical': 0.002312198129688624, 'mean_word_length': 1.6745911776831113, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 46}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7553118', 'n_tokens_mistral': 4953, 'n_tokens_neox': 4533, 'n_words': 1086}