com.handlerexploit.news.data.models.Article.java Source code

Java tutorial

Introduction

Here is the source code for com.handlerexploit.news.data.models.Article.java

Source

/*
 *  Copyright (c) 2011 Daniel Huckaby
 *
 *  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.handlerexploit.news.data.models;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

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

import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;

@SuppressWarnings("serial")
public class Article implements Serializable {

    private transient static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z",
            Locale.ENGLISH);

    private String title;
    private String link;
    private Date pubDate;
    private String description;
    private String source;
    private String imageThumbnailUrl;

    public Article(JSONObject jsonObject) throws JSONException, ParseException {

        // TODO Clean this up

        link = jsonObject.getString("link");
        pubDate = simpleDateFormat.parse(jsonObject.getString("pubDate"));
        description = jsonObject.getString("description");
        String[] splitString = jsonObject.getString("title").split("-");
        source = splitString[splitString.length - 1].trim();
        title = jsonObject.getString("title").replace(source, "").replace("-", " ").replace("  ", " ").trim();
        Html.fromHtml(getDescription(), new ImageGetter() {
            public Drawable getDrawable(String source) {
                if (imageThumbnailUrl == null) {
                    imageThumbnailUrl = source;
                }
                return null;
            }
        }, null);
    }

    public String getTitle() {
        return title;
    }

    public String getLink() {
        return link;
    }

    public Date getPublishDate() {
        return pubDate;
    }

    public String getDescription() {
        return description;
    }

    public String getSource() {
        return source;
    }

    public String getImageThumbnail() {
        return imageThumbnailUrl;
    }
}