com.lillicoder.newsblurry.stories.StoryParser.java Source code

Java tutorial

Introduction

Here is the source code for com.lillicoder.newsblurry.stories.StoryParser.java

Source

/**
 * Copyright 2012 Scott Weeden-Moody
 *
 * 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 com.lillicoder.newsblurry.stories;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

/**
 * Utility class that can generate {@link Story}.
 * @author lillicoder
 */
public class StoryParser {

    private static final String TAG = StoryParser.class.getSimpleName();

    private static final String FAILED_TO_PARSE_STORY = "Failed to parse story at index %d, skipping story.";

    private static final String JSON_KEY_ID = "id";
    private static final String JSON_KEY_READ_STATUS = "read_status";
    private static final String JSON_KEY_CONTENT = "story_content";
    private static final String JSON_KEY_PERMALINK = "story_permalink";
    private static final String JSON_KEY_TITLE = "story_title";

    /**
     * Parses the given root stories {@link JSONObject} into a collection
     * of {@link Story}.
     * @param storiesJson Root stories {@link JSONObject} to parse.
     * @return {@link List} of parsed {@link Story}.
     * @throws JSONException Thrown when given root stories JSON node has an invalid format
     *                    or cannot reliably be parsed.
     */
    public List<Story> parseStories(JSONObject storiesJson) throws JSONException {
        List<Story> stories = new ArrayList<Story>();

        JSONArray storiesArray = storiesJson.getJSONArray("stories");
        for (int index = 0; index < storiesArray.length(); index++) {
            try {
                JSONObject storyJson = storiesArray.getJSONObject(index);
                Story story = this.parseStory(storyJson);

                stories.add(story);
            } catch (JSONException e) {
                Log.w(TAG, String.format(FAILED_TO_PARSE_STORY, index));
            }
        }

        return stories;
    }

    /**
     * Parses the given story {@link JSONObject} into a {@link Story} instance.
     * @param storyJson Story {@link JSONObject} to parse.
     * @return Parsed {@link Story} instance.
     * @throws JSONException Thrown if the given story JSON node has an invalid format
     *                    or cannot be reliably parsed.
     */
    private Story parseStory(JSONObject storyJson) throws JSONException {
        String id = storyJson.getString(JSON_KEY_ID);
        String title = storyJson.getString(JSON_KEY_TITLE);
        String permalink = storyJson.getString(JSON_KEY_PERMALINK);
        String content = storyJson.getString(JSON_KEY_CONTENT);
        Boolean isRead = storyJson.getInt(JSON_KEY_READ_STATUS) == 1;

        return new Story(id, title, permalink, content, isRead);
    }

}