Skip to content

Commit

Permalink
Add a list of subcategories to the Itunes Category (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
filaruina authored and mishako committed Nov 23, 2017
1 parent 2b93b6f commit aff28ea
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.rometools.modules.itunes.io;

import com.rometools.modules.itunes.types.Subcategory;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -75,9 +76,9 @@ public void generate(final Module module, final Element element) {
final Element category = generateSimpleElement("category", "");
category.setAttribute("text", cat.getName());

if (cat.getSubcategory() != null) {
for (Subcategory subcategory : cat.getSubcategories()) {
final Element subcat = generateSimpleElement("category", "");
subcat.setAttribute("text", cat.getSubcategory().getName());
subcat.setAttribute("text", subcategory.getName());
category.addContent(subcat);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ public com.rometools.rome.feed.module.Module parse(final Element element, final
final Category cat = new Category();
cat.setName(category.getAttribute("text").getValue().trim());

final Element subcategory = category.getChild("category", ns);

if (subcategory != null && subcategory.getAttribute("text") != null) {
final Subcategory subcat = new Subcategory();
subcat.setName(subcategory.getAttribute("text").getValue().trim());
cat.setSubcategory(subcat);
final List<Element> subCategories = category.getChildren("category", ns);

for (Element subCategory : subCategories) {
if (subCategory.getAttribute("text") != null) {
final Subcategory subcat = new Subcategory();
subcat.setName(subCategory.getAttribute("text").getValue().trim());
cat.addSubcategory(subcat);
}
}


feedInfo.getCategories().add(cat);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
package com.rometools.modules.itunes.types;

import com.rometools.utils.Lists;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* This Category information. Basically a name and an optional Subcategory. Categories are defined
Expand All @@ -26,7 +29,7 @@ public class Category implements Serializable {

private static final long serialVersionUID = 1L;
private String name;
private Subcategory subcategory;
private List<Subcategory> subcategories = new ArrayList<Subcategory>();

public Category() {
}
Expand Down Expand Up @@ -64,7 +67,16 @@ public void setName(final String name) {
* @return Returns the Subcategory object for this category
*/
public Subcategory getSubcategory() {
return subcategory;
return subcategories.isEmpty() ? null : subcategories.get(0);
}

/**
* Returns the list of subcategories under this category
*
* @return List of subcategories
*/
public List<Subcategory> getSubcategories() {
return subcategories;
}

/**
Expand All @@ -73,7 +85,15 @@ public Subcategory getSubcategory() {
* @param subcategory Sets the Subcategory object for this category
*/
public void setSubcategory(final Subcategory subcategory) {
this.subcategory = subcategory;
subcategories = Lists.create(subcategory);
}

public void setSubcategories(final List<Subcategory> subcategories) {
this.subcategories = subcategories;
}

public void addSubcategory(final Subcategory subcategory) {
this.subcategories.add(subcategory);
}

/**
Expand All @@ -85,10 +105,7 @@ public void setSubcategory(final Subcategory subcategory) {
public Object clone() {
final Category c = new Category();
c.setName(getName());

if (getSubcategory() != null) {
c.setSubcategory((Subcategory) getSubcategory().clone());
}
c.setSubcategories(getSubcategories());

return c;
}
Expand All @@ -97,8 +114,8 @@ public Object clone() {
public String toString() {
final StringBuffer sb = new StringBuffer(getName());

if (getSubcategory() != null) {
sb.append(" -> " + getSubcategory().toString());
for (Subcategory subcategory : getSubcategories()) {
sb.append(" -> " + subcategory.toString());
}

return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void testGetNamespaceUri() {
* Test of parse method, of class com.totsp.xml.syndication.itunes.ITunesParser.
*/
public void testParse() throws Exception {
File feed = new File(getTestFile("xml/leshow.xml"));
File feed = new File(getTestFile("itunes/leshow.xml"));
final SyndFeedInput input = new SyndFeedInput();
SyndFeed syndfeed = input.build(new XmlReader(feed.toURI().toURL()));

Expand All @@ -86,7 +86,14 @@ public void testParse() throws Exception {
assertEquals("owner", "Harry Shearer", feedInfo.getOwnerName());
assertEquals("email", "", feedInfo.getOwnerEmailAddress());
assertEquals("image", "http://a1.phobos.apple.com/Music/y2005/m06/d26/h21/mcdrrifv.jpg", feedInfo.getImage().toExternalForm());
assertEquals("category", "Comedy", feedInfo.getCategories().get(0).getName());
assertEquals("category1", "Comedy", feedInfo.getCategories().get(0).getName());
assertEquals("category2",
"Arts & Entertainment",
feedInfo.getCategories().get(1).getName());
assertEquals(
"subCategory",
"Entertainment",
feedInfo.getCategories().get(1).getSubcategories().get(0).getName());
assertEquals(
"summary",
"A weekly, hour-long romp through the worlds of media, politics, sports and show business, leavened with an eclectic mix of mysterious music, hosted by Harry Shearer.",
Expand Down
Loading

0 comments on commit aff28ea

Please sign in to comment.