Refactored code

This commit is contained in:
Patrick Gotthard 2014-04-17 23:11:59 +02:00
parent ef9345d55e
commit bc845fe0aa
61 changed files with 3204 additions and 3069 deletions

View file

@ -97,6 +97,10 @@
</build> </build>
<dependencies> <dependencies>
<dependency>
<groupId>com.rometools</groupId>
<artifactId>rome-utils</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.jdom</groupId> <groupId>org.jdom</groupId>
<artifactId>jdom</artifactId> <artifactId>jdom</artifactId>

View file

@ -18,11 +18,11 @@
package com.sun.syndication.feed; package com.sun.syndication.feed;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.jdom2.Element; import org.jdom2.Element;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
import com.sun.syndication.feed.module.Extendable; import com.sun.syndication.feed.module.Extendable;
import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.Module;
@ -43,8 +43,11 @@ import com.sun.syndication.feed.module.impl.ModuleUtils;
* *
*/ */
public abstract class WireFeed implements Cloneable, Serializable, Extendable { public abstract class WireFeed implements Cloneable, Serializable, Extendable {
private static final long serialVersionUID = -3608120400805691829L; private static final long serialVersionUID = -3608120400805691829L;
private final ObjectBean objBean; private final ObjectBean objBean;
private String feedType; private String feedType;
private String encoding; private String encoding;
private String styleSheet; private String styleSheet;
@ -53,8 +56,6 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
/** /**
* Default constructor, for bean cloning purposes only. * Default constructor, for bean cloning purposes only.
* <p>
*
*/ */
protected WireFeed() { protected WireFeed() {
objBean = new ObjectBean(this.getClass(), this); objBean = new ObjectBean(this.getClass(), this);
@ -62,7 +63,6 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
/** /**
* Creates a feed for a given type. * Creates a feed for a given type.
* <p>
* *
* @param type of the feed to create. * @param type of the feed to create.
* *
@ -74,7 +74,6 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
/** /**
* Creates a deep 'bean' clone of the object. * Creates a deep 'bean' clone of the object.
* <p>
* *
* @return a clone of the object. * @return a clone of the object.
* @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned.
@ -88,7 +87,6 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
/** /**
* Indicates whether some other object is "equal to" this one as defined by the Object equals() * Indicates whether some other object is "equal to" this one as defined by the Object equals()
* method. * method.
* <p>
* *
* @param other he reference object with which to compare. * @param other he reference object with which to compare.
* @return <b>true</b> if 'this' object is equal to the 'other' object. * @return <b>true</b> if 'this' object is equal to the 'other' object.
@ -96,12 +94,15 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
*/ */
@Override @Override
public boolean equals(final Object other) { public boolean equals(final Object other) {
if (other == null) { if (other == null) {
return false; return false;
} }
if (!(other instanceof WireFeed)) { if (!(other instanceof WireFeed)) {
return false; return false;
} }
// can't use foreign markup in equals, due to JDOM equals impl // can't use foreign markup in equals, due to JDOM equals impl
final List<Element> fm = getForeignMarkup(); final List<Element> fm = getForeignMarkup();
setForeignMarkup(((WireFeed) other).getForeignMarkup()); setForeignMarkup(((WireFeed) other).getForeignMarkup());
@ -109,6 +110,7 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
// restore foreign markup // restore foreign markup
setForeignMarkup(fm); setForeignMarkup(fm);
return ret; return ret;
} }
/** /**
@ -194,10 +196,7 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
*/ */
@Override @Override
public List<Module> getModules() { public List<Module> getModules() {
if (modules == null) { return modules = Lists.createWhenNull(modules);
modules = new ArrayList<Module>();
}
return modules;
} }
/** /**
@ -233,10 +232,7 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
* *
*/ */
public List<Element> getForeignMarkup() { public List<Element> getForeignMarkup() {
if (foreignMarkup == null) { return foreignMarkup = Lists.createWhenNull(foreignMarkup);
foreignMarkup = new ArrayList<Element>();
}
return foreignMarkup;
} }
/** /**
@ -269,4 +265,5 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
public void setStyleSheet(final String styleSheet) { public void setStyleSheet(final String styleSheet) {
this.styleSheet = styleSheet; this.styleSheet = styleSheet;
} }
} }

View file

@ -18,6 +18,7 @@ package com.sun.syndication.feed.atom;
import java.io.Serializable; import java.io.Serializable;
import com.rometools.utils.Alternatives;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
/** /**
@ -27,6 +28,7 @@ import com.sun.syndication.feed.impl.ObjectBean;
* @author Dave Johnson (added for Atom 1.0) * @author Dave Johnson (added for Atom 1.0)
*/ */
public class Category implements Cloneable, Serializable { public class Category implements Cloneable, Serializable {
private static final long serialVersionUID = -2034251366664065410L; private static final long serialVersionUID = -2034251366664065410L;
private final ObjectBean objBean; private final ObjectBean objBean;
@ -146,11 +148,7 @@ public class Category implements Cloneable, Serializable {
} }
public String getSchemeResolved() { public String getSchemeResolved() {
if (schemeResolved != null) { return Alternatives.firstNotNull(schemeResolved, scheme);
return schemeResolved;
} else {
return scheme;
}
} }
/** /**
@ -172,4 +170,5 @@ public class Category implements Cloneable, Serializable {
public void setTerm(final String term) { public void setTerm(final String term) {
this.term = term; this.term = term;
} }
} }

View file

@ -20,6 +20,7 @@ import java.io.Serializable;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import com.rometools.utils.Strings;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
/** /**
@ -30,6 +31,7 @@ import com.sun.syndication.feed.impl.ObjectBean;
* @author Dave Johnson (updated for Atom 1.0) * @author Dave Johnson (updated for Atom 1.0)
*/ */
public class Content implements Cloneable, Serializable { public class Content implements Cloneable, Serializable {
private static final long serialVersionUID = 2036205883043031310L; private static final long serialVersionUID = 2036205883043031310L;
private final ObjectBean objBean; private final ObjectBean objBean;
@ -173,14 +175,11 @@ public class Content implements Cloneable, Serializable {
* *
* @param mode the content mode, <b>null</b> if none. * @param mode the content mode, <b>null</b> if none.
*/ */
public void setMode(String mode) { public void setMode(final String mode) {
if (mode != null) { this.mode = Strings.toLowerCase(mode);
mode = mode.toLowerCase();
}
if (mode == null || !MODES.contains(mode)) { if (mode == null || !MODES.contains(mode)) {
throw new IllegalArgumentException("Invalid mode [" + mode + "]"); throw new IllegalArgumentException("Invalid mode [" + mode + "]");
} }
this.mode = mode;
} }
/** /**

View file

@ -17,12 +17,13 @@
package com.sun.syndication.feed.atom; package com.sun.syndication.feed.atom;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.jdom2.Element; import org.jdom2.Element;
import com.rometools.utils.Dates;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
import com.sun.syndication.feed.module.Extendable; import com.sun.syndication.feed.module.Extendable;
import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.Module;
@ -37,7 +38,9 @@ import com.sun.syndication.feed.synd.SyndPerson;
* @author Dave Johnson (updated for Atom 1.0) * @author Dave Johnson (updated for Atom 1.0)
*/ */
public class Entry implements Cloneable, Serializable, Extendable { public class Entry implements Cloneable, Serializable, Extendable {
private static final long serialVersionUID = 4874483180016783939L; private static final long serialVersionUID = 4874483180016783939L;
private Content summary; private Content summary;
private Content title; private Content title;
private Date created; // Atom 0.3 only private Date created; // Atom 0.3 only
@ -84,10 +87,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @return a list of Link elements with the entry alternate links, an empty list if none. * @return a list of Link elements with the entry alternate links, an empty list if none.
*/ */
public List<Link> getAlternateLinks() { public List<Link> getAlternateLinks() {
if (alternateLinks == null) { return alternateLinks = Lists.createWhenNull(alternateLinks);
alternateLinks = new ArrayList<Link>();
}
return alternateLinks;
} }
/** /**
@ -109,10 +109,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* *
*/ */
public List<SyndPerson> getAuthors() { public List<SyndPerson> getAuthors() {
if (authors == null) { return authors = Lists.createWhenNull(authors);
authors = new ArrayList<SyndPerson>();
}
return authors;
} }
/** /**
@ -134,10 +131,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @since Atom 1.0 * @since Atom 1.0
*/ */
public List<Category> getCategories() { public List<Category> getCategories() {
if (categories == null) { return categories = Lists.createWhenNull(categories);
categories = new ArrayList<Category>();
}
return categories;
} }
/** /**
@ -158,10 +152,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @return a list of Content elements with the entry contents, an empty list if none. * @return a list of Content elements with the entry contents, an empty list if none.
*/ */
public List<Content> getContents() { public List<Content> getContents() {
if (contents == null) { return contents = Lists.createWhenNull(contents);
contents = new ArrayList<Content>();
}
return contents;
} }
/** /**
@ -184,10 +175,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* *
*/ */
public List<SyndPerson> getContributors() { public List<SyndPerson> getContributors() {
if (contributors == null) { return contributors = Lists.createWhenNull(contributors);
contributors = new ArrayList<SyndPerson>();
}
return contributors;
} }
/** /**
@ -197,7 +185,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @param created the entry created date, <b>null</b> if none. * @param created the entry created date, <b>null</b> if none.
*/ */
public void setCreated(final Date created) { public void setCreated(final Date created) {
this.created = new Date(created.getTime()); this.created = Dates.copy(created);
} }
/** /**
@ -207,11 +195,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @return the entry created date, <b>null</b> if none. * @return the entry created date, <b>null</b> if none.
*/ */
public Date getCreated() { public Date getCreated() {
if (created == null) { return Dates.copy(created);
return null;
} else {
return new Date(created.getTime());
}
} }
/** /**
@ -233,10 +217,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* *
*/ */
public List<Element> getForeignMarkup() { public List<Element> getForeignMarkup() {
if (foreignMarkup == null) { return foreignMarkup = Lists.createWhenNull(foreignMarkup);
foreignMarkup = new ArrayList<Element>();
}
return foreignMarkup;
} }
/** /**
@ -268,11 +249,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @param issued the entry issued date, <b>null</b> if none. * @param issued the entry issued date, <b>null</b> if none.
*/ */
public void setIssued(final Date issued) { public void setIssued(final Date issued) {
if (issued == null) { published = Dates.copy(issued);
published = null;
} else {
published = new Date(issued.getTime());
}
} }
/** /**
@ -282,11 +259,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @return the entry issued date, <b>null</b> if none. * @return the entry issued date, <b>null</b> if none.
*/ */
public Date getIssued() { public Date getIssued() {
if (published == null) { return Dates.copy(published);
return null;
} else {
return new Date(published.getTime());
}
} }
/** /**
@ -297,15 +270,12 @@ public class Entry implements Cloneable, Serializable, Extendable {
public boolean isMediaEntry() { public boolean isMediaEntry() {
boolean mediaEntry = false; boolean mediaEntry = false;
final List<Link> links = getOtherLinks(); final List<Link> links = getOtherLinks();
for (final Link link : links) { for (final Link link : links) {
if ("edit-media".equals(link.getRel())) { if ("edit-media".equals(link.getRel())) {
mediaEntry = true; mediaEntry = true;
break; break;
} }
} }
return mediaEntry; return mediaEntry;
} }
@ -316,11 +286,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @param modified the entry modified date, <b>null</b> if none. * @param modified the entry modified date, <b>null</b> if none.
*/ */
public void setModified(final Date modified) { public void setModified(final Date modified) {
if (modified == null) { updated = Dates.copy(modified);
updated = null;
} else {
updated = new Date(modified.getTime());
}
} }
/** /**
@ -330,11 +296,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @return the entry modified date, <b>null</b> if none. * @return the entry modified date, <b>null</b> if none.
*/ */
public Date getModified() { public Date getModified() {
if (updated == null) { return Dates.copy(updated);
return null;
} else {
return new Date(updated.getTime());
}
} }
/** /**
@ -371,10 +333,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
*/ */
@Override @Override
public List<Module> getModules() { public List<Module> getModules() {
if (modules == null) { return modules = Lists.createWhenNull(modules);
modules = new ArrayList<Module>();
}
return modules;
} }
/** /**
@ -396,10 +355,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* none. * none.
*/ */
public List<Link> getOtherLinks() { public List<Link> getOtherLinks() {
if (otherLinks == null) { return otherLinks = Lists.createWhenNull(otherLinks);
otherLinks = new ArrayList<Link>();
}
return otherLinks;
} }
/** /**
@ -410,11 +366,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @since Atom 1.0 * @since Atom 1.0
*/ */
public void setPublished(final Date published) { public void setPublished(final Date published) {
if (published == null) { this.published = Dates.copy(published);
this.published = null;
} else {
this.published = new Date(published.getTime());
}
} }
/** /**
@ -425,11 +377,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @since Atom 1.0 * @since Atom 1.0
*/ */
public Date getPublished() { public Date getPublished() {
if (published == null) { return Dates.copy(published);
return null;
} else {
return new Date(published.getTime());
}
} }
/** /**
@ -507,7 +455,6 @@ public class Entry implements Cloneable, Serializable, Extendable {
if (this.title == null) { if (this.title == null) {
this.title = new Content(); this.title = new Content();
} }
this.title.setValue(title); this.title.setValue(title);
} }
@ -522,7 +469,6 @@ public class Entry implements Cloneable, Serializable, Extendable {
if (title != null) { if (title != null) {
return title.getValue(); return title.getValue();
} }
return null; return null;
} }
@ -556,11 +502,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @since Atom 1.0 * @since Atom 1.0
*/ */
public void setUpdated(final Date updated) { public void setUpdated(final Date updated) {
if (updated == null) { this.updated = Dates.copy(updated);
this.updated = null;
} else {
this.updated = new Date(updated.getTime());
}
} }
/** /**
@ -571,11 +513,7 @@ public class Entry implements Cloneable, Serializable, Extendable {
* @since Atom 1.0 * @since Atom 1.0
*/ */
public Date getUpdated() { public Date getUpdated() {
if (updated == null) { return Dates.copy(updated);
return null;
} else {
return new Date(updated.getTime());
}
} }
/** /**
@ -668,11 +606,12 @@ public class Entry implements Cloneable, Serializable, Extendable {
} }
public Link findRelatedLink(final String relation) { public Link findRelatedLink(final String relation) {
for (final Link l : otherLinks) { for (final Link link : otherLinks) {
if (relation.equals(l.getRel())) { if (relation.equals(link.getRel())) {
return l; return link;
} }
} }
return null; return null;
} }
} }

View file

@ -16,10 +16,10 @@
*/ */
package com.sun.syndication.feed.atom; package com.sun.syndication.feed.atom;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.Module;
import com.sun.syndication.feed.module.impl.ModuleUtils; import com.sun.syndication.feed.module.impl.ModuleUtils;
@ -35,7 +35,9 @@ import com.sun.syndication.feed.synd.SyndPerson;
* @author Dave Johnson (updated for Atom 1.0) * @author Dave Johnson (updated for Atom 1.0)
*/ */
public class Feed extends WireFeed { public class Feed extends WireFeed {
private static final long serialVersionUID = -9175445106675422528L; private static final long serialVersionUID = -9175445106675422528L;
private String xmlBase; private String xmlBase;
private List<Category> categories; private List<Category> categories;
private List<SyndPerson> authors; private List<SyndPerson> authors;
@ -154,10 +156,7 @@ public class Feed extends WireFeed {
* @return a list of Link elements with the feed alternate links, an empty list if none. * @return a list of Link elements with the feed alternate links, an empty list if none.
*/ */
public List<Link> getAlternateLinks() { public List<Link> getAlternateLinks() {
if (alternateLinks == null) { return alternateLinks = Lists.createWhenNull(alternateLinks);
alternateLinks = new ArrayList<Link>();
}
return alternateLinks;
} }
/** /**
@ -179,10 +178,7 @@ public class Feed extends WireFeed {
* if none. * if none.
*/ */
public List<Link> getOtherLinks() { public List<Link> getOtherLinks() {
if (otherLinks == null) { return otherLinks = Lists.createWhenNull(otherLinks);
otherLinks = new ArrayList<Link>();
}
return otherLinks;
} }
/** /**
@ -204,10 +200,7 @@ public class Feed extends WireFeed {
* *
*/ */
public List<SyndPerson> getAuthors() { public List<SyndPerson> getAuthors() {
if (authors == null) { return authors = Lists.createWhenNull(authors);
authors = new ArrayList<SyndPerson>();
}
return authors;
} }
/** /**
@ -229,10 +222,7 @@ public class Feed extends WireFeed {
* *
*/ */
public List<SyndPerson> getContributors() { public List<SyndPerson> getContributors() {
if (contributors == null) { return contributors = Lists.createWhenNull(contributors);
contributors = new ArrayList<SyndPerson>();
}
return contributors;
} }
/** /**
@ -380,10 +370,7 @@ public class Feed extends WireFeed {
* *
*/ */
public List<Entry> getEntries() { public List<Entry> getEntries() {
if (entries == null) { return entries = Lists.createWhenNull(entries);
entries = new ArrayList<Entry>();
}
return entries;
} }
/** /**
@ -407,10 +394,7 @@ public class Feed extends WireFeed {
*/ */
@Override @Override
public List<Module> getModules() { public List<Module> getModules() {
if (modules == null) { return modules = Lists.createWhenNull(modules);
modules = new ArrayList<Module>();
}
return modules;
} }
/** /**
@ -446,10 +430,7 @@ public class Feed extends WireFeed {
* @since Atom 1.0 * @since Atom 1.0
*/ */
public List<Category> getCategories() { public List<Category> getCategories() {
if (categories == null) { return categories = Lists.createWhenNull(categories);
categories = new ArrayList<Category>();
}
return categories;
} }
/** /**

View file

@ -18,6 +18,7 @@ package com.sun.syndication.feed.atom;
import java.io.Serializable; import java.io.Serializable;
import com.rometools.utils.Alternatives;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
/** /**
@ -28,6 +29,7 @@ import com.sun.syndication.feed.impl.ObjectBean;
* @author Dave Johnson (updated for Atom 1.0) * @author Dave Johnson (updated for Atom 1.0)
*/ */
public class Link implements Cloneable, Serializable { public class Link implements Cloneable, Serializable {
private static final long serialVersionUID = 670365139518027828L; private static final long serialVersionUID = 670365139518027828L;
private final ObjectBean objBean; private final ObjectBean objBean;
@ -174,11 +176,7 @@ public class Link implements Cloneable, Serializable {
} }
public String getHrefResolved() { public String getHrefResolved() {
if (hrefResolved != null) { return Alternatives.firstNotNull(hrefResolved, href);
return hrefResolved;
} else {
return href;
}
} }
/** /**

View file

@ -17,9 +17,10 @@
package com.sun.syndication.feed.atom; package com.sun.syndication.feed.atom;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.rometools.utils.Alternatives;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
import com.sun.syndication.feed.module.Extendable; import com.sun.syndication.feed.module.Extendable;
import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.Module;
@ -156,11 +157,7 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable {
} }
public String getUriResolved(final String resolveURI) { public String getUriResolved(final String resolveURI) {
if (uriResolved != null) { return Alternatives.firstNotNull(uriResolved, uri);
return uriResolved;
} else {
return uri;
}
} }
/** /**
@ -220,10 +217,7 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable {
*/ */
@Override @Override
public List<Module> getModules() { public List<Module> getModules() {
if (modules == null) { return modules = Lists.createWhenNull(modules);
modules = new ArrayList<Module>();
}
return modules;
} }
/** /**

View file

@ -16,7 +16,6 @@
*/ */
package com.sun.syndication.feed.module; package com.sun.syndication.feed.module;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -25,6 +24,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.CopyFrom; import com.sun.syndication.feed.CopyFrom;
import com.sun.syndication.feed.impl.CopyFromHelper; import com.sun.syndication.feed.impl.CopyFromHelper;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
@ -38,7 +38,9 @@ import com.sun.syndication.feed.impl.ObjectBean;
* *
*/ */
public class DCModuleImpl extends ModuleImpl implements DCModule { public class DCModuleImpl extends ModuleImpl implements DCModule {
private static final long serialVersionUID = -6502372914221178645L; private static final long serialVersionUID = -6502372914221178645L;
private final ObjectBean objBean; private final ObjectBean objBean;
private List<String> title; private List<String> title;
private List<String> creator; private List<String> creator;
@ -106,10 +108,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getTitles() { public List<String> getTitles() {
if (title == null) { return title = Lists.createWhenNull(title);
title = new ArrayList<String>();
}
return title;
} }
/** /**
@ -134,11 +133,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getTitle() { public String getTitle() {
if (title != null && !title.isEmpty()) { return Lists.firstEntry(title);
return title.get(0);
} else {
return null;
}
} }
/** /**
@ -151,8 +146,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setTitle(final String title) { public void setTitle(final String title) {
this.title = new ArrayList<String>(); this.title = Lists.create(title);
this.title.add(title);
} }
/** /**
@ -164,10 +158,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getCreators() { public List<String> getCreators() {
if (creator == null) { return creator = Lists.createWhenNull(creator);
creator = new ArrayList<String>();
}
return creator;
} }
/** /**
@ -192,11 +183,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getCreator() { public String getCreator() {
if (creator != null && !creator.isEmpty()) { return Lists.firstEntry(creator);
return creator.get(0);
} else {
return null;
}
} }
/** /**
@ -209,8 +196,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setCreator(final String creator) { public void setCreator(final String creator) {
this.creator = new ArrayList<String>(); this.creator = Lists.create(creator);
this.creator.add(creator);
} }
/** /**
@ -223,10 +209,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<DCSubject> getSubjects() { public List<DCSubject> getSubjects() {
if (subject == null) { return subject = Lists.createWhenNull(subject);
subject = new ArrayList<DCSubject>();
}
return subject;
} }
/** /**
@ -251,11 +234,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public DCSubject getSubject() { public DCSubject getSubject() {
if (subject != null && !subject.isEmpty()) { return Lists.firstEntry(subject);
return subject.get(0);
} else {
return null;
}
} }
/** /**
@ -268,8 +247,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setSubject(final DCSubject subject) { public void setSubject(final DCSubject subject) {
this.subject = new ArrayList<DCSubject>(); this.subject = Lists.create(subject);
this.subject.add(subject);
} }
/** /**
@ -282,10 +260,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getDescriptions() { public List<String> getDescriptions() {
if (description == null) { return description = Lists.createWhenNull(description);
description = new ArrayList<String>();
}
return description;
} }
/** /**
@ -310,11 +285,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getDescription() { public String getDescription() {
if (description != null && !description.isEmpty()) { return Lists.firstEntry(description);
return description.get(0);
} else {
return null;
}
} }
/** /**
@ -327,8 +298,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setDescription(final String description) { public void setDescription(final String description) {
this.description = new ArrayList<String>(); this.description = Lists.create(description);
this.description.add(description);
} }
/** /**
@ -341,10 +311,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getPublishers() { public List<String> getPublishers() {
if (publisher == null) { return publisher = Lists.createWhenNull(publisher);
publisher = new ArrayList<String>();
}
return publisher;
} }
/** /**
@ -369,11 +336,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getPublisher() { public String getPublisher() {
if (publisher != null && !publisher.isEmpty()) { return Lists.firstEntry(publisher);
return publisher.get(0);
} else {
return null;
}
} }
/** /**
@ -386,8 +349,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setPublisher(final String publisher) { public void setPublisher(final String publisher) {
this.publisher = new ArrayList<String>(); this.publisher = Lists.create(publisher);
this.publisher.add(publisher);
} }
/** /**
@ -400,10 +362,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getContributors() { public List<String> getContributors() {
if (contributors == null) { return contributors = Lists.createWhenNull(contributors);
contributors = new ArrayList<String>();
}
return contributors;
} }
/** /**
@ -428,11 +387,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getContributor() { public String getContributor() {
if (contributors != null && !contributors.isEmpty()) { return Lists.firstEntry(contributors);
return contributors.get(0);
} else {
return null;
}
} }
/** /**
@ -445,8 +400,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setContributor(final String contributor) { public void setContributor(final String contributor) {
contributors = new ArrayList<String>(); contributors = Lists.create(contributor);
contributors.add(contributor);
} }
/** /**
@ -458,10 +412,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<Date> getDates() { public List<Date> getDates() {
if (date == null) { return date = Lists.createWhenNull(date);
date = new ArrayList<Date>();
}
return date;
} }
/** /**
@ -486,11 +437,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public Date getDate() { public Date getDate() {
if (date != null && !date.isEmpty()) { return Lists.firstEntry(date);
return date.get(0);
} else {
return null;
}
} }
/** /**
@ -503,8 +450,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setDate(final Date date) { public void setDate(final Date date) {
this.date = new ArrayList<Date>(); this.date = Lists.create(date);
this.date.add(date);
} }
/** /**
@ -516,10 +462,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getTypes() { public List<String> getTypes() {
if (type == null) { return type = Lists.createWhenNull(type);
type = new ArrayList<String>();
}
return type;
} }
/** /**
@ -544,11 +487,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getType() { public String getType() {
if (type != null && !type.isEmpty()) { return Lists.firstEntry(type);
return type.get(0);
} else {
return null;
}
} }
/** /**
@ -561,8 +500,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setType(final String type) { public void setType(final String type) {
this.type = new ArrayList<String>(); this.type = Lists.create(type);
this.type.add(type);
} }
/** /**
@ -574,10 +512,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getFormats() { public List<String> getFormats() {
if (format == null) { return format = Lists.createWhenNull(format);
format = new ArrayList<String>();
}
return format;
} }
/** /**
@ -602,11 +537,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getFormat() { public String getFormat() {
if (format != null && !format.isEmpty()) { return Lists.firstEntry(format);
return format.get(0);
} else {
return null;
}
} }
/** /**
@ -619,8 +550,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setFormat(final String format) { public void setFormat(final String format) {
this.format = new ArrayList<String>(); this.format = Lists.create(format);
this.format.add(format);
} }
/** /**
@ -633,10 +563,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getIdentifiers() { public List<String> getIdentifiers() {
if (identifier == null) { return identifier = Lists.createWhenNull(identifier);
identifier = new ArrayList<String>();
}
return identifier;
} }
/** /**
@ -661,11 +588,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getIdentifier() { public String getIdentifier() {
if (identifier != null && !identifier.isEmpty()) { return Lists.firstEntry(identifier);
return identifier.get(0);
} else {
return null;
}
} }
/** /**
@ -678,8 +601,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setIdentifier(final String identifier) { public void setIdentifier(final String identifier) {
this.identifier = new ArrayList<String>(); this.identifier = Lists.create(identifier);
this.identifier.add(identifier);
} }
/** /**
@ -691,10 +613,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getSources() { public List<String> getSources() {
if (source == null) { return source = Lists.createWhenNull(source);
source = new ArrayList<String>();
}
return source;
} }
/** /**
@ -719,11 +638,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getSource() { public String getSource() {
if (source != null && !source.isEmpty()) { return Lists.firstEntry(source);
return source.get(0);
} else {
return null;
}
} }
/** /**
@ -736,8 +651,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setSource(final String source) { public void setSource(final String source) {
this.source = new ArrayList<String>(); this.source = Lists.create(source);
this.source.add(source);
} }
/** /**
@ -749,10 +663,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getLanguages() { public List<String> getLanguages() {
if (language == null) { return language = Lists.createWhenNull(language);
language = new ArrayList<String>();
}
return language;
} }
/** /**
@ -777,11 +688,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getLanguage() { public String getLanguage() {
if (language != null && !language.isEmpty()) { return Lists.firstEntry(language);
return language.get(0);
} else {
return null;
}
} }
/** /**
@ -794,8 +701,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setLanguage(final String language) { public void setLanguage(final String language) {
this.language = new ArrayList<String>(); this.language = Lists.create(language);
this.language.add(language);
} }
/** /**
@ -807,10 +713,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getRelations() { public List<String> getRelations() {
if (relation == null) { return relation = Lists.createWhenNull(relation);
relation = new ArrayList<String>();
}
return relation;
} }
/** /**
@ -835,11 +738,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getRelation() { public String getRelation() {
if (relation != null && !relation.isEmpty()) { return Lists.firstEntry(relation);
return relation.get(0);
} else {
return null;
}
} }
/** /**
@ -852,8 +751,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setRelation(final String relation) { public void setRelation(final String relation) {
this.relation = new ArrayList<String>(); this.relation = Lists.create(relation);
this.relation.add(relation);
} }
/** /**
@ -865,10 +763,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getCoverages() { public List<String> getCoverages() {
if (coverage == null) { return coverage = Lists.createWhenNull(coverage);
coverage = new ArrayList<String>();
}
return coverage;
} }
/** /**
@ -893,11 +788,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getCoverage() { public String getCoverage() {
if (coverage != null && !coverage.isEmpty()) { return Lists.firstEntry(coverage);
return coverage.get(0);
} else {
return null;
}
} }
/** /**
@ -910,8 +801,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setCoverage(final String coverage) { public void setCoverage(final String coverage) {
this.coverage = new ArrayList<String>(); this.coverage = Lists.create(coverage);
this.coverage.add(coverage);
} }
/** /**
@ -923,10 +813,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public List<String> getRightsList() { public List<String> getRightsList() {
if (rights == null) { return rights = Lists.createWhenNull(rights);
rights = new ArrayList<String>();
}
return rights;
} }
/** /**
@ -951,11 +838,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public String getRights() { public String getRights() {
if (rights != null && !rights.isEmpty()) { return Lists.firstEntry(rights);
return rights.get(0);
} else {
return null;
}
} }
/** /**
@ -968,8 +851,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
*/ */
@Override @Override
public void setRights(final String rights) { public void setRights(final String rights) {
this.rights = new ArrayList<String>(); this.rights = Lists.create(rights);
this.rights.add(rights);
} }
/** /**
@ -1060,4 +942,5 @@ public class DCModuleImpl extends ModuleImpl implements DCModule {
COPY_FROM_HELPER = new CopyFromHelper(DCModule.class, basePropInterfaceMap, basePropClassImplMap); COPY_FROM_HELPER = new CopyFromHelper(DCModule.class, basePropInterfaceMap, basePropClassImplMap);
} }
} }

View file

@ -34,11 +34,24 @@ import com.sun.syndication.feed.impl.ObjectBean;
* *
*/ */
public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { public class DCSubjectImpl implements Cloneable, Serializable, DCSubject {
private static final long serialVersionUID = 6276396184267118968L; private static final long serialVersionUID = 6276396184267118968L;
private static final CopyFromHelper COPY_FROM_HELPER;
private final ObjectBean objBean; private final ObjectBean objBean;
private String taxonomyUri; private String taxonomyUri;
private String value; private String value;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("taxonomyUri", String.class);
basePropInterfaceMap.put("value", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(DCSubject.class, basePropInterfaceMap, basePropClassImplMap);
}
/** /**
* Default constructor. All properties are set to <b>null</b>. * Default constructor. All properties are set to <b>null</b>.
* <p> * <p>
@ -162,16 +175,4 @@ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject {
COPY_FROM_HELPER.copy(this, obj); COPY_FROM_HELPER.copy(this, obj);
} }
private static final CopyFromHelper COPY_FROM_HELPER;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("taxonomyUri", String.class);
basePropInterfaceMap.put("value", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(DCSubject.class, basePropInterfaceMap, basePropClassImplMap);
}
} }

View file

@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.rometools.utils.Dates;
import com.sun.syndication.feed.CopyFrom; import com.sun.syndication.feed.CopyFrom;
import com.sun.syndication.feed.impl.CopyFromHelper; import com.sun.syndication.feed.impl.CopyFromHelper;
@ -35,20 +36,32 @@ import com.sun.syndication.feed.impl.CopyFromHelper;
* *
*/ */
public class SyModuleImpl extends ModuleImpl implements SyModule { public class SyModuleImpl extends ModuleImpl implements SyModule {
private static final long serialVersionUID = -8345879299577437933L; private static final long serialVersionUID = -8345879299577437933L;
private static final Set<String> PERIODS = new HashSet<String>(); private static final Set<String> PERIODS = new HashSet<String>();
private static final CopyFromHelper COPY_FROM_HELPER;
private String updatePeriod;
private int updateFrequency;
private Date updateBase;
static { static {
PERIODS.add(HOURLY); PERIODS.add(HOURLY);
PERIODS.add(DAILY); PERIODS.add(DAILY);
PERIODS.add(WEEKLY); PERIODS.add(WEEKLY);
PERIODS.add(MONTHLY); PERIODS.add(MONTHLY);
PERIODS.add(YEARLY); PERIODS.add(YEARLY);
}
private String updatePeriod; final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
private int updateFrequency; basePropInterfaceMap.put("updatePeriod", String.class);
private Date updateBase; basePropInterfaceMap.put("updateFrequency", Integer.TYPE);
basePropInterfaceMap.put("updateBase", Date.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyModule.class, basePropInterfaceMap, basePropClassImplMap);
}
/** /**
* Default constructor. All properties are set to <b>null</b>. * Default constructor. All properties are set to <b>null</b>.
@ -119,7 +132,7 @@ public class SyModuleImpl extends ModuleImpl implements SyModule {
*/ */
@Override @Override
public Date getUpdateBase() { public Date getUpdateBase() {
return updateBase != null ? new Date(updateBase.getTime()) : null; return Dates.copy(updateBase);
} }
/** /**
@ -131,7 +144,7 @@ public class SyModuleImpl extends ModuleImpl implements SyModule {
*/ */
@Override @Override
public void setUpdateBase(final Date updateBase) { public void setUpdateBase(final Date updateBase) {
this.updateBase = new Date(updateBase.getTime()); this.updateBase = Dates.copy(updateBase);
} }
@Override @Override
@ -144,17 +157,4 @@ public class SyModuleImpl extends ModuleImpl implements SyModule {
COPY_FROM_HELPER.copy(this, obj); COPY_FROM_HELPER.copy(this, obj);
} }
private static final CopyFromHelper COPY_FROM_HELPER;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("updatePeriod", String.class);
basePropInterfaceMap.put("updateFrequency", Integer.TYPE);
basePropInterfaceMap.put("updateBase", Date.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyModule.class, basePropInterfaceMap, basePropClassImplMap);
}
} }

View file

@ -17,13 +17,14 @@
*/ */
package com.sun.syndication.feed.rss; package com.sun.syndication.feed.rss;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import com.rometools.utils.Dates;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.Module;
import com.sun.syndication.feed.module.impl.ModuleUtils; import com.sun.syndication.feed.module.impl.ModuleUtils;
@ -39,7 +40,9 @@ import com.sun.syndication.feed.module.impl.ModuleUtils;
* *
*/ */
public class Channel extends WireFeed { public class Channel extends WireFeed {
private static final long serialVersionUID = 745207486449728472L; private static final long serialVersionUID = 745207486449728472L;
public static final String SUNDAY = "sunday"; public static final String SUNDAY = "sunday";
public static final String MONDAY = "monday"; public static final String MONDAY = "monday";
public static final String TUESDAY = "tuesday"; public static final String TUESDAY = "tuesday";
@ -219,10 +222,7 @@ public class Channel extends WireFeed {
* *
*/ */
public List<Item> getItems() { public List<Item> getItems() {
if (items == null) { return items = Lists.createWhenNull(items);
items = new ArrayList<Item>();
}
return items;
} }
/** /**
@ -333,11 +333,7 @@ public class Channel extends WireFeed {
* *
*/ */
public Date getPubDate() { public Date getPubDate() {
if (pubDate == null) { return Dates.copy(pubDate);
return null;
} else {
return new Date(pubDate.getTime());
}
} }
/** /**
@ -348,11 +344,7 @@ public class Channel extends WireFeed {
* *
*/ */
public void setPubDate(final Date pubDate) { public void setPubDate(final Date pubDate) {
if (pubDate == null) { this.pubDate = Dates.copy(pubDate);
this.pubDate = null;
} else {
this.pubDate = new Date(pubDate.getTime());
}
} }
/** /**
@ -363,11 +355,7 @@ public class Channel extends WireFeed {
* *
*/ */
public Date getLastBuildDate() { public Date getLastBuildDate() {
if (lastBuildDate == null) { return Dates.copy(lastBuildDate);
return null;
} else {
return new Date(lastBuildDate.getTime());
}
} }
/** /**
@ -378,11 +366,7 @@ public class Channel extends WireFeed {
* *
*/ */
public void setLastBuildDate(final Date lastBuildDate) { public void setLastBuildDate(final Date lastBuildDate) {
if (lastBuildDate == null) { this.lastBuildDate = Dates.copy(lastBuildDate);
this.lastBuildDate = null;
} else {
this.lastBuildDate = new Date(lastBuildDate.getTime());
}
} }
/** /**
@ -459,11 +443,7 @@ public class Channel extends WireFeed {
* *
*/ */
public List<Integer> getSkipHours() { public List<Integer> getSkipHours() {
if (skipHours != null) { return Lists.createWhenNull(skipHours);
return skipHours;
} else {
return new ArrayList<Integer>();
}
} }
/** /**
@ -499,11 +479,7 @@ public class Channel extends WireFeed {
* *
*/ */
public List<String> getSkipDays() { public List<String> getSkipDays() {
if (skipDays != null) { return Lists.createWhenNull(skipDays);
return skipDays;
} else {
return new ArrayList<String>();
}
} }
/** /**
@ -562,10 +538,7 @@ public class Channel extends WireFeed {
* *
*/ */
public List<Category> getCategories() { public List<Category> getCategories() {
if (categories == null) { return categories = Lists.createWhenNull(categories);
categories = new ArrayList<Category>();
}
return categories;
} }
/** /**
@ -633,10 +606,7 @@ public class Channel extends WireFeed {
*/ */
@Override @Override
public List<Module> getModules() { public List<Module> getModules() {
if (modules == null) { return modules = Lists.createWhenNull(modules);
modules = new ArrayList<Module>();
}
return modules;
} }
/** /**

View file

@ -18,12 +18,13 @@
package com.sun.syndication.feed.rss; package com.sun.syndication.feed.rss;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.jdom2.Element; import org.jdom2.Element;
import com.rometools.utils.Dates;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
import com.sun.syndication.feed.module.Extendable; import com.sun.syndication.feed.module.Extendable;
import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.Module;
@ -42,8 +43,11 @@ import com.sun.syndication.feed.module.impl.ModuleUtils;
* *
*/ */
public class Item implements Cloneable, Serializable, Extendable { public class Item implements Cloneable, Serializable, Extendable {
private static final long serialVersionUID = 3741763947754555947L; private static final long serialVersionUID = 3741763947754555947L;
private final ObjectBean objBean; private final ObjectBean objBean;
private String title; private String title;
private String link; private String link;
private String uri; private String uri;
@ -269,10 +273,7 @@ public class Item implements Cloneable, Serializable, Extendable {
* *
*/ */
public List<Enclosure> getEnclosures() { public List<Enclosure> getEnclosures() {
if (enclosures == null) { return enclosures = Lists.createWhenNull(enclosures);
enclosures = new ArrayList<Enclosure>();
}
return enclosures;
} }
/** /**
@ -295,10 +296,7 @@ public class Item implements Cloneable, Serializable, Extendable {
* *
*/ */
public List<Category> getCategories() { public List<Category> getCategories() {
if (categories == null) { return categories = Lists.createWhenNull(categories);
categories = new ArrayList<Category>();
}
return categories;
} }
/** /**
@ -388,10 +386,7 @@ public class Item implements Cloneable, Serializable, Extendable {
*/ */
@Override @Override
public List<Module> getModules() { public List<Module> getModules() {
if (modules == null) { return modules = Lists.createWhenNull(modules);
modules = new ArrayList<Module>();
}
return modules;
} }
/** /**
@ -427,11 +422,7 @@ public class Item implements Cloneable, Serializable, Extendable {
* *
*/ */
public Date getPubDate() { public Date getPubDate() {
if (pubDate == null) { return Dates.copy(pubDate);
return null;
} else {
return new Date(pubDate.getTime());
}
} }
/** /**
@ -442,11 +433,7 @@ public class Item implements Cloneable, Serializable, Extendable {
* *
*/ */
public void setPubDate(final Date pubDate) { public void setPubDate(final Date pubDate) {
if (pubDate == null) { this.pubDate = Dates.copy(pubDate);
this.pubDate = null;
} else {
this.pubDate = new Date(pubDate.getTime());
}
} }
/** /**
@ -457,11 +444,7 @@ public class Item implements Cloneable, Serializable, Extendable {
* *
*/ */
public Date getExpirationDate() { public Date getExpirationDate() {
if (expirationDate == null) { return Dates.copy(expirationDate);
return null;
} else {
return new Date(expirationDate.getTime());
}
} }
/** /**
@ -472,11 +455,7 @@ public class Item implements Cloneable, Serializable, Extendable {
* *
*/ */
public void setExpirationDate(final Date expirationDate) { public void setExpirationDate(final Date expirationDate) {
if (expirationDate == null) { this.expirationDate = Dates.copy(expirationDate);
this.expirationDate = null;
} else {
this.expirationDate = new Date(expirationDate.getTime());
}
} }
/** /**
@ -487,10 +466,7 @@ public class Item implements Cloneable, Serializable, Extendable {
* *
*/ */
public List<Element> getForeignMarkup() { public List<Element> getForeignMarkup() {
if (foreignMarkup == null) { return foreignMarkup = Lists.createWhenNull(foreignMarkup);
foreignMarkup = new ArrayList<Element>();
}
return foreignMarkup;
} }
/** /**

View file

@ -18,11 +18,8 @@
package com.sun.syndication.feed.synd; package com.sun.syndication.feed.synd;
import java.io.Serializable; import java.io.Serializable;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.sun.syndication.feed.CopyFrom; import com.sun.syndication.feed.CopyFrom;
@ -39,10 +36,22 @@ import com.sun.syndication.feed.module.DCSubjectImpl;
* *
*/ */
public class SyndCategoryImpl implements Serializable, SyndCategory { public class SyndCategoryImpl implements Serializable, SyndCategory {
private static final long serialVersionUID = -2151815243404151131L; private static final long serialVersionUID = -2151815243404151131L;
private static final CopyFromHelper COPY_FROM_HELPER;
private final ObjectBean objBean; private final ObjectBean objBean;
private final DCSubject subject; private final DCSubject subject;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("name", String.class);
basePropInterfaceMap.put("taxonomyUri", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndCategory.class, basePropInterfaceMap, basePropClassImplMap);
}
/** /**
* For implementations extending SyndContentImpl to be able to use the ObjectBean functionality * For implementations extending SyndContentImpl to be able to use the ObjectBean functionality
* with extended interfaces. * with extended interfaces.
@ -189,168 +198,4 @@ public class SyndCategoryImpl implements Serializable, SyndCategory {
COPY_FROM_HELPER.copy(this, obj); COPY_FROM_HELPER.copy(this, obj);
} }
private static final CopyFromHelper COPY_FROM_HELPER;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("name", String.class);
basePropInterfaceMap.put("taxonomyUri", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndCategory.class, basePropInterfaceMap, basePropClassImplMap);
}
}
/**
* List implementation for SyndCategoryImpl elements. To be directly used by the SyndFeedImpl and
* SyndEntryImpl classes only.
* <p>
* It acts as a facade on top of the DCSubjectImpl elements of the underlying list and remains in
* synch with it. It is possible to work on either list, the categories one or the subjects one and
* they remain in synch.
* <p>
* This is necessary because the SyndFeedImpl categories are just a convenience to access the
* DublinCore subjects.
* <P>
* All this mess to avoid making DCSubjectImpl implement SyndCategory (which it would be odd).
* <p>
*
* @author Alejandro Abdelnur
*
*/
class SyndCategoryListFacade extends AbstractList<SyndCategory> {
private final List<DCSubject> subjects;
/**
* Default constructor. Creates and empty list.
*/
public SyndCategoryListFacade() {
this(new ArrayList<DCSubject>());
}
/**
* Creates a facade list of categories on top the given subject list.
* <P>
*
* @param subjects the list of subjects to create the facade.
*
*/
public SyndCategoryListFacade(final List<DCSubject> subjects) {
this.subjects = subjects;
}
/**
* Gets the category by index.
* <p>
*
* @param index the index position to retrieve the category.
* @return the SyndCategoryImpl in position index, <b>null</b> if none.
*
*/
@Override
public SyndCategory get(final int index) {
return new SyndCategoryImpl(subjects.get(index));
}
/**
* Returns the size of the list.
* <p>
*
* @return the size of the list.
*
*/
@Override
public int size() {
return subjects.size();
}
/**
* Sets a category in an existing position in the list.
* <p>
*
* @param index position to set the category.
* @param obj the SyndCategoryImpl object to set.
* @return the SyndCategoryImpl object that is being replaced, <b>null</b> if none.
*
*/
@Override
public SyndCategory set(final int index, final SyndCategory obj) {
final SyndCategoryImpl sCat = (SyndCategoryImpl) obj;
DCSubject subject;
if (sCat != null) {
subject = sCat.getSubject();
} else {
subject = null;
}
subject = subjects.set(index, subject);
if (subject != null) {
return new SyndCategoryImpl(subject);
} else {
return null;
}
}
/**
* Adds a category to the list.
* <p>
*
* @param index position to add the category.
* @param obj the SyndCategoryImpl object to add.
*
*/
@Override
public void add(final int index, final SyndCategory obj) {
final SyndCategoryImpl sCat = (SyndCategoryImpl) obj;
DCSubject subject;
if (sCat != null) {
subject = sCat.getSubject();
} else {
subject = null;
}
subjects.add(index, subject);
}
/**
* Removes a category element from a specific position.
* <p>
*
* @param index position to remove the category from.
* @return the SyndCategoryImpl being removed from position index, <b>null</b> if none.
*
*/
@Override
public SyndCategory remove(final int index) {
final DCSubject subject = subjects.remove(index);
if (subject != null) {
return new SyndCategoryImpl(subject);
} else {
return null;
}
}
/**
* Returns a list with the DCSubject elements of the SyndCategoryImpl list facade. To be used by
* the SyndFeedImpl class only.
* <p>
*
* @param cList the list with SyndCategoryImpl elements to convert to subject list.
* @return a list with DCSubject elements corresponding to the categories in the given list.
*
*/
public static List<DCSubject> convertElementsSyndCategoryToSubject(final List<SyndCategory> cList) {
List<DCSubject> sList = null;
if (cList != null) {
sList = new ArrayList<DCSubject>();
for (int i = 0; i < cList.size(); i++) {
final SyndCategoryImpl sCat = (SyndCategoryImpl) cList.get(i);
DCSubject subject = null;
if (sCat != null) {
subject = sCat.getSubject();
}
sList.add(subject);
}
}
return sList;
}
} }

View file

@ -0,0 +1,162 @@
package com.sun.syndication.feed.synd;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import com.sun.syndication.feed.module.DCSubject;
/**
* List implementation for SyndCategoryImpl elements. To be directly used by the SyndFeedImpl and
* SyndEntryImpl classes only.
* <p>
* It acts as a facade on top of the DCSubjectImpl elements of the underlying list and remains in
* synch with it. It is possible to work on either list, the categories one or the subjects one and
* they remain in synch.
* <p>
* This is necessary because the SyndFeedImpl categories are just a convenience to access the
* DublinCore subjects.
* <P>
* All this mess to avoid making DCSubjectImpl implement SyndCategory (which it would be odd).
* <p>
*
* @author Alejandro Abdelnur
*
*/
class SyndCategoryListFacade extends AbstractList<SyndCategory> {
private final List<DCSubject> subjects;
/**
* Default constructor. Creates and empty list.
*/
public SyndCategoryListFacade() {
this(new ArrayList<DCSubject>());
}
/**
* Creates a facade list of categories on top the given subject list.
* <P>
*
* @param subjects the list of subjects to create the facade.
*
*/
public SyndCategoryListFacade(final List<DCSubject> subjects) {
this.subjects = subjects;
}
/**
* Gets the category by index.
* <p>
*
* @param index the index position to retrieve the category.
* @return the SyndCategoryImpl in position index, <b>null</b> if none.
*
*/
@Override
public SyndCategory get(final int index) {
return new SyndCategoryImpl(subjects.get(index));
}
/**
* Returns the size of the list.
* <p>
*
* @return the size of the list.
*
*/
@Override
public int size() {
return subjects.size();
}
/**
* Sets a category in an existing position in the list.
* <p>
*
* @param index position to set the category.
* @param obj the SyndCategoryImpl object to set.
* @return the SyndCategoryImpl object that is being replaced, <b>null</b> if none.
*
*/
@Override
public SyndCategory set(final int index, final SyndCategory obj) {
final SyndCategoryImpl sCat = (SyndCategoryImpl) obj;
DCSubject subject;
if (sCat != null) {
subject = sCat.getSubject();
} else {
subject = null;
}
subject = subjects.set(index, subject);
if (subject != null) {
return new SyndCategoryImpl(subject);
} else {
return null;
}
}
/**
* Adds a category to the list.
* <p>
*
* @param index position to add the category.
* @param obj the SyndCategoryImpl object to add.
*
*/
@Override
public void add(final int index, final SyndCategory obj) {
final SyndCategoryImpl sCat = (SyndCategoryImpl) obj;
DCSubject subject;
if (sCat != null) {
subject = sCat.getSubject();
} else {
subject = null;
}
subjects.add(index, subject);
}
/**
* Removes a category element from a specific position.
* <p>
*
* @param index position to remove the category from.
* @return the SyndCategoryImpl being removed from position index, <b>null</b> if none.
*
*/
@Override
public SyndCategory remove(final int index) {
final DCSubject subject = subjects.remove(index);
if (subject != null) {
return new SyndCategoryImpl(subject);
} else {
return null;
}
}
/**
* Returns a list with the DCSubject elements of the SyndCategoryImpl list facade. To be used by
* the SyndFeedImpl class only.
* <p>
*
* @param cList the list with SyndCategoryImpl elements to convert to subject list.
* @return a list with DCSubject elements corresponding to the categories in the given list.
*
*/
public static List<DCSubject> convertElementsSyndCategoryToSubject(final List<SyndCategory> cList) {
List<DCSubject> sList = null;
if (cList != null) {
sList = new ArrayList<DCSubject>();
for (int i = 0; i < cList.size(); i++) {
final SyndCategoryImpl sCat = (SyndCategoryImpl) cList.get(i);
DCSubject subject = null;
if (sCat != null) {
subject = sCat.getSubject();
}
sList.add(subject);
}
}
return sList;
}
}

View file

@ -33,12 +33,27 @@ import com.sun.syndication.feed.impl.ObjectBean;
* *
*/ */
public class SyndContentImpl implements Serializable, SyndContent { public class SyndContentImpl implements Serializable, SyndContent {
private static final long serialVersionUID = -8831050456661121113L; private static final long serialVersionUID = -8831050456661121113L;
private static final CopyFromHelper COPY_FROM_HELPER;
private final ObjectBean objBean; private final ObjectBean objBean;
private String type; private String type;
private String value; private String value;
private String mode; private String mode;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("type", String.class);
basePropInterfaceMap.put("value", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndContent.class, basePropInterfaceMap, basePropClassImplMap);
}
/** /**
* Default constructor. All properties are set to <b>null</b>. * Default constructor. All properties are set to <b>null</b>.
* <p> * <p>
@ -185,16 +200,4 @@ public class SyndContentImpl implements Serializable, SyndContent {
COPY_FROM_HELPER.copy(this, obj); COPY_FROM_HELPER.copy(this, obj);
} }
private static final CopyFromHelper COPY_FROM_HELPER;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("type", String.class);
basePropInterfaceMap.put("value", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndContent.class, basePropInterfaceMap, basePropClassImplMap);
}
} }

View file

@ -13,12 +13,28 @@ import com.sun.syndication.feed.impl.ObjectBean;
* @author Alejandro Abdelnur * @author Alejandro Abdelnur
*/ */
public class SyndEnclosureImpl implements Serializable, SyndEnclosure { public class SyndEnclosureImpl implements Serializable, SyndEnclosure {
private static final long serialVersionUID = -5813049622142257411L; private static final long serialVersionUID = -5813049622142257411L;
private static final CopyFromHelper COPY_FROM_HELPER;
private final ObjectBean objBean; private final ObjectBean objBean;
private String url; private String url;
private String type; private String type;
private long length; private long length;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("url", String.class);
basePropInterfaceMap.put("type", String.class);
basePropInterfaceMap.put("length", Long.TYPE);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndEnclosure.class, basePropInterfaceMap, basePropClassImplMap);
}
/** /**
* Default constructor. All properties are set to <b>null</b>. * Default constructor. All properties are set to <b>null</b>.
* <p> * <p>
@ -157,17 +173,4 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure {
COPY_FROM_HELPER.copy(this, obj); COPY_FROM_HELPER.copy(this, obj);
} }
private static final CopyFromHelper COPY_FROM_HELPER;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("url", String.class);
basePropInterfaceMap.put("type", String.class);
basePropInterfaceMap.put("length", Long.TYPE);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndEnclosure.class, basePropInterfaceMap, basePropClassImplMap);
}
} }

View file

@ -28,6 +28,9 @@ import java.util.Set;
import org.jdom2.Element; import org.jdom2.Element;
import com.rometools.utils.Dates;
import com.rometools.utils.Lists;
import com.rometools.utils.Strings;
import com.sun.syndication.feed.CopyFrom; import com.sun.syndication.feed.CopyFrom;
import com.sun.syndication.feed.impl.CopyFromHelper; import com.sun.syndication.feed.impl.CopyFromHelper;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
@ -47,8 +50,13 @@ import com.sun.syndication.feed.synd.impl.URINormalizer;
* *
*/ */
public class SyndEntryImpl implements Serializable, SyndEntry { public class SyndEntryImpl implements Serializable, SyndEntry {
private static final long serialVersionUID = 1944144041409866698L; private static final long serialVersionUID = 1944144041409866698L;
private static final CopyFromHelper COPY_FROM_HELPER;
private final ObjectBean objBean; private final ObjectBean objBean;
private String uri; private String uri;
private String link; private String link;
private String comments; private String comments;
@ -63,8 +71,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
private List<SyndPerson> contributors; private List<SyndPerson> contributors;
private SyndFeed source; private SyndFeed source;
private List<Element> foreignMarkup; private List<Element> foreignMarkup;
private Object wireEntry; // com.sun.syndication.feed.atom.Entry or
// com.sun.syndication.feed.rss.Item // com.sun.syndication.feed.atom.Entry or com.sun.syndication.feed.rss.Item
private Object wireEntry;
// ISSUE: some converters assume this is never null // ISSUE: some converters assume this is never null
private List<SyndCategory> categories = new ArrayList<SyndCategory>(); private List<SyndCategory> categories = new ArrayList<SyndCategory>();
@ -80,8 +89,30 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
public static final Set<String> CONVENIENCE_PROPERTIES = Collections.unmodifiableSet(IGNORE_PROPERTIES); public static final Set<String> CONVENIENCE_PROPERTIES = Collections.unmodifiableSet(IGNORE_PROPERTIES);
static { static {
IGNORE_PROPERTIES.add("publishedDate"); IGNORE_PROPERTIES.add("publishedDate");
IGNORE_PROPERTIES.add("author"); IGNORE_PROPERTIES.add("author");
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("description", SyndContent.class);
basePropInterfaceMap.put("contents", SyndContent.class);
basePropInterfaceMap.put("enclosures", SyndEnclosure.class);
basePropInterfaceMap.put("modules", Module.class);
basePropInterfaceMap.put("categories", SyndCategory.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = new HashMap<Class<? extends CopyFrom>, Class<?>>();
basePropClassImplMap.put(SyndContent.class, SyndContentImpl.class);
basePropClassImplMap.put(SyndEnclosure.class, SyndEnclosureImpl.class);
basePropClassImplMap.put(SyndCategory.class, SyndCategoryImpl.class);
basePropClassImplMap.put(DCModule.class, DCModuleImpl.class);
basePropClassImplMap.put(SyModule.class, SyModuleImpl.class);
COPY_FROM_HELPER = new CopyFromHelper(SyndEntry.class, basePropInterfaceMap, basePropClassImplMap);
} }
/** /**
@ -322,10 +353,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/ */
@Override @Override
public List<SyndContent> getContents() { public List<SyndContent> getContents() {
if (contents == null) { return contents = Lists.createWhenNull(contents);
contents = new ArrayList<SyndContent>();
}
return contents;
} }
/** /**
@ -350,10 +378,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/ */
@Override @Override
public List<SyndEnclosure> getEnclosures() { public List<SyndEnclosure> getEnclosures() {
if (enclosures == null) { return enclosures = Lists.createWhenNull(enclosures);
enclosures = new ArrayList<SyndEnclosure>();
}
return enclosures;
} }
/** /**
@ -433,9 +458,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/ */
@Override @Override
public List<Module> getModules() { public List<Module> getModules() {
if (modules == null) { modules = Lists.createWhenNull(modules);
modules = new ArrayList<Module>();
}
if (ModuleUtils.getModule(modules, DCModule.URI) == null) { if (ModuleUtils.getModule(modules, DCModule.URI) == null) {
modules.add(new DCModuleImpl()); modules.add(new DCModuleImpl());
} }
@ -487,30 +510,6 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
COPY_FROM_HELPER.copy(this, obj); COPY_FROM_HELPER.copy(this, obj);
} }
private static final CopyFromHelper COPY_FROM_HELPER;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("description", SyndContent.class);
basePropInterfaceMap.put("contents", SyndContent.class);
basePropInterfaceMap.put("enclosures", SyndEnclosure.class);
basePropInterfaceMap.put("modules", Module.class);
basePropInterfaceMap.put("categories", SyndCategory.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = new HashMap<Class<? extends CopyFrom>, Class<?>>();
basePropClassImplMap.put(SyndContent.class, SyndContentImpl.class);
basePropClassImplMap.put(SyndEnclosure.class, SyndEnclosureImpl.class);
basePropClassImplMap.put(SyndCategory.class, SyndCategoryImpl.class);
basePropClassImplMap.put(DCModule.class, DCModuleImpl.class);
basePropClassImplMap.put(SyModule.class, SyModuleImpl.class);
COPY_FROM_HELPER = new CopyFromHelper(SyndEntry.class, basePropInterfaceMap, basePropClassImplMap);
}
/** /**
* Returns the links * Returns the links
* <p> * <p>
@ -519,10 +518,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/ */
@Override @Override
public List<SyndLink> getLinks() { public List<SyndLink> getLinks() {
if (links == null) { return links = Lists.createWhenNull(links);
links = new ArrayList<SyndLink>();
}
return links;
} }
/** /**
@ -544,11 +540,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/ */
@Override @Override
public Date getUpdatedDate() { public Date getUpdatedDate() {
if (updatedDate == null) { return Dates.copy(updatedDate);
return null;
} else {
return new Date(updatedDate.getTime());
}
} }
/** /**
@ -564,16 +556,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
@Override @Override
public List<SyndPerson> getAuthors() { public List<SyndPerson> getAuthors() {
if (authors == null) { return authors = Lists.createWhenNull(authors);
authors = new ArrayList<SyndPerson>();
}
return authors;
} }
/*
* (non-Javadoc)
* @see com.sun.syndication.feed.synd.SyndEntry#setAuthors(java.util.List)
*/
@Override @Override
public void setAuthors(final List<SyndPerson> authors) { public void setAuthors(final List<SyndPerson> authors) {
this.authors = authors; this.authors = authors;
@ -590,20 +575,23 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/ */
@Override @Override
public String getAuthor() { public String getAuthor() {
String author; String author;
// Start out looking for one or more authors in authors. For non-Atom // Start out looking for one or more authors in authors. For non-Atom
// feeds, authors may actually be null. // feeds, authors may actually be null.
if (authors != null && !authors.isEmpty()) { if (Lists.isNotEmpty(authors)) {
author = authors.get(0).getName(); author = authors.get(0).getName();
} else { } else {
author = getDCModule().getCreator(); author = getDCModule().getCreator();
} }
if (author == null) { if (author == null) {
author = ""; author = "";
} }
return author; return author;
} }
/** /**
@ -617,22 +605,18 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/ */
@Override @Override
public void setAuthor(final String author) { public void setAuthor(final String author) {
// Get the DCModule so that we can check to see if "creator" is already // Get the DCModule so that we can check to see if "creator" is already set.
// set.
final DCModule dcModule = getDCModule(); final DCModule dcModule = getDCModule();
final String currentValue = dcModule.getCreator(); final String currentValue = dcModule.getCreator();
if (currentValue == null || currentValue.length() == 0) { if (Strings.isEmpty(currentValue)) {
getDCModule().setCreator(author); getDCModule().setCreator(author);
} }
} }
@Override @Override
public List<SyndPerson> getContributors() { public List<SyndPerson> getContributors() {
if (contributors == null) { return contributors = Lists.createWhenNull(contributors);
contributors = new ArrayList<SyndPerson>();
}
return contributors;
} }
@Override @Override
@ -659,10 +643,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/ */
@Override @Override
public List<Element> getForeignMarkup() { public List<Element> getForeignMarkup() {
if (foreignMarkup == null) { return foreignMarkup = Lists.createWhenNull(foreignMarkup);
foreignMarkup = new ArrayList<Element>();
}
return foreignMarkup;
} }
/** /**
@ -678,17 +659,11 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
this.foreignMarkup = foreignMarkup; this.foreignMarkup = foreignMarkup;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public String getComments() { public String getComments() {
return comments; return comments;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void setComments(final String comments) { public void setComments(final String comments) {
this.comments = comments; this.comments = comments;
@ -705,11 +680,13 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
@Override @Override
public SyndLink findRelatedLink(final String relation) { public SyndLink findRelatedLink(final String relation) {
for (final SyndLink l : getLinks()) { final List<SyndLink> syndLinks = getLinks();
if (relation.equals(l.getRel())) { for (final SyndLink syndLink : syndLinks) {
return l; if (relation.equals(syndLink.getRel())) {
return syndLink;
} }
} }
return null; return null;
} }
} }

View file

@ -17,7 +17,6 @@
package com.sun.syndication.feed.synd; package com.sun.syndication.feed.synd;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -28,6 +27,7 @@ import java.util.Set;
import org.jdom2.Element; import org.jdom2.Element;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.CopyFrom; import com.sun.syndication.feed.CopyFrom;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.impl.CopyFromHelper; import com.sun.syndication.feed.impl.CopyFromHelper;
@ -52,8 +52,11 @@ import com.sun.syndication.feed.synd.impl.URINormalizer;
* *
*/ */
public class SyndFeedImpl implements Serializable, SyndFeed { public class SyndFeedImpl implements Serializable, SyndFeed {
private static final long serialVersionUID = -2529165503200548045L; private static final long serialVersionUID = -2529165503200548045L;
private static final CopyFromHelper COPY_FROM_HELPER;
private final ObjectBean objBean; private final ObjectBean objBean;
private String encoding; private String encoding;
@ -92,11 +95,34 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
public static final Set<String> CONVENIENCE_PROPERTIES = Collections.unmodifiableSet(IGNORE_PROPERTIES); public static final Set<String> CONVENIENCE_PROPERTIES = Collections.unmodifiableSet(IGNORE_PROPERTIES);
static { static {
IGNORE_PROPERTIES.add("publishedDate"); IGNORE_PROPERTIES.add("publishedDate");
IGNORE_PROPERTIES.add("author"); IGNORE_PROPERTIES.add("author");
IGNORE_PROPERTIES.add("copyright"); IGNORE_PROPERTIES.add("copyright");
IGNORE_PROPERTIES.add("categories"); IGNORE_PROPERTIES.add("categories");
IGNORE_PROPERTIES.add("language"); IGNORE_PROPERTIES.add("language");
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("feedType", String.class);
basePropInterfaceMap.put("encoding", String.class);
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("description", String.class);
basePropInterfaceMap.put("image", SyndImage.class);
basePropInterfaceMap.put("entries", SyndEntry.class);
basePropInterfaceMap.put("modules", Module.class);
basePropInterfaceMap.put("categories", SyndCategory.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = new HashMap<Class<? extends CopyFrom>, Class<?>>();
basePropClassImplMap.put(SyndEntry.class, SyndEntryImpl.class);
basePropClassImplMap.put(SyndImage.class, SyndImageImpl.class);
basePropClassImplMap.put(SyndCategory.class, SyndCategoryImpl.class);
basePropClassImplMap.put(DCModule.class, DCModuleImpl.class);
basePropClassImplMap.put(SyModule.class, SyModuleImpl.class);
COPY_FROM_HELPER = new CopyFromHelper(SyndFeed.class, basePropInterfaceMap, basePropClassImplMap);
} }
/** /**
@ -257,14 +283,18 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
*/ */
@Override @Override
public WireFeed createWireFeed(final String feedType) { public WireFeed createWireFeed(final String feedType) {
if (feedType == null) { if (feedType == null) {
throw new IllegalArgumentException("Feed type cannot be null"); throw new IllegalArgumentException("Feed type cannot be null");
} }
final Converter converter = CONVERTERS.getConverter(feedType); final Converter converter = CONVERTERS.getConverter(feedType);
if (converter == null) { if (converter == null) {
throw new IllegalArgumentException("Invalid feed type [" + feedType + "]"); throw new IllegalArgumentException("Invalid feed type [" + feedType + "]");
} }
return converter.createRealFeed(this); return converter.createRealFeed(this);
} }
/** /**
@ -656,10 +686,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
*/ */
@Override @Override
public List<SyndEntry> getEntries() { public List<SyndEntry> getEntries() {
if (entries == null) { return entries = Lists.createWhenNull(entries);
entries = new ArrayList<SyndEntry>();
}
return entries;
} }
/** /**
@ -712,9 +739,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
*/ */
@Override @Override
public List<Module> getModules() { public List<Module> getModules() {
if (modules == null) { modules = Lists.createWhenNull(modules);
modules = new ArrayList<Module>();
}
if (ModuleUtils.getModule(modules, DCModule.URI) == null) { if (ModuleUtils.getModule(modules, DCModule.URI) == null) {
modules.add(new DCModuleImpl()); modules.add(new DCModuleImpl());
} }
@ -766,33 +791,6 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
COPY_FROM_HELPER.copy(this, obj); COPY_FROM_HELPER.copy(this, obj);
} }
// TODO We need to find out how to refactor this one in a nice reusable way.
private static final CopyFromHelper COPY_FROM_HELPER;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("feedType", String.class);
basePropInterfaceMap.put("encoding", String.class);
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("description", String.class);
basePropInterfaceMap.put("image", SyndImage.class);
basePropInterfaceMap.put("entries", SyndEntry.class);
basePropInterfaceMap.put("modules", Module.class);
basePropInterfaceMap.put("categories", SyndCategory.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = new HashMap<Class<? extends CopyFrom>, Class<?>>();
basePropClassImplMap.put(SyndEntry.class, SyndEntryImpl.class);
basePropClassImplMap.put(SyndImage.class, SyndImageImpl.class);
basePropClassImplMap.put(SyndCategory.class, SyndCategoryImpl.class);
basePropClassImplMap.put(DCModule.class, DCModuleImpl.class);
basePropClassImplMap.put(SyModule.class, SyModuleImpl.class);
COPY_FROM_HELPER = new CopyFromHelper(SyndFeed.class, basePropInterfaceMap, basePropClassImplMap);
}
/** /**
* Returns the links * Returns the links
* <p> * <p>
@ -801,10 +799,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
*/ */
@Override @Override
public List<SyndLink> getLinks() { public List<SyndLink> getLinks() {
if (links == null) { return links = Lists.createWhenNull(links);
links = new ArrayList<SyndLink>();
}
return links;
} }
/** /**
@ -820,10 +815,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
@Override @Override
public List<SyndPerson> getAuthors() { public List<SyndPerson> getAuthors() {
if (authors == null) { return authors = Lists.createWhenNull(authors);
authors = new ArrayList<SyndPerson>();
}
return authors;
} }
@Override @Override
@ -861,10 +853,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
@Override @Override
public List<SyndPerson> getContributors() { public List<SyndPerson> getContributors() {
if (contributors == null) { return contributors = Lists.createWhenNull(contributors);
contributors = new ArrayList<SyndPerson>();
}
return contributors;
} }
@Override @Override
@ -881,10 +870,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
*/ */
@Override @Override
public List<Element> getForeignMarkup() { public List<Element> getForeignMarkup() {
if (foreignMarkup == null) { return foreignMarkup = Lists.createWhenNull(foreignMarkup);
foreignMarkup = new ArrayList<Element>();
}
return foreignMarkup;
} }
/** /**
@ -904,83 +890,54 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
return preserveWireFeed; return preserveWireFeed;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public String getDocs() { public String getDocs() {
return docs; return docs;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void setDocs(final String docs) { public void setDocs(final String docs) {
this.docs = docs; this.docs = docs;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public String getGenerator() { public String getGenerator() {
return generator; return generator;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void setGenerator(final String generator) { public void setGenerator(final String generator) {
this.generator = generator; this.generator = generator;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public String getManagingEditor() { public String getManagingEditor() {
return managingEditor; return managingEditor;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void setManagingEditor(final String managingEditor) { public void setManagingEditor(final String managingEditor) {
this.managingEditor = managingEditor; this.managingEditor = managingEditor;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public String getWebMaster() { public String getWebMaster() {
return webMaster; return webMaster;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void setWebMaster(final String webMaster) { public void setWebMaster(final String webMaster) {
this.webMaster = webMaster; this.webMaster = webMaster;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public String getStyleSheet() { public String getStyleSheet() {
return styleSheet; return styleSheet;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void setStyleSheet(final String styleSheet) { public void setStyleSheet(final String styleSheet) {
this.styleSheet = styleSheet; this.styleSheet = styleSheet;
} }
} }

View file

@ -33,13 +33,30 @@ import com.sun.syndication.feed.impl.ObjectBean;
* *
*/ */
public class SyndImageImpl implements Serializable, SyndImage { public class SyndImageImpl implements Serializable, SyndImage {
private static final long serialVersionUID = 5078981553559513247L; private static final long serialVersionUID = 5078981553559513247L;
private static final CopyFromHelper COPY_FROM_HELPER;
private final ObjectBean objBean; private final ObjectBean objBean;
private String title; private String title;
private String url; private String url;
private String link; private String link;
private String description; private String description;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("url", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("description", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndImage.class, basePropInterfaceMap, basePropClassImplMap);
}
/** /**
* Default constructor. All properties are set to <b>null</b>. * Default constructor. All properties are set to <b>null</b>.
* <p> * <p>
@ -208,18 +225,4 @@ public class SyndImageImpl implements Serializable, SyndImage {
COPY_FROM_HELPER.copy(this, syndImage); COPY_FROM_HELPER.copy(this, syndImage);
} }
private static final CopyFromHelper COPY_FROM_HELPER;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("url", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("description", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndImage.class, basePropInterfaceMap, basePropClassImplMap);
}
} }

View file

@ -18,9 +18,9 @@
package com.sun.syndication.feed.synd; package com.sun.syndication.feed.synd;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.impl.ObjectBean;
import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.Module;
import com.sun.syndication.feed.module.impl.ModuleUtils; import com.sun.syndication.feed.module.impl.ModuleUtils;
@ -33,8 +33,11 @@ import com.sun.syndication.feed.module.impl.ModuleUtils;
* *
*/ */
public class SyndPersonImpl implements Serializable, SyndPerson { public class SyndPersonImpl implements Serializable, SyndPerson {
private static final long serialVersionUID = 8523373264589239335L; private static final long serialVersionUID = 8523373264589239335L;
private final ObjectBean objBean; private final ObjectBean objBean;
private String name; private String name;
private String uri; private String uri;
private String email; private String email;
@ -184,10 +187,7 @@ public class SyndPersonImpl implements Serializable, SyndPerson {
*/ */
@Override @Override
public List<Module> getModules() { public List<Module> getModules() {
if (modules == null) { return modules = Lists.createWhenNull(modules);
modules = new ArrayList<Module>();
}
return modules;
} }
/** /**
@ -214,4 +214,5 @@ public class SyndPersonImpl implements Serializable, SyndPerson {
public Module getModule(final String uri) { public Module getModule(final String uri) {
return ModuleUtils.getModule(getModules(), uri); return ModuleUtils.getModule(getModules(), uri);
} }
} }

View file

@ -21,6 +21,11 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.jdom2.Element;
import com.rometools.utils.Alternatives;
import com.rometools.utils.Lists;
import com.rometools.utils.Strings;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.atom.Content; import com.sun.syndication.feed.atom.Content;
import com.sun.syndication.feed.atom.Entry; import com.sun.syndication.feed.atom.Entry;
@ -46,6 +51,7 @@ import com.sun.syndication.feed.synd.SyndPersonImpl;
/** /**
*/ */
public class ConverterForAtom03 implements Converter { public class ConverterForAtom03 implements Converter {
private final String type; private final String type;
public ConverterForAtom03() { public ConverterForAtom03() {
@ -63,24 +69,29 @@ public class ConverterForAtom03 implements Converter {
@Override @Override
public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { public void copyInto(final WireFeed feed, final SyndFeed syndFeed) {
final Feed aFeed = (Feed) feed; final Feed aFeed = (Feed) feed;
syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules())); syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules()));
if (!feed.getForeignMarkup().isEmpty()) { final List<Element> foreignMarkup = feed.getForeignMarkup();
syndFeed.setForeignMarkup(feed.getForeignMarkup()); if (Lists.isNotEmpty(foreignMarkup)) {
syndFeed.setForeignMarkup(foreignMarkup);
} }
syndFeed.setEncoding(aFeed.getEncoding()); syndFeed.setEncoding(aFeed.getEncoding());
syndFeed.setStyleSheet(aFeed.getStyleSheet()); syndFeed.setStyleSheet(aFeed.getStyleSheet());
if (aFeed.getLogo() != null) { final String logo = aFeed.getLogo();
final String icon = aFeed.getIcon();
if (logo != null) {
final SyndImage image = new SyndImageImpl(); final SyndImage image = new SyndImageImpl();
image.setUrl(aFeed.getLogo()); image.setUrl(logo);
syndFeed.setImage(image); syndFeed.setImage(image);
} else if (aFeed.getIcon() != null) { } else if (icon != null) {
final SyndImage image = new SyndImageImpl(); final SyndImage image = new SyndImageImpl();
image.setUrl(aFeed.getIcon()); image.setUrl(icon);
syndFeed.setImage(image); syndFeed.setImage(image);
} }
@ -89,17 +100,19 @@ public class ConverterForAtom03 implements Converter {
syndFeed.setTitle(aFeed.getTitle()); syndFeed.setTitle(aFeed.getTitle());
// use first alternate links as THE link // use first alternate links as THE link
if (aFeed.getAlternateLinks() != null && !aFeed.getAlternateLinks().isEmpty()) { final List<Link> alternateLinks = aFeed.getAlternateLinks();
final Link theLink = aFeed.getAlternateLinks().get(0); if (Lists.isNotEmpty(alternateLinks)) {
syndFeed.setLink(theLink.getHrefResolved()); final Link link = alternateLinks.get(0);
syndFeed.setLink(link.getHrefResolved());
} }
// lump alternate and other links together // lump alternate and other links together
final List<SyndLink> syndLinks = new ArrayList<SyndLink>(); final List<SyndLink> syndLinks = new ArrayList<SyndLink>();
if (aFeed.getAlternateLinks() != null && !aFeed.getAlternateLinks().isEmpty()) { if (Lists.isNotEmpty(alternateLinks)) {
syndLinks.addAll(createSyndLinks(aFeed.getAlternateLinks())); syndLinks.addAll(createSyndLinks(alternateLinks));
} }
if (aFeed.getOtherLinks() != null && !aFeed.getOtherLinks().isEmpty()) { final List<Link> otherLinks = aFeed.getOtherLinks();
syndLinks.addAll(createSyndLinks(aFeed.getOtherLinks())); if (Lists.isNotEmpty(otherLinks)) {
syndLinks.addAll(createSyndLinks(otherLinks));
} }
syndFeed.setLinks(syndLinks); syndFeed.setLinks(syndLinks);
@ -109,7 +122,7 @@ public class ConverterForAtom03 implements Converter {
} }
final List<Entry> aEntries = aFeed.getEntries(); final List<Entry> aEntries = aFeed.getEntries();
if (aEntries != null) { if (Lists.isNotEmpty(aEntries)) {
syndFeed.setEntries(createSyndEntries(aEntries, syndFeed.isPreservingWireFeed())); syndFeed.setEntries(createSyndEntries(aEntries, syndFeed.isPreservingWireFeed()));
} }
@ -122,7 +135,7 @@ public class ConverterForAtom03 implements Converter {
} }
final List<SyndPerson> authors = aFeed.getAuthors(); final List<SyndPerson> authors = aFeed.getAuthors();
if (authors != null && !authors.isEmpty()) { if (Lists.isNotEmpty(authors)) {
syndFeed.setAuthors(createSyndPersons(authors)); syndFeed.setAuthors(createSyndPersons(authors));
} }
@ -138,16 +151,16 @@ public class ConverterForAtom03 implements Converter {
} }
protected List<SyndLink> createSyndLinks(final List<Link> aLinks) { protected List<SyndLink> createSyndLinks(final List<Link> atomLinks) {
final ArrayList<SyndLink> sLinks = new ArrayList<SyndLink>(); final ArrayList<SyndLink> syndLinks = new ArrayList<SyndLink>();
for (final Link link2 : aLinks) { for (final Link atomLink : atomLinks) {
final Link link = link2; final Link link = atomLink;
if (!link.getRel().equals("enclosure")) { if (!link.getRel().equals("enclosure")) {
final SyndLink sLink = createSyndLink(link); final SyndLink syndLink = createSyndLink(link);
sLinks.add(sLink); syndLinks.add(syndLink);
} }
} }
return sLinks; return syndLinks;
} }
public SyndLink createSyndLink(final Link link) { public SyndLink createSyndLink(final Link link) {
@ -161,38 +174,42 @@ public class ConverterForAtom03 implements Converter {
protected List<SyndEntry> createSyndEntries(final List<Entry> atomEntries, final boolean preserveWireItems) { protected List<SyndEntry> createSyndEntries(final List<Entry> atomEntries, final boolean preserveWireItems) {
final List<SyndEntry> syndEntries = new ArrayList<SyndEntry>(); final List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
for (int i = 0; i < atomEntries.size(); i++) { for (final Entry atomEntry : atomEntries) {
syndEntries.add(createSyndEntry(atomEntries.get(i), preserveWireItems)); syndEntries.add(createSyndEntry(atomEntry, preserveWireItems));
} }
return syndEntries; return syndEntries;
} }
protected SyndEntry createSyndEntry(final Entry entry, final boolean preserveWireItem) { protected SyndEntry createSyndEntry(final Entry entry, final boolean preserveWireItem) {
final SyndEntryImpl syndEntry = new SyndEntryImpl(); final SyndEntryImpl syndEntry = new SyndEntryImpl();
if (preserveWireItem) { if (preserveWireItem) {
syndEntry.setWireEntry(entry); syndEntry.setWireEntry(entry);
} }
syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules())); syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules()));
if (!entry.getForeignMarkup().isEmpty()) { final List<Element> foreignMarkup = entry.getForeignMarkup();
syndEntry.setForeignMarkup(entry.getForeignMarkup()); if (Lists.isNotEmpty(foreignMarkup)) {
syndEntry.setForeignMarkup(foreignMarkup);
} }
syndEntry.setTitle(entry.getTitle()); syndEntry.setTitle(entry.getTitle());
// if there is exactly one alternate link, use that as THE link // if there is exactly one alternate link, use that as THE link
if (entry.getAlternateLinks() != null && entry.getAlternateLinks().size() == 1) { final List<Link> alternateLinks = entry.getAlternateLinks();
final Link theLink = entry.getAlternateLinks().get(0); if (Lists.sizeIs(alternateLinks, 1)) {
final Link theLink = alternateLinks.get(0);
syndEntry.setLink(theLink.getHrefResolved()); syndEntry.setLink(theLink.getHrefResolved());
} }
// Create synd enclosures from enclosure links // Create synd enclosures from enclosure links
final List<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>(); final List<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>();
if (entry.getOtherLinks() != null && !entry.getOtherLinks().isEmpty()) { final List<Link> otherLinks = entry.getOtherLinks();
final List<Link> oLinks = entry.getOtherLinks(); if (Lists.isNotEmpty(otherLinks)) {
for (final Link link : oLinks) { for (final Link otherLink : otherLinks) {
final Link thisLink = link; final Link thisLink = otherLink;
if ("enclosure".equals(thisLink.getRel())) { if ("enclosure".equals(thisLink.getRel())) {
syndEnclosures.add(createSyndEnclosure(entry, thisLink)); syndEnclosures.add(createSyndEnclosure(entry, thisLink));
} }
@ -202,63 +219,64 @@ public class ConverterForAtom03 implements Converter {
// lump alternate and other links together // lump alternate and other links together
final List<SyndLink> syndLinks = new ArrayList<SyndLink>(); final List<SyndLink> syndLinks = new ArrayList<SyndLink>();
if (entry.getAlternateLinks() != null && !entry.getAlternateLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(entry.getAlternateLinks())); if (Lists.isNotEmpty(alternateLinks)) {
syndLinks.addAll(createSyndLinks(alternateLinks));
} }
if (entry.getOtherLinks() != null && !entry.getOtherLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(entry.getOtherLinks())); if (Lists.isNotEmpty(otherLinks)) {
syndLinks.addAll(createSyndLinks(otherLinks));
} }
syndEntry.setLinks(syndLinks); syndEntry.setLinks(syndLinks);
final String id = entry.getId(); final String id = entry.getId();
if (id != null) { if (id != null) {
syndEntry.setUri(entry.getId()); syndEntry.setUri(id);
} else { } else {
syndEntry.setUri(syndEntry.getLink()); final String link = syndEntry.getLink();
syndEntry.setUri(link);
} }
Content content = entry.getSummary(); Content summary = entry.getSummary();
if (content == null) { if (summary == null) {
final List<Content> contents = entry.getContents(); final List<Content> contents = entry.getContents();
if (contents != null && !contents.isEmpty()) { if (Lists.isNotEmpty(contents)) {
content = contents.get(0); summary = contents.get(0);
} }
} } else {
if (content != null) {
final SyndContent sContent = new SyndContentImpl(); final SyndContent sContent = new SyndContentImpl();
sContent.setType(content.getType()); sContent.setType(summary.getType());
sContent.setValue(content.getValue()); sContent.setValue(summary.getValue());
syndEntry.setDescription(sContent); syndEntry.setDescription(sContent);
} }
final List<Content> contents = entry.getContents(); final List<Content> contents = entry.getContents();
if (!contents.isEmpty()) { if (Lists.isNotEmpty(contents)) {
final List<SyndContent> sContents = new ArrayList<SyndContent>(); final List<SyndContent> sContents = new ArrayList<SyndContent>();
for (int i = 0; i < contents.size(); i++) { for (final Content content : contents) {
content = contents.get(i);
final SyndContent sContent = new SyndContentImpl(); final SyndContent sContent = new SyndContentImpl();
sContent.setType(content.getType()); sContent.setType(content.getType());
sContent.setValue(content.getValue()); sContent.setValue(content.getValue());
sContent.setMode(content.getMode()); sContent.setMode(content.getMode());
sContents.add(sContent); sContents.add(sContent);
} }
syndEntry.setContents(sContents); syndEntry.setContents(sContents);
} }
final List<SyndPerson> authors = entry.getAuthors(); final List<SyndPerson> authors = entry.getAuthors();
if (authors != null && !authors.isEmpty()) { if (Lists.isNotEmpty(authors)) {
syndEntry.setAuthors(createSyndPersons(authors)); syndEntry.setAuthors(createSyndPersons(authors));
final SyndPerson person0 = syndEntry.getAuthors().get(0); final SyndPerson firstPerson = syndEntry.getAuthors().get(0);
syndEntry.setAuthor(person0.getName()); syndEntry.setAuthor(firstPerson.getName());
} }
Date date = entry.getModified(); Date date = entry.getModified();
if (date == null) { if (date == null) {
date = entry.getIssued(); date = Alternatives.firstNotNull(entry.getIssued(), entry.getCreated());
if (date == null) {
date = entry.getCreated();
}
} }
if (date != null) { if (date != null) {
syndEntry.setPublishedDate(date); syndEntry.setPublishedDate(date);
} }
@ -286,13 +304,16 @@ public class ConverterForAtom03 implements Converter {
final SyndContent sTitle = syndFeed.getTitleEx(); final SyndContent sTitle = syndFeed.getTitleEx();
if (sTitle != null) { if (sTitle != null) {
final Content title = new Content(); final Content title = new Content();
if (sTitle.getType() != null) { final String type = sTitle.getType();
title.setType(sTitle.getType()); if (type != null) {
title.setType(type);
} }
if (sTitle.getMode() != null) { final String mode = sTitle.getMode();
title.setMode(sTitle.getMode()); if (mode != null) {
title.setMode(mode);
} }
title.setValue(sTitle.getValue()); title.setValue(sTitle.getValue());
@ -302,12 +323,14 @@ public class ConverterForAtom03 implements Converter {
// separate SyndEntry's links collection into alternate and other links // separate SyndEntry's links collection into alternate and other links
final List<Link> alternateLinks = new ArrayList<Link>(); final List<Link> alternateLinks = new ArrayList<Link>();
final List<Link> otherLinks = new ArrayList<Link>(); final List<Link> otherLinks = new ArrayList<Link>();
final List<SyndLink> slinks = syndFeed.getLinks(); final List<SyndLink> slinks = syndFeed.getLinks();
if (slinks != null) { if (slinks != null) {
for (final SyndLink syndLink2 : slinks) { for (final SyndLink syndLink2 : slinks) {
final SyndLink syndLink = syndLink2; final SyndLink syndLink = syndLink2;
final Link link = createAtomLink(syndLink); final Link link = createAtomLink(syndLink);
if (link.getRel() == null || "".equals(link.getRel().trim()) || "alternate".equals(link.getRel())) { final String rel = link.getRel();
if (Strings.isBlank(rel) || "alternate".equals(rel)) {
alternateLinks.add(link); alternateLinks.add(link);
} else { } else {
otherLinks.add(link); otherLinks.add(link);
@ -315,10 +338,11 @@ public class ConverterForAtom03 implements Converter {
} }
} }
// no alternate link? then use THE link if there is one // no alternate link? then use THE link if there is one
if (alternateLinks.isEmpty() && syndFeed.getLink() != null) { final String sLink = syndFeed.getLink();
if (alternateLinks.isEmpty() && sLink != null) {
final Link link = new Link(); final Link link = new Link();
link.setRel("alternate"); link.setRel("alternate");
link.setHref(syndFeed.getLink()); link.setHref(sLink);
alternateLinks.add(link); alternateLinks.add(link);
} }
@ -339,7 +363,7 @@ public class ConverterForAtom03 implements Converter {
aFeed.setLanguage(syndFeed.getLanguage()); aFeed.setLanguage(syndFeed.getLanguage());
final List<SyndPerson> authors = syndFeed.getAuthors(); final List<SyndPerson> authors = syndFeed.getAuthors();
if (authors != null && !authors.isEmpty()) { if (Lists.isNotEmpty(authors)) {
aFeed.setAuthors(createAtomPersons(authors)); aFeed.setAuthors(createAtomPersons(authors));
} }
@ -384,8 +408,8 @@ public class ConverterForAtom03 implements Converter {
protected List<Entry> createAtomEntries(final List<SyndEntry> syndEntries) { protected List<Entry> createAtomEntries(final List<SyndEntry> syndEntries) {
final List<Entry> atomEntries = new ArrayList<Entry>(); final List<Entry> atomEntries = new ArrayList<Entry>();
for (int i = 0; i < syndEntries.size(); i++) { for (final SyndEntry syndEntry : syndEntries) {
atomEntries.add(createAtomEntry(syndEntries.get(i))); atomEntries.add(createAtomEntry(syndEntry));
} }
return atomEntries; return atomEntries;
} }
@ -399,12 +423,14 @@ public class ConverterForAtom03 implements Converter {
final SyndContent sTitle = sEntry.getTitleEx(); final SyndContent sTitle = sEntry.getTitleEx();
if (sTitle != null) { if (sTitle != null) {
final Content title = new Content(); final Content title = new Content();
if (sTitle.getType() != null) { final String type = sTitle.getType();
title.setType(sTitle.getType()); if (type != null) {
title.setType(type);
} }
if (sTitle.getMode() != null) { final String mode = sTitle.getMode();
title.setMode(sTitle.getMode()); if (mode != null) {
title.setMode(mode);
} }
title.setValue(sTitle.getValue()); title.setValue(sTitle.getValue());
@ -414,12 +440,13 @@ public class ConverterForAtom03 implements Converter {
// separate SyndEntry's links collection into alternate and other links // separate SyndEntry's links collection into alternate and other links
final List<Link> alternateLinks = new ArrayList<Link>(); final List<Link> alternateLinks = new ArrayList<Link>();
final List<Link> otherLinks = new ArrayList<Link>(); final List<Link> otherLinks = new ArrayList<Link>();
final List<SyndLink> slinks = sEntry.getLinks(); final List<SyndLink> syndLinks = sEntry.getLinks();
if (slinks != null) {
for (final SyndLink syndLink2 : slinks) { if (syndLinks != null) {
final SyndLink syndLink = syndLink2; for (final SyndLink syndLink : syndLinks) {
final Link link = createAtomLink(syndLink); final Link link = createAtomLink(syndLink);
if (link.getRel() == null || "".equals(link.getRel().trim()) || "alternate".equals(link.getRel())) { final String rel = link.getRel();
if (Strings.isBlank(rel) || "alternate".equals(rel)) {
alternateLinks.add(link); alternateLinks.add(link);
} else { } else {
otherLinks.add(link); otherLinks.add(link);
@ -427,17 +454,17 @@ public class ConverterForAtom03 implements Converter {
} }
} }
// no alternate link? then use THE link if there is one // no alternate link? then use THE link if there is one
if (alternateLinks.isEmpty() && sEntry.getLink() != null) { final String sLink = sEntry.getLink();
if (alternateLinks.isEmpty() && sLink != null) {
final Link link = new Link(); final Link link = new Link();
link.setRel("alternate"); link.setRel("alternate");
link.setHref(sEntry.getLink()); link.setHref(sLink);
alternateLinks.add(link); alternateLinks.add(link);
} }
final List<SyndEnclosure> sEnclosures = sEntry.getEnclosures(); final List<SyndEnclosure> sEnclosures = sEntry.getEnclosures();
if (sEnclosures != null) { if (sEnclosures != null) {
for (final SyndEnclosure syndEnclosure2 : sEnclosures) { for (final SyndEnclosure syndEnclosure : sEnclosures) {
final SyndEnclosure syndEnclosure = syndEnclosure2;
final Link link = createAtomEnclosure(syndEnclosure); final Link link = createAtomEnclosure(syndEnclosure);
otherLinks.add(link); otherLinks.add(link);
} }
@ -450,7 +477,7 @@ public class ConverterForAtom03 implements Converter {
aEntry.setOtherLinks(otherLinks); aEntry.setOtherLinks(otherLinks);
} }
SyndContent sContent = sEntry.getDescription(); final SyndContent sContent = sEntry.getDescription();
if (sContent != null) { if (sContent != null) {
final Content content = new Content(); final Content content = new Content();
content.setType(sContent.getType()); content.setType(sContent.getType());
@ -462,24 +489,23 @@ public class ConverterForAtom03 implements Converter {
final List<SyndContent> contents = sEntry.getContents(); final List<SyndContent> contents = sEntry.getContents();
if (!contents.isEmpty()) { if (!contents.isEmpty()) {
final List<Content> aContents = new ArrayList<Content>(); final List<Content> aContents = new ArrayList<Content>();
for (int i = 0; i < contents.size(); i++) { for (final SyndContent syndContent : contents) {
sContent = contents.get(i);
final Content content = new Content(); final Content content = new Content();
content.setType(sContent.getType()); content.setType(syndContent.getType());
content.setValue(sContent.getValue()); content.setValue(syndContent.getValue());
content.setMode(sContent.getMode()); content.setMode(syndContent.getMode());
aContents.add(content); aContents.add(content);
} }
aEntry.setContents(aContents); aEntry.setContents(aContents);
} }
final List<SyndPerson> sAuthors = sEntry.getAuthors(); final List<SyndPerson> sAuthors = sEntry.getAuthors();
if (sAuthors != null && !sAuthors.isEmpty()) { final String author = sEntry.getAuthor();
if (Lists.isNotEmpty(sAuthors)) {
aEntry.setAuthors(createAtomPersons(sAuthors)); aEntry.setAuthors(createAtomPersons(sAuthors));
} else if (sEntry.getAuthor() != null) { } else if (author != null) {
final Person person = new Person(); final Person person = new Person();
person.setName(sEntry.getAuthor()); person.setName(author);
final List<SyndPerson> authors = new ArrayList<SyndPerson>(); final List<SyndPerson> authors = new ArrayList<SyndPerson>();
authors.add(person); authors.add(person);
aEntry.setAuthors(authors); aEntry.setAuthors(authors);

View file

@ -20,6 +20,10 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.jdom2.Element;
import com.rometools.utils.Lists;
import com.rometools.utils.Strings;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.atom.Category; import com.sun.syndication.feed.atom.Category;
import com.sun.syndication.feed.atom.Content; import com.sun.syndication.feed.atom.Content;
@ -48,6 +52,7 @@ import com.sun.syndication.feed.synd.SyndPerson;
/** /**
*/ */
public class ConverterForAtom10 implements Converter { public class ConverterForAtom10 implements Converter {
private final String type; private final String type;
public ConverterForAtom10() { public ConverterForAtom10() {
@ -65,24 +70,29 @@ public class ConverterForAtom10 implements Converter {
@Override @Override
public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { public void copyInto(final WireFeed feed, final SyndFeed syndFeed) {
final Feed aFeed = (Feed) feed; final Feed aFeed = (Feed) feed;
syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules())); syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules()));
if (!feed.getForeignMarkup().isEmpty()) { final List<Element> foreignMarkup = feed.getForeignMarkup();
syndFeed.setForeignMarkup(feed.getForeignMarkup()); if (!foreignMarkup.isEmpty()) {
syndFeed.setForeignMarkup(foreignMarkup);
} }
syndFeed.setEncoding(aFeed.getEncoding()); syndFeed.setEncoding(aFeed.getEncoding());
syndFeed.setStyleSheet(aFeed.getStyleSheet()); syndFeed.setStyleSheet(aFeed.getStyleSheet());
if (aFeed.getLogo() != null) { final String logo = aFeed.getLogo();
final String icon = aFeed.getIcon();
if (logo != null) {
final SyndImage image = new SyndImageImpl(); final SyndImage image = new SyndImageImpl();
image.setUrl(aFeed.getLogo()); image.setUrl(logo);
syndFeed.setImage(image); syndFeed.setImage(image);
} else if (aFeed.getIcon() != null) { } else if (icon != null) {
final SyndImage image = new SyndImageImpl(); final SyndImage image = new SyndImageImpl();
image.setUrl(aFeed.getIcon()); image.setUrl(icon);
syndFeed.setImage(image); syndFeed.setImage(image);
} }
@ -105,18 +115,24 @@ public class ConverterForAtom10 implements Converter {
} }
// use first alternate links as THE link // use first alternate links as THE link
if (aFeed.getAlternateLinks() != null && !aFeed.getAlternateLinks().isEmpty()) { final List<Link> alternateLinks = aFeed.getAlternateLinks();
final Link theLink = aFeed.getAlternateLinks().get(0); if (Lists.isNotEmpty(alternateLinks)) {
final Link theLink = alternateLinks.get(0);
syndFeed.setLink(theLink.getHrefResolved()); syndFeed.setLink(theLink.getHrefResolved());
} }
// lump alternate and other links together // lump alternate and other links together
final List<SyndLink> syndLinks = new ArrayList<SyndLink>(); final List<SyndLink> syndLinks = new ArrayList<SyndLink>();
if (aFeed.getAlternateLinks() != null && !aFeed.getAlternateLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(aFeed.getAlternateLinks())); if (Lists.isNotEmpty(alternateLinks)) {
syndLinks.addAll(createSyndLinks(alternateLinks));
} }
if (aFeed.getOtherLinks() != null && !aFeed.getOtherLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(aFeed.getOtherLinks())); final List<Link> otherLinks = aFeed.getOtherLinks();
if (Lists.isNotEmpty(otherLinks)) {
syndLinks.addAll(createSyndLinks(otherLinks));
} }
syndFeed.setLinks(syndLinks); syndFeed.setLinks(syndLinks);
final List<Entry> aEntries = aFeed.getEntries(); final List<Entry> aEntries = aFeed.getEntries();
@ -128,12 +144,12 @@ public class ConverterForAtom10 implements Converter {
// over DC equivalent info. // over DC equivalent info.
final List<SyndPerson> authors = aFeed.getAuthors(); final List<SyndPerson> authors = aFeed.getAuthors();
if (authors != null && !authors.isEmpty()) { if (Lists.isNotEmpty(authors)) {
syndFeed.setAuthors(ConverterForAtom03.createSyndPersons(authors)); syndFeed.setAuthors(ConverterForAtom03.createSyndPersons(authors));
} }
final List<SyndPerson> contributors = aFeed.getContributors(); final List<SyndPerson> contributors = aFeed.getContributors();
if (contributors != null && !contributors.isEmpty()) { if (Lists.isNotEmpty(contributors)) {
syndFeed.setContributors(ConverterForAtom03.createSyndPersons(contributors)); syndFeed.setContributors(ConverterForAtom03.createSyndPersons(contributors));
} }
@ -149,20 +165,19 @@ public class ConverterForAtom10 implements Converter {
} }
protected List<SyndLink> createSyndLinks(final List<Link> aLinks) { protected List<SyndLink> createSyndLinks(final List<Link> atomLinks) {
final ArrayList<SyndLink> sLinks = new ArrayList<SyndLink>(); final ArrayList<SyndLink> syndLinks = new ArrayList<SyndLink>();
for (final Link link2 : aLinks) { for (final Link atomLink : atomLinks) {
final Link link = link2; final SyndLink syndLink = createSyndLink(atomLink);
final SyndLink sLink = createSyndLink(link); syndLinks.add(syndLink);
sLinks.add(sLink);
} }
return sLinks; return syndLinks;
} }
protected List<SyndEntry> createSyndEntries(final Feed feed, final List<Entry> atomEntries, final boolean preserveWireItems) { protected List<SyndEntry> createSyndEntries(final Feed feed, final List<Entry> atomEntries, final boolean preserveWireItems) {
final List<SyndEntry> syndEntries = new ArrayList<SyndEntry>(); final List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
for (int i = 0; i < atomEntries.size(); i++) { for (final Entry atomEntry : atomEntries) {
syndEntries.add(createSyndEntry(feed, atomEntries.get(i), preserveWireItems)); syndEntries.add(createSyndEntry(feed, atomEntry, preserveWireItems));
} }
return syndEntries; return syndEntries;
} }
@ -174,8 +189,9 @@ public class ConverterForAtom10 implements Converter {
} }
syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules())); syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules()));
if (!entry.getForeignMarkup().isEmpty()) { final List<Element> foreignMarkup = entry.getForeignMarkup();
syndEntry.setForeignMarkup(entry.getForeignMarkup()); if (!foreignMarkup.isEmpty()) {
syndEntry.setForeignMarkup(foreignMarkup);
} }
final Content eTitle = entry.getTitleEx(); final Content eTitle = entry.getTitleEx();
@ -189,24 +205,23 @@ public class ConverterForAtom10 implements Converter {
} }
final List<Content> contents = entry.getContents(); final List<Content> contents = entry.getContents();
if (contents != null && !contents.isEmpty()) { if (Lists.isNotEmpty(contents)) {
final List<SyndContent> sContents = new ArrayList<SyndContent>(); final List<SyndContent> sContents = new ArrayList<SyndContent>();
for (final Content content2 : contents) { for (final Content content : contents) {
final Content content = content2;
sContents.add(createSyndContent(content)); sContents.add(createSyndContent(content));
} }
syndEntry.setContents(sContents); syndEntry.setContents(sContents);
} }
final List<SyndPerson> authors = entry.getAuthors(); final List<SyndPerson> authors = entry.getAuthors();
if (authors != null && !authors.isEmpty()) { if (Lists.isNotEmpty(authors)) {
syndEntry.setAuthors(ConverterForAtom03.createSyndPersons(authors)); syndEntry.setAuthors(ConverterForAtom03.createSyndPersons(authors));
final SyndPerson person0 = syndEntry.getAuthors().get(0); final SyndPerson person0 = syndEntry.getAuthors().get(0);
syndEntry.setAuthor(person0.getName()); syndEntry.setAuthor(person0.getName());
} }
final List<SyndPerson> contributors = entry.getContributors(); final List<SyndPerson> contributors = entry.getContributors();
if (contributors != null && !contributors.isEmpty()) { if (Lists.isNotEmpty(contributors)) {
syndEntry.setContributors(ConverterForAtom03.createSyndPersons(contributors)); syndEntry.setContributors(ConverterForAtom03.createSyndPersons(contributors));
} }
@ -224,10 +239,9 @@ public class ConverterForAtom10 implements Converter {
if (categories != null) { if (categories != null) {
final List<SyndCategory> syndCategories = new ArrayList<SyndCategory>(); final List<SyndCategory> syndCategories = new ArrayList<SyndCategory>();
for (final Category category : categories) { for (final Category category : categories) {
final Category c = category;
final SyndCategory syndCategory = new SyndCategoryImpl(); final SyndCategory syndCategory = new SyndCategoryImpl();
syndCategory.setName(c.getTerm()); syndCategory.setName(category.getTerm());
syndCategory.setTaxonomyUri(c.getSchemeResolved()); syndCategory.setTaxonomyUri(category.getSchemeResolved());
// TODO: categories MAY have labels // TODO: categories MAY have labels
// syndCategory.setLabel(c.getLabel()); // syndCategory.setLabel(c.getLabel());
syndCategories.add(syndCategory); syndCategories.add(syndCategory);
@ -236,19 +250,20 @@ public class ConverterForAtom10 implements Converter {
} }
// use first alternate link as THE link // use first alternate link as THE link
if (entry.getAlternateLinks() != null && !entry.getAlternateLinks().isEmpty()) { final List<Link> alternateLinks = entry.getAlternateLinks();
final Link theLink = entry.getAlternateLinks().get(0); if (Lists.isNotEmpty(alternateLinks)) {
final Link theLink = alternateLinks.get(0);
syndEntry.setLink(theLink.getHrefResolved()); syndEntry.setLink(theLink.getHrefResolved());
} }
// Create synd enclosures from enclosure links // Create synd enclosures from enclosure links
final List<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>(); final List<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>();
if (entry.getOtherLinks() != null && !entry.getOtherLinks().isEmpty()) { final List<Link> otherLinks = entry.getOtherLinks();
final List<Link> oLinks = entry.getOtherLinks(); if (Lists.isNotEmpty(otherLinks)) {
final List<Link> oLinks = otherLinks;
for (final Link link : oLinks) { for (final Link link : oLinks) {
final Link thisLink = link; if ("enclosure".equals(link.getRel())) {
if ("enclosure".equals(thisLink.getRel())) { syndEnclosures.add(createSyndEnclosure(feed, entry, link));
syndEnclosures.add(createSyndEnclosure(feed, entry, thisLink));
} }
} }
} }
@ -256,11 +271,11 @@ public class ConverterForAtom10 implements Converter {
// lump alternate and other links together // lump alternate and other links together
final List<SyndLink> syndLinks = new ArrayList<SyndLink>(); final List<SyndLink> syndLinks = new ArrayList<SyndLink>();
if (entry.getAlternateLinks() != null && !entry.getAlternateLinks().isEmpty()) { if (Lists.isNotEmpty(alternateLinks)) {
syndLinks.addAll(createSyndLinks(entry.getAlternateLinks())); syndLinks.addAll(createSyndLinks(alternateLinks));
} }
if (entry.getOtherLinks() != null && !entry.getOtherLinks().isEmpty()) { if (Lists.isNotEmpty(otherLinks)) {
syndLinks.addAll(createSyndLinks(entry.getOtherLinks())); syndLinks.addAll(createSyndLinks(otherLinks));
} }
syndEntry.setLinks(syndLinks); syndEntry.setLinks(syndLinks);
@ -352,10 +367,10 @@ public class ConverterForAtom10 implements Converter {
final List<Link> otherLinks = new ArrayList<Link>(); final List<Link> otherLinks = new ArrayList<Link>();
final List<SyndLink> slinks = syndFeed.getLinks(); final List<SyndLink> slinks = syndFeed.getLinks();
if (slinks != null) { if (slinks != null) {
for (final SyndLink syndLink2 : slinks) { for (final SyndLink syndLink : slinks) {
final SyndLink syndLink = syndLink2;
final Link link = createAtomLink(syndLink); final Link link = createAtomLink(syndLink);
if (link.getRel() == null || "".equals(link.getRel().trim()) || "alternate".equals(link.getRel())) { final String rel = link.getRel();
if (Strings.isBlank(rel) || "alternate".equals(rel)) {
alternateLinks.add(link); alternateLinks.add(link);
} else { } else {
otherLinks.add(link); otherLinks.add(link);
@ -379,8 +394,7 @@ public class ConverterForAtom10 implements Converter {
final List<SyndCategory> sCats = syndFeed.getCategories(); final List<SyndCategory> sCats = syndFeed.getCategories();
final List<Category> aCats = new ArrayList<Category>(); final List<Category> aCats = new ArrayList<Category>();
if (sCats != null) { if (sCats != null) {
for (final SyndCategory syndCategory : sCats) { for (final SyndCategory sCat : sCats) {
final SyndCategory sCat = syndCategory;
final Category aCat = new Category(); final Category aCat = new Category();
aCat.setTerm(sCat.getName()); aCat.setTerm(sCat.getName());
// TODO: aCat.setLabel(sCat.getLabel()); // TODO: aCat.setLabel(sCat.getLabel());
@ -393,12 +407,12 @@ public class ConverterForAtom10 implements Converter {
} }
final List<SyndPerson> authors = syndFeed.getAuthors(); final List<SyndPerson> authors = syndFeed.getAuthors();
if (authors != null && !authors.isEmpty()) { if (Lists.isNotEmpty(authors)) {
aFeed.setAuthors(ConverterForAtom03.createAtomPersons(authors)); aFeed.setAuthors(ConverterForAtom03.createAtomPersons(authors));
} }
final List<SyndPerson> contributors = syndFeed.getContributors(); final List<SyndPerson> contributors = syndFeed.getContributors();
if (contributors != null && !contributors.isEmpty()) { if (Lists.isNotEmpty(contributors)) {
aFeed.setContributors(ConverterForAtom03.createAtomPersons(contributors)); aFeed.setContributors(ConverterForAtom03.createAtomPersons(contributors));
} }
@ -411,10 +425,13 @@ public class ConverterForAtom10 implements Converter {
aFeed.setEntries(createAtomEntries(sEntries)); aFeed.setEntries(createAtomEntries(sEntries));
} }
if (!syndFeed.getForeignMarkup().isEmpty()) { final List<Element> foreignMarkup = syndFeed.getForeignMarkup();
aFeed.setForeignMarkup(syndFeed.getForeignMarkup()); if (!foreignMarkup.isEmpty()) {
aFeed.setForeignMarkup(foreignMarkup);
} }
return aFeed; return aFeed;
} }
protected SyndContent createSyndContent(final Content content) { protected SyndContent createSyndContent(final Content content) {
@ -426,8 +443,8 @@ public class ConverterForAtom10 implements Converter {
protected List<Entry> createAtomEntries(final List<SyndEntry> syndEntries) { protected List<Entry> createAtomEntries(final List<SyndEntry> syndEntries) {
final List<Entry> atomEntries = new ArrayList<Entry>(); final List<Entry> atomEntries = new ArrayList<Entry>();
for (int i = 0; i < syndEntries.size(); i++) { for (final SyndEntry syndEntry : syndEntries) {
atomEntries.add(createAtomEntry(syndEntries.get(i))); atomEntries.add(createAtomEntry(syndEntry));
} }
return atomEntries; return atomEntries;
} }
@ -441,14 +458,16 @@ public class ConverterForAtom10 implements Converter {
protected List<Content> createAtomContents(final List<SyndContent> syndContents) { protected List<Content> createAtomContents(final List<SyndContent> syndContents) {
final List<Content> atomContents = new ArrayList<Content>(); final List<Content> atomContents = new ArrayList<Content>();
for (int i = 0; i < syndContents.size(); i++) { for (final SyndContent syndContent : syndContents) {
atomContents.add(createAtomContent(syndContents.get(i))); atomContents.add(createAtomContent(syndContent));
} }
return atomContents; return atomContents;
} }
protected Entry createAtomEntry(final SyndEntry sEntry) { protected Entry createAtomEntry(final SyndEntry sEntry) {
final Entry aEntry = new Entry(); final Entry aEntry = new Entry();
aEntry.setModules(ModuleUtils.cloneModules(sEntry.getModules())); aEntry.setModules(ModuleUtils.cloneModules(sEntry.getModules()));
aEntry.setId(sEntry.getUri()); aEntry.setId(sEntry.getUri());
@ -472,26 +491,31 @@ public class ConverterForAtom10 implements Converter {
// separate SyndEntry's links collection into alternate and other links // separate SyndEntry's links collection into alternate and other links
final List<Link> alternateLinks = new ArrayList<Link>(); final List<Link> alternateLinks = new ArrayList<Link>();
final List<Link> otherLinks = new ArrayList<Link>(); final List<Link> otherLinks = new ArrayList<Link>();
boolean linkRelEnclosureExists = false;
final List<SyndLink> slinks = sEntry.getLinks(); final List<SyndLink> slinks = sEntry.getLinks();
final List<SyndEnclosure> enclosures = sEntry.getEnclosures(); final List<SyndEnclosure> enclosures = sEntry.getEnclosures();
boolean linkRelEnclosureExists = false;
if (slinks != null) { if (slinks != null) {
for (final SyndLink syndLink2 : slinks) { for (final SyndLink syndLink : slinks) {
final SyndLink syndLink = syndLink2;
final Link link = createAtomLink(syndLink); final Link link = createAtomLink(syndLink);
// Set this flag if there's a link of rel = enclosure so that // Set this flag if there's a link of rel = enclosure so that
// enclosures won't be duplicated when pulled from // enclosures won't be duplicated when pulled from
// SyndEntry.getEnclosures() // SyndEntry.getEnclosures()
if (syndLink.getRel() != null && "enclosure".equals(syndLink.getRel())) { final String sRel = syndLink.getRel();
if (sRel != null && "enclosure".equals(sRel)) {
linkRelEnclosureExists = true; linkRelEnclosureExists = true;
} }
if (link.getRel() == null || "".equals(link.getRel().trim()) || "alternate".equals(syndLink.getRel())) {
final String lRel = link.getRel();
if (Strings.isBlank(lRel) || "alternate".equals(sRel)) {
alternateLinks.add(link); alternateLinks.add(link);
} else { } else {
otherLinks.add(link); otherLinks.add(link);
} }
} }
} }
// no alternate link? then use THE link if there is one // no alternate link? then use THE link if there is one
if (alternateLinks.isEmpty() && sEntry.getLink() != null) { if (alternateLinks.isEmpty() && sEntry.getLink() != null) {
final Link link = new Link(); final Link link = new Link();
@ -499,6 +523,7 @@ public class ConverterForAtom10 implements Converter {
link.setHref(sEntry.getLink()); link.setHref(sEntry.getLink());
alternateLinks.add(link); alternateLinks.add(link);
} }
// add SyndEnclosures as links with rel="enclosure" ONLY if // add SyndEnclosures as links with rel="enclosure" ONLY if
// there are no SyndEntry.getLinks() with rel="enclosure" // there are no SyndEntry.getLinks() with rel="enclosure"
if (enclosures != null && linkRelEnclosureExists == false) { if (enclosures != null && linkRelEnclosureExists == false) {
@ -508,9 +533,11 @@ public class ConverterForAtom10 implements Converter {
otherLinks.add(link); otherLinks.add(link);
} }
} }
if (!alternateLinks.isEmpty()) { if (!alternateLinks.isEmpty()) {
aEntry.setAlternateLinks(alternateLinks); aEntry.setAlternateLinks(alternateLinks);
} }
if (!otherLinks.isEmpty()) { if (!otherLinks.isEmpty()) {
aEntry.setOtherLinks(otherLinks); aEntry.setOtherLinks(otherLinks);
} }
@ -518,8 +545,7 @@ public class ConverterForAtom10 implements Converter {
final List<SyndCategory> sCats = sEntry.getCategories(); final List<SyndCategory> sCats = sEntry.getCategories();
final List<Category> aCats = new ArrayList<Category>(); final List<Category> aCats = new ArrayList<Category>();
if (sCats != null) { if (sCats != null) {
for (final SyndCategory syndCategory : sCats) { for (final SyndCategory sCat : sCats) {
final SyndCategory sCat = syndCategory;
final Category aCat = new Category(); final Category aCat = new Category();
aCat.setTerm(sCat.getName()); aCat.setTerm(sCat.getName());
// TODO: aCat.setLabel(sCat.getLabel()); // TODO: aCat.setLabel(sCat.getLabel());
@ -527,6 +553,7 @@ public class ConverterForAtom10 implements Converter {
aCats.add(aCat); aCats.add(aCat);
} }
} }
if (!aCats.isEmpty()) { if (!aCats.isEmpty()) {
aEntry.setCategories(aCats); aEntry.setCategories(aCats);
} }
@ -535,18 +562,19 @@ public class ConverterForAtom10 implements Converter {
aEntry.setContents(createAtomContents(syndContents)); aEntry.setContents(createAtomContents(syndContents));
List<SyndPerson> authors = sEntry.getAuthors(); List<SyndPerson> authors = sEntry.getAuthors();
if (authors != null && !authors.isEmpty()) { final String author = sEntry.getAuthor();
if (Lists.isNotEmpty(authors)) {
aEntry.setAuthors(ConverterForAtom03.createAtomPersons(authors)); aEntry.setAuthors(ConverterForAtom03.createAtomPersons(authors));
} else if (sEntry.getAuthor() != null) { } else if (author != null) {
final Person person = new Person(); final Person person = new Person();
person.setName(sEntry.getAuthor()); person.setName(author);
authors = new ArrayList<SyndPerson>(); authors = new ArrayList<SyndPerson>();
authors.add(person); authors.add(person);
aEntry.setAuthors(authors); aEntry.setAuthors(authors);
} }
final List<SyndPerson> contributors = sEntry.getContributors(); final List<SyndPerson> contributors = sEntry.getContributors();
if (contributors != null && !contributors.isEmpty()) { if (Lists.isNotEmpty(contributors)) {
aEntry.setContributors(ConverterForAtom03.createAtomPersons(contributors)); aEntry.setContributors(ConverterForAtom03.createAtomPersons(contributors));
} }
@ -561,8 +589,9 @@ public class ConverterForAtom10 implements Converter {
aEntry.setUpdated(sEntry.getPublishedDate()); aEntry.setUpdated(sEntry.getPublishedDate());
} }
if (!sEntry.getForeignMarkup().isEmpty()) { final List<Element> foreignMarkup = sEntry.getForeignMarkup();
aEntry.setForeignMarkup(sEntry.getForeignMarkup()); if (!foreignMarkup.isEmpty()) {
aEntry.setForeignMarkup(foreignMarkup);
} }
final SyndFeed sSource = sEntry.getSource(); final SyndFeed sSource = sEntry.getSource();

View file

@ -19,6 +19,8 @@ package com.sun.syndication.feed.synd.impl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.jdom2.Element;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.module.impl.ModuleUtils; import com.sun.syndication.feed.module.impl.ModuleUtils;
import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Channel;
@ -32,6 +34,7 @@ import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl; import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.feed.synd.SyndImage; import com.sun.syndication.feed.synd.SyndImage;
import com.sun.syndication.feed.synd.SyndImageImpl; import com.sun.syndication.feed.synd.SyndImageImpl;
import com.sun.syndication.feed.synd.SyndLink;
/** /**
*/ */
@ -53,13 +56,20 @@ public class ConverterForRSS090 implements Converter {
@Override @Override
public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { public void copyInto(final WireFeed feed, final SyndFeed syndFeed) {
syndFeed.setModules(ModuleUtils.cloneModules(feed.getModules())); syndFeed.setModules(ModuleUtils.cloneModules(feed.getModules()));
if (!feed.getForeignMarkup().isEmpty()) {
syndFeed.setForeignMarkup(feed.getForeignMarkup()); final List<Element> foreignMarkup = feed.getForeignMarkup();
if (!foreignMarkup.isEmpty()) {
syndFeed.setForeignMarkup(foreignMarkup);
} }
syndFeed.setStyleSheet(feed.getStyleSheet()); syndFeed.setStyleSheet(feed.getStyleSheet());
syndFeed.setEncoding(feed.getEncoding()); syndFeed.setEncoding(feed.getEncoding());
final Channel channel = (Channel) feed; final Channel channel = (Channel) feed;
syndFeed.setTitle(channel.getTitle()); syndFeed.setTitle(channel.getTitle());
syndFeed.setLink(channel.getLink()); syndFeed.setLink(channel.getLink());
syndFeed.setDescription(channel.getDescription()); syndFeed.setDescription(channel.getDescription());
@ -85,22 +95,25 @@ public class ConverterForRSS090 implements Converter {
protected List<SyndEntry> createSyndEntries(final List<Item> rssItems, final boolean preserveWireItems) { protected List<SyndEntry> createSyndEntries(final List<Item> rssItems, final boolean preserveWireItems) {
final List<SyndEntry> syndEntries = new ArrayList<SyndEntry>(); final List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
for (int i = 0; i < rssItems.size(); i++) { for (final Item item : rssItems) {
syndEntries.add(createSyndEntry(rssItems.get(i), preserveWireItems)); syndEntries.add(createSyndEntry(item, preserveWireItems));
} }
return syndEntries; return syndEntries;
} }
protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) {
final SyndEntryImpl syndEntry = new SyndEntryImpl(); final SyndEntryImpl syndEntry = new SyndEntryImpl();
if (preserveWireItem) { if (preserveWireItem) {
syndEntry.setWireEntry(item); syndEntry.setWireEntry(item);
} }
syndEntry.setModules(ModuleUtils.cloneModules(item.getModules())); syndEntry.setModules(ModuleUtils.cloneModules(item.getModules()));
if (!item.getForeignMarkup().isEmpty()) { final List<Element> foreignMarkup = item.getForeignMarkup();
syndEntry.setForeignMarkup(item.getForeignMarkup()); if (!foreignMarkup.isEmpty()) {
syndEntry.setForeignMarkup(foreignMarkup);
} }
syndEntry.setUri(item.getUri()); syndEntry.setUri(item.getUri());
@ -134,12 +147,16 @@ public class ConverterForRSS090 implements Converter {
channel.setEncoding(syndFeed.getEncoding()); channel.setEncoding(syndFeed.getEncoding());
channel.setTitle(syndFeed.getTitle()); channel.setTitle(syndFeed.getTitle());
if (syndFeed.getLink() != null) { final String link = syndFeed.getLink();
channel.setLink(syndFeed.getLink()); final List<SyndLink> links = syndFeed.getLinks();
} else if (!syndFeed.getLinks().isEmpty()) { if (link != null) {
channel.setLink(syndFeed.getLinks().get(0).getHref()); channel.setLink(link);
} else if (!links.isEmpty()) {
channel.setLink(links.get(0).getHref());
} }
channel.setDescription(syndFeed.getDescription()); channel.setDescription(syndFeed.getDescription());
final SyndImage sImage = syndFeed.getImage(); final SyndImage sImage = syndFeed.getImage();
if (sImage != null) { if (sImage != null) {
channel.setImage(createRSSImage(sImage)); channel.setImage(createRSSImage(sImage));
@ -150,9 +167,11 @@ public class ConverterForRSS090 implements Converter {
channel.setItems(createRSSItems(sEntries)); channel.setItems(createRSSItems(sEntries));
} }
if (!syndFeed.getForeignMarkup().isEmpty()) { final List<Element> foreignMarkup = syndFeed.getForeignMarkup();
channel.setForeignMarkup(syndFeed.getForeignMarkup()); if (!foreignMarkup.isEmpty()) {
channel.setForeignMarkup(foreignMarkup);
} }
return channel; return channel;
} }
@ -166,22 +185,29 @@ public class ConverterForRSS090 implements Converter {
protected List<Item> createRSSItems(final List<SyndEntry> sEntries) { protected List<Item> createRSSItems(final List<SyndEntry> sEntries) {
final List<Item> list = new ArrayList<Item>(); final List<Item> list = new ArrayList<Item>();
for (int i = 0; i < sEntries.size(); i++) { for (final SyndEntry syndEntry : sEntries) {
list.add(createRSSItem(sEntries.get(i))); list.add(createRSSItem(syndEntry));
} }
return list; return list;
} }
protected Item createRSSItem(final SyndEntry sEntry) { protected Item createRSSItem(final SyndEntry sEntry) {
final Item item = new Item(); final Item item = new Item();
item.setModules(ModuleUtils.cloneModules(sEntry.getModules())); item.setModules(ModuleUtils.cloneModules(sEntry.getModules()));
item.setTitle(sEntry.getTitle()); item.setTitle(sEntry.getTitle());
item.setLink(sEntry.getLink()); item.setLink(sEntry.getLink());
if (!sEntry.getForeignMarkup().isEmpty()) {
item.setForeignMarkup(sEntry.getForeignMarkup()); final List<Element> foreignMarkup = sEntry.getForeignMarkup();
if (!foreignMarkup.isEmpty()) {
item.setForeignMarkup(foreignMarkup);
} }
item.setSource(createSource(sEntry.getSource())); item.setSource(createSource(sEntry.getSource()));
final String uri = sEntry.getUri(); final String uri = sEntry.getUri();
if (uri != null) { if (uri != null) {
item.setUri(uri); item.setUri(uri);

View file

@ -22,6 +22,7 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.module.DCModule; import com.sun.syndication.feed.module.DCModule;
import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Channel;
@ -36,9 +37,8 @@ import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndImage; import com.sun.syndication.feed.synd.SyndImage;
import com.sun.syndication.feed.synd.SyndPerson; import com.sun.syndication.feed.synd.SyndPerson;
/**
*/
public class ConverterForRSS091Userland extends ConverterForRSS090 { public class ConverterForRSS091Userland extends ConverterForRSS090 {
public ConverterForRSS091Userland() { public ConverterForRSS091Userland() {
this("rss_0.91U"); this("rss_0.91U");
} }
@ -49,13 +49,21 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 {
@Override @Override
public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { public void copyInto(final WireFeed feed, final SyndFeed syndFeed) {
final Channel channel = (Channel) feed; final Channel channel = (Channel) feed;
super.copyInto(channel, syndFeed); super.copyInto(channel, syndFeed);
syndFeed.setLanguage(channel.getLanguage()); // c syndFeed.setLanguage(channel.getLanguage()); // c
syndFeed.setCopyright(channel.getCopyright()); // c syndFeed.setCopyright(channel.getCopyright()); // c
syndFeed.setDocs(channel.getDocs()); syndFeed.setDocs(channel.getDocs());
syndFeed.setManagingEditor(channel.getManagingEditor()); syndFeed.setManagingEditor(channel.getManagingEditor());
syndFeed.setWebMaster(channel.getWebMaster()); syndFeed.setWebMaster(channel.getWebMaster());
syndFeed.setGenerator(channel.getGenerator()); syndFeed.setGenerator(channel.getGenerator());
final Date pubDate = channel.getPubDate(); final Date pubDate = channel.getPubDate();
@ -69,12 +77,12 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 {
final String author = channel.getManagingEditor(); final String author = channel.getManagingEditor();
if (author != null) { if (author != null) {
final List<String> creators = ((DCModule) syndFeed.getModule(DCModule.URI)).getCreators(); final List<String> creators = ((DCModule) syndFeed.getModule(DCModule.URI)).getCreators();
if (!creators.contains(author)) { if (!creators.contains(author)) {
final Set<String> s = new LinkedHashSet<String>(); // using a set to // using a set to remove duplicates
// remove final Set<String> s = new LinkedHashSet<String>();
// duplicates
s.addAll(creators); // DC creators s.addAll(creators); // DC creators
s.add(author); // feed native author s.add(author); // feed native author
creators.clear(); creators.clear();
@ -87,7 +95,6 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 {
final Description desc = new Description(); final Description desc = new Description();
desc.setValue(sContent.getValue()); desc.setValue(sContent.getValue());
desc.setType(sContent.getType()); desc.setType(sContent.getType());
return desc; return desc;
} }
@ -95,7 +102,6 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 {
protected Image createRSSImage(final SyndImage sImage) { protected Image createRSSImage(final SyndImage sImage) {
final Image image = super.createRSSImage(sImage); final Image image = super.createRSSImage(sImage);
image.setDescription(sImage.getDescription()); image.setDescription(sImage.getDescription());
return image; return image;
} }
@ -104,8 +110,11 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 {
// synd.description -> rss.description // synd.description -> rss.description
@Override @Override
protected Item createRSSItem(final SyndEntry sEntry) { protected Item createRSSItem(final SyndEntry sEntry) {
final Item item = super.createRSSItem(sEntry); final Item item = super.createRSSItem(sEntry);
item.setComments(sEntry.getComments()); item.setComments(sEntry.getComments());
final SyndContent sContent = sEntry.getDescription(); final SyndContent sContent = sEntry.getDescription();
if (sContent != null) { if (sContent != null) {
@ -114,7 +123,7 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 {
final List<SyndContent> contents = sEntry.getContents(); final List<SyndContent> contents = sEntry.getContents();
if (contents != null && !contents.isEmpty()) { if (Lists.isNotEmpty(contents)) {
final SyndContent syndContent = contents.get(0); final SyndContent syndContent = contents.get(0);
final Content cont = new Content(); final Content cont = new Content();
cont.setValue(syndContent.getValue()); cont.setValue(syndContent.getValue());
@ -136,8 +145,9 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 {
channel.setWebMaster(syndFeed.getWebMaster()); channel.setWebMaster(syndFeed.getWebMaster());
channel.setGenerator(syndFeed.getGenerator()); channel.setGenerator(syndFeed.getGenerator());
if (syndFeed.getAuthors() != null && !syndFeed.getAuthors().isEmpty()) { final List<SyndPerson> authors = syndFeed.getAuthors();
final SyndPerson author = syndFeed.getAuthors().get(0); if (Lists.isNotEmpty(authors)) {
final SyndPerson author = authors.get(0);
channel.setManagingEditor(author.getName()); channel.setManagingEditor(author.getName());
} }
@ -149,8 +159,11 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 {
// rss.description -> synd.description // rss.description -> synd.description
@Override @Override
protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) {
final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem); final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem);
final Description desc = item.getDescription(); final Description desc = item.getDescription();
syndEntry.setComments(item.getComments()); syndEntry.setComments(item.getComments());
if (desc != null) { if (desc != null) {
@ -179,7 +192,7 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 {
protected SyndImage createSyndImage(final Image rssImage) { protected SyndImage createSyndImage(final Image rssImage) {
final SyndImage syndImage = super.createSyndImage(rssImage); final SyndImage syndImage = super.createSyndImage(rssImage);
syndImage.setDescription(rssImage.getDescription()); syndImage.setDescription(rssImage.getDescription());
return syndImage; return syndImage;
} }
} }

View file

@ -44,40 +44,38 @@ public class ConverterForRSS092 extends ConverterForRSS091Userland {
@Override @Override
protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) {
final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem); final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem);
final List<Category> cats = item.getCategories(); final List<Category> cats = item.getCategories();
if (!cats.isEmpty()) { if (!cats.isEmpty()) {
final Set<SyndCategory> s = new LinkedHashSet<SyndCategory>(); // using
// a // using a set to remove duplicates and use a LinkedHashSet to try to retain the
// set to // document order
// remove final Set<SyndCategory> s = new LinkedHashSet<SyndCategory>();
// duplicates
// and use // feed native categories (as syndcat)
// a s.addAll(createSyndCategories(cats));
// LinkedHashSet
// to try // DC subjects (as syndcat)
// to s.addAll(syndEntry.getCategories());
// retain
// the
// document
// order
s.addAll(createSyndCategories(cats)); // feed native categories
// (as
// syndcat)
s.addAll(syndEntry.getCategories()); // DC subjects (as syndcat)
syndEntry.setCategories(new ArrayList<SyndCategory>(s)); // c syndEntry.setCategories(new ArrayList<SyndCategory>(s)); // c
} }
final List<Enclosure> enclosures = item.getEnclosures(); final List<Enclosure> enclosures = item.getEnclosures();
if (!enclosures.isEmpty()) { if (!enclosures.isEmpty()) {
syndEntry.setEnclosures(createSyndEnclosures(enclosures)); syndEntry.setEnclosures(createSyndEnclosures(enclosures));
} }
return syndEntry; return syndEntry;
} }
protected List<SyndCategory> createSyndCategories(final List<Category> rssCats) { protected List<SyndCategory> createSyndCategories(final List<Category> rssCats) {
final List<SyndCategory> syndCats = new ArrayList<SyndCategory>(); final List<SyndCategory> syndCats = new ArrayList<SyndCategory>();
for (int i = 0; i < rssCats.size(); i++) { for (final Category rssCat : rssCats) {
final Category rssCat = rssCats.get(i);
final SyndCategory sCat = new SyndCategoryImpl(); final SyndCategory sCat = new SyndCategoryImpl();
sCat.setTaxonomyUri(rssCat.getDomain()); sCat.setTaxonomyUri(rssCat.getDomain());
sCat.setName(rssCat.getValue()); sCat.setName(rssCat.getValue());
@ -88,8 +86,7 @@ public class ConverterForRSS092 extends ConverterForRSS091Userland {
protected List<SyndEnclosure> createSyndEnclosures(final List<Enclosure> enclosures) { protected List<SyndEnclosure> createSyndEnclosures(final List<Enclosure> enclosures) {
final List<SyndEnclosure> sEnclosures = new ArrayList<SyndEnclosure>(); final List<SyndEnclosure> sEnclosures = new ArrayList<SyndEnclosure>();
for (int i = 0; i < enclosures.size(); i++) { for (final Enclosure enc : enclosures) {
final Enclosure enc = enclosures.get(i);
final SyndEnclosure sEnc = new SyndEnclosureImpl(); final SyndEnclosure sEnc = new SyndEnclosureImpl();
sEnc.setUrl(enc.getUrl()); sEnc.setUrl(enc.getUrl());
sEnc.setType(enc.getType()); sEnc.setType(enc.getType());
@ -101,23 +98,26 @@ public class ConverterForRSS092 extends ConverterForRSS091Userland {
@Override @Override
protected Item createRSSItem(final SyndEntry sEntry) { protected Item createRSSItem(final SyndEntry sEntry) {
final Item item = super.createRSSItem(sEntry); final Item item = super.createRSSItem(sEntry);
final List<SyndCategory> sCats = sEntry.getCategories(); // c final List<SyndCategory> sCats = sEntry.getCategories(); // c
if (!sCats.isEmpty()) { if (!sCats.isEmpty()) {
item.setCategories(createRSSCategories(sCats)); item.setCategories(createRSSCategories(sCats));
} }
final List<SyndEnclosure> sEnclosures = sEntry.getEnclosures(); final List<SyndEnclosure> sEnclosures = sEntry.getEnclosures();
if (!sEnclosures.isEmpty()) { if (!sEnclosures.isEmpty()) {
item.setEnclosures(createEnclosures(sEnclosures)); item.setEnclosures(createEnclosures(sEnclosures));
} }
return item; return item;
} }
protected List<Category> createRSSCategories(final List<SyndCategory> sCats) { protected List<Category> createRSSCategories(final List<SyndCategory> sCats) {
final List<Category> cats = new ArrayList<Category>(); final List<Category> cats = new ArrayList<Category>();
for (int i = 0; i < sCats.size(); i++) { for (final SyndCategory sCat : sCats) {
final SyndCategory sCat = sCats.get(i);
final Category cat = new Category(); final Category cat = new Category();
cat.setDomain(sCat.getTaxonomyUri()); cat.setDomain(sCat.getTaxonomyUri());
cat.setValue(sCat.getName()); cat.setValue(sCat.getName());
@ -128,8 +128,7 @@ public class ConverterForRSS092 extends ConverterForRSS091Userland {
protected List<Enclosure> createEnclosures(final List<SyndEnclosure> sEnclosures) { protected List<Enclosure> createEnclosures(final List<SyndEnclosure> sEnclosures) {
final List<Enclosure> enclosures = new ArrayList<Enclosure>(); final List<Enclosure> enclosures = new ArrayList<Enclosure>();
for (int i = 0; i < sEnclosures.size(); i++) { for (final SyndEnclosure sEnc : sEnclosures) {
final SyndEnclosure sEnc = sEnclosures.get(i);
final Enclosure enc = new Enclosure(); final Enclosure enc = new Enclosure();
enc.setUrl(sEnc.getUrl()); enc.setUrl(sEnc.getUrl());
enc.setType(sEnc.getType()); enc.setType(sEnc.getType());

View file

@ -21,8 +21,6 @@ import java.util.Date;
import com.sun.syndication.feed.rss.Item; import com.sun.syndication.feed.rss.Item;
import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndEntry;
/**
*/
public class ConverterForRSS093 extends ConverterForRSS092 { public class ConverterForRSS093 extends ConverterForRSS092 {
public ConverterForRSS093() { public ConverterForRSS093() {
@ -35,11 +33,15 @@ public class ConverterForRSS093 extends ConverterForRSS092 {
@Override @Override
protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) {
final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem); final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem);
final Date pubDate = item.getPubDate(); final Date pubDate = item.getPubDate();
if (pubDate != null && syndEntry.getPublishedDate() == null) { final Date publishedDate = syndEntry.getPublishedDate();
if (pubDate != null && publishedDate == null) {
syndEntry.setPublishedDate(pubDate); // c syndEntry.setPublishedDate(pubDate); // c
} }
return syndEntry; return syndEntry;
} }

View file

@ -21,6 +21,7 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.module.DCModule; import com.sun.syndication.feed.module.DCModule;
import com.sun.syndication.feed.rss.Category; import com.sun.syndication.feed.rss.Category;
@ -34,8 +35,6 @@ import com.sun.syndication.feed.synd.SyndLink;
import com.sun.syndication.feed.synd.SyndLinkImpl; import com.sun.syndication.feed.synd.SyndLinkImpl;
import com.sun.syndication.feed.synd.SyndPerson; import com.sun.syndication.feed.synd.SyndPerson;
/**
*/
public class ConverterForRSS094 extends ConverterForRSS093 { public class ConverterForRSS094 extends ConverterForRSS093 {
public ConverterForRSS094() { public ConverterForRSS094() {
@ -48,24 +47,32 @@ public class ConverterForRSS094 extends ConverterForRSS093 {
@Override @Override
public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { public void copyInto(final WireFeed feed, final SyndFeed syndFeed) {
final Channel channel = (Channel) feed; final Channel channel = (Channel) feed;
super.copyInto(channel, syndFeed); super.copyInto(channel, syndFeed);
final List<Category> cats = channel.getCategories(); // c final List<Category> cats = channel.getCategories(); // c
if (!cats.isEmpty()) { if (!cats.isEmpty()) {
final Set<SyndCategory> s = new LinkedHashSet<SyndCategory>(); // using a
// set to // using a set to remove duplicates
// remove final Set<SyndCategory> s = new LinkedHashSet<SyndCategory>();
// duplicates
s.addAll(createSyndCategories(cats)); // feed native categories // feed native categories (as syndcat)
// (as s.addAll(createSyndCategories(cats));
// syndcat)
s.addAll(syndFeed.getCategories()); // DC subjects (as syndcat) // DC subjects (as syndcat)
s.addAll(syndFeed.getCategories());
syndFeed.setCategories(new ArrayList<SyndCategory>(s)); syndFeed.setCategories(new ArrayList<SyndCategory>(s));
} }
} }
@Override @Override
protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) {
final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem); final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem);
// adding native feed author to DC creators list // adding native feed author to DC creators list
@ -73,32 +80,42 @@ public class ConverterForRSS094 extends ConverterForRSS093 {
if (author != null) { if (author != null) {
final List<String> creators = ((DCModule) syndEntry.getModule(DCModule.URI)).getCreators(); final List<String> creators = ((DCModule) syndEntry.getModule(DCModule.URI)).getCreators();
if (!creators.contains(author)) { if (!creators.contains(author)) {
final Set<String> s = new LinkedHashSet<String>(); // using a set to
// remove // using a set to remove duplicates
// duplicates final Set<String> s = new LinkedHashSet<String>();
s.addAll(creators); // DC creators
s.add(author); // feed native author // DC creators
s.addAll(creators);
// feed native author
s.add(author);
creators.clear(); creators.clear();
creators.addAll(s); creators.addAll(s);
} }
} }
final Guid guid = item.getGuid(); final Guid guid = item.getGuid();
final String itemLink = item.getLink();
if (guid != null) { if (guid != null) {
syndEntry.setUri(guid.getValue()); final String guidValue = guid.getValue();
if (item.getLink() == null && guid.isPermaLink()) { syndEntry.setUri(guidValue);
syndEntry.setLink(guid.getValue()); if (itemLink == null && guid.isPermaLink()) {
syndEntry.setLink(guidValue);
} }
} else { } else {
syndEntry.setUri(item.getLink()); syndEntry.setUri(itemLink);
} }
if (item.getComments() != null) { if (item.getComments() != null) {
final SyndLinkImpl comments = new SyndLinkImpl(); final SyndLinkImpl comments = new SyndLinkImpl();
comments.setRel("comments"); comments.setRel("comments");
comments.setHref(item.getComments()); comments.setHref(item.getComments());
comments.setType("text/html"); comments.setType("text/html");
} }
return syndEntry; return syndEntry;
} }
@Override @Override
@ -113,32 +130,36 @@ public class ConverterForRSS094 extends ConverterForRSS093 {
@Override @Override
protected Item createRSSItem(final SyndEntry sEntry) { protected Item createRSSItem(final SyndEntry sEntry) {
final Item item = super.createRSSItem(sEntry); final Item item = super.createRSSItem(sEntry);
if (sEntry.getAuthors() != null && !sEntry.getAuthors().isEmpty()) {
final SyndPerson author = sEntry.getAuthors().get(0); final List<SyndPerson> authors = sEntry.getAuthors();
if (Lists.isNotEmpty(authors)) {
final SyndPerson author = authors.get(0);
item.setAuthor(author.getEmail()); item.setAuthor(author.getEmail());
} }
Guid guid = null; Guid guid = null;
final String uri = sEntry.getUri(); final String uri = sEntry.getUri();
final String link = sEntry.getLink();
if (uri != null) { if (uri != null) {
guid = new Guid(); guid = new Guid();
guid.setPermaLink(false); guid.setPermaLink(false);
guid.setValue(uri); guid.setValue(uri);
} else { } else if (link != null) {
final String link = sEntry.getLink(); guid = new Guid();
if (link != null) { guid.setPermaLink(true);
guid = new Guid(); guid.setValue(link);
guid.setPermaLink(true);
guid.setValue(link);
}
} }
item.setGuid(guid); item.setGuid(guid);
final SyndLink comments = sEntry.findRelatedLink("comments"); final SyndLink comments = sEntry.findRelatedLink("comments");
if (comments != null && (comments.getType() == null || comments.getType().endsWith("html"))) { if (comments != null && (comments.getType() == null || comments.getType().endsWith("html"))) {
item.setComments(comments.getHref()); item.setComments(comments.getHref());
} }
return item; return item;
} }
} }

View file

@ -19,6 +19,7 @@ package com.sun.syndication.feed.synd.impl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Content; import com.sun.syndication.feed.rss.Content;
@ -29,8 +30,6 @@ import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.feed.synd.SyndFeed;
/**
*/
public class ConverterForRSS10 extends ConverterForRSS090 { public class ConverterForRSS10 extends ConverterForRSS090 {
public ConverterForRSS10() { public ConverterForRSS10() {
@ -43,13 +42,18 @@ public class ConverterForRSS10 extends ConverterForRSS090 {
@Override @Override
public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { public void copyInto(final WireFeed feed, final SyndFeed syndFeed) {
final Channel channel = (Channel) feed; final Channel channel = (Channel) feed;
super.copyInto(channel, syndFeed); super.copyInto(channel, syndFeed);
if (channel.getUri() != null) {
syndFeed.setUri(channel.getUri()); final String uri = channel.getUri();
if (uri != null) {
syndFeed.setUri(uri);
} else { } else {
// if URI is not set use the value for link // if URI is not set use the value for link
syndFeed.setUri(channel.getLink()); final String link = channel.getLink();
syndFeed.setUri(link);
} }
} }
@ -59,6 +63,7 @@ public class ConverterForRSS10 extends ConverterForRSS090 {
@Override @Override
protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) {
final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem); final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem);
final Description desc = item.getDescription(); final Description desc = item.getDescription();
@ -68,27 +73,36 @@ public class ConverterForRSS10 extends ConverterForRSS090 {
descContent.setValue(desc.getValue()); descContent.setValue(desc.getValue());
syndEntry.setDescription(descContent); syndEntry.setDescription(descContent);
} }
final Content cont = item.getContent(); final Content cont = item.getContent();
if (cont != null) { if (cont != null) {
final SyndContent contContent = new SyndContentImpl(); final SyndContent contContent = new SyndContentImpl();
contContent.setType(cont.getType()); contContent.setType(cont.getType());
contContent.setValue(cont.getValue()); contContent.setValue(cont.getValue());
final List<SyndContent> contents = new ArrayList<SyndContent>(); final List<SyndContent> contents = new ArrayList<SyndContent>();
contents.add(contContent); contents.add(contContent);
syndEntry.setContents(contents); syndEntry.setContents(contents);
} }
return syndEntry; return syndEntry;
} }
@Override @Override
protected WireFeed createRealFeed(final String type, final SyndFeed syndFeed) { protected WireFeed createRealFeed(final String type, final SyndFeed syndFeed) {
final Channel channel = (Channel) super.createRealFeed(type, syndFeed); final Channel channel = (Channel) super.createRealFeed(type, syndFeed);
if (syndFeed.getUri() != null) {
channel.setUri(syndFeed.getUri()); final String uri = syndFeed.getUri();
if (uri != null) {
channel.setUri(uri);
} else { } else {
// if URI is not set use the value for link // if URI is not set use the value for link
channel.setUri(syndFeed.getLink()); final String link = syndFeed.getLink();
channel.setUri(link);
} }
return channel; return channel;
@ -100,14 +114,16 @@ public class ConverterForRSS10 extends ConverterForRSS090 {
@Override @Override
protected Item createRSSItem(final SyndEntry sEntry) { protected Item createRSSItem(final SyndEntry sEntry) {
final Item item = super.createRSSItem(sEntry); final Item item = super.createRSSItem(sEntry);
final SyndContent desc = sEntry.getDescription(); final SyndContent desc = sEntry.getDescription();
if (desc != null) { if (desc != null) {
item.setDescription(createItemDescription(desc)); item.setDescription(createItemDescription(desc));
} }
final List<SyndContent> contents = sEntry.getContents(); final List<SyndContent> contents = sEntry.getContents();
if (contents != null && !contents.isEmpty()) { if (Lists.isNotEmpty(contents)) {
item.setContent(createItemContent(contents.get(0))); item.setContent(createItemContent(contents.get(0)));
} }

View file

@ -16,8 +16,6 @@
*/ */
package com.sun.syndication.feed.synd.impl; package com.sun.syndication.feed.synd.impl;
/**
*/
public class ConverterForRSS20 extends ConverterForRSS094 { public class ConverterForRSS20 extends ConverterForRSS094 {
public ConverterForRSS20() { public ConverterForRSS20() {

View file

@ -19,7 +19,6 @@ package com.sun.syndication.io;
/** /**
* Exception thrown by WireFeedInput, WireFeedOutput, WireFeedParser and WireFeedGenerator instances * Exception thrown by WireFeedInput, WireFeedOutput, WireFeedParser and WireFeedGenerator instances
* if they can not parse or generate a feed. * if they can not parse or generate a feed.
* <p>
* *
* @author Alejandro Abdelnur * @author Alejandro Abdelnur
* *
@ -30,7 +29,6 @@ public class FeedException extends Exception {
/** /**
* Creates a FeedException with a message. * Creates a FeedException with a message.
* <p>
* *
* @param msg exception message. * @param msg exception message.
* *
@ -41,7 +39,6 @@ public class FeedException extends Exception {
/** /**
* Creates a FeedException with a message and a root cause exception. * Creates a FeedException with a message and a root cause exception.
* <p>
* *
* @param msg exception message. * @param msg exception message.
* @param rootCause root cause exception. * @param rootCause root cause exception.

View file

@ -20,7 +20,6 @@ import org.jdom2.input.JDOMParseException;
/** /**
* Exception thrown by WireFeedInput instance if it can not parse a feed. * Exception thrown by WireFeedInput instance if it can not parse a feed.
* <p>
* *
* @author Elaine Chien * @author Elaine Chien
* *
@ -31,7 +30,6 @@ public class ParsingFeedException extends FeedException {
/** /**
* Creates a FeedException with a message. * Creates a FeedException with a message.
* <p>
* *
* @param msg exception message. * @param msg exception message.
* *
@ -42,7 +40,6 @@ public class ParsingFeedException extends FeedException {
/** /**
* Creates a FeedException with a message and a root cause exception. * Creates a FeedException with a message and a root cause exception.
* <p>
* *
* @param msg exception message. * @param msg exception message.
* @param rootCause root cause exception. * @param rootCause root cause exception.

View file

@ -35,16 +35,8 @@ import com.sun.syndication.feed.synd.SyndFeed;
* *
*/ */
public class SyndFeedOutput { public class SyndFeedOutput {
private final WireFeedOutput feedOutput;
/** private final WireFeedOutput feedOutput = new WireFeedOutput();
* Creates a SyndFeedOutput instance.
* <p>
*
*/
public SyndFeedOutput() {
feedOutput = new WireFeedOutput();
}
/** /**
* Creates a String with the XML representation for the given SyndFeedImpl. * Creates a String with the XML representation for the given SyndFeedImpl.

View file

@ -58,8 +58,16 @@ import com.sun.syndication.io.impl.XmlFixerReader;
*/ */
public class WireFeedInput { public class WireFeedInput {
private static final InputSource EMPTY_INPUTSOURCE = new InputSource(new ByteArrayInputStream(new byte[0]));
private static final EntityResolver RESOLVER = new EmptyEntityResolver();
private static Map<ClassLoader, FeedParsers> clMap = new WeakHashMap<ClassLoader, FeedParsers>(); private static Map<ClassLoader, FeedParsers> clMap = new WeakHashMap<ClassLoader, FeedParsers>();
private final boolean validate;
private final Locale locale;
private boolean xmlHealerOn;
private static FeedParsers getFeedParsers() { private static FeedParsers getFeedParsers() {
synchronized (WireFeedInput.class) { synchronized (WireFeedInput.class) {
final ClassLoader classLoader = ConfigurableClassLoader.INSTANCE.getClassLoader(); final ClassLoader classLoader = ConfigurableClassLoader.INSTANCE.getClassLoader();
@ -72,9 +80,6 @@ public class WireFeedInput {
} }
} }
private static final InputSource EMPTY_INPUTSOURCE = new InputSource(new ByteArrayInputStream(new byte[0]));
private static final EntityResolver RESOLVER = new EmptyEntityResolver();
private static class EmptyEntityResolver implements EntityResolver { private static class EmptyEntityResolver implements EntityResolver {
@Override @Override
public InputSource resolveEntity(final String publicId, final String systemId) { public InputSource resolveEntity(final String publicId, final String systemId) {
@ -85,11 +90,6 @@ public class WireFeedInput {
} }
} }
private final boolean validate;
private boolean xmlHealerOn;
private final Locale locale;
/** /**
* Returns the list of supported input feed types. * Returns the list of supported input feed types.
* <p> * <p>
@ -357,4 +357,5 @@ public class WireFeedInput {
saxBuilder.setExpandEntities(false); saxBuilder.setExpandEntities(false);
return saxBuilder; return saxBuilder;
} }
} }

View file

@ -17,6 +17,7 @@
package com.sun.syndication.io.impl; package com.sun.syndication.io.impl;
import java.io.StringReader; import java.io.StringReader;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -26,6 +27,7 @@ import org.jdom2.Element;
import org.jdom2.Namespace; import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder; import org.jdom2.input.SAXBuilder;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.atom.Content; import com.sun.syndication.feed.atom.Content;
import com.sun.syndication.feed.atom.Entry; import com.sun.syndication.feed.atom.Entry;
@ -44,6 +46,7 @@ import com.sun.syndication.io.FeedException;
*/ */
public class Atom03Generator extends BaseWireFeedGenerator { public class Atom03Generator extends BaseWireFeedGenerator {
private static final String ATOM_03_URI = "http://purl.org/atom/ns#"; private static final String ATOM_03_URI = "http://purl.org/atom/ns#";
private static final Namespace ATOM_NS = Namespace.getNamespace(ATOM_03_URI); private static final Namespace ATOM_NS = Namespace.getNamespace(ATOM_03_URI);
@ -102,9 +105,9 @@ public class Atom03Generator extends BaseWireFeedGenerator {
} }
protected void addEntries(final Feed feed, final Element parent) throws FeedException { protected void addEntries(final Feed feed, final Element parent) throws FeedException {
final List<Entry> items = feed.getEntries(); final List<Entry> entries = feed.getEntries();
for (int i = 0; i < items.size(); i++) { for (final Entry entry : entries) {
addEntry(items.get(i), parent); addEntry(entry, parent);
} }
checkEntriesConstraints(parent); checkEntriesConstraints(parent);
} }
@ -118,129 +121,151 @@ public class Atom03Generator extends BaseWireFeedGenerator {
} }
protected void populateFeedHeader(final Feed feed, final Element eFeed) throws FeedException { protected void populateFeedHeader(final Feed feed, final Element eFeed) throws FeedException {
if (feed.getTitleEx() != null) {
final Content titleEx = feed.getTitleEx();
if (titleEx != null) {
final Element titleElement = new Element("title", getFeedNamespace()); final Element titleElement = new Element("title", getFeedNamespace());
fillContentElement(titleElement, feed.getTitleEx()); fillContentElement(titleElement, titleEx);
eFeed.addContent(titleElement); eFeed.addContent(titleElement);
} }
List<Link> links = feed.getAlternateLinks(); List<Link> links = feed.getAlternateLinks();
for (int i = 0; i < links.size(); i++) { for (final Link link : links) {
eFeed.addContent(generateLinkElement(links.get(i))); eFeed.addContent(generateLinkElement(link));
} }
links = feed.getOtherLinks(); links = feed.getOtherLinks();
for (int i = 0; i < links.size(); i++) { for (final Link link : links) {
eFeed.addContent(generateLinkElement(links.get(i))); eFeed.addContent(generateLinkElement(link));
} }
if (feed.getAuthors() != null && !feed.getAuthors().isEmpty()) {
final List<SyndPerson> authors = feed.getAuthors();
if (Lists.isNotEmpty(authors)) {
final Element authorElement = new Element("author", getFeedNamespace()); final Element authorElement = new Element("author", getFeedNamespace());
fillPersonElement(authorElement, feed.getAuthors().get(0)); fillPersonElement(authorElement, authors.get(0));
eFeed.addContent(authorElement); eFeed.addContent(authorElement);
} }
final List<SyndPerson> contributors = feed.getContributors(); final List<SyndPerson> contributors = feed.getContributors();
for (int i = 0; i < contributors.size(); i++) { for (final SyndPerson contributor : contributors) {
final Element contributorElement = new Element("contributor", getFeedNamespace()); final Element contributorElement = new Element("contributor", getFeedNamespace());
fillPersonElement(contributorElement, contributors.get(i)); fillPersonElement(contributorElement, contributor);
eFeed.addContent(contributorElement); eFeed.addContent(contributorElement);
} }
if (feed.getTagline() != null) { final Content tagline = feed.getTagline();
if (tagline != null) {
final Element taglineElement = new Element("tagline", getFeedNamespace()); final Element taglineElement = new Element("tagline", getFeedNamespace());
fillContentElement(taglineElement, feed.getTagline()); fillContentElement(taglineElement, tagline);
eFeed.addContent(taglineElement); eFeed.addContent(taglineElement);
} }
if (feed.getId() != null) { final String id = feed.getId();
eFeed.addContent(generateSimpleElement("id", feed.getId())); if (id != null) {
eFeed.addContent(generateSimpleElement("id", id));
} }
if (feed.getGenerator() != null) { final Generator generator = feed.getGenerator();
eFeed.addContent(generateGeneratorElement(feed.getGenerator())); if (generator != null) {
eFeed.addContent(generateGeneratorElement(generator));
} }
if (feed.getCopyright() != null) { final String copyright = feed.getCopyright();
eFeed.addContent(generateSimpleElement("copyright", feed.getCopyright())); if (copyright != null) {
eFeed.addContent(generateSimpleElement("copyright", copyright));
} }
if (feed.getInfo() != null) { final Content info = feed.getInfo();
if (info != null) {
final Element infoElement = new Element("info", getFeedNamespace()); final Element infoElement = new Element("info", getFeedNamespace());
fillContentElement(infoElement, feed.getInfo()); fillContentElement(infoElement, info);
eFeed.addContent(infoElement); eFeed.addContent(infoElement);
} }
if (feed.getModified() != null) { final Date modified = feed.getModified();
if (modified != null) {
final Element modifiedElement = new Element("modified", getFeedNamespace()); final Element modifiedElement = new Element("modified", getFeedNamespace());
modifiedElement.addContent(DateParser.formatW3CDateTime(feed.getModified(), Locale.US)); modifiedElement.addContent(DateParser.formatW3CDateTime(modified, Locale.US));
eFeed.addContent(modifiedElement); eFeed.addContent(modifiedElement);
} }
} }
protected void populateEntry(final Entry entry, final Element eEntry) throws FeedException { protected void populateEntry(final Entry entry, final Element eEntry) throws FeedException {
if (entry.getTitleEx() != null) {
final Content titleEx = entry.getTitleEx();
if (titleEx != null) {
final Element titleElement = new Element("title", getFeedNamespace()); final Element titleElement = new Element("title", getFeedNamespace());
fillContentElement(titleElement, entry.getTitleEx()); fillContentElement(titleElement, titleEx);
eEntry.addContent(titleElement); eEntry.addContent(titleElement);
} }
List<Link> links = entry.getAlternateLinks();
for (int i = 0; i < links.size(); i++) { final List<Link> alternateLinks = entry.getAlternateLinks();
eEntry.addContent(generateLinkElement(links.get(i))); for (final Link link : alternateLinks) {
eEntry.addContent(generateLinkElement(link));
} }
links = entry.getOtherLinks(); final List<Link> otherLinks = entry.getOtherLinks();
for (int i = 0; i < links.size(); i++) { for (final Link link : otherLinks) {
eEntry.addContent(generateLinkElement(links.get(i))); eEntry.addContent(generateLinkElement(link));
} }
if (entry.getAuthors() != null && !entry.getAuthors().isEmpty()) { final List<SyndPerson> authors = entry.getAuthors();
if (Lists.isNotEmpty(authors)) {
final Element authorElement = new Element("author", getFeedNamespace()); final Element authorElement = new Element("author", getFeedNamespace());
fillPersonElement(authorElement, entry.getAuthors().get(0)); fillPersonElement(authorElement, authors.get(0));
eEntry.addContent(authorElement); eEntry.addContent(authorElement);
} }
final List<SyndPerson> contributors = entry.getContributors(); final List<SyndPerson> contributors = entry.getContributors();
for (int i = 0; i < contributors.size(); i++) { for (final SyndPerson contributor : contributors) {
final Element contributorElement = new Element("contributor", getFeedNamespace()); final Element contributorElement = new Element("contributor", getFeedNamespace());
fillPersonElement(contributorElement, contributors.get(i)); fillPersonElement(contributorElement, contributor);
eEntry.addContent(contributorElement); eEntry.addContent(contributorElement);
} }
if (entry.getId() != null) {
eEntry.addContent(generateSimpleElement("id", entry.getId())); final String id = entry.getId();
if (id != null) {
eEntry.addContent(generateSimpleElement("id", id));
} }
if (entry.getModified() != null) { final Date modified = entry.getModified();
if (modified != null) {
final Element modifiedElement = new Element("modified", getFeedNamespace()); final Element modifiedElement = new Element("modified", getFeedNamespace());
modifiedElement.addContent(DateParser.formatW3CDateTime(entry.getModified(), Locale.US)); modifiedElement.addContent(DateParser.formatW3CDateTime(modified, Locale.US));
eEntry.addContent(modifiedElement); eEntry.addContent(modifiedElement);
} }
if (entry.getIssued() != null) { final Date issued = entry.getIssued();
if (issued != null) {
final Element issuedElement = new Element("issued", getFeedNamespace()); final Element issuedElement = new Element("issued", getFeedNamespace());
issuedElement.addContent(DateParser.formatW3CDateTime(entry.getIssued(), Locale.US)); issuedElement.addContent(DateParser.formatW3CDateTime(issued, Locale.US));
eEntry.addContent(issuedElement); eEntry.addContent(issuedElement);
} }
if (entry.getCreated() != null) { final Date created = entry.getCreated();
if (created != null) {
final Element createdElement = new Element("created", getFeedNamespace()); final Element createdElement = new Element("created", getFeedNamespace());
createdElement.addContent(DateParser.formatW3CDateTime(entry.getCreated(), Locale.US)); createdElement.addContent(DateParser.formatW3CDateTime(created, Locale.US));
eEntry.addContent(createdElement); eEntry.addContent(createdElement);
} }
if (entry.getSummary() != null) { final Content summary = entry.getSummary();
if (summary != null) {
final Element summaryElement = new Element("summary", getFeedNamespace()); final Element summaryElement = new Element("summary", getFeedNamespace());
fillContentElement(summaryElement, entry.getSummary()); fillContentElement(summaryElement, summary);
eEntry.addContent(summaryElement); eEntry.addContent(summaryElement);
} }
final List<Content> contents = entry.getContents(); final List<Content> contents = entry.getContents();
for (int i = 0; i < contents.size(); i++) { for (final Content content : contents) {
final Element contentElement = new Element("content", getFeedNamespace()); final Element contentElement = new Element("content", getFeedNamespace());
fillContentElement(contentElement, contents.get(i)); fillContentElement(contentElement, content);
eEntry.addContent(contentElement); eEntry.addContent(contentElement);
} }
generateForeignMarkup(eEntry, entry.getForeignMarkup()); generateForeignMarkup(eEntry, entry.getForeignMarkup());
} }
protected void checkFeedHeaderConstraints(final Element eFeed) throws FeedException { protected void checkFeedHeaderConstraints(final Element eFeed) throws FeedException {
@ -253,75 +278,98 @@ public class Atom03Generator extends BaseWireFeedGenerator {
} }
protected Element generateLinkElement(final Link link) { protected Element generateLinkElement(final Link link) {
final Element linkElement = new Element("link", getFeedNamespace()); final Element linkElement = new Element("link", getFeedNamespace());
if (link.getRel() != null) { final String rel = link.getRel();
final Attribute relAttribute = new Attribute("rel", link.getRel().toString()); if (rel != null) {
final Attribute relAttribute = new Attribute("rel", rel.toString());
linkElement.setAttribute(relAttribute); linkElement.setAttribute(relAttribute);
} }
if (link.getType() != null) { final String type = link.getType();
final Attribute typeAttribute = new Attribute("type", link.getType()); if (type != null) {
final Attribute typeAttribute = new Attribute("type", type);
linkElement.setAttribute(typeAttribute); linkElement.setAttribute(typeAttribute);
} }
if (link.getHref() != null) { final String href = link.getHref();
final Attribute hrefAttribute = new Attribute("href", link.getHref()); if (href != null) {
final Attribute hrefAttribute = new Attribute("href", href);
linkElement.setAttribute(hrefAttribute); linkElement.setAttribute(hrefAttribute);
} }
return linkElement; return linkElement;
} }
protected void fillPersonElement(final Element element, final SyndPerson person) { protected void fillPersonElement(final Element element, final SyndPerson person) {
if (person.getName() != null) {
element.addContent(generateSimpleElement("name", person.getName())); final String name = person.getName();
} if (name != null) {
if (person.getUri() != null) { element.addContent(generateSimpleElement("name", name));
element.addContent(generateSimpleElement("url", person.getUri()));
} }
if (person.getEmail() != null) { final String uri = person.getUri();
element.addContent(generateSimpleElement("email", person.getEmail())); if (uri != null) {
element.addContent(generateSimpleElement("url", uri));
} }
final String email = person.getEmail();
if (email != null) {
element.addContent(generateSimpleElement("email", email));
}
} }
protected Element generateTagLineElement(final Content tagline) { protected Element generateTagLineElement(final Content tagline) {
final Element taglineElement = new Element("tagline", getFeedNamespace()); final Element taglineElement = new Element("tagline", getFeedNamespace());
if (tagline.getType() != null) { final String type = tagline.getType();
final Attribute typeAttribute = new Attribute("type", tagline.getType()); if (type != null) {
final Attribute typeAttribute = new Attribute("type", type);
taglineElement.setAttribute(typeAttribute); taglineElement.setAttribute(typeAttribute);
} }
if (tagline.getValue() != null) { final String value = tagline.getValue();
taglineElement.addContent(tagline.getValue()); if (value != null) {
taglineElement.addContent(value);
} }
return taglineElement; return taglineElement;
} }
protected void fillContentElement(final Element contentElement, final Content content) throws FeedException { protected void fillContentElement(final Element contentElement, final Content content) throws FeedException {
if (content.getType() != null) { final String type = content.getType();
final Attribute typeAttribute = new Attribute("type", content.getType()); if (type != null) {
final Attribute typeAttribute = new Attribute("type", type);
contentElement.setAttribute(typeAttribute); contentElement.setAttribute(typeAttribute);
} }
final String mode = content.getMode(); final String mode = content.getMode();
if (mode != null) { if (mode != null) {
final Attribute modeAttribute = new Attribute("mode", content.getMode().toString()); final Attribute modeAttribute = new Attribute("mode", mode.toString());
contentElement.setAttribute(modeAttribute); contentElement.setAttribute(modeAttribute);
} }
if (content.getValue() != null) { final String value = content.getValue();
if (value != null) {
if (mode == null || mode.equals(Content.ESCAPED)) { if (mode == null || mode.equals(Content.ESCAPED)) {
contentElement.addContent(content.getValue());
contentElement.addContent(value);
} else if (mode.equals(Content.BASE64)) { } else if (mode.equals(Content.BASE64)) {
contentElement.addContent(Base64.encode(content.getValue()));
contentElement.addContent(Base64.encode(value));
} else if (mode.equals(Content.XML)) { } else if (mode.equals(Content.XML)) {
final StringBuffer tmpDocString = new StringBuffer("<tmpdoc>"); final StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
tmpDocString.append(content.getValue()); tmpDocString.append(value);
tmpDocString.append("</tmpdoc>"); tmpDocString.append("</tmpdoc>");
final StringReader tmpDocReader = new StringReader(tmpDocString.toString()); final StringReader tmpDocReader = new StringReader(tmpDocString.toString());
Document tmpDoc; Document tmpDoc;
@ -336,24 +384,29 @@ public class Atom03Generator extends BaseWireFeedGenerator {
final List<org.jdom2.Content> children = tmpDoc.getRootElement().removeContent(); final List<org.jdom2.Content> children = tmpDoc.getRootElement().removeContent();
contentElement.addContent(children); contentElement.addContent(children);
} }
} }
} }
protected Element generateGeneratorElement(final Generator generator) { protected Element generateGeneratorElement(final Generator generator) {
final Element generatorElement = new Element("generator", getFeedNamespace()); final Element generatorElement = new Element("generator", getFeedNamespace());
if (generator.getUrl() != null) { final String url = generator.getUrl();
final Attribute urlAttribute = new Attribute("url", generator.getUrl()); if (url != null) {
final Attribute urlAttribute = new Attribute("url", url);
generatorElement.setAttribute(urlAttribute); generatorElement.setAttribute(urlAttribute);
} }
if (generator.getVersion() != null) { final String version = generator.getVersion();
final Attribute versionAttribute = new Attribute("version", generator.getVersion()); if (version != null) {
final Attribute versionAttribute = new Attribute("version", version);
generatorElement.setAttribute(versionAttribute); generatorElement.setAttribute(versionAttribute);
} }
if (generator.getValue() != null) { final String value = generator.getValue();
generatorElement.addContent(generator.getValue()); if (value != null) {
generatorElement.addContent(value);
} }
return generatorElement; return generatorElement;

View file

@ -25,6 +25,7 @@ import org.jdom2.Element;
import org.jdom2.Namespace; import org.jdom2.Namespace;
import org.jdom2.output.XMLOutputter; import org.jdom2.output.XMLOutputter;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.atom.Content; import com.sun.syndication.feed.atom.Content;
import com.sun.syndication.feed.atom.Entry; import com.sun.syndication.feed.atom.Entry;
@ -35,9 +36,8 @@ import com.sun.syndication.feed.atom.Person;
import com.sun.syndication.feed.synd.SyndPerson; import com.sun.syndication.feed.synd.SyndPerson;
import com.sun.syndication.io.FeedException; import com.sun.syndication.io.FeedException;
/**
*/
public class Atom03Parser extends BaseWireFeedParser { public class Atom03Parser extends BaseWireFeedParser {
private static final String ATOM_03_URI = "http://purl.org/atom/ns#"; private static final String ATOM_03_URI = "http://purl.org/atom/ns#";
private static final Namespace ATOM_03_NS = Namespace.getNamespace(ATOM_03_URI); private static final Namespace ATOM_03_NS = Namespace.getNamespace(ATOM_03_URI);
@ -62,125 +62,137 @@ public class Atom03Parser extends BaseWireFeedParser {
@Override @Override
public WireFeed parse(final Document document, final boolean validate, final Locale locale) throws IllegalArgumentException, FeedException { public WireFeed parse(final Document document, final boolean validate, final Locale locale) throws IllegalArgumentException, FeedException {
if (validate) { if (validate) {
validateFeed(document); validateFeed(document);
} }
final Element rssRoot = document.getRootElement(); final Element rssRoot = document.getRootElement();
return parseFeed(rssRoot, locale); return parseFeed(rssRoot, locale);
} }
protected void validateFeed(final Document document) throws FeedException { protected void validateFeed(final Document document) throws FeedException {
// TBD // TODO here we have to validate the Feed against a schema or whatever not sure how to do it
// here we have to validate the Feed against a schema or whatever // one posibility would be to produce an ouput and attempt to parse it again with validation
// not sure how to do it // turned on. otherwise will have to check the document elements by hand.
// one posibility would be to produce an ouput and attempt to parse it
// again
// with validation turned on.
// otherwise will have to check the document elements by hand.
} }
protected WireFeed parseFeed(final Element eFeed, final Locale locale) { protected WireFeed parseFeed(final Element eFeed, final Locale locale) {
final Feed feed = new Feed(getType()); final String type = getType();
feed.setStyleSheet(getStyleSheet(eFeed.getDocument())); final Document document = eFeed.getDocument();
final String styleSheet = getStyleSheet(document);
Element e = eFeed.getChild("title", getAtomNamespace()); final Feed feed = new Feed(type);
if (e != null) { feed.setStyleSheet(styleSheet);
feed.setTitleEx(parseContent(e));
final Element title = eFeed.getChild("title", getAtomNamespace());
if (title != null) {
feed.setTitleEx(parseContent(title));
} }
List<Element> eList = eFeed.getChildren("link", getAtomNamespace()); final List<Element> links = eFeed.getChildren("link", getAtomNamespace());
feed.setAlternateLinks(parseAlternateLinks(eList)); feed.setAlternateLinks(parseAlternateLinks(links));
feed.setOtherLinks(parseOtherLinks(eList)); feed.setOtherLinks(parseOtherLinks(links));
e = eFeed.getChild("author", getAtomNamespace()); final Element author = eFeed.getChild("author", getAtomNamespace());
if (e != null) { if (author != null) {
final List<SyndPerson> authors = new ArrayList<SyndPerson>(); final List<SyndPerson> authors = new ArrayList<SyndPerson>();
authors.add(parsePerson(e)); authors.add(parsePerson(author));
feed.setAuthors(authors); feed.setAuthors(authors);
} }
eList = eFeed.getChildren("contributor", getAtomNamespace()); final List<Element> contributors = eFeed.getChildren("contributor", getAtomNamespace());
if (!eList.isEmpty()) { if (!contributors.isEmpty()) {
feed.setContributors(parsePersons(eList)); feed.setContributors(parsePersons(contributors));
} }
e = eFeed.getChild("tagline", getAtomNamespace()); final Element tagline = eFeed.getChild("tagline", getAtomNamespace());
if (e != null) { if (tagline != null) {
feed.setTagline(parseContent(e)); feed.setTagline(parseContent(tagline));
} }
e = eFeed.getChild("id", getAtomNamespace()); final Element id = eFeed.getChild("id", getAtomNamespace());
if (e != null) { if (id != null) {
feed.setId(e.getText()); feed.setId(id.getText());
} }
e = eFeed.getChild("generator", getAtomNamespace()); final Element generator = eFeed.getChild("generator", getAtomNamespace());
if (e != null) { if (generator != null) {
final Generator gen = new Generator(); final Generator gen = new Generator();
gen.setValue(e.getText()); gen.setValue(generator.getText());
String att = getAttributeValue(e, "url"); String att = getAttributeValue(generator, "url");
if (att != null) { if (att != null) {
gen.setUrl(att); gen.setUrl(att);
} }
att = getAttributeValue(e, "version"); att = getAttributeValue(generator, "version");
if (att != null) { if (att != null) {
gen.setVersion(att); gen.setVersion(att);
} }
feed.setGenerator(gen); feed.setGenerator(gen);
} }
e = eFeed.getChild("copyright", getAtomNamespace()); final Element copyright = eFeed.getChild("copyright", getAtomNamespace());
if (e != null) { if (copyright != null) {
feed.setCopyright(e.getText()); feed.setCopyright(copyright.getText());
} }
e = eFeed.getChild("info", getAtomNamespace()); final Element info = eFeed.getChild("info", getAtomNamespace());
if (e != null) { if (info != null) {
feed.setInfo(parseContent(e)); feed.setInfo(parseContent(info));
} }
e = eFeed.getChild("modified", getAtomNamespace()); final Element modified = eFeed.getChild("modified", getAtomNamespace());
if (e != null) { if (modified != null) {
feed.setModified(DateParser.parseDate(e.getText(), locale)); feed.setModified(DateParser.parseDate(modified.getText(), locale));
} }
feed.setModules(parseFeedModules(eFeed, locale)); feed.setModules(parseFeedModules(eFeed, locale));
eList = eFeed.getChildren("entry", getAtomNamespace()); final List<Element> entries = eFeed.getChildren("entry", getAtomNamespace());
if (!eList.isEmpty()) { if (!entries.isEmpty()) {
feed.setEntries(parseEntries(eList, locale)); feed.setEntries(parseEntries(entries, locale));
} }
final List<Element> foreignMarkup = extractForeignMarkup(eFeed, feed, getAtomNamespace()); final List<Element> foreignMarkup = extractForeignMarkup(eFeed, feed, getAtomNamespace());
if (!foreignMarkup.isEmpty()) { if (!foreignMarkup.isEmpty()) {
feed.setForeignMarkup(foreignMarkup); feed.setForeignMarkup(foreignMarkup);
} }
return feed; return feed;
} }
private Link parseLink(final Element eLink) { private Link parseLink(final Element eLink) {
final Link link = new Link(); final Link link = new Link();
String att = getAttributeValue(eLink, "rel");
if (att != null) { final String rel = getAttributeValue(eLink, "rel");
link.setRel(att); if (rel != null) {
link.setRel(rel);
} }
att = getAttributeValue(eLink, "type");
if (att != null) { final String type = getAttributeValue(eLink, "type");
link.setType(att); if (type != null) {
link.setType(type);
} }
att = getAttributeValue(eLink, "href");
if (att != null) { final String href = getAttributeValue(eLink, "href");
link.setHref(att); if (href != null) {
link.setHref(href);
} }
return link; return link;
} }
// List(Elements) -> List(Link)
private List<Link> parseLinks(final List<Element> eLinks, final boolean alternate) { private List<Link> parseLinks(final List<Element> eLinks, final boolean alternate) {
final List<Link> links = new ArrayList<Link>(); final List<Link> links = new ArrayList<Link>();
for (int i = 0; i < eLinks.size(); i++) {
final Element eLink = eLinks.get(i); for (final Element eLink : eLinks) {
final String rel = getAttributeValue(eLink, "rel"); final String rel = getAttributeValue(eLink, "rel");
if (alternate) { if (alternate) {
if ("alternate".equals(rel)) { if ("alternate".equals(rel)) {
@ -192,11 +204,9 @@ public class Atom03Parser extends BaseWireFeedParser {
} }
} }
} }
if (!links.isEmpty()) {
return links; return Lists.emptyToNull(links);
} else {
return null;
}
} }
// List(Elements) -> List(Link) // List(Elements) -> List(Link)
@ -210,51 +220,67 @@ public class Atom03Parser extends BaseWireFeedParser {
} }
private Person parsePerson(final Element ePerson) { private Person parsePerson(final Element ePerson) {
final Person person = new Person(); final Person person = new Person();
Element e = ePerson.getChild("name", getAtomNamespace());
if (e != null) { final Element name = ePerson.getChild("name", getAtomNamespace());
person.setName(e.getText());
if (name != null) {
person.setName(name.getText());
} }
e = ePerson.getChild("url", getAtomNamespace());
if (e != null) { final Element url = ePerson.getChild("url", getAtomNamespace());
person.setUrl(e.getText()); if (url != null) {
person.setUrl(url.getText());
} }
e = ePerson.getChild("email", getAtomNamespace());
if (e != null) { final Element email = ePerson.getChild("email", getAtomNamespace());
person.setEmail(e.getText()); if (email != null) {
person.setEmail(email.getText());
} }
return person; return person;
} }
// List(Elements) -> List(Persons) // List(Elements) -> List(Persons)
private List<SyndPerson> parsePersons(final List<Element> ePersons) { private List<SyndPerson> parsePersons(final List<Element> ePersons) {
final List<SyndPerson> persons = new ArrayList<SyndPerson>(); final List<SyndPerson> persons = new ArrayList<SyndPerson>();
for (int i = 0; i < ePersons.size(); i++) {
persons.add(parsePerson(ePersons.get(i))); for (final Element person : ePersons) {
} persons.add(parsePerson(person));
if (!persons.isEmpty()) {
return persons;
} else {
return null;
} }
return Lists.emptyToNull(persons);
} }
private Content parseContent(final Element e) { private Content parseContent(final Element e) {
String value = null; String value = null;
String type = getAttributeValue(e, "type"); String type = getAttributeValue(e, "type");
if (type == null) { if (type == null) {
type = "text/plain"; type = "text/plain";
} }
String mode = getAttributeValue(e, "mode"); String mode = getAttributeValue(e, "mode");
if (mode == null) { if (mode == null) {
mode = Content.XML; // default to xml content mode = Content.XML; // default to xml content
} }
if (mode.equals(Content.ESCAPED)) { if (mode.equals(Content.ESCAPED)) {
// do nothing XML Parser took care of this // do nothing XML Parser took care of this
value = e.getText(); value = e.getText();
} else if (mode.equals(Content.BASE64)) { } else if (mode.equals(Content.BASE64)) {
value = Base64.decode(e.getText()); value = Base64.decode(e.getText());
} else if (mode.equals(Content.XML)) { } else if (mode.equals(Content.XML)) {
final XMLOutputter outputter = new XMLOutputter(); final XMLOutputter outputter = new XMLOutputter();
final List<org.jdom2.Content> contents = e.getContent(); final List<org.jdom2.Content> contents = e.getContent();
for (final org.jdom2.Content content : contents) { for (final org.jdom2.Content content : contents) {
@ -267,6 +293,7 @@ public class Atom03Parser extends BaseWireFeedParser {
} }
value = outputter.outputString(contents); value = outputter.outputString(contents);
} }
final Content content = new Content(); final Content content = new Content();
@ -278,71 +305,71 @@ public class Atom03Parser extends BaseWireFeedParser {
// List(Elements) -> List(Entries) // List(Elements) -> List(Entries)
private List<Entry> parseEntries(final List<Element> eEntries, final Locale locale) { private List<Entry> parseEntries(final List<Element> eEntries, final Locale locale) {
final List<Entry> entries = new ArrayList<Entry>(); final List<Entry> entries = new ArrayList<Entry>();
for (int i = 0; i < eEntries.size(); i++) { for (final Element entry : eEntries) {
entries.add(parseEntry(eEntries.get(i), locale)); entries.add(parseEntry(entry, locale));
}
if (!entries.isEmpty()) {
return entries;
} else {
return null;
} }
return Lists.emptyToNull(entries);
} }
private Entry parseEntry(final Element eEntry, final Locale locale) { private Entry parseEntry(final Element eEntry, final Locale locale) {
final Entry entry = new Entry(); final Entry entry = new Entry();
Element e = eEntry.getChild("title", getAtomNamespace()); final Element title = eEntry.getChild("title", getAtomNamespace());
if (e != null) { if (title != null) {
entry.setTitleEx(parseContent(e)); entry.setTitleEx(parseContent(title));
} }
List<Element> eList = eEntry.getChildren("link", getAtomNamespace()); final List<Element> links = eEntry.getChildren("link", getAtomNamespace());
entry.setAlternateLinks(parseAlternateLinks(eList)); entry.setAlternateLinks(parseAlternateLinks(links));
entry.setOtherLinks(parseOtherLinks(eList)); entry.setOtherLinks(parseOtherLinks(links));
e = eEntry.getChild("author", getAtomNamespace()); final Element author = eEntry.getChild("author", getAtomNamespace());
if (e != null) { if (author != null) {
final List<SyndPerson> authors = new ArrayList<SyndPerson>(); final List<SyndPerson> authors = new ArrayList<SyndPerson>();
authors.add(parsePerson(e)); authors.add(parsePerson(author));
entry.setAuthors(authors); entry.setAuthors(authors);
} }
eList = eEntry.getChildren("contributor", getAtomNamespace()); final List<Element> contributors = eEntry.getChildren("contributor", getAtomNamespace());
if (!eList.isEmpty()) { if (!contributors.isEmpty()) {
entry.setContributors(parsePersons(eList)); entry.setContributors(parsePersons(contributors));
} }
e = eEntry.getChild("id", getAtomNamespace()); final Element id = eEntry.getChild("id", getAtomNamespace());
if (e != null) { if (id != null) {
entry.setId(e.getText()); entry.setId(id.getText());
} }
e = eEntry.getChild("modified", getAtomNamespace()); final Element modified = eEntry.getChild("modified", getAtomNamespace());
if (e != null) { if (modified != null) {
entry.setModified(DateParser.parseDate(e.getText(), locale)); entry.setModified(DateParser.parseDate(modified.getText(), locale));
} }
e = eEntry.getChild("issued", getAtomNamespace()); final Element issued = eEntry.getChild("issued", getAtomNamespace());
if (e != null) { if (issued != null) {
entry.setIssued(DateParser.parseDate(e.getText(), locale)); entry.setIssued(DateParser.parseDate(issued.getText(), locale));
} }
e = eEntry.getChild("created", getAtomNamespace()); final Element created = eEntry.getChild("created", getAtomNamespace());
if (e != null) { if (created != null) {
entry.setCreated(DateParser.parseDate(e.getText(), locale)); entry.setCreated(DateParser.parseDate(created.getText(), locale));
} }
e = eEntry.getChild("summary", getAtomNamespace()); final Element summary = eEntry.getChild("summary", getAtomNamespace());
if (e != null) { if (summary != null) {
entry.setSummary(parseContent(e)); entry.setSummary(parseContent(summary));
} }
eList = eEntry.getChildren("content", getAtomNamespace()); final List<Element> contents = eEntry.getChildren("content", getAtomNamespace());
if (!eList.isEmpty()) { if (!contents.isEmpty()) {
final List<Content> content = new ArrayList<Content>(); final List<Content> content = new ArrayList<Content>();
for (int i = 0; i < eList.size(); i++) { for (final Element eContent : contents) {
content.add(parseContent(eList.get(i))); content.add(parseContent(eContent));
} }
entry.setContents(content); entry.setContents(content);
} }
@ -353,7 +380,9 @@ public class Atom03Parser extends BaseWireFeedParser {
if (!foreignMarkup.isEmpty()) { if (!foreignMarkup.isEmpty()) {
entry.setForeignMarkup(foreignMarkup); entry.setForeignMarkup(foreignMarkup);
} }
return entry; return entry;
} }
} }

View file

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.io.Writer; import java.io.Writer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -30,6 +31,7 @@ import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder; import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter; import org.jdom2.output.XMLOutputter;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.atom.Category; import com.sun.syndication.feed.atom.Category;
import com.sun.syndication.feed.atom.Content; import com.sun.syndication.feed.atom.Content;
@ -49,8 +51,8 @@ import com.sun.syndication.io.WireFeedOutput;
* @author Dave Johnson (updated for Atom 1.0) * @author Dave Johnson (updated for Atom 1.0)
* *
*/ */
public class Atom10Generator extends BaseWireFeedGenerator { public class Atom10Generator extends BaseWireFeedGenerator {
private static final String ATOM_10_URI = "http://www.w3.org/2005/Atom"; private static final String ATOM_10_URI = "http://www.w3.org/2005/Atom";
private static final Namespace ATOM_NS = Namespace.getNamespace(ATOM_10_URI); private static final Namespace ATOM_NS = Namespace.getNamespace(ATOM_10_URI);
@ -87,15 +89,23 @@ public class Atom10Generator extends BaseWireFeedGenerator {
} }
protected Element createRootElement(final Feed feed) { protected Element createRootElement(final Feed feed) {
final Element root = new Element("feed", getFeedNamespace()); final Element root = new Element("feed", getFeedNamespace());
root.addNamespaceDeclaration(getFeedNamespace()); root.addNamespaceDeclaration(getFeedNamespace());
// Attribute version = new Attribute("version", getVersion()); // Attribute version = new Attribute("version", getVersion());
// root.setAttribute(version); // root.setAttribute(version);
if (feed.getXmlBase() != null) {
root.setAttribute("base", feed.getXmlBase(), Namespace.XML_NAMESPACE); final String xmlBase = feed.getXmlBase();
if (xmlBase != null) {
root.setAttribute("base", xmlBase, Namespace.XML_NAMESPACE);
} }
generateModuleNamespaceDefs(root); generateModuleNamespaceDefs(root);
return root; return root;
} }
protected void populateFeed(final Feed feed, final Element parent) throws FeedException { protected void populateFeed(final Feed feed, final Element parent) throws FeedException {
@ -113,41 +123,49 @@ public class Atom10Generator extends BaseWireFeedGenerator {
protected void addEntries(final Feed feed, final Element parent) throws FeedException { protected void addEntries(final Feed feed, final Element parent) throws FeedException {
final List<Entry> items = feed.getEntries(); final List<Entry> items = feed.getEntries();
for (int i = 0; i < items.size(); i++) { for (final Entry entry : items) {
addEntry(items.get(i), parent); addEntry(entry, parent);
} }
checkEntriesConstraints(parent); checkEntriesConstraints(parent);
} }
protected void addEntry(final Entry entry, final Element parent) throws FeedException { protected void addEntry(final Entry entry, final Element parent) throws FeedException {
final Element eEntry = new Element("entry", getFeedNamespace()); final Element eEntry = new Element("entry", getFeedNamespace());
if (entry.getXmlBase() != null) {
eEntry.setAttribute("base", entry.getXmlBase(), Namespace.XML_NAMESPACE); final String xmlBase = entry.getXmlBase();
if (xmlBase != null) {
eEntry.setAttribute("base", xmlBase, Namespace.XML_NAMESPACE);
} }
populateEntry(entry, eEntry); populateEntry(entry, eEntry);
generateForeignMarkup(eEntry, entry.getForeignMarkup()); generateForeignMarkup(eEntry, entry.getForeignMarkup());
checkEntryConstraints(eEntry); checkEntryConstraints(eEntry);
generateItemModules(entry.getModules(), eEntry); generateItemModules(entry.getModules(), eEntry);
parent.addContent(eEntry); parent.addContent(eEntry);
} }
protected void populateFeedHeader(final Feed feed, final Element eFeed) throws FeedException { protected void populateFeedHeader(final Feed feed, final Element eFeed) throws FeedException {
if (feed.getTitleEx() != null) {
final Content titleEx = feed.getTitleEx();
if (titleEx != null) {
final Element titleElement = new Element("title", getFeedNamespace()); final Element titleElement = new Element("title", getFeedNamespace());
fillContentElement(titleElement, feed.getTitleEx()); fillContentElement(titleElement, titleEx);
eFeed.addContent(titleElement); eFeed.addContent(titleElement);
} }
List<Link> links = feed.getAlternateLinks(); final List<Link> alternateLinks = feed.getAlternateLinks();
if (links != null) { if (alternateLinks != null) {
for (int i = 0; i < links.size(); i++) { for (final Link link : alternateLinks) {
eFeed.addContent(generateLinkElement(links.get(i))); eFeed.addContent(generateLinkElement(link));
} }
} }
links = feed.getOtherLinks();
if (links != null) { final List<Link> otherLinks = feed.getOtherLinks();
for (int j = 0; j < links.size(); j++) { if (otherLinks != null) {
eFeed.addContent(generateLinkElement(links.get(j))); for (final Link link : otherLinks) {
eFeed.addContent(generateLinkElement(link));
} }
} }
@ -159,133 +177,153 @@ public class Atom10Generator extends BaseWireFeedGenerator {
} }
final List<SyndPerson> authors = feed.getAuthors(); final List<SyndPerson> authors = feed.getAuthors();
if (authors != null && !authors.isEmpty()) { if (Lists.isNotEmpty(authors)) {
for (int i = 0; i < authors.size(); i++) { for (final SyndPerson author : authors) {
final Element authorElement = new Element("author", getFeedNamespace()); final Element authorElement = new Element("author", getFeedNamespace());
fillPersonElement(authorElement, feed.getAuthors().get(i)); fillPersonElement(authorElement, author);
eFeed.addContent(authorElement); eFeed.addContent(authorElement);
} }
} }
final List<SyndPerson> contributors = feed.getContributors(); final List<SyndPerson> contributors = feed.getContributors();
if (contributors != null && !contributors.isEmpty()) { if (Lists.isNotEmpty(contributors)) {
for (int i = 0; i < contributors.size(); i++) { for (final SyndPerson contributor : contributors) {
final Element contributorElement = new Element("contributor", getFeedNamespace()); final Element contributorElement = new Element("contributor", getFeedNamespace());
fillPersonElement(contributorElement, contributors.get(i)); fillPersonElement(contributorElement, contributor);
eFeed.addContent(contributorElement); eFeed.addContent(contributorElement);
} }
} }
if (feed.getSubtitle() != null) { final Content subtitle = feed.getSubtitle();
if (subtitle != null) {
final Element subtitleElement = new Element("subtitle", getFeedNamespace()); final Element subtitleElement = new Element("subtitle", getFeedNamespace());
fillContentElement(subtitleElement, feed.getSubtitle()); fillContentElement(subtitleElement, subtitle);
eFeed.addContent(subtitleElement); eFeed.addContent(subtitleElement);
} }
if (feed.getId() != null) { final String id = feed.getId();
eFeed.addContent(generateSimpleElement("id", feed.getId())); if (id != null) {
eFeed.addContent(generateSimpleElement("id", id));
} }
if (feed.getGenerator() != null) { final Generator generator = feed.getGenerator();
eFeed.addContent(generateGeneratorElement(feed.getGenerator())); if (generator != null) {
eFeed.addContent(generateGeneratorElement(generator));
} }
if (feed.getRights() != null) { final String rights = feed.getRights();
eFeed.addContent(generateSimpleElement("rights", feed.getRights())); if (rights != null) {
eFeed.addContent(generateSimpleElement("rights", rights));
} }
if (feed.getIcon() != null) { final String icon = feed.getIcon();
eFeed.addContent(generateSimpleElement("icon", feed.getIcon())); if (icon != null) {
eFeed.addContent(generateSimpleElement("icon", icon));
} }
if (feed.getLogo() != null) { final String logo = feed.getLogo();
eFeed.addContent(generateSimpleElement("logo", feed.getLogo())); if (logo != null) {
eFeed.addContent(generateSimpleElement("logo", logo));
} }
if (feed.getUpdated() != null) { final Date updated = feed.getUpdated();
if (updated != null) {
final Element updatedElement = new Element("updated", getFeedNamespace()); final Element updatedElement = new Element("updated", getFeedNamespace());
updatedElement.addContent(DateParser.formatW3CDateTime(feed.getUpdated(), Locale.US)); updatedElement.addContent(DateParser.formatW3CDateTime(updated, Locale.US));
eFeed.addContent(updatedElement); eFeed.addContent(updatedElement);
} }
} }
protected void populateEntry(final Entry entry, final Element eEntry) throws FeedException { protected void populateEntry(final Entry entry, final Element eEntry) throws FeedException {
if (entry.getTitleEx() != null) {
final Content titleEx = entry.getTitleEx();
if (titleEx != null) {
final Element titleElement = new Element("title", getFeedNamespace()); final Element titleElement = new Element("title", getFeedNamespace());
fillContentElement(titleElement, entry.getTitleEx()); fillContentElement(titleElement, titleEx);
eEntry.addContent(titleElement); eEntry.addContent(titleElement);
} }
List<Link> links = entry.getAlternateLinks();
if (links != null) { final List<Link> alternateLinks = entry.getAlternateLinks();
for (int i = 0; i < links.size(); i++) { if (alternateLinks != null) {
eEntry.addContent(generateLinkElement(links.get(i))); for (final Link link : alternateLinks) {
eEntry.addContent(generateLinkElement(link));
} }
} }
links = entry.getOtherLinks();
if (links != null) { final List<Link> otherLinks = entry.getOtherLinks();
for (int i = 0; i < links.size(); i++) { if (otherLinks != null) {
eEntry.addContent(generateLinkElement(links.get(i))); for (final Link link : otherLinks) {
eEntry.addContent(generateLinkElement(link));
} }
} }
final List<Category> cats = entry.getCategories(); final List<Category> cats = entry.getCategories();
if (cats != null) { if (cats != null) {
for (int i = 0; i < cats.size(); i++) { for (final Category category : cats) {
eEntry.addContent(generateCategoryElement(cats.get(i))); eEntry.addContent(generateCategoryElement(category));
} }
} }
final List<SyndPerson> authors = entry.getAuthors(); final List<SyndPerson> authors = entry.getAuthors();
if (authors != null && !authors.isEmpty()) { if (Lists.isNotEmpty(authors)) {
for (int i = 0; i < authors.size(); i++) { for (final SyndPerson author : authors) {
final Element authorElement = new Element("author", getFeedNamespace()); final Element authorElement = new Element("author", getFeedNamespace());
fillPersonElement(authorElement, entry.getAuthors().get(i)); fillPersonElement(authorElement, author);
eEntry.addContent(authorElement); eEntry.addContent(authorElement);
} }
} }
final List<SyndPerson> contributors = entry.getContributors(); final List<SyndPerson> contributors = entry.getContributors();
if (contributors != null && !contributors.isEmpty()) { if (Lists.isNotEmpty(contributors)) {
for (int i = 0; i < contributors.size(); i++) { for (final SyndPerson contributor : contributors) {
final Element contributorElement = new Element("contributor", getFeedNamespace()); final Element contributorElement = new Element("contributor", getFeedNamespace());
fillPersonElement(contributorElement, contributors.get(i)); fillPersonElement(contributorElement, contributor);
eEntry.addContent(contributorElement); eEntry.addContent(contributorElement);
} }
} }
if (entry.getId() != null) {
eEntry.addContent(generateSimpleElement("id", entry.getId())); final String id = entry.getId();
if (id != null) {
eEntry.addContent(generateSimpleElement("id", id));
} }
if (entry.getUpdated() != null) { final Date updated = entry.getUpdated();
if (updated != null) {
final Element updatedElement = new Element("updated", getFeedNamespace()); final Element updatedElement = new Element("updated", getFeedNamespace());
updatedElement.addContent(DateParser.formatW3CDateTime(entry.getUpdated(), Locale.US)); updatedElement.addContent(DateParser.formatW3CDateTime(updated, Locale.US));
eEntry.addContent(updatedElement); eEntry.addContent(updatedElement);
} }
if (entry.getPublished() != null) { final Date published = entry.getPublished();
if (published != null) {
final Element publishedElement = new Element("published", getFeedNamespace()); final Element publishedElement = new Element("published", getFeedNamespace());
publishedElement.addContent(DateParser.formatW3CDateTime(entry.getPublished(), Locale.US)); publishedElement.addContent(DateParser.formatW3CDateTime(published, Locale.US));
eEntry.addContent(publishedElement); eEntry.addContent(publishedElement);
} }
if (entry.getContents() != null && !entry.getContents().isEmpty()) { final List<Content> contents = entry.getContents();
if (Lists.isNotEmpty(contents)) {
final Element contentElement = new Element("content", getFeedNamespace()); final Element contentElement = new Element("content", getFeedNamespace());
final Content content = entry.getContents().get(0); final Content content = contents.get(0);
fillContentElement(contentElement, content); fillContentElement(contentElement, content);
eEntry.addContent(contentElement); eEntry.addContent(contentElement);
} }
if (entry.getSummary() != null) { final Content summary = entry.getSummary();
if (summary != null) {
final Element summaryElement = new Element("summary", getFeedNamespace()); final Element summaryElement = new Element("summary", getFeedNamespace());
fillContentElement(summaryElement, entry.getSummary()); fillContentElement(summaryElement, summary);
eEntry.addContent(summaryElement); eEntry.addContent(summaryElement);
} }
if (entry.getSource() != null) { final Feed source = entry.getSource();
if (source != null) {
final Element sourceElement = new Element("source", getFeedNamespace()); final Element sourceElement = new Element("source", getFeedNamespace());
populateFeedHeader(entry.getSource(), sourceElement); populateFeedHeader(source, sourceElement);
eEntry.addContent(sourceElement); eEntry.addContent(sourceElement);
} }
} }
protected void checkFeedHeaderConstraints(final Element eFeed) throws FeedException { protected void checkFeedHeaderConstraints(final Element eFeed) throws FeedException {
@ -298,90 +336,122 @@ public class Atom10Generator extends BaseWireFeedGenerator {
} }
protected Element generateCategoryElement(final Category cat) { protected Element generateCategoryElement(final Category cat) {
final Element catElement = new Element("category", getFeedNamespace());
if (cat.getTerm() != null) { final Namespace namespace = getFeedNamespace();
final Attribute termAttribute = new Attribute("term", cat.getTerm()); final Element catElement = new Element("category", namespace);
final String term = cat.getTerm();
if (term != null) {
final Attribute termAttribute = new Attribute("term", term);
catElement.setAttribute(termAttribute); catElement.setAttribute(termAttribute);
} }
if (cat.getLabel() != null) { final String label = cat.getLabel();
final Attribute labelAttribute = new Attribute("label", cat.getLabel()); if (label != null) {
final Attribute labelAttribute = new Attribute("label", label);
catElement.setAttribute(labelAttribute); catElement.setAttribute(labelAttribute);
} }
if (cat.getScheme() != null) { final String scheme = cat.getScheme();
final Attribute schemeAttribute = new Attribute("scheme", cat.getScheme()); if (scheme != null) {
final Attribute schemeAttribute = new Attribute("scheme", scheme);
catElement.setAttribute(schemeAttribute); catElement.setAttribute(schemeAttribute);
} }
return catElement; return catElement;
} }
protected Element generateLinkElement(final Link link) { protected Element generateLinkElement(final Link link) {
final Element linkElement = new Element("link", getFeedNamespace());
if (link.getRel() != null) { final Namespace namespace = getFeedNamespace();
final Attribute relAttribute = new Attribute("rel", link.getRel()); final Element linkElement = new Element("link", namespace);
final String rel = link.getRel();
if (rel != null) {
final Attribute relAttribute = new Attribute("rel", rel);
linkElement.setAttribute(relAttribute); linkElement.setAttribute(relAttribute);
} }
if (link.getType() != null) { final String type = link.getType();
final Attribute typeAttribute = new Attribute("type", link.getType()); if (type != null) {
final Attribute typeAttribute = new Attribute("type", type);
linkElement.setAttribute(typeAttribute); linkElement.setAttribute(typeAttribute);
} }
if (link.getHref() != null) { final String href = link.getHref();
final Attribute hrefAttribute = new Attribute("href", link.getHref()); if (href != null) {
final Attribute hrefAttribute = new Attribute("href", href);
linkElement.setAttribute(hrefAttribute); linkElement.setAttribute(hrefAttribute);
} }
if (link.getHreflang() != null) { final String hreflang = link.getHreflang();
final Attribute hreflangAttribute = new Attribute("hreflang", link.getHreflang()); if (hreflang != null) {
final Attribute hreflangAttribute = new Attribute("hreflang", hreflang);
linkElement.setAttribute(hreflangAttribute); linkElement.setAttribute(hreflangAttribute);
} }
if (link.getTitle() != null) {
final Attribute title = new Attribute("title", link.getTitle()); final String linkTitle = link.getTitle();
if (linkTitle != null) {
final Attribute title = new Attribute("title", linkTitle);
linkElement.setAttribute(title); linkElement.setAttribute(title);
} }
if (link.getLength() != 0) { if (link.getLength() != 0) {
final Attribute lenght = new Attribute("length", Long.toString(link.getLength())); final Attribute lenght = new Attribute("length", Long.toString(link.getLength()));
linkElement.setAttribute(lenght); linkElement.setAttribute(lenght);
} }
return linkElement; return linkElement;
} }
protected void fillPersonElement(final Element element, final SyndPerson person) { protected void fillPersonElement(final Element element, final SyndPerson person) {
if (person.getName() != null) {
element.addContent(generateSimpleElement("name", person.getName())); final String name = person.getName();
} if (name != null) {
if (person.getUri() != null) { element.addContent(generateSimpleElement("name", name));
element.addContent(generateSimpleElement("uri", person.getUri()));
} }
if (person.getEmail() != null) { final String uri = person.getUri();
element.addContent(generateSimpleElement("email", person.getEmail())); if (uri != null) {
element.addContent(generateSimpleElement("uri", uri));
} }
final String email = person.getEmail();
if (email != null) {
element.addContent(generateSimpleElement("email", email));
}
generatePersonModules(person.getModules(), element); generatePersonModules(person.getModules(), element);
} }
protected Element generateTagLineElement(final Content tagline) { protected Element generateTagLineElement(final Content tagline) {
final Element taglineElement = new Element("subtitle", getFeedNamespace()); final Element taglineElement = new Element("subtitle", getFeedNamespace());
if (tagline.getType() != null) { final String type = tagline.getType();
final Attribute typeAttribute = new Attribute("type", tagline.getType()); if (type != null) {
final Attribute typeAttribute = new Attribute("type", type);
taglineElement.setAttribute(typeAttribute); taglineElement.setAttribute(typeAttribute);
} }
if (tagline.getValue() != null) { final String value = tagline.getValue();
taglineElement.addContent(tagline.getValue()); if (value != null) {
taglineElement.addContent(value);
} }
return taglineElement; return taglineElement;
} }
protected void fillContentElement(final Element contentElement, final Content content) throws FeedException { protected void fillContentElement(final Element contentElement, final Content content) throws FeedException {
final String type = content.getType(); final String type = content.getType();
String atomType = type; String atomType = type;
if (type != null) { if (type != null) {
// Fix for issue #39 "Atom 1.0 Text Types Not Set Correctly" // Fix for issue #39 "Atom 1.0 Text Types Not Set Correctly"
// we're not sure who set this value, so ensure Atom types are used // we're not sure who set this value, so ensure Atom types are used
@ -396,16 +466,20 @@ public class Atom10Generator extends BaseWireFeedGenerator {
final Attribute typeAttribute = new Attribute("type", atomType); final Attribute typeAttribute = new Attribute("type", atomType);
contentElement.setAttribute(typeAttribute); contentElement.setAttribute(typeAttribute);
} }
final String href = content.getSrc(); final String href = content.getSrc();
if (href != null) { if (href != null) {
final Attribute srcAttribute = new Attribute("src", href); final Attribute srcAttribute = new Attribute("src", href);
contentElement.setAttribute(srcAttribute); contentElement.setAttribute(srcAttribute);
} }
if (content.getValue() != null) {
final String value = content.getValue();
if (value != null) {
if (atomType != null && (atomType.equals(Content.XHTML) || atomType.indexOf("/xml") != -1 || atomType.indexOf("+xml") != -1)) { if (atomType != null && (atomType.equals(Content.XHTML) || atomType.indexOf("/xml") != -1 || atomType.indexOf("+xml") != -1)) {
final StringBuffer tmpDocString = new StringBuffer("<tmpdoc>"); final StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
tmpDocString.append(content.getValue()); tmpDocString.append(value);
tmpDocString.append("</tmpdoc>"); tmpDocString.append("</tmpdoc>");
final StringReader tmpDocReader = new StringReader(tmpDocString.toString()); final StringReader tmpDocReader = new StringReader(tmpDocString.toString());
Document tmpDoc; Document tmpDoc;
@ -417,29 +491,37 @@ public class Atom10Generator extends BaseWireFeedGenerator {
} }
final List<org.jdom2.Content> children = tmpDoc.getRootElement().removeContent(); final List<org.jdom2.Content> children = tmpDoc.getRootElement().removeContent();
contentElement.addContent(children); contentElement.addContent(children);
} else { } else {
// must be type html, text or some other non-XML format // must be type html, text or some other non-XML format
// JDOM will escape property for XML // JDOM will escape property for XML
contentElement.addContent(content.getValue()); contentElement.addContent(value);
} }
} }
} }
protected Element generateGeneratorElement(final Generator generator) { protected Element generateGeneratorElement(final Generator generator) {
final Element generatorElement = new Element("generator", getFeedNamespace()); final Element generatorElement = new Element("generator", getFeedNamespace());
if (generator.getUrl() != null) { final String url = generator.getUrl();
final Attribute urlAttribute = new Attribute("uri", generator.getUrl()); if (url != null) {
final Attribute urlAttribute = new Attribute("uri", url);
generatorElement.setAttribute(urlAttribute); generatorElement.setAttribute(urlAttribute);
} }
if (generator.getVersion() != null) { final String version2 = generator.getVersion();
final Attribute versionAttribute = new Attribute("version", generator.getVersion()); if (version2 != null) {
final Attribute versionAttribute = new Attribute("version", version2);
generatorElement.setAttribute(versionAttribute); generatorElement.setAttribute(versionAttribute);
} }
if (generator.getValue() != null) { final String value = generator.getValue();
generatorElement.addContent(generator.getValue()); if (value != null) {
generatorElement.addContent(value);
} }
return generatorElement; return generatorElement;

View file

@ -33,6 +33,7 @@ import org.jdom2.Parent;
import org.jdom2.input.SAXBuilder; import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter; import org.jdom2.output.XMLOutputter;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.WireFeed;
import com.sun.syndication.feed.atom.Category; import com.sun.syndication.feed.atom.Category;
import com.sun.syndication.feed.atom.Content; import com.sun.syndication.feed.atom.Content;
@ -52,6 +53,7 @@ import com.sun.syndication.io.WireFeedOutput;
* @author Dave Johnson * @author Dave Johnson
*/ */
public class Atom10Parser extends BaseWireFeedParser { public class Atom10Parser extends BaseWireFeedParser {
private static final String ATOM_10_URI = "http://www.w3.org/2005/Atom"; private static final String ATOM_10_URI = "http://www.w3.org/2005/Atom";
private static final Namespace ATOM_10_NS = Namespace.getNamespace(ATOM_10_URI); private static final Namespace ATOM_10_NS = Namespace.getNamespace(ATOM_10_URI);
@ -94,13 +96,9 @@ public class Atom10Parser extends BaseWireFeedParser {
} }
protected void validateFeed(final Document document) throws FeedException { protected void validateFeed(final Document document) throws FeedException {
// TBD // TBD here we have to validate the Feed against a schema or whatever not sure how to do it
// here we have to validate the Feed against a schema or whatever // one posibility would be to produce an ouput and attempt to parse it again with validation
// not sure how to do it // turned on. otherwise will have to check the document elements by hand.
// one posibility would be to produce an ouput and attempt to parse it
// again
// with validation turned on.
// otherwise will have to check the document elements by hand.
} }
protected WireFeed parseFeed(final Element eFeed, final Locale locale) throws FeedException { protected WireFeed parseFeed(final Element eFeed, final Locale locale) throws FeedException {
@ -135,203 +133,225 @@ public class Atom10Parser extends BaseWireFeedParser {
} }
private Feed parseFeedMetadata(final String baseURI, final Element eFeed, final Locale locale) { private Feed parseFeedMetadata(final String baseURI, final Element eFeed, final Locale locale) {
final com.sun.syndication.feed.atom.Feed feed = new com.sun.syndication.feed.atom.Feed(getType()); final com.sun.syndication.feed.atom.Feed feed = new com.sun.syndication.feed.atom.Feed(getType());
Element e = eFeed.getChild("title", getAtomNamespace()); final Element title = eFeed.getChild("title", getAtomNamespace());
if (e != null) { if (title != null) {
final Content c = new Content(); final Content c = new Content();
c.setValue(parseTextConstructToString(e)); c.setValue(parseTextConstructToString(title));
c.setType(getAttributeValue(e, "type")); c.setType(getAttributeValue(title, "type"));
feed.setTitleEx(c); feed.setTitleEx(c);
} }
List<Element> eList = eFeed.getChildren("link", getAtomNamespace()); final List<Element> links = eFeed.getChildren("link", getAtomNamespace());
feed.setAlternateLinks(parseAlternateLinks(feed, null, baseURI, eList)); feed.setAlternateLinks(parseAlternateLinks(feed, null, baseURI, links));
feed.setOtherLinks(parseOtherLinks(feed, null, baseURI, eList)); feed.setOtherLinks(parseOtherLinks(feed, null, baseURI, links));
final List<Element> cList = eFeed.getChildren("category", getAtomNamespace()); final List<Element> categories = eFeed.getChildren("category", getAtomNamespace());
feed.setCategories(parseCategories(baseURI, cList)); feed.setCategories(parseCategories(baseURI, categories));
eList = eFeed.getChildren("author", getAtomNamespace()); final List<Element> authors = eFeed.getChildren("author", getAtomNamespace());
if (!eList.isEmpty()) { if (!authors.isEmpty()) {
feed.setAuthors(parsePersons(baseURI, eList, locale)); feed.setAuthors(parsePersons(baseURI, authors, locale));
} }
eList = eFeed.getChildren("contributor", getAtomNamespace()); final List<Element> contributors = eFeed.getChildren("contributor", getAtomNamespace());
if (!eList.isEmpty()) { if (!contributors.isEmpty()) {
feed.setContributors(parsePersons(baseURI, eList, locale)); feed.setContributors(parsePersons(baseURI, contributors, locale));
} }
e = eFeed.getChild("subtitle", getAtomNamespace()); final Element subtitle = eFeed.getChild("subtitle", getAtomNamespace());
if (e != null) { if (subtitle != null) {
final Content subtitle = new Content(); final Content content = new Content();
subtitle.setValue(parseTextConstructToString(e)); content.setValue(parseTextConstructToString(subtitle));
subtitle.setType(getAttributeValue(e, "type")); content.setType(getAttributeValue(subtitle, "type"));
feed.setSubtitle(subtitle); feed.setSubtitle(content);
} }
e = eFeed.getChild("id", getAtomNamespace()); final Element id = eFeed.getChild("id", getAtomNamespace());
if (e != null) { if (id != null) {
feed.setId(e.getText()); feed.setId(id.getText());
} }
e = eFeed.getChild("generator", getAtomNamespace()); final Element generator = eFeed.getChild("generator", getAtomNamespace());
if (e != null) { if (generator != null) {
final Generator gen = new Generator(); final Generator gen = new Generator();
gen.setValue(e.getText()); gen.setValue(generator.getText());
String att = getAttributeValue(e, "uri");
if (att != null) { final String uri = getAttributeValue(generator, "uri");
gen.setUrl(att); if (uri != null) {
gen.setUrl(uri);
} }
att = getAttributeValue(e, "version");
if (att != null) { final String version = getAttributeValue(generator, "version");
gen.setVersion(att); if (version != null) {
gen.setVersion(version);
} }
feed.setGenerator(gen); feed.setGenerator(gen);
} }
e = eFeed.getChild("rights", getAtomNamespace()); final Element rights = eFeed.getChild("rights", getAtomNamespace());
if (e != null) { if (rights != null) {
feed.setRights(parseTextConstructToString(e)); feed.setRights(parseTextConstructToString(rights));
} }
e = eFeed.getChild("icon", getAtomNamespace()); final Element icon = eFeed.getChild("icon", getAtomNamespace());
if (e != null) { if (icon != null) {
feed.setIcon(e.getText()); feed.setIcon(icon.getText());
} }
e = eFeed.getChild("logo", getAtomNamespace()); final Element logo = eFeed.getChild("logo", getAtomNamespace());
if (e != null) { if (logo != null) {
feed.setLogo(e.getText()); feed.setLogo(logo.getText());
} }
e = eFeed.getChild("updated", getAtomNamespace()); final Element updated = eFeed.getChild("updated", getAtomNamespace());
if (e != null) { if (updated != null) {
feed.setUpdated(DateParser.parseDate(e.getText(), locale)); feed.setUpdated(DateParser.parseDate(updated.getText(), locale));
} }
return feed; return feed;
} }
private Link parseLink(final Feed feed, final Entry entry, final String baseURI, final Element eLink) { private Link parseLink(final Feed feed, final Entry entry, final String baseURI, final Element eLink) {
final Link link = new Link(); final Link link = new Link();
String att = getAttributeValue(eLink, "rel");
if (att != null) { final String rel = getAttributeValue(eLink, "rel");
link.setRel(att); if (rel != null) {
link.setRel(rel);
} }
att = getAttributeValue(eLink, "type");
if (att != null) { final String type = getAttributeValue(eLink, "type");
link.setType(att); if (type != null) {
link.setType(type);
} }
att = getAttributeValue(eLink, "href");
if (att != null) { final String href = getAttributeValue(eLink, "href");
link.setHref(att); if (href != null) {
if (isRelativeURI(att)) { link.setHref(href);
link.setHrefResolved(resolveURI(baseURI, eLink, att)); if (isRelativeURI(href)) {
link.setHrefResolved(resolveURI(baseURI, eLink, href));
} }
} }
att = getAttributeValue(eLink, "title");
if (att != null) { final String title = getAttributeValue(eLink, "title");
link.setTitle(att); if (title != null) {
link.setTitle(title);
} }
att = getAttributeValue(eLink, "hreflang");
if (att != null) { final String hrefLang = getAttributeValue(eLink, "hreflang");
link.setHreflang(att); if (hrefLang != null) {
link.setHreflang(hrefLang);
} }
att = getAttributeValue(eLink, "length");
if (att != null) { final String length = getAttributeValue(eLink, "length");
final Long val = NumberParser.parseLong(att); if (length != null) {
final Long val = NumberParser.parseLong(length);
if (val != null) { if (val != null) {
link.setLength(val.longValue()); link.setLength(val.longValue());
} }
} }
return link; return link;
} }
// List(Elements) -> List(Link) // List(Elements) -> List(Link)
private List<Link> parseAlternateLinks(final Feed feed, final Entry entry, final String baseURI, final List<Element> eLinks) { private List<Link> parseAlternateLinks(final Feed feed, final Entry entry, final String baseURI, final List<Element> eLinks) {
final List<Link> links = new ArrayList<Link>(); final List<Link> links = new ArrayList<Link>();
for (int i = 0; i < eLinks.size(); i++) { for (final Element eLink : eLinks) {
final Element eLink = eLinks.get(i);
final Link link = parseLink(feed, entry, baseURI, eLink); final Link link = parseLink(feed, entry, baseURI, eLink);
if (link.getRel() == null || "".equals(link.getRel().trim()) || "alternate".equals(link.getRel())) { if (link.getRel() == null || "".equals(link.getRel().trim()) || "alternate".equals(link.getRel())) {
links.add(link); links.add(link);
} }
} }
if (!links.isEmpty()) {
return links; return Lists.emptyToNull(links);
} else {
return null;
}
} }
private List<Link> parseOtherLinks(final Feed feed, final Entry entry, final String baseURI, final List<Element> eLinks) { private List<Link> parseOtherLinks(final Feed feed, final Entry entry, final String baseURI, final List<Element> eLinks) {
final List<Link> links = new ArrayList<Link>(); final List<Link> links = new ArrayList<Link>();
for (int i = 0; i < eLinks.size(); i++) { for (final Element eLink : eLinks) {
final Element eLink = eLinks.get(i);
final Link link = parseLink(feed, entry, baseURI, eLink); final Link link = parseLink(feed, entry, baseURI, eLink);
if (!"alternate".equals(link.getRel())) { if (!"alternate".equals(link.getRel())) {
links.add(link); links.add(link);
} }
} }
if (!links.isEmpty()) {
return links; return Lists.emptyToNull(links);
} else {
return null;
}
} }
private Person parsePerson(final String baseURI, final Element ePerson, final Locale locale) { private Person parsePerson(final String baseURI, final Element ePerson, final Locale locale) {
final Person person = new Person(); final Person person = new Person();
Element e = ePerson.getChild("name", getAtomNamespace());
if (e != null) { final Element name = ePerson.getChild("name", getAtomNamespace());
person.setName(e.getText()); if (name != null) {
person.setName(name.getText());
} }
e = ePerson.getChild("uri", getAtomNamespace());
if (e != null) { final Element uri = ePerson.getChild("uri", getAtomNamespace());
person.setUri(e.getText()); if (uri != null) {
if (isRelativeURI(e.getText())) { person.setUri(uri.getText());
person.setUriResolved(resolveURI(baseURI, ePerson, e.getText())); if (isRelativeURI(uri.getText())) {
person.setUriResolved(resolveURI(baseURI, ePerson, uri.getText()));
} }
} }
e = ePerson.getChild("email", getAtomNamespace());
if (e != null) { final Element email = ePerson.getChild("email", getAtomNamespace());
person.setEmail(e.getText()); if (email != null) {
person.setEmail(email.getText());
} }
person.setModules(parsePersonModules(ePerson, locale)); person.setModules(parsePersonModules(ePerson, locale));
return person; return person;
} }
// List(Elements) -> List(Persons) // List(Elements) -> List(Persons)
private List<SyndPerson> parsePersons(final String baseURI, final List<Element> ePersons, final Locale locale) { private List<SyndPerson> parsePersons(final String baseURI, final List<Element> ePersons, final Locale locale) {
final List<SyndPerson> persons = new ArrayList<SyndPerson>(); final List<SyndPerson> persons = new ArrayList<SyndPerson>();
for (int i = 0; i < ePersons.size(); i++) { for (final Element ePerson : ePersons) {
persons.add(parsePerson(baseURI, ePersons.get(i), locale)); persons.add(parsePerson(baseURI, ePerson, locale));
}
if (!persons.isEmpty()) {
return persons;
} else {
return null;
} }
return Lists.emptyToNull(persons);
} }
private Content parseContent(final Element e) { private Content parseContent(final Element e) {
final String value = parseTextConstructToString(e); final String value = parseTextConstructToString(e);
final String src = getAttributeValue(e, "src"); final String src = getAttributeValue(e, "src");
final String type = getAttributeValue(e, "type"); final String type = getAttributeValue(e, "type");
final Content content = new Content(); final Content content = new Content();
content.setSrc(src); content.setSrc(src);
content.setType(type); content.setType(type);
content.setValue(value); content.setValue(value);
return content; return content;
} }
private String parseTextConstructToString(final Element e) { private String parseTextConstructToString(final Element e) {
String value = null;
String type = getAttributeValue(e, "type"); String type = getAttributeValue(e, "type");
if (type == null) { if (type == null) {
type = Content.TEXT; type = Content.TEXT;
} }
String value = null;
if (type.equals(Content.XHTML) || type.indexOf("/xml") != -1 || type.indexOf("+xml") != -1) { if (type.equals(Content.XHTML) || type.indexOf("/xml") != -1 || type.indexOf("+xml") != -1) {
// XHTML content needs special handling // XHTML content needs special handling
final XMLOutputter outputter = new XMLOutputter(); final XMLOutputter outputter = new XMLOutputter();
@ -349,23 +369,25 @@ public class Atom10Parser extends BaseWireFeedParser {
// Everything else comes in verbatim // Everything else comes in verbatim
value = e.getText(); value = e.getText();
} }
return value; return value;
} }
// List(Elements) -> List(Entries) // List(Elements) -> List(Entries)
protected List<Entry> parseEntries(final Feed feed, final String baseURI, final List<Element> eEntries, final Locale locale) { protected List<Entry> parseEntries(final Feed feed, final String baseURI, final List<Element> eEntries, final Locale locale) {
final List<Entry> entries = new ArrayList<Entry>(); final List<Entry> entries = new ArrayList<Entry>();
for (int i = 0; i < eEntries.size(); i++) { for (final Element entry : eEntries) {
entries.add(this.parseEntry(feed, eEntries.get(i), baseURI, locale)); entries.add(this.parseEntry(feed, entry, baseURI, locale));
}
if (!entries.isEmpty()) {
return entries;
} else {
return null;
} }
return Lists.emptyToNull(entries);
} }
protected Entry parseEntry(final Feed feed, final Element eEntry, final String baseURI, final Locale locale) { protected Entry parseEntry(final Feed feed, final Element eEntry, final String baseURI, final Locale locale) {
final Entry entry = new Entry(); final Entry entry = new Entry();
final String xmlBase = eEntry.getAttributeValue("base", Namespace.XML_NAMESPACE); final String xmlBase = eEntry.getAttributeValue("base", Namespace.XML_NAMESPACE);
@ -373,67 +395,67 @@ public class Atom10Parser extends BaseWireFeedParser {
entry.setXmlBase(xmlBase); entry.setXmlBase(xmlBase);
} }
Element e = eEntry.getChild("title", getAtomNamespace()); final Element title = eEntry.getChild("title", getAtomNamespace());
if (e != null) { if (title != null) {
final Content c = new Content(); final Content c = new Content();
c.setValue(parseTextConstructToString(e)); c.setValue(parseTextConstructToString(title));
c.setType(getAttributeValue(e, "type")); c.setType(getAttributeValue(title, "type"));
entry.setTitleEx(c); entry.setTitleEx(c);
} }
List<Element> eList = eEntry.getChildren("link", getAtomNamespace()); final List<Element> links = eEntry.getChildren("link", getAtomNamespace());
entry.setAlternateLinks(parseAlternateLinks(feed, entry, baseURI, eList)); entry.setAlternateLinks(parseAlternateLinks(feed, entry, baseURI, links));
entry.setOtherLinks(parseOtherLinks(feed, entry, baseURI, eList)); entry.setOtherLinks(parseOtherLinks(feed, entry, baseURI, links));
eList = eEntry.getChildren("author", getAtomNamespace()); final List<Element> authors = eEntry.getChildren("author", getAtomNamespace());
if (!eList.isEmpty()) { if (!authors.isEmpty()) {
entry.setAuthors(parsePersons(baseURI, eList, locale)); entry.setAuthors(parsePersons(baseURI, authors, locale));
} }
eList = eEntry.getChildren("contributor", getAtomNamespace()); final List<Element> contributors = eEntry.getChildren("contributor", getAtomNamespace());
if (!eList.isEmpty()) { if (!contributors.isEmpty()) {
entry.setContributors(parsePersons(baseURI, eList, locale)); entry.setContributors(parsePersons(baseURI, contributors, locale));
} }
e = eEntry.getChild("id", getAtomNamespace()); final Element id = eEntry.getChild("id", getAtomNamespace());
if (e != null) { if (id != null) {
entry.setId(e.getText()); entry.setId(id.getText());
} }
e = eEntry.getChild("updated", getAtomNamespace()); final Element updated = eEntry.getChild("updated", getAtomNamespace());
if (e != null) { if (updated != null) {
entry.setUpdated(DateParser.parseDate(e.getText(), locale)); entry.setUpdated(DateParser.parseDate(updated.getText(), locale));
} }
e = eEntry.getChild("published", getAtomNamespace()); final Element published = eEntry.getChild("published", getAtomNamespace());
if (e != null) { if (published != null) {
entry.setPublished(DateParser.parseDate(e.getText(), locale)); entry.setPublished(DateParser.parseDate(published.getText(), locale));
} }
e = eEntry.getChild("summary", getAtomNamespace()); final Element summary = eEntry.getChild("summary", getAtomNamespace());
if (e != null) { if (summary != null) {
entry.setSummary(parseContent(e)); entry.setSummary(parseContent(summary));
} }
e = eEntry.getChild("content", getAtomNamespace()); final Element content = eEntry.getChild("content", getAtomNamespace());
if (e != null) { if (content != null) {
final List<Content> contents = new ArrayList<Content>(); final List<Content> contents = new ArrayList<Content>();
contents.add(parseContent(e)); contents.add(parseContent(content));
entry.setContents(contents); entry.setContents(contents);
} }
e = eEntry.getChild("rights", getAtomNamespace()); final Element rights = eEntry.getChild("rights", getAtomNamespace());
if (e != null) { if (rights != null) {
entry.setRights(e.getText()); entry.setRights(rights.getText());
} }
final List<Element> cList = eEntry.getChildren("category", getAtomNamespace()); final List<Element> categories = eEntry.getChildren("category", getAtomNamespace());
entry.setCategories(parseCategories(baseURI, cList)); entry.setCategories(parseCategories(baseURI, categories));
// TODO: SHOULD handle Atom entry source element // TODO: SHOULD handle Atom entry source element
e = eEntry.getChild("source", getAtomNamespace()); final Element source = eEntry.getChild("source", getAtomNamespace());
if (e != null) { if (source != null) {
entry.setSource(parseFeedMetadata(baseURI, e, locale)); entry.setSource(parseFeedMetadata(baseURI, source, locale));
} }
entry.setModules(parseItemModules(eEntry, locale)); entry.setModules(parseItemModules(eEntry, locale));
@ -442,39 +464,43 @@ public class Atom10Parser extends BaseWireFeedParser {
if (!foreignMarkup.isEmpty()) { if (!foreignMarkup.isEmpty()) {
entry.setForeignMarkup(foreignMarkup); entry.setForeignMarkup(foreignMarkup);
} }
return entry; return entry;
} }
private List<Category> parseCategories(final String baseURI, final List<Element> eCategories) { private List<Category> parseCategories(final String baseURI, final List<Element> eCategories) {
final List<Category> cats = new ArrayList<Category>(); final List<Category> cats = new ArrayList<Category>();
for (int i = 0; i < eCategories.size(); i++) { for (final Element eCategory : eCategories) {
final Element eCategory = eCategories.get(i);
cats.add(parseCategory(baseURI, eCategory)); cats.add(parseCategory(baseURI, eCategory));
} }
if (!cats.isEmpty()) {
return cats; return Lists.emptyToNull(cats);
} else {
return null;
}
} }
private Category parseCategory(final String baseURI, final Element eCategory) { private Category parseCategory(final String baseURI, final Element eCategory) {
final Category category = new Category(); final Category category = new Category();
String att = getAttributeValue(eCategory, "term");
if (att != null) { final String term = getAttributeValue(eCategory, "term");
category.setTerm(att); if (term != null) {
category.setTerm(term);
} }
att = getAttributeValue(eCategory, "scheme");
if (att != null) { final String scheme = getAttributeValue(eCategory, "scheme");
category.setScheme(att); if (scheme != null) {
if (isRelativeURI(att)) { category.setScheme(scheme);
category.setSchemeResolved(resolveURI(baseURI, eCategory, att)); if (isRelativeURI(scheme)) {
category.setSchemeResolved(resolveURI(baseURI, eCategory, scheme));
} }
} }
att = getAttributeValue(eCategory, "label");
if (att != null) { final String label = getAttributeValue(eCategory, "label");
category.setLabel(att); if (label != null) {
category.setLabel(label);
} }
return category; return category;
} }
@ -506,10 +532,13 @@ public class Atom10Parser extends BaseWireFeedParser {
* @param url URL to be resolved * @param url URL to be resolved
*/ */
public static String resolveURI(final String baseURI, final Parent parent, String url) { public static String resolveURI(final String baseURI, final Parent parent, String url) {
if (!resolveURIs) { if (!resolveURIs) {
return url; return url;
} }
if (isRelativeURI(url)) { if (isRelativeURI(url)) {
if (".".equals(url) || "./".equals(url)) { if (".".equals(url) || "./".equals(url)) {
url = ""; url = "";
} }
@ -560,7 +589,9 @@ public class Atom10Parser extends BaseWireFeedParser {
return formURI(baseURI, url); return formURI(baseURI, url);
} }
} }
return url; return url;
} }
/** /**
@ -658,7 +689,8 @@ public class Atom10Parser extends BaseWireFeedParser {
* Parse entry from reader. * Parse entry from reader.
*/ */
public static Entry parseEntry(final Reader rd, final String baseURI, final Locale locale) throws JDOMException, IOException, IllegalArgumentException, public static Entry parseEntry(final Reader rd, final String baseURI, final Locale locale) throws JDOMException, IOException, IllegalArgumentException,
FeedException { FeedException {
// Parse entry into JDOM tree // Parse entry into JDOM tree
final SAXBuilder builder = new SAXBuilder(); final SAXBuilder builder = new SAXBuilder();
final Document entryDoc = builder.build(rd); final Document entryDoc = builder.build(rd);
@ -681,4 +713,5 @@ public class Atom10Parser extends BaseWireFeedParser {
final Feed parsedFeed = (Feed) input.build(feedDoc); final Feed parsedFeed = (Feed) input.build(feedDoc);
return parsedFeed.getEntries().get(0); return parsedFeed.getEntries().get(0);
} }
} }

View file

@ -122,25 +122,28 @@ public abstract class BaseWireFeedGenerator implements WireFeedGenerator {
// ConcurrentModificationException // ConcurrentModificationException
// below // below
for (int i = 0; i < additionalNamespaces.size(); i++) { for (final Namespace ns : additionalNamespaces) {
final Namespace ns = additionalNamespaces.get(i);
final String prefix = ns.getPrefix(); final String prefix = ns.getPrefix();
if (prefix != null && prefix.length() > 0 && !usedPrefixes.contains(prefix)) { if (prefix != null && prefix.length() > 0 && !usedPrefixes.contains(prefix)) {
root.removeNamespaceDeclaration(ns); root.removeNamespaceDeclaration(ns);
} }
} }
} }
private static void collectUsedPrefixes(final Element el, final Set<String> collector) { private static void collectUsedPrefixes(final Element el, final Set<String> collector) {
final String prefix = el.getNamespacePrefix(); final String prefix = el.getNamespacePrefix();
if (prefix != null && prefix.length() > 0 && !collector.contains(prefix)) { if (prefix != null && prefix.length() > 0 && !collector.contains(prefix)) {
collector.add(prefix); collector.add(prefix);
} }
final List<Element> kids = el.getChildren(); final List<Element> kids = el.getChildren();
for (int i = 0; i < kids.size(); i++) { for (final Element kid : kids) {
collectUsedPrefixes(kids.get(i), collector); // recursion // recursion- worth it
// - worth it collectUsedPrefixes(kid, collector);
} }
} }
} }

View file

@ -100,56 +100,86 @@ public class DCModuleGenerator implements ModuleGenerator {
*/ */
@Override @Override
public final void generate(final Module module, final Element element) { public final void generate(final Module module, final Element element) {
final DCModule dcModule = (DCModule) module; final DCModule dcModule = (DCModule) module;
if (dcModule.getTitle() != null) { final String title = dcModule.getTitle();
if (title != null) {
element.addContent(generateSimpleElementList("title", dcModule.getTitles())); element.addContent(generateSimpleElementList("title", dcModule.getTitles()));
} }
if (dcModule.getCreator() != null) {
final String creator = dcModule.getCreator();
if (creator != null) {
element.addContent(generateSimpleElementList("creator", dcModule.getCreators())); element.addContent(generateSimpleElementList("creator", dcModule.getCreators()));
} }
final List<DCSubject> subjects = dcModule.getSubjects(); final List<DCSubject> subjects = dcModule.getSubjects();
for (int i = 0; i < subjects.size(); i++) { for (final DCSubject dcSubject : subjects) {
element.addContent(generateSubjectElement(subjects.get(i))); element.addContent(generateSubjectElement(dcSubject));
} }
if (dcModule.getDescription() != null) {
final String description = dcModule.getDescription();
if (description != null) {
element.addContent(generateSimpleElementList("description", dcModule.getDescriptions())); element.addContent(generateSimpleElementList("description", dcModule.getDescriptions()));
} }
if (dcModule.getPublisher() != null) {
final String publisher = dcModule.getPublisher();
if (publisher != null) {
element.addContent(generateSimpleElementList("publisher", dcModule.getPublishers())); element.addContent(generateSimpleElementList("publisher", dcModule.getPublishers()));
} }
if (dcModule.getContributors() != null) {
element.addContent(generateSimpleElementList("contributor", dcModule.getContributors())); final List<String> contributors = dcModule.getContributors();
if (contributors != null) {
element.addContent(generateSimpleElementList("contributor", contributors));
} }
if (dcModule.getDate() != null) {
final Date dcDate = dcModule.getDate();
if (dcDate != null) {
for (final Date date : dcModule.getDates()) { for (final Date date : dcModule.getDates()) {
element.addContent(generateSimpleElement("date", DateParser.formatW3CDateTime(date, Locale.US))); element.addContent(generateSimpleElement("date", DateParser.formatW3CDateTime(date, Locale.US)));
} }
} }
if (dcModule.getType() != null) {
final String type = dcModule.getType();
if (type != null) {
element.addContent(generateSimpleElementList("type", dcModule.getTypes())); element.addContent(generateSimpleElementList("type", dcModule.getTypes()));
} }
if (dcModule.getFormat() != null) {
final String format = dcModule.getFormat();
if (format != null) {
element.addContent(generateSimpleElementList("format", dcModule.getFormats())); element.addContent(generateSimpleElementList("format", dcModule.getFormats()));
} }
if (dcModule.getIdentifier() != null) {
final String identifier = dcModule.getIdentifier();
if (identifier != null) {
element.addContent(generateSimpleElementList("identifier", dcModule.getIdentifiers())); element.addContent(generateSimpleElementList("identifier", dcModule.getIdentifiers()));
} }
if (dcModule.getSource() != null) {
final String source = dcModule.getSource();
if (source != null) {
element.addContent(generateSimpleElementList("source", dcModule.getSources())); element.addContent(generateSimpleElementList("source", dcModule.getSources()));
} }
if (dcModule.getLanguage() != null) {
final String language = dcModule.getLanguage();
if (language != null) {
element.addContent(generateSimpleElementList("language", dcModule.getLanguages())); element.addContent(generateSimpleElementList("language", dcModule.getLanguages()));
} }
if (dcModule.getRelation() != null) {
final String relation = dcModule.getRelation();
if (relation != null) {
element.addContent(generateSimpleElementList("relation", dcModule.getRelations())); element.addContent(generateSimpleElementList("relation", dcModule.getRelations()));
} }
if (dcModule.getCoverage() != null) {
final String coverage = dcModule.getCoverage();
if (coverage != null) {
element.addContent(generateSimpleElementList("coverage", dcModule.getCoverages())); element.addContent(generateSimpleElementList("coverage", dcModule.getCoverages()));
} }
if (dcModule.getRights() != null) {
final String rights = dcModule.getRights();
if (rights != null) {
element.addContent(generateSimpleElementList("rights", dcModule.getRightsList())); element.addContent(generateSimpleElementList("rights", dcModule.getRightsList()));
} }
} }
/** /**
@ -160,24 +190,34 @@ public class DCModuleGenerator implements ModuleGenerator {
* @return the element for the subject. * @return the element for the subject.
*/ */
protected final Element generateSubjectElement(final DCSubject subject) { protected final Element generateSubjectElement(final DCSubject subject) {
final Element subjectElement = new Element("subject", getDCNamespace()); final Element subjectElement = new Element("subject", getDCNamespace());
if (subject.getTaxonomyUri() != null) { final String taxonomyUri = subject.getTaxonomyUri();
final Element descriptionElement = new Element("Description", getRDFNamespace()); final String value = subject.getValue();
if (taxonomyUri != null) {
final Attribute resourceAttribute = new Attribute("resource", taxonomyUri, getRDFNamespace());
final Element topicElement = new Element("topic", getTaxonomyNamespace()); final Element topicElement = new Element("topic", getTaxonomyNamespace());
final Attribute resourceAttribute = new Attribute("resource", subject.getTaxonomyUri(), getRDFNamespace());
topicElement.setAttribute(resourceAttribute); topicElement.setAttribute(resourceAttribute);
final Element descriptionElement = new Element("Description", getRDFNamespace());
descriptionElement.addContent(topicElement); descriptionElement.addContent(topicElement);
if (subject.getValue() != null) { if (value != null) {
final Element valueElement = new Element("value", getRDFNamespace()); final Element valueElement = new Element("value", getRDFNamespace());
valueElement.addContent(subject.getValue()); valueElement.addContent(value);
descriptionElement.addContent(valueElement); descriptionElement.addContent(valueElement);
} }
subjectElement.addContent(descriptionElement); subjectElement.addContent(descriptionElement);
} else { } else {
subjectElement.addContent(subject.getValue()); subjectElement.addContent(value);
} }
return subjectElement; return subjectElement;
} }
@ -192,7 +232,6 @@ public class DCModuleGenerator implements ModuleGenerator {
protected final Element generateSimpleElement(final String name, final String value) { protected final Element generateSimpleElement(final String name, final String value) {
final Element element = new Element(name, getDCNamespace()); final Element element = new Element(name, getDCNamespace());
element.addContent(value); element.addContent(value);
return element; return element;
} }
@ -201,15 +240,14 @@ public class DCModuleGenerator implements ModuleGenerator {
* <p> * <p>
* *
* @param name the name of the element list to generate. * @param name the name of the element list to generate.
* @param value the list of values for the elements. * @param values the list of values for the elements.
* @return a list of Elements created. * @return a list of Elements created.
*/ */
protected final List<Element> generateSimpleElementList(final String name, final List<String> value) { protected final List<Element> generateSimpleElementList(final String name, final List<String> values) {
final List<Element> elements = new ArrayList<Element>(); final List<Element> elements = new ArrayList<Element>();
for (final String string : value) { for (final String value : values) {
elements.add(generateSimpleElement(name, string)); elements.add(generateSimpleElement(name, value));
} }
return elements; return elements;
} }
} }

View file

@ -71,89 +71,106 @@ public class DCModuleParser implements ModuleParser {
*/ */
@Override @Override
public Module parse(final Element dcRoot, final Locale locale) { public Module parse(final Element dcRoot, final Locale locale) {
boolean foundSomething = false; boolean foundSomething = false;
final DCModule dcm = new DCModuleImpl(); final DCModule dcm = new DCModuleImpl();
List<Element> eList = dcRoot.getChildren("title", getDCNamespace()); final List<Element> titles = dcRoot.getChildren("title", getDCNamespace());
if (!eList.isEmpty()) { if (!titles.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setTitles(parseElementList(eList)); dcm.setTitles(parseElementList(titles));
} }
eList = dcRoot.getChildren("creator", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> creators = dcRoot.getChildren("creator", getDCNamespace());
if (!creators.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setCreators(parseElementList(eList)); dcm.setCreators(parseElementList(creators));
} }
eList = dcRoot.getChildren("subject", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> subjects = dcRoot.getChildren("subject", getDCNamespace());
if (!subjects.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setSubjects(parseSubjects(eList)); dcm.setSubjects(parseSubjects(subjects));
} }
eList = dcRoot.getChildren("description", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> descriptions = dcRoot.getChildren("description", getDCNamespace());
if (!descriptions.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setDescriptions(parseElementList(eList)); dcm.setDescriptions(parseElementList(descriptions));
} }
eList = dcRoot.getChildren("publisher", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> publishers = dcRoot.getChildren("publisher", getDCNamespace());
if (!publishers.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setPublishers(parseElementList(eList)); dcm.setPublishers(parseElementList(publishers));
} }
eList = dcRoot.getChildren("contributor", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> contributors = dcRoot.getChildren("contributor", getDCNamespace());
if (!contributors.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setContributors(parseElementList(eList)); dcm.setContributors(parseElementList(contributors));
} }
eList = dcRoot.getChildren("date", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> dates = dcRoot.getChildren("date", getDCNamespace());
if (!dates.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setDates(parseElementListDate(eList, locale)); dcm.setDates(parseElementListDate(dates, locale));
} }
eList = dcRoot.getChildren("type", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> types = dcRoot.getChildren("type", getDCNamespace());
if (!types.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setTypes(parseElementList(eList)); dcm.setTypes(parseElementList(types));
} }
eList = dcRoot.getChildren("format", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> formats = dcRoot.getChildren("format", getDCNamespace());
if (!formats.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setFormats(parseElementList(eList)); dcm.setFormats(parseElementList(formats));
} }
eList = dcRoot.getChildren("identifier", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> identifiers = dcRoot.getChildren("identifier", getDCNamespace());
if (!identifiers.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setIdentifiers(parseElementList(eList)); dcm.setIdentifiers(parseElementList(identifiers));
} }
eList = dcRoot.getChildren("source", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> sources = dcRoot.getChildren("source", getDCNamespace());
if (!sources.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setSources(parseElementList(eList)); dcm.setSources(parseElementList(sources));
} }
eList = dcRoot.getChildren("language", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> languages = dcRoot.getChildren("language", getDCNamespace());
if (!languages.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setLanguages(parseElementList(eList)); dcm.setLanguages(parseElementList(languages));
} }
eList = dcRoot.getChildren("relation", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> relations = dcRoot.getChildren("relation", getDCNamespace());
if (!relations.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setRelations(parseElementList(eList)); dcm.setRelations(parseElementList(relations));
} }
eList = dcRoot.getChildren("coverage", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> coverages = dcRoot.getChildren("coverage", getDCNamespace());
if (!coverages.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setCoverages(parseElementList(eList)); dcm.setCoverages(parseElementList(coverages));
} }
eList = dcRoot.getChildren("rights", getDCNamespace());
if (!eList.isEmpty()) { final List<Element> rights = dcRoot.getChildren("rights", getDCNamespace());
if (!rights.isEmpty()) {
foundSomething = true; foundSomething = true;
dcm.setRightsList(parseElementList(eList)); dcm.setRightsList(parseElementList(rights));
} }
if (foundSomething) { if (foundSomething) {
return dcm; return dcm;
} else { } else {
return null; return null;
} }
} }
/** /**
@ -164,15 +181,15 @@ public class DCModuleParser implements ModuleParser {
* @return the string contained in the resource of the element. * @return the string contained in the resource of the element.
*/ */
protected final String getTaxonomy(final Element desc) { protected final String getTaxonomy(final Element desc) {
String d = null; String taxonomy = null;
final Element taxo = desc.getChild("topic", getTaxonomyNamespace()); final Element topic = desc.getChild("topic", getTaxonomyNamespace());
if (taxo != null) { if (topic != null) {
final Attribute a = taxo.getAttribute("resource", getRDFNamespace()); final Attribute resource = topic.getAttribute("resource", getRDFNamespace());
if (a != null) { if (resource != null) {
d = a.getValue(); taxonomy = resource.getValue();
} }
} }
return d; return taxonomy;
} }
/** /**
@ -183,20 +200,27 @@ public class DCModuleParser implements ModuleParser {
* @return a list of subjects parsed from the elements. * @return a list of subjects parsed from the elements.
*/ */
protected final List<DCSubject> parseSubjects(final List<Element> eList) { protected final List<DCSubject> parseSubjects(final List<Element> eList) {
final List<DCSubject> subjects = new ArrayList<DCSubject>(); final List<DCSubject> subjects = new ArrayList<DCSubject>();
for (final Element element : eList) {
final Element eSubject = element; for (final Element eSubject : eList) {
final Element eDesc = eSubject.getChild("Description", getRDFNamespace());
if (eDesc != null) { final Element description = eSubject.getChild("Description", getRDFNamespace());
final String taxonomy = getTaxonomy(eDesc);
final List<Element> eValues = eDesc.getChildren("value", getRDFNamespace()); if (description != null) {
for (final Element element2 : eValues) {
final Element eValue = element2; final String taxonomy = getTaxonomy(description);
final List<Element> values = description.getChildren("value", getRDFNamespace());
for (final Element value : values) {
final DCSubject subject = new DCSubjectImpl(); final DCSubject subject = new DCSubjectImpl();
subject.setTaxonomyUri(taxonomy); subject.setTaxonomyUri(taxonomy);
subject.setValue(eValue.getText()); subject.setValue(value.getText());
subjects.add(subject); subjects.add(subject);
} }
} else { } else {
final DCSubject subject = new DCSubjectImpl(); final DCSubject subject = new DCSubjectImpl();
subject.setValue(eSubject.getText()); subject.setValue(eSubject.getText());
@ -211,16 +235,14 @@ public class DCModuleParser implements ModuleParser {
* Utility method to parse a list of strings out of a list of elements. * Utility method to parse a list of strings out of a list of elements.
* <p> * <p>
* *
* @param eList the list of elements to parse. * @param elements the list of elements to parse.
* @return the list of strings * @return the list of strings
*/ */
protected final List<String> parseElementList(final List<Element> eList) { protected final List<String> parseElementList(final List<Element> elements) {
final List<String> values = new ArrayList<String>(); final List<String> values = new ArrayList<String>();
for (final Element element : eList) { for (final Element element : elements) {
final Element e = element; values.add(element.getText());
values.add(e.getText());
} }
return values; return values;
} }
@ -228,16 +250,15 @@ public class DCModuleParser implements ModuleParser {
* Utility method to parse a list of dates out of a list of elements. * Utility method to parse a list of dates out of a list of elements.
* <p> * <p>
* *
* @param eList the list of elements to parse. * @param elements the list of elements to parse.
* @return the list of dates. * @return the list of dates.
*/ */
protected final List<Date> parseElementListDate(final List<Element> eList, final Locale locale) { protected final List<Date> parseElementListDate(final List<Element> elements, final Locale locale) {
final List<Date> values = new ArrayList<Date>(); final List<Date> values = new ArrayList<Date>();
for (final Element element : eList) { for (final Element element : elements) {
final Element e = element; values.add(DateParser.parseDate(element.getText(), locale));
values.add(DateParser.parseDate(e.getText(), locale));
} }
return values; return values;
} }
} }

View file

@ -39,36 +39,26 @@ public class DateParser {
private static String[] ADDITIONAL_MASKS; private static String[] ADDITIONAL_MASKS;
static { // order is like this because the SimpleDateFormat.parse does not fail with exception if it can
ADDITIONAL_MASKS = PropertiesLoader.getPropertiesLoader().getTokenizedProperty("datetime.extra.masks", "|"); // parse a valid date out of a substring of the full string given the mask so we have to check
} // the most complete format first, then it fails with exception
// order is like this because the SimpleDateFormat.parse does not fail with
// exception
// if it can parse a valid date out of a substring of the full string given
// the mask
// so we have to check the most complete format first, then it fails with
// exception
private static final String[] RFC822_MASKS = { "EEE, dd MMM yy HH:mm:ss z", "EEE, dd MMM yy HH:mm z", "dd MMM yy HH:mm:ss z", "dd MMM yy HH:mm z" }; private static final String[] RFC822_MASKS = { "EEE, dd MMM yy HH:mm:ss z", "EEE, dd MMM yy HH:mm z", "dd MMM yy HH:mm:ss z", "dd MMM yy HH:mm z" };
// order is like this because the SimpleDateFormat.parse does not fail with // order is like this because the SimpleDateFormat.parse does not fail with exception if it can
// exception // parse a valid date out of a substring of the full string given the mask so we have to check
// if it can parse a valid date out of a substring of the full string given // the most complete format first, then it fails with exception
// the mask
// so we have to check the most complete format first, then it fails with
// exception
private static final String[] W3CDATETIME_MASKS = { "yyyy-MM-dd'T'HH:mm:ss.SSSz", "yyyy-MM-dd't'HH:mm:ss.SSSz", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", private static final String[] W3CDATETIME_MASKS = { "yyyy-MM-dd'T'HH:mm:ss.SSSz", "yyyy-MM-dd't'HH:mm:ss.SSSz", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd't'HH:mm:ss.SSS'z'", "yyyy-MM-dd'T'HH:mm:ssz", "yyyy-MM-dd't'HH:mm:ssz", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd't'HH:mm:ssZ", "yyyy-MM-dd't'HH:mm:ss.SSS'z'", "yyyy-MM-dd'T'HH:mm:ssz", "yyyy-MM-dd't'HH:mm:ssz", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd't'HH:mm:ssZ",
"yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd't'HH:mm:ss'z'", "yyyy-MM-dd'T'HH:mmz", // together "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd't'HH:mm:ss'z'", "yyyy-MM-dd'T'HH:mmz", // together
// with // with
// logic // logic
// in // in
// the // the
// parseW3CDateTime // parseW3CDateTime
// they // they
"yyyy-MM'T'HH:mmz", // handle W3C dates without time forcing them to "yyyy-MM'T'HH:mmz", // handle W3C dates without time forcing them to
// be GMT // be GMT
"yyyy'T'HH:mmz", "yyyy-MM-dd't'HH:mmz", "yyyy-MM-dd'T'HH:mm'Z'", "yyyy-MM-dd't'HH:mm'z'", "yyyy-MM-dd", "yyyy-MM", "yyyy" }; "yyyy'T'HH:mmz", "yyyy-MM-dd't'HH:mmz", "yyyy-MM-dd'T'HH:mm'Z'", "yyyy-MM-dd't'HH:mm'z'", "yyyy-MM-dd", "yyyy-MM", "yyyy" };
/** /**
* The masks used to validate and parse the input to this Atom date. These are a lot more * The masks used to validate and parse the input to this Atom date. These are a lot more
@ -77,14 +67,18 @@ public class DateParser {
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static final String[] masks = { "yyyy-MM-dd'T'HH:mm:ss.SSSz", "yyyy-MM-dd't'HH:mm:ss.SSSz", // invalid private static final String[] masks = { "yyyy-MM-dd'T'HH:mm:ss.SSSz", "yyyy-MM-dd't'HH:mm:ss.SSSz", // invalid
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd't'HH:mm:ss.SSS'z'", // invalid "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd't'HH:mm:ss.SSS'z'", // invalid
"yyyy-MM-dd'T'HH:mm:ssz", "yyyy-MM-dd't'HH:mm:ssz", // invalid "yyyy-MM-dd'T'HH:mm:ssz", "yyyy-MM-dd't'HH:mm:ssz", // invalid
"yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd't'HH:mm:ss'z'", // invalid "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd't'HH:mm:ss'z'", // invalid
"yyyy-MM-dd'T'HH:mmz", // invalid "yyyy-MM-dd'T'HH:mmz", // invalid
"yyyy-MM-dd't'HH:mmz", // invalid "yyyy-MM-dd't'HH:mmz", // invalid
"yyyy-MM-dd'T'HH:mm'Z'", // invalid "yyyy-MM-dd'T'HH:mm'Z'", // invalid
"yyyy-MM-dd't'HH:mm'z'", // invalid "yyyy-MM-dd't'HH:mm'z'", // invalid
"yyyy-MM-dd", "yyyy-MM", "yyyy" }; "yyyy-MM-dd", "yyyy-MM", "yyyy" };
static {
ADDITIONAL_MASKS = PropertiesLoader.getPropertiesLoader().getTokenizedProperty("datetime.extra.masks", "|");
}
/** /**
* Private constructor to avoid DateParser instances creation. * Private constructor to avoid DateParser instances creation.
@ -182,9 +176,8 @@ public class DateParser {
* *
*/ */
public static Date parseW3CDateTime(String sDate, final Locale locale) { public static Date parseW3CDateTime(String sDate, final Locale locale) {
// if sDate has time on it, it injects 'GTM' before de TZ displacement // if sDate has time on it, it injects 'GTM' before de TZ displacement to allow the
// to // SimpleDateFormat parser to parse it properly
// allow the SimpleDateFormat parser to parse it properly
final int tIndex = sDate.indexOf("T"); final int tIndex = sDate.indexOf("T");
if (tIndex > -1) { if (tIndex > -1) {
if (sDate.endsWith("Z")) { if (sDate.endsWith("Z")) {
@ -219,14 +212,14 @@ public class DateParser {
* *
* */ * */
public static Date parseDate(final String sDate, final Locale locale) { public static Date parseDate(final String sDate, final Locale locale) {
Date d = parseW3CDateTime(sDate, locale); Date date = parseW3CDateTime(sDate, locale);
if (d == null) { if (date == null) {
d = parseRFC822(sDate, locale); date = parseRFC822(sDate, locale);
if (d == null && ADDITIONAL_MASKS.length > 0) { if (date == null && ADDITIONAL_MASKS.length > 0) {
d = parseUsingMask(ADDITIONAL_MASKS, sDate, locale); date = parseUsingMask(ADDITIONAL_MASKS, sDate, locale);
} }
} }
return d; return date;
} }
/** /**

View file

@ -70,14 +70,12 @@ public class FeedParsers extends PluginManager<WireFeedParser> {
*/ */
public WireFeedParser getParserFor(final Document document) { public WireFeedParser getParserFor(final Document document) {
final List<WireFeedParser> parsers = getPlugins(); final List<WireFeedParser> parsers = getPlugins();
WireFeedParser parser = null; for (final WireFeedParser parser : parsers) {
for (int i = 0; parser == null && i < parsers.size(); i++) { if (parser.isMyType(document)) {
parser = parsers.get(i); return parser;
if (!parser.isMyType(document)) {
parser = null;
} }
} }
return parser; return null;
} }
@Override @Override

View file

@ -27,9 +27,8 @@ import org.jdom2.Namespace;
import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.Module;
import com.sun.syndication.io.ModuleGenerator; import com.sun.syndication.io.ModuleGenerator;
/**
*/
public class ModuleGenerators extends PluginManager<ModuleGenerator> { public class ModuleGenerators extends PluginManager<ModuleGenerator> {
private Set<Namespace> allNamespaces; private Set<Namespace> allNamespaces;
public ModuleGenerators(final String propertyKey, final BaseWireFeedGenerator parentGenerator) { public ModuleGenerators(final String propertyKey, final BaseWireFeedGenerator parentGenerator) {
@ -51,8 +50,7 @@ public class ModuleGenerators extends PluginManager<ModuleGenerator> {
public void generateModules(final List<Module> modules, final Element element) { public void generateModules(final List<Module> modules, final Element element) {
final Map<String, ModuleGenerator> generators = getPluginMap(); final Map<String, ModuleGenerator> generators = getPluginMap();
for (int i = 0; i < modules.size(); i++) { for (final Module module : modules) {
final Module module = modules.get(i);
final String namespaceUri = module.getUri(); final String namespaceUri = module.getUri();
final ModuleGenerator generator = generators.get(namespaceUri); final ModuleGenerator generator = generators.get(namespaceUri);
if (generator != null) { if (generator != null) {
@ -65,11 +63,12 @@ public class ModuleGenerators extends PluginManager<ModuleGenerator> {
if (allNamespaces == null) { if (allNamespaces == null) {
allNamespaces = new HashSet<Namespace>(); allNamespaces = new HashSet<Namespace>();
final List<String> mUris = getModuleNamespaces(); final List<String> mUris = getModuleNamespaces();
for (int i = 0; i < mUris.size(); i++) { for (final String mUri : mUris) {
final ModuleGenerator mGen = getGenerator(mUris.get(i)); final ModuleGenerator mGen = getGenerator(mUri);
allNamespaces.addAll(mGen.getNamespaces()); allNamespaces.addAll(mGen.getNamespaces());
} }
} }
return allNamespaces; return allNamespaces;
} }
} }

View file

@ -16,13 +16,13 @@
*/ */
package com.sun.syndication.io.impl; package com.sun.syndication.io.impl;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import org.jdom2.Element; import org.jdom2.Element;
import org.jdom2.Namespace; import org.jdom2.Namespace;
import com.rometools.utils.Lists;
import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.Module;
import com.sun.syndication.io.ModuleParser; import com.sun.syndication.io.ModuleParser;
import com.sun.syndication.io.WireFeedParser; import com.sun.syndication.io.WireFeedParser;
@ -30,6 +30,7 @@ import com.sun.syndication.io.WireFeedParser;
/** /**
*/ */
public class ModuleParsers extends PluginManager<ModuleParser> { public class ModuleParsers extends PluginManager<ModuleParser> {
public ModuleParsers(final String propertyKey, final WireFeedParser parentParser) { public ModuleParsers(final String propertyKey, final WireFeedParser parentParser) {
super(propertyKey, parentParser, null); super(propertyKey, parentParser, null);
} }
@ -46,16 +47,13 @@ public class ModuleParsers extends PluginManager<ModuleParser> {
public List<Module> parseModules(final Element root, final Locale locale) { public List<Module> parseModules(final Element root, final Locale locale) {
final List<ModuleParser> parsers = getPlugins(); final List<ModuleParser> parsers = getPlugins();
List<Module> modules = null; List<Module> modules = null;
for (int i = 0; i < parsers.size(); i++) { for (final ModuleParser parser : parsers) {
final ModuleParser parser = parsers.get(i);
final String namespaceUri = parser.getNamespaceUri(); final String namespaceUri = parser.getNamespaceUri();
final Namespace namespace = Namespace.getNamespace(namespaceUri); final Namespace namespace = Namespace.getNamespace(namespaceUri);
if (hasElementsFrom(root, namespace)) { if (hasElementsFrom(root, namespace)) {
final Module module = parser.parse(root, locale); final Module module = parser.parse(root, locale);
if (module != null) { if (module != null) {
if (modules == null) { modules = Lists.createWhenNull(modules);
modules = new ArrayList<Module>();
}
modules.add(module); modules.add(module);
} }
} }
@ -65,15 +63,14 @@ public class ModuleParsers extends PluginManager<ModuleParser> {
private boolean hasElementsFrom(final Element root, final Namespace namespace) { private boolean hasElementsFrom(final Element root, final Namespace namespace) {
boolean hasElements = false; boolean hasElements = false;
// boolean hasElements = namespace.equals(root.getNamespace()); for (final Element child : root.getChildren()) {
final Namespace childNamespace = child.getNamespace();
if (!hasElements) { if (namespace.equals(childNamespace)) {
final List<Element> children = root.getChildren(); hasElements = true;
for (int i = 0; !hasElements && i < children.size(); i++) { break;
final Element child = children.get(i);
hasElements = namespace.equals(child.getNamespace());
} }
} }
return hasElements; return hasElements;
} }
} }

View file

@ -37,16 +37,17 @@ import com.sun.syndication.io.WireFeedParser;
* *
*/ */
public abstract class PluginManager<T> { public abstract class PluginManager<T> {
private final String[] propertyValues; private final String[] propertyValues;
private Map<String, T> pluginsMap;
private List<T> pluginsList;
private final List<String> keys; private final List<String> keys;
private final WireFeedParser parentParser; private final WireFeedParser parentParser;
private final WireFeedGenerator parentGenerator; private final WireFeedGenerator parentGenerator;
private Map<String, T> pluginsMap;
private List<T> pluginsList;
/** /**
* Creates a PluginManager * Creates a PluginManager
* <p>
* *
* @param propertyKey property key defining the plugins classes * @param propertyKey property key defining the plugins classes
* *
@ -86,25 +87,32 @@ public abstract class PluginManager<T> {
// PRIVATE - LOADER PART // PRIVATE - LOADER PART
private void loadPlugins() { private void loadPlugins() {
final List<T> finalPluginsList = new ArrayList<T>(); final List<T> finalPluginsList = new ArrayList<T>();
pluginsList = new ArrayList<T>(); pluginsList = new ArrayList<T>();
pluginsMap = new HashMap<String, T>(); pluginsMap = new HashMap<String, T>();
String className = null; String className = null;
try { try {
final Class<T>[] classes = getClasses(); final Class<T>[] classes = getClasses();
for (final Class<T> classe : classes) { for (final Class<T> clazz : classes) {
className = classe.getName();
final T plugin = classe.newInstance(); className = clazz.getName();
final T plugin = clazz.newInstance();
if (plugin instanceof DelegatingModuleParser) { if (plugin instanceof DelegatingModuleParser) {
((DelegatingModuleParser) plugin).setFeedParser(parentParser); ((DelegatingModuleParser) plugin).setFeedParser(parentParser);
} }
if (plugin instanceof DelegatingModuleGenerator) { if (plugin instanceof DelegatingModuleGenerator) {
((DelegatingModuleGenerator) plugin).setFeedGenerator(parentGenerator); ((DelegatingModuleGenerator) plugin).setFeedGenerator(parentGenerator);
} }
pluginsMap.put(getKey(plugin), plugin); pluginsMap.put(getKey(plugin), plugin);
// to preserve the order of definition in the rome.properties files // to preserve the order of definition in the rome.properties files
pluginsList.add(plugin); pluginsList.add(plugin);
} }
final Collection<T> plugins = pluginsMap.values(); final Collection<T> plugins = pluginsMap.values();
@ -143,9 +151,13 @@ public abstract class PluginManager<T> {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Class<T>[] getClasses() throws ClassNotFoundException { private Class<T>[] getClasses() throws ClassNotFoundException {
final ClassLoader classLoader = ConfigurableClassLoader.INSTANCE.getClassLoader(); final ClassLoader classLoader = ConfigurableClassLoader.INSTANCE.getClassLoader();
final List<Class<T>> classes = new ArrayList<Class<T>>(); final List<Class<T>> classes = new ArrayList<Class<T>>();
final boolean useLoadClass = Boolean.valueOf(System.getProperty("rome.pluginmanager.useloadclass", "false")).booleanValue(); final boolean useLoadClass = Boolean.valueOf(System.getProperty("rome.pluginmanager.useloadclass", "false")).booleanValue();
for (final String propertyValue : propertyValues) { for (final String propertyValue : propertyValues) {
final Class<T> mClass; final Class<T> mClass;
if (useLoadClass) { if (useLoadClass) {
@ -155,6 +167,7 @@ public abstract class PluginManager<T> {
} }
classes.add(mClass); classes.add(mClass);
} }
final Class<T>[] array = new Class[classes.size()]; final Class<T>[] array = new Class[classes.size()];
classes.toArray(array); classes.toArray(array);
return array; return array;

View file

@ -17,7 +17,6 @@
package com.sun.syndication.io.impl; package com.sun.syndication.io.impl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -56,44 +55,40 @@ public class RSS090Parser extends BaseWireFeedParser {
@Override @Override
public boolean isMyType(final Document document) { public boolean isMyType(final Document document) {
boolean ok = false;
final Element rssRoot = document.getRootElement(); final Element rssRoot = document.getRootElement();
final Namespace defaultNS = rssRoot.getNamespace(); final Namespace defaultNS = rssRoot.getNamespace();
final List<Namespace> additionalNSs = rssRoot.getAdditionalNamespaces(); final List<Namespace> additionalNSs = rssRoot.getAdditionalNamespaces();
ok = defaultNS != null && defaultNS.equals(getRDFNamespace()); boolean myType = false;
if (ok) { if (defaultNS != null && defaultNS.equals(getRDFNamespace()) && additionalNSs != null) {
if (additionalNSs == null) { for (final Namespace namespace : additionalNSs) {
ok = false; if (getRSSNamespace().equals(namespace)) {
} else { myType = true;
ok = false; break;
for (int i = 0; !ok && i < additionalNSs.size(); i++) {
ok = getRSSNamespace().equals(additionalNSs.get(i));
} }
} }
} }
return ok; return myType;
} }
@Override @Override
public WireFeed parse(final Document document, final boolean validate, final Locale locale) throws IllegalArgumentException, FeedException { public WireFeed parse(final Document document, final boolean validate, final Locale locale) throws IllegalArgumentException, FeedException {
if (validate) { if (validate) {
validateFeed(document); validateFeed(document);
} }
final Element rssRoot = document.getRootElement(); final Element rssRoot = document.getRootElement();
return parseChannel(rssRoot, locale); return parseChannel(rssRoot, locale);
} }
protected void validateFeed(final Document document) throws FeedException { protected void validateFeed(final Document document) throws FeedException {
// TBD // TODO here we have to validate the Feed against a schema or whatever not sure how to do it
// here we have to validate the Feed against a schema or whatever // one posibility would be to inject our own schema for the feed (they don't exist out
// not sure how to do it // there) to the document, produce an ouput and attempt to parse it again with validation
// one posibility would be to inject our own schema for the feed (they // turned on. otherwise will have to check the document elements by hand.
// don't exist out there)
// to the document, produce an ouput and attempt to parse it again with
// validation turned on.
// otherwise will have to check the document elements by hand.
} }
/** /**
@ -147,41 +142,46 @@ public class RSS090Parser extends BaseWireFeedParser {
* @return the parsed Channel bean. * @return the parsed Channel bean.
*/ */
protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { protected WireFeed parseChannel(final Element rssRoot, final Locale locale) {
final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());
final Channel channel = new Channel(getType()); final Channel channel = new Channel(getType());
channel.setStyleSheet(getStyleSheet(rssRoot.getDocument())); channel.setStyleSheet(getStyleSheet(rssRoot.getDocument()));
Element e = eChannel.getChild("title", getRSSNamespace()); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());
if (e != null) {
channel.setTitle(e.getText()); final Element title = eChannel.getChild("title", getRSSNamespace());
if (title != null) {
channel.setTitle(title.getText());
} }
e = eChannel.getChild("link", getRSSNamespace());
if (e != null) { final Element link = eChannel.getChild("link", getRSSNamespace());
channel.setLink(e.getText()); if (link != null) {
channel.setLink(link.getText());
} }
e = eChannel.getChild("description", getRSSNamespace());
if (e != null) { final Element description = eChannel.getChild("description", getRSSNamespace());
channel.setDescription(e.getText()); if (description != null) {
channel.setDescription(description.getText());
} }
channel.setImage(parseImage(rssRoot)); channel.setImage(parseImage(rssRoot));
channel.setTextInput(parseTextInput(rssRoot)); channel.setTextInput(parseTextInput(rssRoot));
// Unfortunately Microsoft's SSE extension has a special case of // Unfortunately Microsoft's SSE extension has a special case of effectively putting the
// effectively putting the sharing channel module inside the RSS tag // sharing channel module inside the RSS tag and not inside the channel itself. So we also
// and not inside the channel itself. So we also need to look for // need to look for channel modules from the root RSS element.
// channel modules from the root RSS element.
final List<Module> allFeedModules = new ArrayList<Module>(); final List<Module> allFeedModules = new ArrayList<Module>();
final List<Module> rootModules = parseFeedModules(rssRoot, locale); final List<Module> rootModules = parseFeedModules(rssRoot, locale);
final List<Module> channelModules = parseFeedModules(eChannel, locale); final List<Module> channelModules = parseFeedModules(eChannel, locale);
if (rootModules != null) { if (rootModules != null) {
allFeedModules.addAll(rootModules); allFeedModules.addAll(rootModules);
} }
if (channelModules != null) { if (channelModules != null) {
allFeedModules.addAll(channelModules); allFeedModules.addAll(channelModules);
} }
channel.setModules(allFeedModules); channel.setModules(allFeedModules);
channel.setItems(parseItems(rssRoot, locale)); channel.setItems(parseItems(rssRoot, locale));
@ -189,7 +189,9 @@ public class RSS090Parser extends BaseWireFeedParser {
if (!foreignMarkup.isEmpty()) { if (!foreignMarkup.isEmpty()) {
channel.setForeignMarkup(foreignMarkup); channel.setForeignMarkup(foreignMarkup);
} }
return channel; return channel;
} }
/** /**
@ -232,25 +234,33 @@ public class RSS090Parser extends BaseWireFeedParser {
* @return the parsed image bean. * @return the parsed image bean.
*/ */
protected Image parseImage(final Element rssRoot) { protected Image parseImage(final Element rssRoot) {
Image image = null; Image image = null;
final Element eImage = getImage(rssRoot); final Element eImage = getImage(rssRoot);
if (eImage != null) { if (eImage != null) {
image = new Image(); image = new Image();
Element e = eImage.getChild("title", getRSSNamespace()); final Element title = eImage.getChild("title", getRSSNamespace());
if (e != null) { if (title != null) {
image.setTitle(e.getText()); image.setTitle(title.getText());
} }
e = eImage.getChild("url", getRSSNamespace());
if (e != null) { final Element url = eImage.getChild("url", getRSSNamespace());
image.setUrl(e.getText()); if (url != null) {
image.setUrl(url.getText());
} }
e = eImage.getChild("link", getRSSNamespace());
if (e != null) { final Element link = eImage.getChild("link", getRSSNamespace());
image.setLink(e.getText()); if (link != null) {
image.setLink(link.getText());
} }
} }
return image; return image;
} }
/** /**
@ -265,12 +275,9 @@ public class RSS090Parser extends BaseWireFeedParser {
* @return a list with all the parsed RSSItem beans. * @return a list with all the parsed RSSItem beans.
*/ */
protected List<Item> parseItems(final Element rssRoot, final Locale locale) { protected List<Item> parseItems(final Element rssRoot, final Locale locale) {
final Collection<Element> eItems = getItems(rssRoot);
final List<Item> items = new ArrayList<Item>(); final List<Item> items = new ArrayList<Item>();
for (final Element element : eItems) { for (final Element item : getItems(rssRoot)) {
final Element eItem = element; items.add(parseItem(rssRoot, item, locale));
items.add(parseItem(rssRoot, eItem, locale));
} }
return items; return items;
} }
@ -286,29 +293,32 @@ public class RSS090Parser extends BaseWireFeedParser {
* @return the parsed RSSItem bean. * @return the parsed RSSItem bean.
*/ */
protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) {
final Item item = new Item(); final Item item = new Item();
Element e = eItem.getChild("title", getRSSNamespace());
if (e != null) { final Element title = eItem.getChild("title", getRSSNamespace());
item.setTitle(e.getText()); if (title != null) {
item.setTitle(title.getText());
} }
e = eItem.getChild("link", getRSSNamespace());
if (e != null) { final Element link = eItem.getChild("link", getRSSNamespace());
item.setLink(e.getText()); if (link != null) {
item.setUri(e.getText()); item.setLink(link.getText());
item.setUri(link.getText());
} }
item.setModules(parseItemModules(eItem, locale)); item.setModules(parseItemModules(eItem, locale));
final List<Element> foreignMarkup = extractForeignMarkup(eItem, item, getRSSNamespace()); final List<Element> foreignMarkup = extractForeignMarkup(eItem, item, getRSSNamespace());
// content:encoded elements are treated special, without a module, they // content:encoded elements are treated special, without a module, they have to be removed
// have to be removed from the foreign // from the foreign markup to avoid duplication in case of read/write. Note that this fix
// markup to avoid duplication in case of read/write. Note that this fix // will break if a content module is used
// will break if a content module is
// used
final Iterator<Element> iterator = foreignMarkup.iterator(); final Iterator<Element> iterator = foreignMarkup.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
final Element element = iterator.next(); final Element element = iterator.next();
if (getContentNamespace().equals(element.getNamespace()) && element.getName().equals("encoded")) { final Namespace eNamespace = element.getNamespace();
final String eName = element.getName();
if (getContentNamespace().equals(eNamespace) && eName.equals("encoded")) {
iterator.remove(); iterator.remove();
} }
} }
@ -330,28 +340,38 @@ public class RSS090Parser extends BaseWireFeedParser {
* @return the parsed RSSTextInput bean. * @return the parsed RSSTextInput bean.
*/ */
protected TextInput parseTextInput(final Element rssRoot) { protected TextInput parseTextInput(final Element rssRoot) {
TextInput textInput = null; TextInput textInput = null;
final Element eTextInput = getTextInput(rssRoot); final Element eTextInput = getTextInput(rssRoot);
if (eTextInput != null) { if (eTextInput != null) {
textInput = new TextInput(); textInput = new TextInput();
Element e = eTextInput.getChild("title", getRSSNamespace());
if (e != null) { final Element title = eTextInput.getChild("title", getRSSNamespace());
textInput.setTitle(e.getText()); if (title != null) {
textInput.setTitle(title.getText());
} }
e = eTextInput.getChild("description", getRSSNamespace());
if (e != null) { final Element description = eTextInput.getChild("description", getRSSNamespace());
textInput.setDescription(e.getText()); if (description != null) {
textInput.setDescription(description.getText());
} }
e = eTextInput.getChild("name", getRSSNamespace());
if (e != null) { final Element name = eTextInput.getChild("name", getRSSNamespace());
textInput.setName(e.getText()); if (name != null) {
textInput.setName(name.getText());
} }
e = eTextInput.getChild("link", getRSSNamespace());
if (e != null) { final Element link = eTextInput.getChild("link", getRSSNamespace());
textInput.setLink(e.getText()); if (link != null) {
textInput.setLink(link.getText());
} }
} }
return textInput; return textInput;
} }
} }

View file

@ -21,8 +21,6 @@ import org.jdom2.DocType;
import org.jdom2.Document; import org.jdom2.Document;
import org.jdom2.Element; import org.jdom2.Element;
/**
*/
public class RSS091NetscapeParser extends RSS091UserlandParser { public class RSS091NetscapeParser extends RSS091UserlandParser {
public RSS091NetscapeParser() { public RSS091NetscapeParser() {
@ -39,27 +37,15 @@ public class RSS091NetscapeParser extends RSS091UserlandParser {
@Override @Override
public boolean isMyType(final Document document) { public boolean isMyType(final Document document) {
boolean ok = false;
final Element rssRoot = document.getRootElement();
ok = rssRoot.getName().equals("rss");
if (ok) {
ok = false;
final Attribute version = rssRoot.getAttribute("version");
if (version != null) {
ok = version.getValue().equals(getRSSVersion());
if (ok) {
ok = false;
final DocType docType = document.getDocType();
if (docType != null) { final Element rssRoot = document.getRootElement();
ok = ELEMENT_NAME.equals(docType.getElementName()); final String name = rssRoot.getName();
ok = ok && PUBLIC_ID.equals(docType.getPublicID()); final Attribute version = rssRoot.getAttribute("version");
ok = ok && SYSTEM_ID.equals(docType.getSystemID()); final DocType docType = document.getDocType();
}
} return name.equals(ELEMENT_NAME) && version != null && version.getValue().equals(getRSSVersion()) && docType != null
} && ELEMENT_NAME.equals(docType.getElementName()) && PUBLIC_ID.equals(docType.getPublicID()) && SYSTEM_ID.equals(docType.getSystemID());
}
return ok;
} }
@Override @Override

View file

@ -26,6 +26,7 @@ import org.jdom2.Element;
import org.jdom2.Namespace; import org.jdom2.Namespace;
import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Content;
import com.sun.syndication.feed.rss.Description; import com.sun.syndication.feed.rss.Description;
import com.sun.syndication.feed.rss.Image; import com.sun.syndication.feed.rss.Image;
import com.sun.syndication.feed.rss.Item; import com.sun.syndication.feed.rss.Item;
@ -39,6 +40,7 @@ import com.sun.syndication.io.FeedException;
* *
*/ */
public class RSS091UserlandGenerator extends RSS090Generator { public class RSS091UserlandGenerator extends RSS090Generator {
private final String version; private final String version;
public RSS091UserlandGenerator() { public RSS091UserlandGenerator() {
@ -68,6 +70,7 @@ public class RSS091UserlandGenerator extends RSS090Generator {
@Override @Override
protected void addChannel(final Channel channel, final Element parent) throws FeedException { protected void addChannel(final Channel channel, final Element parent) throws FeedException {
super.addChannel(channel, parent); super.addChannel(channel, parent);
final Element eChannel = parent.getChild("channel", getFeedNamespace()); final Element eChannel = parent.getChild("channel", getFeedNamespace());
@ -75,15 +78,16 @@ public class RSS091UserlandGenerator extends RSS090Generator {
addImage(channel, eChannel); addImage(channel, eChannel);
addTextInput(channel, eChannel); addTextInput(channel, eChannel);
addItems(channel, eChannel); addItems(channel, eChannel);
} }
@Override @Override
protected void checkChannelConstraints(final Element eChannel) throws FeedException { protected void checkChannelConstraints(final Element eChannel) throws FeedException {
checkNotNullAndLength(eChannel, "title", 1, 100); checkNotNullAndLength(eChannel, "title", 1, 100);
checkNotNullAndLength(eChannel, "description", 1, 500); checkNotNullAndLength(eChannel, "description", 1, 500);
checkNotNullAndLength(eChannel, "link", 1, 500); checkNotNullAndLength(eChannel, "link", 1, 500);
checkNotNullAndLength(eChannel, "language", 2, 5); checkNotNullAndLength(eChannel, "language", 2, 5);
checkLength(eChannel, "rating", 20, 500); checkLength(eChannel, "rating", 20, 500);
checkLength(eChannel, "copyright", 1, 100); checkLength(eChannel, "copyright", 1, 100);
checkLength(eChannel, "pubDate", 1, 100); checkLength(eChannel, "pubDate", 1, 100);
@ -95,10 +99,10 @@ public class RSS091UserlandGenerator extends RSS090Generator {
final Element skipHours = eChannel.getChild("skipHours"); final Element skipHours = eChannel.getChild("skipHours");
if (skipHours != null) { if (skipHours != null) {
final List<Element> hours = skipHours.getChildren();
for (int i = 0; i < hours.size(); i++) { final List<Element> hours = skipHours.getChildren();
final Element hour = hours.get(i); for (final Element hour : hours) {
final int value = Integer.parseInt(hour.getText().trim()); final int value = Integer.parseInt(hour.getText().trim());
if (isHourFormat24()) { if (isHourFormat24()) {
@ -110,15 +114,17 @@ public class RSS091UserlandGenerator extends RSS090Generator {
throw new FeedException("Invalid hour value " + value + ", it must be between 0 and 23"); throw new FeedException("Invalid hour value " + value + ", it must be between 0 and 23");
} }
} }
} }
} }
} }
@Override @Override
protected void checkImageConstraints(final Element eImage) throws FeedException { protected void checkImageConstraints(final Element eImage) throws FeedException {
checkNotNullAndLength(eImage, "title", 1, 100); checkNotNullAndLength(eImage, "title", 1, 100);
checkNotNullAndLength(eImage, "url", 1, 500); checkNotNullAndLength(eImage, "url", 1, 500);
checkLength(eImage, "link", 1, 500); checkLength(eImage, "link", 1, 500);
checkLength(eImage, "width", 1, 3); checkLength(eImage, "width", 1, 3);
checkLength(eImage, "width", 1, 3); checkLength(eImage, "width", 1, 3);
@ -129,7 +135,6 @@ public class RSS091UserlandGenerator extends RSS090Generator {
protected void checkItemConstraints(final Element eItem) throws FeedException { protected void checkItemConstraints(final Element eItem) throws FeedException {
checkNotNullAndLength(eItem, "title", 1, 100); checkNotNullAndLength(eItem, "title", 1, 100);
checkNotNullAndLength(eItem, "link", 1, 500); checkNotNullAndLength(eItem, "link", 1, 500);
checkLength(eItem, "description", 1, 500); checkLength(eItem, "description", 1, 500);
} }
@ -153,93 +158,80 @@ public class RSS091UserlandGenerator extends RSS090Generator {
root.setAttribute(version); root.setAttribute(version);
root.addNamespaceDeclaration(getContentNamespace()); root.addNamespaceDeclaration(getContentNamespace());
generateModuleNamespaceDefs(root); generateModuleNamespaceDefs(root);
return root; return root;
} }
protected Element generateSkipDaysElement(final List<String> days) { protected Element generateSkipDaysElement(final List<String> days) {
final Element skipDaysElement = new Element("skipDays"); final Element skipDaysElement = new Element("skipDays");
for (final String day : days) {
for (int i = 0; i < days.size(); i++) { skipDaysElement.addContent(generateSimpleElement("day", day.toString()));
skipDaysElement.addContent(generateSimpleElement("day", days.get(i).toString()));
} }
return skipDaysElement; return skipDaysElement;
} }
protected Element generateSkipHoursElement(final List<Integer> hours) { protected Element generateSkipHoursElement(final List<Integer> hours) {
final Element skipHoursElement = new Element("skipHours", getFeedNamespace()); final Element skipHoursElement = new Element("skipHours", getFeedNamespace());
for (final Integer hour : hours) {
for (int i = 0; i < hours.size(); i++) { skipHoursElement.addContent(generateSimpleElement("hour", hour.toString()));
skipHoursElement.addContent(generateSimpleElement("hour", hours.get(i).toString()));
} }
return skipHoursElement; return skipHoursElement;
} }
@Override @Override
protected void populateChannel(final Channel channel, final Element eChannel) { protected void populateChannel(final Channel channel, final Element eChannel) {
super.populateChannel(channel, eChannel); super.populateChannel(channel, eChannel);
final String language = channel.getLanguage(); final String language = channel.getLanguage();
if (language != null) { if (language != null) {
eChannel.addContent(generateSimpleElement("language", language)); eChannel.addContent(generateSimpleElement("language", language));
} }
final String rating = channel.getRating(); final String rating = channel.getRating();
if (rating != null) { if (rating != null) {
eChannel.addContent(generateSimpleElement("rating", rating)); eChannel.addContent(generateSimpleElement("rating", rating));
} }
final String copyright = channel.getCopyright(); final String copyright = channel.getCopyright();
if (copyright != null) { if (copyright != null) {
eChannel.addContent(generateSimpleElement("copyright", copyright)); eChannel.addContent(generateSimpleElement("copyright", copyright));
} }
final Date pubDate = channel.getPubDate(); final Date pubDate = channel.getPubDate();
if (pubDate != null) { if (pubDate != null) {
eChannel.addContent(generateSimpleElement("pubDate", DateParser.formatRFC822(pubDate, Locale.US))); eChannel.addContent(generateSimpleElement("pubDate", DateParser.formatRFC822(pubDate, Locale.US)));
} }
final Date lastBuildDate = channel.getLastBuildDate(); final Date lastBuildDate = channel.getLastBuildDate();
if (lastBuildDate != null) { if (lastBuildDate != null) {
eChannel.addContent(generateSimpleElement("lastBuildDate", DateParser.formatRFC822(lastBuildDate, Locale.US))); eChannel.addContent(generateSimpleElement("lastBuildDate", DateParser.formatRFC822(lastBuildDate, Locale.US)));
} }
final String docs = channel.getDocs(); final String docs = channel.getDocs();
if (docs != null) { if (docs != null) {
eChannel.addContent(generateSimpleElement("docs", docs)); eChannel.addContent(generateSimpleElement("docs", docs));
} }
final String managingEditor = channel.getManagingEditor(); final String managingEditor = channel.getManagingEditor();
if (managingEditor != null) { if (managingEditor != null) {
eChannel.addContent(generateSimpleElement("managingEditor", managingEditor)); eChannel.addContent(generateSimpleElement("managingEditor", managingEditor));
} }
final String webMaster = channel.getWebMaster(); final String webMaster = channel.getWebMaster();
if (webMaster != null) { if (webMaster != null) {
eChannel.addContent(generateSimpleElement("webMaster", webMaster)); eChannel.addContent(generateSimpleElement("webMaster", webMaster));
} }
final List<Integer> skipHours = channel.getSkipHours(); final List<Integer> skipHours = channel.getSkipHours();
if (skipHours != null && !skipHours.isEmpty()) { if (skipHours != null && !skipHours.isEmpty()) {
eChannel.addContent(generateSkipHoursElement(skipHours)); eChannel.addContent(generateSkipHoursElement(skipHours));
} }
final List<String> skipDays = channel.getSkipDays(); final List<String> skipDays = channel.getSkipDays();
if (skipDays != null && !skipDays.isEmpty()) { if (skipDays != null && !skipDays.isEmpty()) {
eChannel.addContent(generateSkipDaysElement(skipDays)); eChannel.addContent(generateSkipDaysElement(skipDays));
} }
} }
@Override @Override
@ -249,41 +241,44 @@ public class RSS091UserlandGenerator extends RSS090Generator {
@Override @Override
protected void populateImage(final Image image, final Element eImage) { protected void populateImage(final Image image, final Element eImage) {
super.populateImage(image, eImage); super.populateImage(image, eImage);
final Integer width = image.getWidth(); final Integer width = image.getWidth();
if (width != null) { if (width != null) {
eImage.addContent(generateSimpleElement("width", String.valueOf(width))); eImage.addContent(generateSimpleElement("width", String.valueOf(width)));
} }
final Integer height = image.getHeight(); final Integer height = image.getHeight();
if (height != null) { if (height != null) {
eImage.addContent(generateSimpleElement("height", String.valueOf(height))); eImage.addContent(generateSimpleElement("height", String.valueOf(height)));
} }
final String description = image.getDescription(); final String description = image.getDescription();
if (description != null) { if (description != null) {
eImage.addContent(generateSimpleElement("description", description)); eImage.addContent(generateSimpleElement("description", description));
} }
} }
@Override @Override
protected void populateItem(final Item item, final Element eItem, final int index) { protected void populateItem(final Item item, final Element eItem, final int index) {
super.populateItem(item, eItem, index); super.populateItem(item, eItem, index);
final Description description = item.getDescription(); final Description description = item.getDescription();
if (description != null) { if (description != null) {
eItem.addContent(generateSimpleElement("description", description.getValue())); eItem.addContent(generateSimpleElement("description", description.getValue()));
} }
if (item.getModule(getContentNamespace().getURI()) == null && item.getContent() != null) { final Namespace contentNamespace = getContentNamespace();
final Element elem = new Element("encoded", getContentNamespace()); final Content content = item.getContent();
elem.addContent(item.getContent().getValue()); if (item.getModule(contentNamespace.getURI()) == null && content != null) {
final Element elem = new Element("encoded", contentNamespace);
elem.addContent(content.getValue());
eItem.addContent(elem); eItem.addContent(elem);
} }
} }
} }

View file

@ -47,17 +47,9 @@ public class RSS091UserlandParser extends RSS090Parser {
@Override @Override
public boolean isMyType(final Document document) { public boolean isMyType(final Document document) {
boolean ok;
final Element rssRoot = document.getRootElement(); final Element rssRoot = document.getRootElement();
ok = rssRoot.getName().equals("rss"); final Attribute version = rssRoot.getAttribute("version");
if (ok) { return rssRoot.getName().equals("rss") && version != null && version.getValue().equals(getRSSVersion());
ok = false;
final Attribute version = rssRoot.getAttribute("version");
if (version != null) {
ok = version.getValue().equals(getRSSVersion());
}
}
return ok;
} }
protected String getRSSVersion() { protected String getRSSVersion() {
@ -88,67 +80,76 @@ public class RSS091UserlandParser extends RSS090Parser {
*/ */
@Override @Override
protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { protected WireFeed parseChannel(final Element rssRoot, final Locale locale) {
final Channel channel = (Channel) super.parseChannel(rssRoot, locale); final Channel channel = (Channel) super.parseChannel(rssRoot, locale);
final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());
Element e = eChannel.getChild("language", getRSSNamespace()); final Element language = eChannel.getChild("language", getRSSNamespace());
if (e != null) { if (language != null) {
channel.setLanguage(e.getText()); channel.setLanguage(language.getText());
} }
e = eChannel.getChild("rating", getRSSNamespace());
if (e != null) { final Element atinge = eChannel.getChild("rating", getRSSNamespace());
channel.setRating(e.getText()); if (atinge != null) {
channel.setRating(atinge.getText());
} }
e = eChannel.getChild("copyright", getRSSNamespace());
if (e != null) { final Element copyright = eChannel.getChild("copyright", getRSSNamespace());
channel.setCopyright(e.getText()); if (copyright != null) {
channel.setCopyright(copyright.getText());
} }
e = eChannel.getChild("pubDate", getRSSNamespace());
if (e != null) { final Element pubDate = eChannel.getChild("pubDate", getRSSNamespace());
channel.setPubDate(DateParser.parseDate(e.getText(), locale)); if (pubDate != null) {
channel.setPubDate(DateParser.parseDate(pubDate.getText(), locale));
} }
e = eChannel.getChild("lastBuildDate", getRSSNamespace());
if (e != null) { final Element lastBuildDate = eChannel.getChild("lastBuildDate", getRSSNamespace());
channel.setLastBuildDate(DateParser.parseDate(e.getText(), locale)); if (lastBuildDate != null) {
channel.setLastBuildDate(DateParser.parseDate(lastBuildDate.getText(), locale));
} }
e = eChannel.getChild("docs", getRSSNamespace());
if (e != null) { final Element docs = eChannel.getChild("docs", getRSSNamespace());
channel.setDocs(e.getText()); if (docs != null) {
channel.setDocs(docs.getText());
} }
e = eChannel.getChild("generator", getRSSNamespace());
if (e != null) { final Element generator = eChannel.getChild("generator", getRSSNamespace());
channel.setGenerator(e.getText()); if (generator != null) {
channel.setGenerator(generator.getText());
} }
e = eChannel.getChild("managingEditor", getRSSNamespace());
if (e != null) { final Element managingEditor = eChannel.getChild("managingEditor", getRSSNamespace());
channel.setManagingEditor(e.getText()); if (managingEditor != null) {
channel.setManagingEditor(managingEditor.getText());
} }
e = eChannel.getChild("webMaster", getRSSNamespace());
if (e != null) { final Element webMaster = eChannel.getChild("webMaster", getRSSNamespace());
channel.setWebMaster(e.getText()); if (webMaster != null) {
channel.setWebMaster(webMaster.getText());
} }
e = eChannel.getChild("skipHours");
if (e != null) { final Element eSkipHours = eChannel.getChild("skipHours");
if (eSkipHours != null) {
final List<Integer> skipHours = new ArrayList<Integer>(); final List<Integer> skipHours = new ArrayList<Integer>();
final List<Element> eHours = e.getChildren("hour", getRSSNamespace()); final List<Element> eHours = eSkipHours.getChildren("hour", getRSSNamespace());
for (int i = 0; i < eHours.size(); i++) { for (final Element eHour : eHours) {
final Element eHour = eHours.get(i);
skipHours.add(new Integer(eHour.getText().trim())); skipHours.add(new Integer(eHour.getText().trim()));
} }
channel.setSkipHours(skipHours); channel.setSkipHours(skipHours);
} }
e = eChannel.getChild("skipDays"); final Element eSkipDays = eChannel.getChild("skipDays");
if (e != null) { if (eSkipDays != null) {
final List<String> skipDays = new ArrayList<String>(); final List<String> skipDays = new ArrayList<String>();
final List<Element> eDays = e.getChildren("day", getRSSNamespace()); final List<Element> eDays = eSkipDays.getChildren("day", getRSSNamespace());
for (int i = 0; i < eDays.size(); i++) { for (final Element eDay : eDays) {
final Element eDay = eDays.get(i);
skipDays.add(eDay.getText().trim()); skipDays.add(eDay.getText().trim());
} }
channel.setSkipDays(skipDays); channel.setSkipDays(skipDays);
} }
return channel; return channel;
} }
@ -164,29 +165,37 @@ public class RSS091UserlandParser extends RSS090Parser {
*/ */
@Override @Override
protected Image parseImage(final Element rssRoot) { protected Image parseImage(final Element rssRoot) {
final Image image = super.parseImage(rssRoot); final Image image = super.parseImage(rssRoot);
if (image != null) { if (image != null) {
final Element eImage = getImage(rssRoot); final Element eImage = getImage(rssRoot);
Element e = eImage.getChild("width", getRSSNamespace());
if (e != null) { final Element width = eImage.getChild("width", getRSSNamespace());
final Integer val = NumberParser.parseInt(e.getText()); if (width != null) {
final Integer val = NumberParser.parseInt(width.getText());
if (val != null) { if (val != null) {
image.setWidth(val.intValue()); image.setWidth(val);
} }
} }
e = eImage.getChild("height", getRSSNamespace());
if (e != null) { final Element height = eImage.getChild("height", getRSSNamespace());
final Integer val = NumberParser.parseInt(e.getText()); if (height != null) {
final Integer val = NumberParser.parseInt(height.getText());
if (val != null) { if (val != null) {
image.setHeight(val.intValue()); image.setHeight(val);
} }
} }
e = eImage.getChild("description", getRSSNamespace());
if (e != null) { final Element description = eImage.getChild("description", getRSSNamespace());
image.setDescription(e.getText()); if (description != null) {
image.setDescription(description.getText());
} }
} }
return image; return image;
} }
/** /**
@ -194,13 +203,15 @@ public class RSS091UserlandParser extends RSS090Parser {
*/ */
@Override @Override
protected List<Element> getItems(final Element rssRoot) { protected List<Element> getItems(final Element rssRoot) {
final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());
final List<Element> emptyList = Collections.emptyList();
if (eChannel != null) { if (eChannel != null) {
return eChannel.getChildren("item", getRSSNamespace()); return eChannel.getChildren("item", getRSSNamespace());
} else { } else {
return emptyList; return Collections.emptyList();
} }
} }
/** /**
@ -208,12 +219,15 @@ public class RSS091UserlandParser extends RSS090Parser {
*/ */
@Override @Override
protected Element getImage(final Element rssRoot) { protected Element getImage(final Element rssRoot) {
final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());
if (eChannel != null) { if (eChannel != null) {
return eChannel.getChild("image", getRSSNamespace()); return eChannel.getChild("image", getRSSNamespace());
} else { } else {
return null; return null;
} }
} }
/** /**
@ -228,13 +242,16 @@ public class RSS091UserlandParser extends RSS090Parser {
*/ */
@Override @Override
protected Element getTextInput(final Element rssRoot) { protected Element getTextInput(final Element rssRoot) {
final String elementName = getTextInputLabel(); final String elementName = getTextInputLabel();
final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());
if (eChannel != null) { if (eChannel != null) {
return eChannel.getChild(elementName, getRSSNamespace()); return eChannel.getChild(elementName, getRSSNamespace());
} else { } else {
return null; return null;
} }
} }
/** /**
@ -250,19 +267,24 @@ public class RSS091UserlandParser extends RSS090Parser {
*/ */
@Override @Override
protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) {
final Item item = super.parseItem(rssRoot, eItem, locale); final Item item = super.parseItem(rssRoot, eItem, locale);
final Element e = eItem.getChild("description", getRSSNamespace());
if (e != null) { final Element description = eItem.getChild("description", getRSSNamespace());
item.setDescription(parseItemDescription(rssRoot, e)); if (description != null) {
item.setDescription(parseItemDescription(rssRoot, description));
} }
final Element ce = eItem.getChild("encoded", getContentNamespace());
if (ce != null) { final Element encoded = eItem.getChild("encoded", getContentNamespace());
if (encoded != null) {
final Content content = new Content(); final Content content = new Content();
content.setType(Content.HTML); content.setType(Content.HTML);
content.setValue(ce.getText()); content.setValue(encoded.getText());
item.setContent(content); item.setContent(content);
} }
return item; return item;
} }
protected Description parseItemDescription(final Element rssRoot, final Element eDesc) { protected Description parseItemDescription(final Element rssRoot, final Element eDesc) {

View file

@ -31,12 +31,10 @@ import com.sun.syndication.io.FeedException;
/** /**
* Feed Generator for RSS 0.92 * Feed Generator for RSS 0.92
* <p/>
* *
* @author Elaine Chien * @author Elaine Chien
* *
*/ */
public class RSS092Generator extends RSS091UserlandGenerator { public class RSS092Generator extends RSS091UserlandGenerator {
public RSS092Generator() { public RSS092Generator() {
@ -49,37 +47,47 @@ public class RSS092Generator extends RSS091UserlandGenerator {
@Override @Override
protected void populateChannel(final Channel channel, final Element eChannel) { protected void populateChannel(final Channel channel, final Element eChannel) {
super.populateChannel(channel, eChannel); super.populateChannel(channel, eChannel);
final Cloud cloud = channel.getCloud(); final Cloud cloud = channel.getCloud();
if (cloud != null) { if (cloud != null) {
eChannel.addContent(generateCloud(cloud)); eChannel.addContent(generateCloud(cloud));
} }
} }
protected Element generateCloud(final Cloud cloud) { protected Element generateCloud(final Cloud cloud) {
final Element eCloud = new Element("cloud", getFeedNamespace()); final Element eCloud = new Element("cloud", getFeedNamespace());
if (cloud.getDomain() != null) { final String domain = cloud.getDomain();
eCloud.setAttribute(new Attribute("domain", cloud.getDomain())); if (domain != null) {
eCloud.setAttribute(new Attribute("domain", domain));
} }
if (cloud.getPort() != 0) { final int port = cloud.getPort();
eCloud.setAttribute(new Attribute("port", String.valueOf(cloud.getPort()))); if (port != 0) {
eCloud.setAttribute(new Attribute("port", String.valueOf(port)));
} }
if (cloud.getPath() != null) { final String path = cloud.getPath();
eCloud.setAttribute(new Attribute("path", cloud.getPath())); if (path != null) {
eCloud.setAttribute(new Attribute("path", path));
} }
if (cloud.getRegisterProcedure() != null) { final String registerProcedure = cloud.getRegisterProcedure();
eCloud.setAttribute(new Attribute("registerProcedure", cloud.getRegisterProcedure())); if (registerProcedure != null) {
eCloud.setAttribute(new Attribute("registerProcedure", registerProcedure));
} }
if (cloud.getProtocol() != null) { final String protocol = cloud.getProtocol();
eCloud.setAttribute(new Attribute("protocol", cloud.getProtocol())); if (protocol != null) {
eCloud.setAttribute(new Attribute("protocol", protocol));
} }
return eCloud; return eCloud;
} }
// Another one to thanks DW for // Another one to thanks DW for
@ -93,6 +101,7 @@ public class RSS092Generator extends RSS091UserlandGenerator {
@Override @Override
protected void populateItem(final Item item, final Element eItem, final int index) { protected void populateItem(final Item item, final Element eItem, final int index) {
super.populateItem(item, eItem, index); super.populateItem(item, eItem, index);
final Source source = item.getSource(); final Source source = item.getSource();
@ -106,40 +115,59 @@ public class RSS092Generator extends RSS091UserlandGenerator {
} }
final List<Category> categories = item.getCategories(); final List<Category> categories = item.getCategories();
for (int i = 0; i < categories.size(); i++) { for (final Category category : categories) {
eItem.addContent(generateCategoryElement(categories.get(i))); eItem.addContent(generateCategoryElement(category));
} }
} }
protected Element generateSourceElement(final Source source) { protected Element generateSourceElement(final Source source) {
final Element sourceElement = new Element("source", getFeedNamespace()); final Element sourceElement = new Element("source", getFeedNamespace());
if (source.getUrl() != null) {
sourceElement.setAttribute(new Attribute("url", source.getUrl())); final String url = source.getUrl();
if (url != null) {
sourceElement.setAttribute(new Attribute("url", url));
} }
sourceElement.addContent(source.getValue()); sourceElement.addContent(source.getValue());
return sourceElement; return sourceElement;
} }
protected Element generateEnclosure(final Enclosure enclosure) { protected Element generateEnclosure(final Enclosure enclosure) {
final Element enclosureElement = new Element("enclosure", getFeedNamespace()); final Element enclosureElement = new Element("enclosure", getFeedNamespace());
if (enclosure.getUrl() != null) {
enclosureElement.setAttribute("url", enclosure.getUrl()); final String url = enclosure.getUrl();
if (url != null) {
enclosureElement.setAttribute("url", url);
} }
if (enclosure.getLength() != 0) {
enclosureElement.setAttribute("length", String.valueOf(enclosure.getLength())); final long length = enclosure.getLength();
if (length != 0) {
enclosureElement.setAttribute("length", String.valueOf(length));
} }
if (enclosure.getType() != null) {
enclosureElement.setAttribute("type", enclosure.getType()); final String type = enclosure.getType();
if (type != null) {
enclosureElement.setAttribute("type", type);
} }
return enclosureElement; return enclosureElement;
} }
protected Element generateCategoryElement(final Category category) { protected Element generateCategoryElement(final Category category) {
final Element categoryElement = new Element("category", getFeedNamespace()); final Element categoryElement = new Element("category", getFeedNamespace());
if (category.getDomain() != null) {
categoryElement.setAttribute("domain", category.getDomain()); final String domain = category.getDomain();
if (domain != null) {
categoryElement.setAttribute("domain", domain);
} }
categoryElement.addContent(category.getValue()); categoryElement.addContent(category.getValue());
return categoryElement; return categoryElement;
} }

View file

@ -56,41 +56,47 @@ public class RSS092Parser extends RSS091UserlandParser {
@Override @Override
protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { protected WireFeed parseChannel(final Element rssRoot, final Locale locale) {
final Channel channel = (Channel) super.parseChannel(rssRoot, locale); final Channel channel = (Channel) super.parseChannel(rssRoot, locale);
final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());
final Element eCloud = eChannel.getChild("cloud", getRSSNamespace()); final Element eCloud = eChannel.getChild("cloud", getRSSNamespace());
if (eCloud != null) { if (eCloud != null) {
final Cloud cloud = new Cloud(); final Cloud cloud = new Cloud();
String att = eCloud.getAttributeValue("domain");// getRSSNamespace());
// DONT KNOW WHY final String domain = eCloud.getAttributeValue("domain");
// DOESN'T WORK if (domain != null) {
if (att != null) { cloud.setDomain(domain);
cloud.setDomain(att);
} }
att = eCloud.getAttributeValue("port");// getRSSNamespace()); DONT
// KNOW WHY DOESN'T WORK // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
if (att != null) { final String port = eCloud.getAttributeValue("port");
cloud.setPort(Integer.parseInt(att.trim())); if (port != null) {
cloud.setPort(Integer.parseInt(port.trim()));
} }
att = eCloud.getAttributeValue("path");// getRSSNamespace()); DONT
// KNOW WHY DOESN'T WORK // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
if (att != null) { final String path = eCloud.getAttributeValue("path");
cloud.setPath(att); if (path != null) {
cloud.setPath(path);
} }
att = eCloud.getAttributeValue("registerProcedure");// getRSSNamespace());
// DONT KNOW WHY // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
// DOESN'T WORK final String registerProcedure = eCloud.getAttributeValue("registerProcedure");
if (att != null) { if (registerProcedure != null) {
cloud.setRegisterProcedure(att); cloud.setRegisterProcedure(registerProcedure);
} }
att = eCloud.getAttributeValue("protocol");// getRSSNamespace());
// DONT KNOW WHY DOESN'T // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
// WORK final String protocol = eCloud.getAttributeValue("protocol");
if (att != null) { if (protocol != null) {
cloud.setProtocol(att); cloud.setProtocol(protocol);
} }
channel.setCloud(cloud); channel.setCloud(cloud);
} }
return channel; return channel;
} }
@ -99,76 +105,83 @@ public class RSS092Parser extends RSS091UserlandParser {
protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) {
final Item item = super.parseItem(rssRoot, eItem, locale); final Item item = super.parseItem(rssRoot, eItem, locale);
Element e = eItem.getChild("source", getRSSNamespace()); final Element eSource = eItem.getChild("source", getRSSNamespace());
if (e != null) { if (eSource != null) {
final Source source = new Source(); final Source source = new Source();
final String url = e.getAttributeValue("url");// getRSSNamespace()); // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
// DONT final String url = eSource.getAttributeValue("url");
// KNOW WHY DOESN'T WORK
source.setUrl(url); source.setUrl(url);
source.setValue(e.getText()); source.setValue(eSource.getText());
item.setSource(source); item.setSource(source);
} }
// 0.92 allows one enclosure occurrence, 0.93 multiple // 0.92 allows one enclosure occurrence, 0.93 multiple just saving to write some code.
// just saving to write some code. // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
final List<Element> eEnclosures = eItem.getChildren("enclosure");// getRSSNamespace()); final List<Element> eEnclosures = eItem.getChildren("enclosure");
// DONT KNOW
// WHY
// DOESN'T
// WORK
if (!eEnclosures.isEmpty()) { if (!eEnclosures.isEmpty()) {
final List<Enclosure> enclosures = new ArrayList<Enclosure>(); final List<Enclosure> enclosures = new ArrayList<Enclosure>();
for (int i = 0; i < eEnclosures.size(); i++) {
e = eEnclosures.get(i); for (final Element eEnclosure : eEnclosures) {
final Enclosure enclosure = new Enclosure(); final Enclosure enclosure = new Enclosure();
String att = e.getAttributeValue("url");// getRSSNamespace()); // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
// DONT KNOW WHY DOESN'T final String url = eEnclosure.getAttributeValue("url");
// WORK if (url != null) {
if (att != null) { enclosure.setUrl(url);
enclosure.setUrl(att);
} }
att = e.getAttributeValue("length");// getRSSNamespace()); DONT
// KNOW WHY DOESN'T WORK
enclosure.setLength(NumberParser.parseLong(att, 0L));
att = e.getAttributeValue("type");// getRSSNamespace()); DONT // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
// KNOW WHY DOESN'T WORK final String length = eEnclosure.getAttributeValue("length");
if (att != null) { enclosure.setLength(NumberParser.parseLong(length, 0L));
enclosure.setType(att);
// getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
final String type = eEnclosure.getAttributeValue("type");
if (type != null) {
enclosure.setType(type);
} }
enclosures.add(enclosure); enclosures.add(enclosure);
} }
item.setEnclosures(enclosures); item.setEnclosures(enclosures);
} }
final List<Element> eCats = eItem.getChildren("category");// getRSSNamespace()); // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
// DONT KNOW WHY final List<Element> categories = eItem.getChildren("category");
// DOESN'T WORK item.setCategories(parseCategories(categories));
item.setCategories(parseCategories(eCats));
return item; return item;
} }
protected List<Category> parseCategories(final List<Element> eCats) { protected List<Category> parseCategories(final List<Element> eCats) {
List<Category> cats = null; List<Category> cats = null;
if (!eCats.isEmpty()) { if (!eCats.isEmpty()) {
cats = new ArrayList<Category>(); cats = new ArrayList<Category>();
for (int i = 0; i < eCats.size(); i++) { for (final Element eCat : eCats) {
final Category cat = new Category(); final Category cat = new Category();
final Element e = eCats.get(i);
final String att = e.getAttributeValue("domain");// getRSSNamespace()); // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
// DONT KNOW WHY final String domain = eCat.getAttributeValue("domain");
// DOESN'T WORK if (domain != null) {
if (att != null) { cat.setDomain(domain);
cat.setDomain(att);
} }
cat.setValue(e.getText());
cat.setValue(eCat.getText());
cats.add(cat); cats.add(cat);
} }
} }
return cats; return cats;
} }
@Override @Override

View file

@ -41,23 +41,29 @@ public class RSS093Parser extends RSS092Parser {
@Override @Override
protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) {
final Item item = super.parseItem(rssRoot, eItem, locale); final Item item = super.parseItem(rssRoot, eItem, locale);
Element e = eItem.getChild("pubDate", getRSSNamespace());
if (e != null) { final Element pubDate = eItem.getChild("pubDate", getRSSNamespace());
item.setPubDate(DateParser.parseDate(e.getText(), locale)); if (pubDate != null) {
item.setPubDate(DateParser.parseDate(pubDate.getText(), locale));
} }
e = eItem.getChild("expirationDate", getRSSNamespace());
if (e != null) { final Element expirationDate = eItem.getChild("expirationDate", getRSSNamespace());
item.setExpirationDate(DateParser.parseDate(e.getText(), locale)); if (expirationDate != null) {
item.setExpirationDate(DateParser.parseDate(expirationDate.getText(), locale));
} }
e = eItem.getChild("description", getRSSNamespace());
if (e != null) { final Element description = eItem.getChild("description", getRSSNamespace());
final String type = e.getAttributeValue("type"); if (description != null) {
final String type = description.getAttributeValue("type");
if (type != null) { if (type != null) {
item.getDescription().setType(type); item.getDescription().setType(type);
} }
} }
return item; return item;
} }
} }

View file

@ -26,8 +26,6 @@ import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Guid; import com.sun.syndication.feed.rss.Guid;
import com.sun.syndication.feed.rss.Item; import com.sun.syndication.feed.rss.Item;
/**
*/
public class RSS094Parser extends RSS093Parser { public class RSS094Parser extends RSS093Parser {
public RSS094Parser() { public RSS094Parser() {
@ -45,22 +43,19 @@ public class RSS094Parser extends RSS093Parser {
@Override @Override
protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { protected WireFeed parseChannel(final Element rssRoot, final Locale locale) {
final Channel channel = (Channel) super.parseChannel(rssRoot, locale); final Channel channel = (Channel) super.parseChannel(rssRoot, locale);
final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());
final List<Element> eCats = eChannel.getChildren("category", getRSSNamespace()); final List<Element> categories = eChannel.getChildren("category", getRSSNamespace());
channel.setCategories(parseCategories(eCats)); channel.setCategories(parseCategories(categories));
final Element eTtl = eChannel.getChild("ttl", getRSSNamespace()); final Element ttl = eChannel.getChild("ttl", getRSSNamespace());
if (eTtl != null && eTtl.getText() != null) { if (ttl != null && ttl.getText() != null) {
Integer ttlValue = null; final Integer ttlValue = NumberParser.parseInt(ttl.getText());
try {
ttlValue = new Integer(eTtl.getText());
} catch (final NumberFormatException nfe) {
; // let it go by
}
if (ttlValue != null) { if (ttlValue != null) {
channel.setTtl(ttlValue.intValue()); channel.setTtl(ttlValue);
} }
} }
@ -69,33 +64,40 @@ public class RSS094Parser extends RSS093Parser {
@Override @Override
public Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { public Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) {
final Item item = super.parseItem(rssRoot, eItem, locale); final Item item = super.parseItem(rssRoot, eItem, locale);
item.setExpirationDate(null); item.setExpirationDate(null);
Element e = eItem.getChild("author", getRSSNamespace()); final Element author = eItem.getChild("author", getRSSNamespace());
if (e != null) { if (author != null) {
item.setAuthor(e.getText()); item.setAuthor(author.getText());
} }
e = eItem.getChild("guid", getRSSNamespace()); final Element eGuid = eItem.getChild("guid", getRSSNamespace());
if (e != null) { if (eGuid != null) {
final Guid guid = new Guid(); final Guid guid = new Guid();
final String att = e.getAttributeValue("isPermaLink");// getRSSNamespace());
// DONT KNOW WHY // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
// DOESN'T WORK final String att = eGuid.getAttributeValue("isPermaLink");
if (att != null) { if (att != null) {
guid.setPermaLink(att.equalsIgnoreCase("true")); guid.setPermaLink(att.equalsIgnoreCase("true"));
} }
guid.setValue(e.getText());
guid.setValue(eGuid.getText());
item.setGuid(guid); item.setGuid(guid);
} }
e = eItem.getChild("comments", getRSSNamespace()); final Element comments = eItem.getChild("comments", getRSSNamespace());
if (e != null) { if (comments != null) {
item.setComments(e.getText()); item.setComments(comments.getText());
} }
return item; return item;
} }
} }

View file

@ -54,22 +54,25 @@ public class RSS10Generator extends RSS090Generator {
@Override @Override
protected void populateChannel(final Channel channel, final Element eChannel) { protected void populateChannel(final Channel channel, final Element eChannel) {
super.populateChannel(channel, eChannel); super.populateChannel(channel, eChannel);
if (channel.getUri() != null) {
eChannel.setAttribute("about", channel.getUri(), getRDFNamespace()); final String channelUri = channel.getUri();
if (channelUri != null) {
eChannel.setAttribute("about", channelUri, getRDFNamespace());
} }
final List<Item> items = channel.getItems(); final List<Item> items = channel.getItems();
if (!items.isEmpty()) { if (!items.isEmpty()) {
final Element eItems = new Element("items", getFeedNamespace()); final Element eItems = new Element("items", getFeedNamespace());
final Element eSeq = new Element("Seq", getRDFNamespace()); final Element eSeq = new Element("Seq", getRDFNamespace());
for (int i = 0; i < items.size(); i++) { for (final Item item : items) {
final Item item = items.get(i); final Element lis = new Element("li", getRDFNamespace());
final Element eLi = new Element("li", getRDFNamespace());
final String uri = item.getUri(); final String uri = item.getUri();
if (uri != null) { if (uri != null) {
eLi.setAttribute("resource", uri, getRDFNamespace()); lis.setAttribute("resource", uri, getRDFNamespace());
} }
eSeq.addContent(eLi); eSeq.addContent(lis);
} }
eItems.addContent(eSeq); eItems.addContent(eSeq);
eChannel.addContent(eItems); eChannel.addContent(eItems);
@ -78,10 +81,11 @@ public class RSS10Generator extends RSS090Generator {
@Override @Override
protected void populateItem(final Item item, final Element eItem, final int index) { protected void populateItem(final Item item, final Element eItem, final int index) {
super.populateItem(item, eItem, index); super.populateItem(item, eItem, index);
final String link = item.getLink(); final String link = item.getLink();
final String uri = item.getUri(); final String uri = item.getUri();
if (uri != null) { if (uri != null) {
eItem.setAttribute("about", uri, getRDFNamespace()); eItem.setAttribute("about", uri, getRDFNamespace());
} else if (link != null) { } else if (link != null) {
@ -92,11 +96,13 @@ public class RSS10Generator extends RSS090Generator {
if (description != null) { if (description != null) {
eItem.addContent(generateSimpleElement("description", description.getValue())); eItem.addContent(generateSimpleElement("description", description.getValue()));
} }
if (item.getModule(getContentNamespace().getURI()) == null && item.getContent() != null) { if (item.getModule(getContentNamespace().getURI()) == null && item.getContent() != null) {
final Element elem = new Element("encoded", getContentNamespace()); final Element elem = new Element("encoded", getContentNamespace());
elem.addContent(item.getContent().getValue()); elem.addContent(item.getContent().getValue());
eItem.addContent(elem); eItem.addContent(elem);
} }
} }
@Override @Override

View file

@ -55,21 +55,9 @@ public class RSS10Parser extends RSS090Parser {
*/ */
@Override @Override
public boolean isMyType(final Document document) { public boolean isMyType(final Document document) {
boolean ok = false;
final Element rssRoot = document.getRootElement(); final Element rssRoot = document.getRootElement();
final Namespace defaultNS = rssRoot.getNamespace(); final Namespace defaultNS = rssRoot.getNamespace();
return defaultNS != null && defaultNS.equals(getRDFNamespace()) && rssRoot.getChild("channel", getRSSNamespace()) != null;
ok = defaultNS != null && defaultNS.equals(getRDFNamespace());
if (ok) {
// now also test if the channel element exists with the right
// namespace
final Element channel = rssRoot.getChild("channel", getRSSNamespace());
if (channel == null) {
ok = false;
}
}
return ok;
} }
/** /**
@ -96,22 +84,25 @@ public class RSS10Parser extends RSS090Parser {
*/ */
@Override @Override
protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) {
final Item item = super.parseItem(rssRoot, eItem, locale); final Item item = super.parseItem(rssRoot, eItem, locale);
final Element e = eItem.getChild("description", getRSSNamespace());
if (e != null) { final Element description = eItem.getChild("description", getRSSNamespace());
item.setDescription(parseItemDescription(rssRoot, e)); if (description != null) {
item.setDescription(parseItemDescription(rssRoot, description));
} }
final Element ce = eItem.getChild("encoded", getContentNamespace());
if (ce != null) { final Element encoded = eItem.getChild("encoded", getContentNamespace());
if (encoded != null) {
final Content content = new Content(); final Content content = new Content();
content.setType(Content.HTML); content.setType(Content.HTML);
content.setValue(ce.getText()); content.setValue(encoded.getText());
item.setContent(content); item.setContent(content);
} }
final String uri = eItem.getAttributeValue("about", getRDFNamespace()); final String about = eItem.getAttributeValue("about", getRDFNamespace());
if (uri != null) { if (about != null) {
item.setUri(uri); item.setUri(about);
} }
return item; return item;
@ -119,6 +110,7 @@ public class RSS10Parser extends RSS090Parser {
@Override @Override
protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { protected WireFeed parseChannel(final Element rssRoot, final Locale locale) {
final Channel channel = (Channel) super.parseChannel(rssRoot, locale); final Channel channel = (Channel) super.parseChannel(rssRoot, locale);
final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());

View file

@ -32,7 +32,6 @@ import com.sun.syndication.feed.rss.Item;
* @author Elaine Chien * @author Elaine Chien
* *
*/ */
public class RSS20Generator extends RSS094Generator { public class RSS20Generator extends RSS094Generator {
public RSS20Generator() { public RSS20Generator() {
@ -45,6 +44,7 @@ public class RSS20Generator extends RSS094Generator {
@Override @Override
protected void populateChannel(final Channel channel, final Element eChannel) { protected void populateChannel(final Channel channel, final Element eChannel) {
super.populateChannel(channel, eChannel); super.populateChannel(channel, eChannel);
final String generator = channel.getGenerator(); final String generator = channel.getGenerator();
@ -58,19 +58,20 @@ public class RSS20Generator extends RSS094Generator {
} }
final List<Category> categories = channel.getCategories(); final List<Category> categories = channel.getCategories();
for (int i = 0; i < categories.size(); i++) { for (final Category category : categories) {
eChannel.addContent(generateCategoryElement(categories.get(i))); eChannel.addContent(generateCategoryElement(category));
} }
} }
@Override @Override
public void populateItem(final Item item, final Element eItem, final int index) { public void populateItem(final Item item, final Element eItem, final int index) {
super.populateItem(item, eItem, index); super.populateItem(item, eItem, index);
final Element eDescription = eItem.getChild("description", getFeedNamespace()); final Element description = eItem.getChild("description", getFeedNamespace());
if (eDescription != null) { if (description != null) {
eDescription.removeAttribute("type"); description.removeAttribute("type");
} }
final String author = item.getAuthor(); final String author = item.getAuthor();

View file

@ -22,8 +22,6 @@ import org.jdom2.Element;
import com.sun.syndication.feed.rss.Description; import com.sun.syndication.feed.rss.Description;
/**
*/
public class RSS20Parser extends RSS094Parser { public class RSS20Parser extends RSS094Parser {
public RSS20Parser() { public RSS20Parser() {
@ -52,19 +50,11 @@ public class RSS20Parser extends RSS094Parser {
@Override @Override
public boolean isMyType(final Document document) { public boolean isMyType(final Document document) {
boolean ok;
final Element rssRoot = document.getRootElement(); final Element rssRoot = document.getRootElement();
ok = rssRoot.getName().equals("rss"); final Attribute version = rssRoot.getAttribute("version");
if (ok) { // as far ROME is concerned RSS 2.0, 2.00 and 2.0.X are all the same, so let's use
ok = false; // startsWith for leniency.
final Attribute version = rssRoot.getAttribute("version"); return rssRoot.getName().equals("rss") && version != null && version.getValue().startsWith(getRSSVersion());
if (version != null) {
// At this point, as far ROME is concerned RSS 2.0, 2.00 and
// 2.0.X are all the same, so let's use startsWith for leniency.
ok = version.getValue().startsWith(getRSSVersion());
}
}
return ok;
} }
} }

View file

@ -36,6 +36,7 @@ import com.sun.syndication.feed.WireFeed;
* *
*/ */
public class RSS20wNSParser extends RSS20Parser { public class RSS20wNSParser extends RSS20Parser {
private static String RSS20_URI = "http://backend.userland.com/rss2"; private static String RSS20_URI = "http://backend.userland.com/rss2";
public RSS20wNSParser() { public RSS20wNSParser() {
@ -50,11 +51,7 @@ public class RSS20wNSParser extends RSS20Parser {
public boolean isMyType(final Document document) { public boolean isMyType(final Document document) {
final Element rssRoot = document.getRootElement(); final Element rssRoot = document.getRootElement();
final Namespace defaultNS = rssRoot.getNamespace(); final Namespace defaultNS = rssRoot.getNamespace();
boolean ok = defaultNS != null && defaultNS.equals(getRSSNamespace()); return defaultNS != null && defaultNS.equals(getRSSNamespace()) && super.isMyType(document);
if (ok) {
ok = super.isMyType(document);
}
return ok;
} }
@Override @Override

View file

@ -17,6 +17,7 @@
package com.sun.syndication.io.impl; package com.sun.syndication.io.impl;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
@ -35,7 +36,6 @@ import com.sun.syndication.io.ModuleGenerator;
* @author Elaine Chien * @author Elaine Chien
* *
*/ */
public class SyModuleGenerator implements ModuleGenerator { public class SyModuleGenerator implements ModuleGenerator {
private static final String SY_URI = "http://purl.org/rss/1.0/modules/syndication/"; private static final String SY_URI = "http://purl.org/rss/1.0/modules/syndication/";
@ -73,9 +73,10 @@ public class SyModuleGenerator implements ModuleGenerator {
final SyModule syModule = (SyModule) module; final SyModule syModule = (SyModule) module;
if (syModule.getUpdatePeriod() != null) { final String updatePeriod = syModule.getUpdatePeriod();
if (updatePeriod != null) {
final Element updatePeriodElement = new Element("updatePeriod", SY_NS); final Element updatePeriodElement = new Element("updatePeriod", SY_NS);
updatePeriodElement.addContent(syModule.getUpdatePeriod()); updatePeriodElement.addContent(updatePeriod);
element.addContent(updatePeriodElement); element.addContent(updatePeriodElement);
} }
@ -83,10 +84,13 @@ public class SyModuleGenerator implements ModuleGenerator {
updateFrequencyElement.addContent(String.valueOf(syModule.getUpdateFrequency())); updateFrequencyElement.addContent(String.valueOf(syModule.getUpdateFrequency()));
element.addContent(updateFrequencyElement); element.addContent(updateFrequencyElement);
if (syModule.getUpdateBase() != null) { final Date updateBase = syModule.getUpdateBase();
if (updateBase != null) {
final Element updateBaseElement = new Element("updateBase", SY_NS); final Element updateBaseElement = new Element("updateBase", SY_NS);
updateBaseElement.addContent(DateParser.formatW3CDateTime(syModule.getUpdateBase(), Locale.US)); updateBaseElement.addContent(DateParser.formatW3CDateTime(updateBase, Locale.US));
element.addContent(updateBaseElement); element.addContent(updateBaseElement);
} }
} }
} }

View file

@ -26,9 +26,8 @@ import com.sun.syndication.feed.module.SyModule;
import com.sun.syndication.feed.module.SyModuleImpl; import com.sun.syndication.feed.module.SyModuleImpl;
import com.sun.syndication.io.ModuleParser; import com.sun.syndication.io.ModuleParser;
/**
*/
public class SyModuleParser implements ModuleParser { public class SyModuleParser implements ModuleParser {
@Override @Override
public String getNamespaceUri() { public String getNamespaceUri() {
return SyModule.URI; return SyModule.URI;
@ -40,29 +39,35 @@ public class SyModuleParser implements ModuleParser {
@Override @Override
public Module parse(final Element syndRoot, final Locale locale) { public Module parse(final Element syndRoot, final Locale locale) {
boolean foundSomething = false; boolean foundSomething = false;
final SyModule sm = new SyModuleImpl(); final SyModule sm = new SyModuleImpl();
Element e = syndRoot.getChild("updatePeriod", getDCNamespace()); final Element updatePeriod = syndRoot.getChild("updatePeriod", getDCNamespace());
if (e != null) { if (updatePeriod != null) {
foundSomething = true; foundSomething = true;
sm.setUpdatePeriod(e.getText()); sm.setUpdatePeriod(updatePeriod.getText());
} }
e = syndRoot.getChild("updateFrequency", getDCNamespace());
if (e != null) { final Element updateFrequency = syndRoot.getChild("updateFrequency", getDCNamespace());
if (updateFrequency != null) {
foundSomething = true; foundSomething = true;
sm.setUpdateFrequency(Integer.parseInt(e.getText().trim())); sm.setUpdateFrequency(Integer.parseInt(updateFrequency.getText().trim()));
} }
e = syndRoot.getChild("updateBase", getDCNamespace());
if (e != null) { final Element updateBase = syndRoot.getChild("updateBase", getDCNamespace());
if (updateBase != null) {
foundSomething = true; foundSomething = true;
sm.setUpdateBase(DateParser.parseDate(e.getText(), locale)); sm.setUpdateBase(DateParser.parseDate(updateBase.getText(), locale));
} }
if (foundSomething) { if (foundSomething) {
return sm; return sm;
} else { } else {
return null; return null;
} }
} }
} }