Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added parsing enclosures within RSS item. #7

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public void onBindViewHolder(RssItemsAdapter.ViewHolder holder, int position) {
final RssItem item = mItems.get(position);
holder.textTitle.setText(item.getTitle());
holder.textPubDate.setText(item.getPublishDate());
if (item.getEnclosures().size() > 0) {
holder.textEnclosure.setText(item.getEnclosures().get(0).getLink());
} else {
holder.textEnclosure.setVisibility(View.GONE);
}

if (item.getImage() != null) {
Picasso.with(mContext).load(item.getImage()).
Expand Down Expand Up @@ -101,6 +106,9 @@ static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tvPubDate)
TextView textPubDate;

@BindView(R.id.tvEnclosure)
TextView textEnclosure;

@BindView(R.id.ivThumb)
ImageView imageThumb;

Expand Down
12 changes: 10 additions & 2 deletions app/src/main/res/layout/row_rss_item.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cardview"
style="@style/SelectableBackground"
android:layout_width="fill_parent"
Expand Down Expand Up @@ -58,6 +56,16 @@
android:textColor="#ffffff"
android:textSize="12sp" />

<TextView
android:id="@+id/tvEnclosure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvPubDate"
android:layout_marginTop="5dp"
android:fontFamily="sans-serif-light"
android:textColor="#ffffff"
android:textSize="12sp" />

<!--<RelativeLayout
android:id="@+id/rl_fav"
android:layout_width="wrap_content"
Expand Down
52 changes: 52 additions & 0 deletions library/src/main/java/me/toptas/rssconverter/RssEnclosure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package me.toptas.rssconverter;

import java.io.Serializable;

/**
* Model for Rss Enclosure
*/
public class RssEnclosure implements Serializable {
private String mType;
private String mLink;
private String mLength;

public String getType() {
return mType;
}

public void setType(String type) {
this.mType = type.replace("&#39;", "'").replace("&#039;", "'");
}

public String getLink() {
return mLink;
}

public void setLink(String link) {
this.mLink = link.trim();
}

public String getLength() {
return mLength;
}

public void setLength(String length) {
this.mLength = length.trim();
}


@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (mType != null) {
builder.append(mType).append("\n");
}
if (mLink != null) {
builder.append(mLink).append("\n");
}
if (mLength != null) {
builder.append(mLength).append("\n");
}
return builder.toString();
}
}
12 changes: 11 additions & 1 deletion library/src/main/java/me/toptas/rssconverter/RssItem.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.toptas.rssconverter;

import java.io.Serializable;
import java.util.List;

/**
* Model for Rss Item
Expand All @@ -12,7 +13,7 @@ public class RssItem implements Serializable {
private String mImage;
private String mPublishDate;
private String mDescription;

private List<RssEnclosure> mEnclosures;

public String getTitle() {
return mTitle;
Expand Down Expand Up @@ -54,6 +55,15 @@ public void setDescription(String description) {
this.mDescription = description;
}

public List<RssEnclosure> getEnclosures() {
return mEnclosures;
}

public void setEnclosures(List<RssEnclosure> enclosures) {
this.mEnclosures = enclosures;
}



@Override
public String toString() {
Expand Down
22 changes: 22 additions & 0 deletions library/src/main/java/me/toptas/rssconverter/XMLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.xml.sax.helpers.DefaultHandler;

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

/**
* RSS Feed XML parser
Expand All @@ -21,6 +22,7 @@ class XMLParser extends DefaultHandler {
private static final String sUrl = "url";
private static final String sImage = "image";
private static final String sPublishDate = "pubdate";
private static final String sEnclosure = "enclosure";

private boolean mElementOn = false;
private boolean mParsingTitle = false;
Expand All @@ -33,6 +35,7 @@ class XMLParser extends DefaultHandler {
private String mImage;
private String mDate;
private String mDescription;
private List<RssEnclosure> mEnclosures;

private RssItem mRssItem;
private final ArrayList<RssItem> mRssItems;
Expand All @@ -50,6 +53,7 @@ public void startElement(String uri, String localName, String qName,
switch (localName.toLowerCase()) {
case sItem:
mRssItem = new RssItem();
mEnclosures = new ArrayList<>();
break;
case sTitle:
if (!qName.contains(sMedia)) {
Expand All @@ -67,6 +71,23 @@ public void startElement(String uri, String localName, String qName,
mLink = sEmptyString;
}
break;
case sEnclosure:
RssEnclosure enclosure = new RssEnclosure();
for (int i = 0; i < attributes.getLength(); i++) {
switch (attributes.getLocalName(i)) {
case "type":
enclosure.setType(attributes.getValue(i));
break;
case "url":
enclosure.setLink(attributes.getValue(i));
break;
case "length":
enclosure.setLength(attributes.getValue(i));
break;
}
}
mEnclosures.add(enclosure);
break;
}
/*if (localName.equals(sItem)) {
mRssItem = new RssItem();
Expand Down Expand Up @@ -104,6 +125,7 @@ public void endElement(String uri, String localName, String qName)
if (mImage == null && mDescription != null && getImageSourceFromDescription(mDescription) != null) {
mRssItem.setImage(getImageSourceFromDescription(mDescription));
}
mRssItem.setEnclosures(mEnclosures);
mRssItems.add(mRssItem);
mLink = sEmptyString;
mImage = null;
Expand Down