diff --git a/pom.xml b/pom.xml index fe40e3f..58c0be9 100644 --- a/pom.xml +++ b/pom.xml @@ -97,6 +97,10 @@ + + com.rometools + rome-utils + org.jdom jdom diff --git a/src/main/java/com/sun/syndication/feed/WireFeed.java b/src/main/java/com/sun/syndication/feed/WireFeed.java index e2f719a..98383e0 100644 --- a/src/main/java/com/sun/syndication/feed/WireFeed.java +++ b/src/main/java/com/sun/syndication/feed/WireFeed.java @@ -18,11 +18,11 @@ package com.sun.syndication.feed; import java.io.Serializable; -import java.util.ArrayList; import java.util.List; import org.jdom2.Element; +import com.rometools.utils.Lists; import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.module.Extendable; import com.sun.syndication.feed.module.Module; @@ -38,13 +38,16 @@ import com.sun.syndication.feed.module.impl.ModuleUtils; * The format of the 'type' property must be [FEEDNAME]_[FEEDVERSION] with the FEEDNAME in lower * case, for example: rss_0.9, rss_0.93, atom_0.3 *

- * + * * @author Alejandro Abdelnur - * + * */ public abstract class WireFeed implements Cloneable, Serializable, Extendable { + private static final long serialVersionUID = -3608120400805691829L; + private final ObjectBean objBean; + private String feedType; private String encoding; private String styleSheet; @@ -53,8 +56,6 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * Default constructor, for bean cloning purposes only. - *

- * */ protected WireFeed() { objBean = new ObjectBean(this.getClass(), this); @@ -62,10 +63,9 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * Creates a feed for a given type. - *

- * + * * @param type of the feed to create. - * + * */ protected WireFeed(final String type) { this(); @@ -74,11 +74,10 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * Creates a deep 'bean' clone of the object. - *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -88,20 +87,22 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. - *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { + if (other == null) { return false; } + if (!(other instanceof WireFeed)) { return false; } + // can't use foreign markup in equals, due to JDOM equals impl final List fm = getForeignMarkup(); setForeignMarkup(((WireFeed) other).getForeignMarkup()); @@ -109,6 +110,7 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { // restore foreign markup setForeignMarkup(fm); return ret; + } /** @@ -116,9 +118,9 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -128,9 +130,9 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -140,9 +142,9 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * Sets the feedType of a the feed. Do not use, for bean cloning purposes only. *

- * + * * @param feedType the feedType of the feed. - * + * */ public void setFeedType(final String feedType) { this.feedType = feedType; @@ -150,7 +152,7 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * Returns the type of the feed. - * + * * @return the type of the feed. */ public String getFeedType() { @@ -163,9 +165,9 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { * This property is not set by feed parsers. But it is used by feed generators to set the * encoding in the XML prolog. *

- * + * * @return the charset encoding of the feed. - * + * */ public String getEncoding() { return encoding; @@ -177,9 +179,9 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { * This property is not set by feed parsers. But it is used by feed generators to set the * encoding in the XML prolog. *

- * + * * @param encoding the charset encoding of the feed. - * + * */ public void setEncoding(final String encoding) { this.encoding = encoding; @@ -188,25 +190,22 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * Returns the channel modules. *

- * + * * @return a list of ModuleImpl elements with the channel modules, an empty list if none. - * + * */ @Override public List getModules() { - if (modules == null) { - modules = new ArrayList(); - } - return modules; + return modules = Lists.createWhenNull(modules); } /** * Sets the channel modules. *

- * + * * @param modules the list of ModuleImpl elements with the channel modules to set, an empty list * or null if none. - * + * */ @Override public void setModules(final List modules) { @@ -216,7 +215,7 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * Returns the module identified by a given URI. *

- * + * * @param uri the URI of the ModuleImpl. * @return The module with the given URI, null if none. */ @@ -228,23 +227,20 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * Returns foreign markup found at channel level. *

- * + * * @return Opaque object to discourage use - * + * */ public List getForeignMarkup() { - if (foreignMarkup == null) { - foreignMarkup = new ArrayList(); - } - return foreignMarkup; + return foreignMarkup = Lists.createWhenNull(foreignMarkup); } /** * Sets foreign markup found at channel level. *

- * + * * @param foreignMarkup Opaque object to discourage use - * + * */ public void setForeignMarkup(final List foreignMarkup) { this.foreignMarkup = foreignMarkup; @@ -252,7 +248,7 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * URL of XSL-Stylesheet. - * + * * @since 2.0.0 * @return styleSheet URL or {@code null} */ @@ -262,11 +258,12 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable { /** * URL of XSL-Stylesheet. - * + * * @since 2.0.0 * @param styleSheet URL or {@code null} */ public void setStyleSheet(final String styleSheet) { this.styleSheet = styleSheet; } + } diff --git a/src/main/java/com/sun/syndication/feed/atom/Category.java b/src/main/java/com/sun/syndication/feed/atom/Category.java index 60eb936..627a653 100644 --- a/src/main/java/com/sun/syndication/feed/atom/Category.java +++ b/src/main/java/com/sun/syndication/feed/atom/Category.java @@ -18,15 +18,17 @@ package com.sun.syndication.feed.atom; import java.io.Serializable; +import com.rometools.utils.Alternatives; import com.sun.syndication.feed.impl.ObjectBean; /** * Bean for category elements of Atom feeds. *

- * + * * @author Dave Johnson (added for Atom 1.0) */ public class Category implements Cloneable, Serializable { + private static final long serialVersionUID = -2034251366664065410L; private final ObjectBean objBean; @@ -39,7 +41,7 @@ public class Category implements Cloneable, Serializable { /** * Default constructor. All properties are set to null. *

- * + * */ public Category() { objBean = new ObjectBean(this.getClass(), this); @@ -48,10 +50,10 @@ public class Category implements Cloneable, Serializable { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -62,10 +64,10 @@ public class Category implements Cloneable, Serializable { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -80,9 +82,9 @@ public class Category implements Cloneable, Serializable { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -92,9 +94,9 @@ public class Category implements Cloneable, Serializable { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -104,7 +106,7 @@ public class Category implements Cloneable, Serializable { /** * Get label for category. *

- * + * * @return Label for category. */ public String getLabel() { @@ -114,7 +116,7 @@ public class Category implements Cloneable, Serializable { /** * Set label for category. *

- * + * * @param label Label for category. */ public void setLabel(final String label) { @@ -124,7 +126,7 @@ public class Category implements Cloneable, Serializable { /** * Get Scheme URI for category. *

- * + * * @return Scheme URI for category. */ public String getScheme() { @@ -134,7 +136,7 @@ public class Category implements Cloneable, Serializable { /** * Set scheme URI for category. *

- * + * * @param scheme Scheme URI for category. */ public void setScheme(final String scheme) { @@ -146,17 +148,13 @@ public class Category implements Cloneable, Serializable { } public String getSchemeResolved() { - if (schemeResolved != null) { - return schemeResolved; - } else { - return scheme; - } + return Alternatives.firstNotNull(schemeResolved, scheme); } /** * Return term for category. *

- * + * * @return Term for category. */ public String getTerm() { @@ -166,10 +164,11 @@ public class Category implements Cloneable, Serializable { /** * Set term for category. *

- * + * * @param term Term for category. */ public void setTerm(final String term) { this.term = term; } + } diff --git a/src/main/java/com/sun/syndication/feed/atom/Content.java b/src/main/java/com/sun/syndication/feed/atom/Content.java index 3662fc8..aad57b4 100644 --- a/src/main/java/com/sun/syndication/feed/atom/Content.java +++ b/src/main/java/com/sun/syndication/feed/atom/Content.java @@ -20,16 +20,18 @@ import java.io.Serializable; import java.util.HashSet; import java.util.Set; +import com.rometools.utils.Strings; import com.sun.syndication.feed.impl.ObjectBean; /** * Bean for content elements of Atom feeds. *

- * + * * @author Alejandro Abdelnur * @author Dave Johnson (updated for Atom 1.0) */ public class Content implements Cloneable, Serializable { + private static final long serialVersionUID = 2036205883043031310L; private final ObjectBean objBean; @@ -67,7 +69,7 @@ public class Content implements Cloneable, Serializable { /** * Default constructor. All properties are set to null. *

- * + * */ public Content() { objBean = new ObjectBean(this.getClass(), this); @@ -76,10 +78,10 @@ public class Content implements Cloneable, Serializable { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -90,10 +92,10 @@ public class Content implements Cloneable, Serializable { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -108,9 +110,9 @@ public class Content implements Cloneable, Serializable { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -120,9 +122,9 @@ public class Content implements Cloneable, Serializable { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -134,7 +136,7 @@ public class Content implements Cloneable, Serializable { *

* The type indicates how the value was/will-be encoded in the XML feed. *

- * + * * @since Atom 1.0 */ public String getType() { @@ -146,7 +148,7 @@ public class Content implements Cloneable, Serializable { *

* The type indicates how the value was/will-be encoded in the XML feed. *

- * + * * @since Atom 1.0 */ public void setType(final String type) { @@ -158,7 +160,7 @@ public class Content implements Cloneable, Serializable { *

* The mode indicates how the value was/will-be encoded in the XML feed. *

- * + * * @return the content mode, null if none. */ public String getMode() { @@ -170,17 +172,14 @@ public class Content implements Cloneable, Serializable { *

* The mode indicates how the value was/will-be encoded in the XML feed. *

- * + * * @param mode the content mode, null if none. */ - public void setMode(String mode) { - if (mode != null) { - mode = mode.toLowerCase(); - } + public void setMode(final String mode) { + this.mode = Strings.toLowerCase(mode); if (mode == null || !MODES.contains(mode)) { throw new IllegalArgumentException("Invalid mode [" + mode + "]"); } - this.mode = mode; } /** @@ -188,9 +187,9 @@ public class Content implements Cloneable, Serializable { *

* The return value should be decoded. *

- * + * * @return the content value, null if none. - * + * */ public String getValue() { return value; @@ -201,9 +200,9 @@ public class Content implements Cloneable, Serializable { *

* The value being set should be decoded. *

- * + * * @param value the content value, null if none. - * + * */ public void setValue(final String value) { this.value = value; @@ -212,7 +211,7 @@ public class Content implements Cloneable, Serializable { /** * Returns the src *

- * + * * @return Returns the src. * @since Atom 1.0 */ @@ -223,7 +222,7 @@ public class Content implements Cloneable, Serializable { /** * Set the src *

- * + * * @param src The src to set. * @since Atom 1.0 */ diff --git a/src/main/java/com/sun/syndication/feed/atom/Entry.java b/src/main/java/com/sun/syndication/feed/atom/Entry.java index e3dc30f..aa43ab1 100644 --- a/src/main/java/com/sun/syndication/feed/atom/Entry.java +++ b/src/main/java/com/sun/syndication/feed/atom/Entry.java @@ -17,12 +17,13 @@ package com.sun.syndication.feed.atom; import java.io.Serializable; -import java.util.ArrayList; import java.util.Date; import java.util.List; 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.module.Extendable; import com.sun.syndication.feed.module.Module; @@ -32,12 +33,14 @@ import com.sun.syndication.feed.synd.SyndPerson; /** * Bean for entry elements of Atom feeds. *

- * + * * @author Alejandro Abdelnur * @author Dave Johnson (updated for Atom 1.0) */ public class Entry implements Cloneable, Serializable, Extendable { + private static final long serialVersionUID = 4874483180016783939L; + private Content summary; private Content title; private Date created; // Atom 0.3 only @@ -60,7 +63,7 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Default constructor. All properties are set to null. *

- * + * */ public Entry() { objBean = new ObjectBean(this.getClass(), this); @@ -69,7 +72,7 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Sets the entry alternate links. *

- * + * * @param alternateLinks the list of Link elements with the entry alternate links to set, an * empty list or null if none. */ @@ -80,22 +83,19 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the entry alternate links. *

- * + * * @return a list of Link elements with the entry alternate links, an empty list if none. */ public List getAlternateLinks() { - if (alternateLinks == null) { - alternateLinks = new ArrayList(); - } - return alternateLinks; + return alternateLinks = Lists.createWhenNull(alternateLinks); } /** * Sets the author of the entry. *

- * + * * @param authors the author of the entry, null if none. - * + * */ public void setAuthors(final List authors) { this.authors = authors; @@ -104,21 +104,18 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the entry author. *

- * + * * @return the entry author, null if none. - * + * */ public List getAuthors() { - if (authors == null) { - authors = new ArrayList(); - } - return authors; + return authors = Lists.createWhenNull(authors); } /** * Set the categories *

- * + * * @param categories The categories to set. * @since Atom 1.0 */ @@ -129,21 +126,18 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the categories *

- * + * * @return Returns the categories. * @since Atom 1.0 */ public List getCategories() { - if (categories == null) { - categories = new ArrayList(); - } - return categories; + return categories = Lists.createWhenNull(categories); } /** * Sets the entry contents. *

- * + * * @param contents the list of Content elements with the entry contents to set, an empty list or * null if none. */ @@ -154,23 +148,20 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the entry contents. *

- * + * * @return a list of Content elements with the entry contents, an empty list if none. */ public List getContents() { - if (contents == null) { - contents = new ArrayList(); - } - return contents; + return contents = Lists.createWhenNull(contents); } /** * Sets the entry contributors. *

- * + * * @param contributors the list of Person elements with the entry contributors to set, an empty * list or null if none. - * + * */ public void setContributors(final List contributors) { this.contributors = contributors; @@ -179,47 +170,40 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the entry contributors. *

- * + * * @return a list of Person elements with the entry contributors, an empty list if none. - * + * */ public List getContributors() { - if (contributors == null) { - contributors = new ArrayList(); - } - return contributors; + return contributors = Lists.createWhenNull(contributors); } /** * Sets the entry created date (Atom 0.3 only) *

- * + * * @param created the entry created date, null if none. */ public void setCreated(final Date created) { - this.created = new Date(created.getTime()); + this.created = Dates.copy(created); } /** * Returns the entry created date (Atom 0.3 only) *

- * + * * @return the entry created date, null if none. */ public Date getCreated() { - if (created == null) { - return null; - } else { - return new Date(created.getTime()); - } + return Dates.copy(created); } /** * Sets foreign markup found at entry level. *

- * + * * @param foreignMarkup Opaque object to discourage use - * + * */ public void setForeignMarkup(final List foreignMarkup) { this.foreignMarkup = foreignMarkup; @@ -228,23 +212,20 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns foreign markup found at entry level. *

- * + * * @return list of Opaque object to discourage use - * + * */ public List getForeignMarkup() { - if (foreignMarkup == null) { - foreignMarkup = new ArrayList(); - } - return foreignMarkup; + return foreignMarkup = Lists.createWhenNull(foreignMarkup); } /** * Sets the entry ID. *

- * + * * @param id the entry ID, null if none. - * + * */ public void setId(final String id) { this.id = id; @@ -253,9 +234,9 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the entry ID. *

- * + * * @return the entry ID, null if none. - * + * */ public String getId() { return id; @@ -264,83 +245,64 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Sets the entry issued date (Atom 0.3, maps to {@link #setPublished(java.util.Date)}). *

- * + * * @param issued the entry issued date, null if none. */ public void setIssued(final Date issued) { - if (issued == null) { - published = null; - } else { - published = new Date(issued.getTime()); - } + published = Dates.copy(issued); } /** * Returns the entry issued date (Atom 0.3, maps to {@link #getPublished()} ). *

- * + * * @return the entry issued date, null if none. */ public Date getIssued() { - if (published == null) { - return null; - } else { - return new Date(published.getTime()); - } + return Dates.copy(published); } /** * Returns true if entry is a media entry, i.e. has rel="edit-media". - * + * * @return true if entry is a media entry */ public boolean isMediaEntry() { boolean mediaEntry = false; final List links = getOtherLinks(); - for (final Link link : links) { if ("edit-media".equals(link.getRel())) { mediaEntry = true; - break; } } - return mediaEntry; } /** * Sets the entry modified date (Atom 0.3, maps to {@link #setUpdated(java.util.Date)}). *

- * + * * @param modified the entry modified date, null if none. */ public void setModified(final Date modified) { - if (modified == null) { - updated = null; - } else { - updated = new Date(modified.getTime()); - } + updated = Dates.copy(modified); } /** * Returns the entry modified date (Atom 0.3, maps to {@link #getUpdated()} ). *

- * + * * @return the entry modified date, null if none. */ public Date getModified() { - if (updated == null) { - return null; - } else { - return new Date(updated.getTime()); - } + return Dates.copy(updated); } /** * Returns the module identified by a given URI. *

- * + * * @param uri the URI of the ModuleImpl. * @return The module with the given URI, null if none. */ @@ -352,10 +314,10 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Sets the entry modules. *

- * + * * @param modules the list of ModuleImpl elements with the entry modules to set, an empty list * or null if none. - * + * */ @Override public void setModules(final List modules) { @@ -365,22 +327,19 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the entry modules. *

- * + * * @return a list of ModuleImpl elements with the entry modules, an emtpy list if none. - * + * */ @Override public List getModules() { - if (modules == null) { - modules = new ArrayList(); - } - return modules; + return modules = Lists.createWhenNull(modules); } /** * Sets the entry non-alternate links. *

- * + * * @param otherLinks the list Link elements with the entry non-alternate links to set, an empty * list or null if none. */ @@ -391,51 +350,40 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the entry non-alternate links. *

- * + * * @return the list of Link elements with the entry non-alternate links to set, an empty list if * none. */ public List getOtherLinks() { - if (otherLinks == null) { - otherLinks = new ArrayList(); - } - return otherLinks; + return otherLinks = Lists.createWhenNull(otherLinks); } /** * Set the published *

- * + * * @param published The published to set. * @since Atom 1.0 */ public void setPublished(final Date published) { - if (published == null) { - this.published = null; - } else { - this.published = new Date(published.getTime()); - } + this.published = Dates.copy(published); } /** * Returns the published *

- * + * * @return Returns the published. * @since Atom 1.0 */ public Date getPublished() { - if (published == null) { - return null; - } else { - return new Date(published.getTime()); - } + return Dates.copy(published); } /** * Set the rights *

- * + * * @param rights The rights to set. * @since Atom 1.0 */ @@ -446,7 +394,7 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the rights *

- * + * * @return Returns the rights. * @since Atom 1.0 */ @@ -457,7 +405,7 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Set the source *

- * + * * @param source The source to set. */ public void setSource(final Feed source) { @@ -467,7 +415,7 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the source *

- * + * * @return Returns the source. */ public Feed getSource() { @@ -477,9 +425,9 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Sets the entry summary. *

- * + * * @param summary the entry summary, null if none. - * + * */ public void setSummary(final Content summary) { this.summary = summary; @@ -488,9 +436,9 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the entry summary. *

- * + * * @return the entry summary, null if none. - * + * */ public Content getSummary() { return summary; @@ -499,39 +447,37 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Sets the entry title. *

- * + * * @param title the entry title, null if none. - * + * */ public void setTitle(final String title) { if (this.title == null) { this.title = new Content(); } - this.title.setValue(title); } /** * Returns the entry title. *

- * + * * @return the entry title, null if none. - * + * */ public String getTitle() { if (title != null) { return title.getValue(); } - return null; } /** * Sets the entry title as a text construct. *

- * + * * @param title the entry title, null if none. - * + * */ public void setTitleEx(final Content title) { this.title = title; @@ -540,9 +486,9 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the entry title as a text construct. *

- * + * * @return the entry title, null if none. - * + * */ public Content getTitleEx() { return title; @@ -551,37 +497,29 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Set the updated *

- * + * * @param updated The updated to set. * @since Atom 1.0 */ public void setUpdated(final Date updated) { - if (updated == null) { - this.updated = null; - } else { - this.updated = new Date(updated.getTime()); - } + this.updated = Dates.copy(updated); } /** * Returns the updated *

- * + * * @return Returns the updated. * @since Atom 1.0 */ public Date getUpdated() { - if (updated == null) { - return null; - } else { - return new Date(updated.getTime()); - } + return Dates.copy(updated); } /** * Set the xmlBase *

- * + * * @param xmlBase The xmlBase to set. * @since Atom 1.0 */ @@ -592,7 +530,7 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the xmlBase *

- * + * * @return Returns the xmlBase. * @since Atom 1.0 */ @@ -603,10 +541,10 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -617,10 +555,10 @@ public class Entry implements Cloneable, Serializable, Extendable { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -646,9 +584,9 @@ public class Entry implements Cloneable, Serializable, Extendable { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -658,9 +596,9 @@ public class Entry implements Cloneable, Serializable, Extendable { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -668,11 +606,12 @@ public class Entry implements Cloneable, Serializable, Extendable { } public Link findRelatedLink(final String relation) { - for (final Link l : otherLinks) { - if (relation.equals(l.getRel())) { - return l; + for (final Link link : otherLinks) { + if (relation.equals(link.getRel())) { + return link; } } return null; } + } diff --git a/src/main/java/com/sun/syndication/feed/atom/Feed.java b/src/main/java/com/sun/syndication/feed/atom/Feed.java index cac4b2c..a066931 100644 --- a/src/main/java/com/sun/syndication/feed/atom/Feed.java +++ b/src/main/java/com/sun/syndication/feed/atom/Feed.java @@ -16,10 +16,10 @@ */ package com.sun.syndication.feed.atom; -import java.util.ArrayList; import java.util.Date; import java.util.List; +import com.rometools.utils.Lists; import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.impl.ModuleUtils; @@ -30,12 +30,14 @@ import com.sun.syndication.feed.synd.SyndPerson; *

* It handles Atom feeds version 0.3 without loosing any feed information. *

- * + * * @author Alejandro Abdelnur * @author Dave Johnson (updated for Atom 1.0) */ public class Feed extends WireFeed { + private static final long serialVersionUID = -9175445106675422528L; + private String xmlBase; private List categories; private List authors; @@ -59,7 +61,7 @@ public class Feed extends WireFeed { /** * Default constructor, for bean cloning purposes only. - * + * */ public Feed() { } @@ -67,9 +69,9 @@ public class Feed extends WireFeed { /** * Feed Constructor. All properties, except the type, are set to null. *

- * + * * @param type the type of the Atom feed. - * + * */ public Feed(final String type) { super(type); @@ -78,9 +80,9 @@ public class Feed extends WireFeed { /** * Returns the feed language (Atom 0.3 only) *

- * + * * @return the feed language, null if none. - * + * */ public String getLanguage() { return language; @@ -89,9 +91,9 @@ public class Feed extends WireFeed { /** * Sets the feed language (Atom 0.3 only) *

- * + * * @param language the feed language to set, null if none. - * + * */ public void setLanguage(final String language) { this.language = language; @@ -100,9 +102,9 @@ public class Feed extends WireFeed { /** * Returns the feed title. *

- * + * * @return the feed title, null if none. - * + * */ public String getTitle() { if (title != null) { @@ -114,9 +116,9 @@ public class Feed extends WireFeed { /** * Sets the feed title. *

- * + * * @param title the feed title to set, null if none. - * + * */ public void setTitle(final String title) { if (this.title == null) { @@ -128,9 +130,9 @@ public class Feed extends WireFeed { /** * Returns the feed title. *

- * + * * @return the feed title, null if none. - * + * */ public Content getTitleEx() { return title; @@ -139,9 +141,9 @@ public class Feed extends WireFeed { /** * Sets the feed title. *

- * + * * @param title the feed title to set, null if none. - * + * */ public void setTitleEx(final Content title) { this.title = title; @@ -150,20 +152,17 @@ public class Feed extends WireFeed { /** * Returns the feed alternate links. *

- * + * * @return a list of Link elements with the feed alternate links, an empty list if none. */ public List getAlternateLinks() { - if (alternateLinks == null) { - alternateLinks = new ArrayList(); - } - return alternateLinks; + return alternateLinks = Lists.createWhenNull(alternateLinks); } /** * Sets the feed alternate links. *

- * + * * @param alternateLinks the list of Link elements with the feed alternate links to set, an * empty list or null if none. */ @@ -174,21 +173,18 @@ public class Feed extends WireFeed { /** * Returns the feed other links (non-alternate ones). *

- * + * * @return a list of Link elements with the feed other links (non-alternate ones), an empty list * if none. */ public List getOtherLinks() { - if (otherLinks == null) { - otherLinks = new ArrayList(); - } - return otherLinks; + return otherLinks = Lists.createWhenNull(otherLinks); } /** * Sets the feed other links (non-alternate ones). *

- * + * * @param otherLinks the list of Link elements with the feed other links (non-alternate ones) to * set, an empty list or null if none. */ @@ -199,23 +195,20 @@ public class Feed extends WireFeed { /** * Returns the feed author. *

- * + * * @return the feed author, null if none. - * + * */ public List getAuthors() { - if (authors == null) { - authors = new ArrayList(); - } - return authors; + return authors = Lists.createWhenNull(authors); } /** * Sets the feed author. *

- * + * * @param authors the feed author to set, null if none. - * + * */ public void setAuthors(final List authors) { this.authors = authors; @@ -224,24 +217,21 @@ public class Feed extends WireFeed { /** * Returns the feed contributors. *

- * + * * @return a list of Person elements with the feed contributors, an empty list if none. - * + * */ public List getContributors() { - if (contributors == null) { - contributors = new ArrayList(); - } - return contributors; + return contributors = Lists.createWhenNull(contributors); } /** * Sets the feed contributors. *

- * + * * @param contributors the list of Person elements with the feed contributors to set, an empty * list or null if none. - * + * */ public void setContributors(final List contributors) { this.contributors = contributors; @@ -250,7 +240,7 @@ public class Feed extends WireFeed { /** * Returns the feed tag line (Atom 0.3, maps to {@link #getSubtitle()}). *

- * + * * @return the feed tag line, null if none. */ public Content getTagline() { @@ -261,7 +251,7 @@ public class Feed extends WireFeed { * Sets the feed tagline (Atom 0.3, maps to * {@link #setSubtitle(com.sun.syndication.feed.atom.Content)}). *

- * + * * @param tagline the feed tagline to set, null if none. */ public void setTagline(final Content tagline) { @@ -271,9 +261,9 @@ public class Feed extends WireFeed { /** * Returns the feed ID. *

- * + * * @return the feed ID, null if none. - * + * */ public String getId() { return id; @@ -282,9 +272,9 @@ public class Feed extends WireFeed { /** * Sets the feed ID. *

- * + * * @param id the feed ID to set, null if none. - * + * */ public void setId(final String id) { this.id = id; @@ -293,9 +283,9 @@ public class Feed extends WireFeed { /** * Returns the feed generator. *

- * + * * @return the feed generator, null if none. - * + * */ public Generator getGenerator() { return generator; @@ -304,9 +294,9 @@ public class Feed extends WireFeed { /** * Sets the feed generator. *

- * + * * @param generator the feed generator to set, null if none. - * + * */ public void setGenerator(final Generator generator) { this.generator = generator; @@ -315,7 +305,7 @@ public class Feed extends WireFeed { /** * Returns the feed copyright (Atom 0.3, maps to {@link #getRights()}). *

- * + * * @return the feed copyright, null if none. */ public String getCopyright() { @@ -325,7 +315,7 @@ public class Feed extends WireFeed { /** * Sets the feed copyright (Atom 0.3, maps to {@link #setRights(java.lang.String)}). *

- * + * * @param copyright the feed copyright to set, null if none. */ public void setCopyright(final String copyright) { @@ -335,7 +325,7 @@ public class Feed extends WireFeed { /** * Returns the feed info (Atom 0.3 only) *

- * + * * @return the feed info, null if none. */ public Content getInfo() { @@ -345,7 +335,7 @@ public class Feed extends WireFeed { /** * Sets the feed info (Atom 0.3 only) *

- * + * * @param info the feed info to set, null if none. */ public void setInfo(final Content info) { @@ -355,7 +345,7 @@ public class Feed extends WireFeed { /** * Returns the feed modified date (Atom 0.3, maps to {@link #getUpdated()}). *

- * + * * @return the feed modified date, null if none. */ public Date getModified() { @@ -365,7 +355,7 @@ public class Feed extends WireFeed { /** * Sets the feed modified date (Atom 0.3, maps to {@link #setUpdated(java.util.Date)}). *

- * + * * @param modified the feed modified date to set, null if none. */ public void setModified(final Date modified) { @@ -375,24 +365,21 @@ public class Feed extends WireFeed { /** * Returns the feed entries. *

- * + * * @return a list of Entry elements with the feed entries, an empty list if none. - * + * */ public List getEntries() { - if (entries == null) { - entries = new ArrayList(); - } - return entries; + return entries = Lists.createWhenNull(entries); } /** * Sets the feed entries. *

- * + * * @param entries the list of Entry elements with the feed entries to set, an empty list or * null if none. - * + * */ public void setEntries(final List entries) { this.entries = entries; @@ -401,25 +388,22 @@ public class Feed extends WireFeed { /** * Returns the feed modules. *

- * + * * @return a list of ModuleImpl elements with the feed modules, an empty list if none. - * + * */ @Override public List getModules() { - if (modules == null) { - modules = new ArrayList(); - } - return modules; + return modules = Lists.createWhenNull(modules); } /** * Sets the feed moduless. *

- * + * * @param modules the list of ModuleImpl elements with the feed moduless to set, an empty list * or null if none. - * + * */ @Override public void setModules(final List modules) { @@ -429,7 +413,7 @@ public class Feed extends WireFeed { /** * Returns the module identified by a given URI. *

- * + * * @param uri the URI of the ModuleImpl. * @return The module with the given URI, null if none. */ @@ -441,21 +425,18 @@ public class Feed extends WireFeed { /** * Returns the categories *

- * + * * @return Returns the categories. * @since Atom 1.0 */ public List getCategories() { - if (categories == null) { - categories = new ArrayList(); - } - return categories; + return categories = Lists.createWhenNull(categories); } /** * Set the categories *

- * + * * @param categories The categories to set. * @since Atom 1.0 */ @@ -466,7 +447,7 @@ public class Feed extends WireFeed { /** * Returns the icon *

- * + * * @return Returns the icon. * @since Atom 1.0 */ @@ -477,7 +458,7 @@ public class Feed extends WireFeed { /** * Set the icon *

- * + * * @param icon The icon to set. * @since Atom 1.0 */ @@ -488,7 +469,7 @@ public class Feed extends WireFeed { /** * Returns the logo *

- * + * * @return Returns the logo. * @since Atom 1.0 */ @@ -499,7 +480,7 @@ public class Feed extends WireFeed { /** * Set the logo *

- * + * * @param logo The logo to set. * @since Atom 1.0 */ @@ -510,7 +491,7 @@ public class Feed extends WireFeed { /** * Returns the rights *

- * + * * @return Returns the rights. * @since Atom 1.0 */ @@ -521,7 +502,7 @@ public class Feed extends WireFeed { /** * Set the rights *

- * + * * @param rights The rights to set. * @since Atom 1.0 */ @@ -532,7 +513,7 @@ public class Feed extends WireFeed { /** * Returns the subtitle *

- * + * * @return Returns the subtitle. * @since Atom 1.0 */ @@ -543,7 +524,7 @@ public class Feed extends WireFeed { /** * Set the subtitle *

- * + * * @param subtitle The subtitle to set. * @since Atom 1.0 */ @@ -554,7 +535,7 @@ public class Feed extends WireFeed { /** * Returns the updated *

- * + * * @return Returns the updated. * @since Atom 1.0 */ @@ -565,7 +546,7 @@ public class Feed extends WireFeed { /** * Set the updated *

- * + * * @param updated The updated to set. * @since Atom 1.0 */ @@ -576,7 +557,7 @@ public class Feed extends WireFeed { /** * Returns the xmlBase *

- * + * * @return Returns the xmlBase. * @since Atom 1.0 */ @@ -587,7 +568,7 @@ public class Feed extends WireFeed { /** * Set the xmlBase *

- * + * * @param xmlBase The xmlBase to set. * @since Atom 1.0 */ diff --git a/src/main/java/com/sun/syndication/feed/atom/Link.java b/src/main/java/com/sun/syndication/feed/atom/Link.java index 6afd330..878d8b8 100644 --- a/src/main/java/com/sun/syndication/feed/atom/Link.java +++ b/src/main/java/com/sun/syndication/feed/atom/Link.java @@ -18,16 +18,18 @@ package com.sun.syndication.feed.atom; import java.io.Serializable; +import com.rometools.utils.Alternatives; import com.sun.syndication.feed.impl.ObjectBean; /** * Bean for link elements of Atom feeds. *

- * + * * @author Alejandro Abdelnur * @author Dave Johnson (updated for Atom 1.0) */ public class Link implements Cloneable, Serializable { + private static final long serialVersionUID = 670365139518027828L; private final ObjectBean objBean; @@ -43,7 +45,7 @@ public class Link implements Cloneable, Serializable { /** * Default constructor. All properties are set to null. *

- * + * */ public Link() { objBean = new ObjectBean(this.getClass(), this); @@ -52,10 +54,10 @@ public class Link implements Cloneable, Serializable { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -66,10 +68,10 @@ public class Link implements Cloneable, Serializable { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -81,9 +83,9 @@ public class Link implements Cloneable, Serializable { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -93,9 +95,9 @@ public class Link implements Cloneable, Serializable { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -105,9 +107,9 @@ public class Link implements Cloneable, Serializable { /** * Returns the link rel. *

- * + * * @return the link rel, null if none. - * + * */ public String getRel() { return rel; @@ -116,9 +118,9 @@ public class Link implements Cloneable, Serializable { /** * Sets the link rel. *

- * + * * @param rel the link rel,, null if none. - * + * */ public void setRel(final String rel) { // TODO add check, ask P@ about the check @@ -128,9 +130,9 @@ public class Link implements Cloneable, Serializable { /** * Returns the link type. *

- * + * * @return the link type, null if none. - * + * */ public String getType() { return type; @@ -139,9 +141,9 @@ public class Link implements Cloneable, Serializable { /** * Sets the link type. *

- * + * * @param type the link type, null if none. - * + * */ public void setType(final String type) { this.type = type; @@ -150,9 +152,9 @@ public class Link implements Cloneable, Serializable { /** * Returns the link href. *

- * + * * @return the link href, null if none. - * + * */ public String getHref() { return href; @@ -161,9 +163,9 @@ public class Link implements Cloneable, Serializable { /** * Sets the link href. *

- * + * * @param href the link href, null if none. - * + * */ public void setHref(final String href) { this.href = href; @@ -174,19 +176,15 @@ public class Link implements Cloneable, Serializable { } public String getHrefResolved() { - if (hrefResolved != null) { - return hrefResolved; - } else { - return href; - } + return Alternatives.firstNotNull(hrefResolved, href); } /** * Returns the link title. *

- * + * * @return the link title, null if none. - * + * */ public String getTitle() { return title; @@ -195,9 +193,9 @@ public class Link implements Cloneable, Serializable { /** * Sets the link title. *

- * + * * @param title the link title, null if none. - * + * */ public void setTitle(final String title) { this.title = title; @@ -206,7 +204,7 @@ public class Link implements Cloneable, Serializable { /** * Returns the hreflang *

- * + * * @return Returns the hreflang. * @since Atom 1.0 */ @@ -217,7 +215,7 @@ public class Link implements Cloneable, Serializable { /** * Set the hreflang *

- * + * * @param hreflang The hreflang to set. * @since Atom 1.0 */ @@ -228,7 +226,7 @@ public class Link implements Cloneable, Serializable { /** * Returns the length *

- * + * * @return Returns the length. */ public long getLength() { @@ -238,7 +236,7 @@ public class Link implements Cloneable, Serializable { /** * Set the length *

- * + * * @param length The length to set. */ public void setLength(final long length) { diff --git a/src/main/java/com/sun/syndication/feed/atom/Person.java b/src/main/java/com/sun/syndication/feed/atom/Person.java index 1968747..d428022 100644 --- a/src/main/java/com/sun/syndication/feed/atom/Person.java +++ b/src/main/java/com/sun/syndication/feed/atom/Person.java @@ -17,9 +17,10 @@ package com.sun.syndication.feed.atom; import java.io.Serializable; -import java.util.ArrayList; 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.module.Extendable; import com.sun.syndication.feed.module.Module; @@ -29,7 +30,7 @@ import com.sun.syndication.feed.synd.SyndPerson; /** * Bean for person elements of Atom feeds. *

- * + * * @author Alejandro Abdelnur * @author Dave Johnson (updated for Atom 1.0) */ @@ -48,7 +49,7 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Default constructor. All properties are set to null. *

- * + * */ public Person() { objBean = new ObjectBean(this.getClass(), this); @@ -57,10 +58,10 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -71,10 +72,10 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -86,9 +87,9 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -98,9 +99,9 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -110,9 +111,9 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Returns the person name. *

- * + * * @return the person name, null if none. - * + * */ @Override public String getName() { @@ -122,9 +123,9 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Sets the personname. *

- * + * * @param name the person name, null if none. - * + * */ @Override public void setName(final String name) { @@ -134,7 +135,7 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Returns the person URL (same as {@link #getUri()}) *

- * + * * @return the person URL, null if none. */ public String getUrl() { @@ -144,7 +145,7 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Sets the person URL (same as {@link #setUri(java.lang.String)}) *

- * + * * @param url the person URL, null if none. */ public void setUrl(final String url) { @@ -156,19 +157,15 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { } public String getUriResolved(final String resolveURI) { - if (uriResolved != null) { - return uriResolved; - } else { - return uri; - } + return Alternatives.firstNotNull(uriResolved, uri); } /** * Returns the person email. *

- * + * * @return the person email, null if none. - * + * */ @Override public String getEmail() { @@ -178,9 +175,9 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Sets the person email. *

- * + * * @param email the person email, null if none. - * + * */ @Override public void setEmail(final String email) { @@ -190,7 +187,7 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Returns the uri *

- * + * * @return Returns the uri. * @since Atom 1.0 */ @@ -202,7 +199,7 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Set the uri *

- * + * * @param uri The uri to set. * @since Atom 1.0 */ @@ -214,25 +211,22 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Returns the entry modules. *

- * + * * @return a list of ModuleImpl elements with the entry modules, an emtpy list if none. - * + * */ @Override public List getModules() { - if (modules == null) { - modules = new ArrayList(); - } - return modules; + return modules = Lists.createWhenNull(modules); } /** * Sets the entry modules. *

- * + * * @param modules the list of ModuleImpl elements with the entry modules to set, an empty list * or null if none. - * + * */ @Override public void setModules(final List modules) { @@ -242,7 +236,7 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable { /** * Returns the module identified by a given URI. *

- * + * * @param uri the URI of the ModuleImpl. * @return The module with the given URI, null if none. */ diff --git a/src/main/java/com/sun/syndication/feed/module/DCModuleImpl.java b/src/main/java/com/sun/syndication/feed/module/DCModuleImpl.java index e1b7b16..a3d2cb5 100644 --- a/src/main/java/com/sun/syndication/feed/module/DCModuleImpl.java +++ b/src/main/java/com/sun/syndication/feed/module/DCModuleImpl.java @@ -16,7 +16,6 @@ */ package com.sun.syndication.feed.module; -import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -25,6 +24,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.rometools.utils.Lists; import com.sun.syndication.feed.CopyFrom; import com.sun.syndication.feed.impl.CopyFromHelper; import com.sun.syndication.feed.impl.ObjectBean; @@ -32,13 +32,15 @@ import com.sun.syndication.feed.impl.ObjectBean; /** * Dublin Core ModuleImpl, default implementation. *

- * + * * @see Dublin Core module. * @author Alejandro Abdelnur - * + * */ public class DCModuleImpl extends ModuleImpl implements DCModule { + private static final long serialVersionUID = -6502372914221178645L; + private final ObjectBean objBean; private List title; private List creator; @@ -90,7 +92,7 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { /** * Default constructor. All properties are set to null. *

- * + * */ public DCModuleImpl() { super(DCModule.class, URI); @@ -100,25 +102,22 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { /** * Returns the DublinCore module titles. *

- * + * * @return a list of Strings representing the DublinCore module title, an empty list if none. - * + * */ @Override public List getTitles() { - if (title == null) { - title = new ArrayList(); - } - return title; + return title = Lists.createWhenNull(title); } /** * Sets the DublinCore module titles. *

- * + * * @param titles the list of String representing the DublinCore module titles to set, an empty * list or null if none. - * + * */ @Override public void setTitles(final List titles) { @@ -129,54 +128,46 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module title. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module title, null if none. */ @Override public String getTitle() { - if (title != null && !title.isEmpty()) { - return title.get(0); - } else { - return null; - } + return Lists.firstEntry(title); } /** * Sets the DublinCore module title. Convenience method that can be used when there is only one * title to set. *

- * + * * @param title the DublinCore module title to set, null if none. - * + * */ @Override public void setTitle(final String title) { - this.title = new ArrayList(); - this.title.add(title); + this.title = Lists.create(title); } /** * Returns the DublinCore module creator. *

- * + * * @return a list of Strings representing the DublinCore module creator, an empty list if none. - * + * */ @Override public List getCreators() { - if (creator == null) { - creator = new ArrayList(); - } - return creator; + return creator = Lists.createWhenNull(creator); } /** * Sets the DublinCore module creators. *

- * + * * @param creators the list of String representing the DublinCore module creators to set, an * empty list or null if none. - * + * */ @Override public void setCreators(final List creators) { @@ -187,55 +178,47 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module title. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module title, null if none. */ @Override public String getCreator() { - if (creator != null && !creator.isEmpty()) { - return creator.get(0); - } else { - return null; - } + return Lists.firstEntry(creator); } /** * Sets the DublinCore module creator. Convenience method that can be used when there is only * one creator to set. *

- * + * * @param creator the DublinCore module creator to set, null if none. - * + * */ @Override public void setCreator(final String creator) { - this.creator = new ArrayList(); - this.creator.add(creator); + this.creator = Lists.create(creator); } /** * Returns the DublinCore module subjects. *

- * + * * @return a list of DCSubject elements with the DublinCore module subjects, an empty list if * none. - * + * */ @Override public List getSubjects() { - if (subject == null) { - subject = new ArrayList(); - } - return subject; + return subject = Lists.createWhenNull(subject); } /** * Sets the DublinCore module subjects. *

- * + * * @param subjects the list of DCSubject elements with the DublinCore module subjects to set, an * empty list or null if none. - * + * */ @Override public void setSubjects(final List subjects) { @@ -246,55 +229,47 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module subject. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module subject, null if none. */ @Override public DCSubject getSubject() { - if (subject != null && !subject.isEmpty()) { - return subject.get(0); - } else { - return null; - } + return Lists.firstEntry(subject); } /** * Sets the DCSubject element. Convenience method that can be used when there is only one * subject to set. *

- * + * * @param subject the DublinCore module subject to set, null if none. - * + * */ @Override public void setSubject(final DCSubject subject) { - this.subject = new ArrayList(); - this.subject.add(subject); + this.subject = Lists.create(subject); } /** * Returns the DublinCore module description. *

- * + * * @return a list of Strings representing the DublinCore module description, an empty list if * none. - * + * */ @Override public List getDescriptions() { - if (description == null) { - description = new ArrayList(); - } - return description; + return description = Lists.createWhenNull(description); } /** * Sets the DublinCore module descriptions. *

- * + * * @param descriptions the list of String representing the DublinCore module descriptions to * set, an empty list or null if none. - * + * */ @Override public void setDescriptions(final List descriptions) { @@ -305,55 +280,47 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module description. Convenience method that can be used to obtain the * first item, null if none. *

- * + * * @return the first DublinCore module description, null if none. */ @Override public String getDescription() { - if (description != null && !description.isEmpty()) { - return description.get(0); - } else { - return null; - } + return Lists.firstEntry(description); } /** * Sets the DublinCore module description. Convenience method that can be used when there is * only one description to set. *

- * + * * @param description the DublinCore module description to set, null if none. - * + * */ @Override public void setDescription(final String description) { - this.description = new ArrayList(); - this.description.add(description); + this.description = Lists.create(description); } /** * Returns the DublinCore module publisher. *

- * + * * @return a list of Strings representing the DublinCore module publisher, an empty list if * none. - * + * */ @Override public List getPublishers() { - if (publisher == null) { - publisher = new ArrayList(); - } - return publisher; + return publisher = Lists.createWhenNull(publisher); } /** * Sets the DublinCore module publishers. *

- * + * * @param publishers the list of String representing the DublinCore module publishers to set, an * empty list or null if none. - * + * */ @Override public void setPublishers(final List publishers) { @@ -364,55 +331,47 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module title. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module title, null if none. */ @Override public String getPublisher() { - if (publisher != null && !publisher.isEmpty()) { - return publisher.get(0); - } else { - return null; - } + return Lists.firstEntry(publisher); } /** * Sets the DublinCore module publisher. Convenience method that can be used when there is only * one publisher to set. *

- * + * * @param publisher the DublinCore module publisher to set, null if none. - * + * */ @Override public void setPublisher(final String publisher) { - this.publisher = new ArrayList(); - this.publisher.add(publisher); + this.publisher = Lists.create(publisher); } /** * Returns the DublinCore module contributor. *

- * + * * @return a list of Strings representing the DublinCore module contributor, an empty list if * none. - * + * */ @Override public List getContributors() { - if (contributors == null) { - contributors = new ArrayList(); - } - return contributors; + return contributors = Lists.createWhenNull(contributors); } /** * Sets the DublinCore module contributors. *

- * + * * @param contributors the list of String representing the DublinCore module contributors to * set, an empty list or null if none. - * + * */ @Override public void setContributors(final List contributors) { @@ -423,54 +382,46 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module contributor. Convenience method that can be used to obtain the * first item, null if none. *

- * + * * @return the first DublinCore module contributor, null if none. */ @Override public String getContributor() { - if (contributors != null && !contributors.isEmpty()) { - return contributors.get(0); - } else { - return null; - } + return Lists.firstEntry(contributors); } /** * Sets the DublinCore module contributor. Convenience method that can be used when there is * only one contributor to set. *

- * + * * @param contributor the DublinCore module contributor to set, null if none. - * + * */ @Override public void setContributor(final String contributor) { - contributors = new ArrayList(); - contributors.add(contributor); + contributors = Lists.create(contributor); } /** * Returns the DublinCore module date. *

- * + * * @return a list of Strings representing the DublinCore module date, an empty list if none. - * + * */ @Override public List getDates() { - if (date == null) { - date = new ArrayList(); - } - return date; + return date = Lists.createWhenNull(date); } /** * Sets the DublinCore module dates. *

- * + * * @param dates the list of Date representing the DublinCore module dates to set, an empty list * or null if none. - * + * */ @Override public void setDates(final List dates) { @@ -481,54 +432,46 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module date. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module date, null if none. */ @Override public Date getDate() { - if (date != null && !date.isEmpty()) { - return date.get(0); - } else { - return null; - } + return Lists.firstEntry(date); } /** * Sets the DublinCore module date. Convenience method that can be used when there is only one * date to set. *

- * + * * @param date the DublinCore module date to set, null if none. - * + * */ @Override public void setDate(final Date date) { - this.date = new ArrayList(); - this.date.add(date); + this.date = Lists.create(date); } /** * Returns the DublinCore module type. *

- * + * * @return a list of Strings representing the DublinCore module type, an empty list if none. - * + * */ @Override public List getTypes() { - if (type == null) { - type = new ArrayList(); - } - return type; + return type = Lists.createWhenNull(type); } /** * Sets the DublinCore module types. *

- * + * * @param types the list of String representing the DublinCore module types to set, an empty * list or null if none. - * + * */ @Override public void setTypes(final List types) { @@ -539,54 +482,46 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module type. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module type, null if none. */ @Override public String getType() { - if (type != null && !type.isEmpty()) { - return type.get(0); - } else { - return null; - } + return Lists.firstEntry(type); } /** * Sets the DublinCore module type. Convenience method that can be used when there is only one * type to set. *

- * + * * @param type the DublinCore module type to set, null if none. - * + * */ @Override public void setType(final String type) { - this.type = new ArrayList(); - this.type.add(type); + this.type = Lists.create(type); } /** * Returns the DublinCore module format. *

- * + * * @return a list of Strings representing the DublinCore module format, an empty list if none. - * + * */ @Override public List getFormats() { - if (format == null) { - format = new ArrayList(); - } - return format; + return format = Lists.createWhenNull(format); } /** * Sets the DublinCore module formats. *

- * + * * @param formats the list of String representing the DublinCore module formats to set, an empty * list or null if none. - * + * */ @Override public void setFormats(final List formats) { @@ -597,55 +532,47 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module format. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module format, null if none. */ @Override public String getFormat() { - if (format != null && !format.isEmpty()) { - return format.get(0); - } else { - return null; - } + return Lists.firstEntry(format); } /** * Sets the DublinCore module format. Convenience method that can be used when there is only one * format to set. *

- * + * * @param format the DublinCore module format to set, null if none. - * + * */ @Override public void setFormat(final String format) { - this.format = new ArrayList(); - this.format.add(format); + this.format = Lists.create(format); } /** * Returns the DublinCore module identifier. *

- * + * * @return a list of Strings representing the DublinCore module identifier, an empty list if * none. - * + * */ @Override public List getIdentifiers() { - if (identifier == null) { - identifier = new ArrayList(); - } - return identifier; + return identifier = Lists.createWhenNull(identifier); } /** * Sets the DublinCore module identifiers. *

- * + * * @param identifiers the list of String representing the DublinCore module identifiers to set, * an empty list or null if none. - * + * */ @Override public void setIdentifiers(final List identifiers) { @@ -656,54 +583,46 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module identifier. Convenience method that can be used to obtain the * first item, null if none. *

- * + * * @return the first DublinCore module identifier, null if none. */ @Override public String getIdentifier() { - if (identifier != null && !identifier.isEmpty()) { - return identifier.get(0); - } else { - return null; - } + return Lists.firstEntry(identifier); } /** * Sets the DublinCore module identifier. Convenience method that can be used when there is only * one identifier to set. *

- * + * * @param identifier the DublinCore module identifier to set, null if none. - * + * */ @Override public void setIdentifier(final String identifier) { - this.identifier = new ArrayList(); - this.identifier.add(identifier); + this.identifier = Lists.create(identifier); } /** * Returns the DublinCore module source. *

- * + * * @return a list of Strings representing the DublinCore module source, an empty list if none. - * + * */ @Override public List getSources() { - if (source == null) { - source = new ArrayList(); - } - return source; + return source = Lists.createWhenNull(source); } /** * Sets the DublinCore module sources. *

- * + * * @param sources the list of String representing the DublinCore module sources to set, an empty * list or null if none. - * + * */ @Override public void setSources(final List sources) { @@ -714,54 +633,46 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module source. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module source, null if none. */ @Override public String getSource() { - if (source != null && !source.isEmpty()) { - return source.get(0); - } else { - return null; - } + return Lists.firstEntry(source); } /** * Sets the DublinCore module source. Convenience method that can be used when there is only one * source to set. *

- * + * * @param source the DublinCore module source to set, null if none. - * + * */ @Override public void setSource(final String source) { - this.source = new ArrayList(); - this.source.add(source); + this.source = Lists.create(source); } /** * Returns the DublinCore module language. *

- * + * * @return a list of Strings representing the DublinCore module language, an empty list if none. - * + * */ @Override public List getLanguages() { - if (language == null) { - language = new ArrayList(); - } - return language; + return language = Lists.createWhenNull(language); } /** * Sets the DublinCore module languages. *

- * + * * @param languages the list of String representing the DublinCore module languages to set, an * empty list or null if none. - * + * */ @Override public void setLanguages(final List languages) { @@ -772,54 +683,46 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module language. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module langauge, null if none. */ @Override public String getLanguage() { - if (language != null && !language.isEmpty()) { - return language.get(0); - } else { - return null; - } + return Lists.firstEntry(language); } /** * Sets the DublinCore module language. Convenience method that can be used when there is only * one language to set. *

- * + * * @param language the DublinCore module language to set, null if none. - * + * */ @Override public void setLanguage(final String language) { - this.language = new ArrayList(); - this.language.add(language); + this.language = Lists.create(language); } /** * Returns the DublinCore module relation. *

- * + * * @return a list of Strings representing the DublinCore module relation, an empty list if none. - * + * */ @Override public List getRelations() { - if (relation == null) { - relation = new ArrayList(); - } - return relation; + return relation = Lists.createWhenNull(relation); } /** * Sets the DublinCore module relations. *

- * + * * @param relations the list of String representing the DublinCore module relations to set, an * empty list or null if none. - * + * */ @Override public void setRelations(final List relations) { @@ -830,54 +733,46 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module relation. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module relation, null if none. */ @Override public String getRelation() { - if (relation != null && !relation.isEmpty()) { - return relation.get(0); - } else { - return null; - } + return Lists.firstEntry(relation); } /** * Sets the DublinCore module relation. Convenience method that can be used when there is only * one relation to set. *

- * + * * @param relation the DublinCore module relation to set, null if none. - * + * */ @Override public void setRelation(final String relation) { - this.relation = new ArrayList(); - this.relation.add(relation); + this.relation = Lists.create(relation); } /** * Returns the DublinCore module coverage. *

- * + * * @return a list of Strings representing the DublinCore module coverage, an empty list if none. - * + * */ @Override public List getCoverages() { - if (coverage == null) { - coverage = new ArrayList(); - } - return coverage; + return coverage = Lists.createWhenNull(coverage); } /** * Sets the DublinCore module coverages. *

- * + * * @param coverages the list of String representing the DublinCore module coverages to set, an * empty list or null if none. - * + * */ @Override public void setCoverages(final List coverages) { @@ -888,54 +783,46 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module coverage. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module coverage, null if none. */ @Override public String getCoverage() { - if (coverage != null && !coverage.isEmpty()) { - return coverage.get(0); - } else { - return null; - } + return Lists.firstEntry(coverage); } /** * Sets the DublinCore module coverage. Convenience method that can be used when there is only * one coverage to set. *

- * + * * @param coverage the DublinCore module coverage to set, null if none. - * + * */ @Override public void setCoverage(final String coverage) { - this.coverage = new ArrayList(); - this.coverage.add(coverage); + this.coverage = Lists.create(coverage); } /** * Returns the DublinCore module rights. *

- * + * * @return a list of Strings representing the DublinCore module rights, an empty list if none. - * + * */ @Override public List getRightsList() { - if (rights == null) { - rights = new ArrayList(); - } - return rights; + return rights = Lists.createWhenNull(rights); } /** * Sets the DublinCore module rights. *

- * + * * @param rights the list of String representing the DublinCore module rights to set, an empty * list or null if none. - * + * */ @Override public void setRightsList(final List rights) { @@ -946,39 +833,34 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Gets the DublinCore module rights. Convenience method that can be used to obtain the first * item, null if none. *

- * + * * @return the first DublinCore module rights, null if none. */ @Override public String getRights() { - if (rights != null && !rights.isEmpty()) { - return rights.get(0); - } else { - return null; - } + return Lists.firstEntry(rights); } /** * Sets the DublinCore module rights. Convenience method that can be used when there is only one * rights to set. *

- * + * * @param rights the DublinCore module rights to set, null if none. - * + * */ @Override public void setRights(final String rights) { - this.rights = new ArrayList(); - this.rights.add(rights); + this.rights = Lists.create(rights); } /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public final Object clone() throws CloneNotSupportedException { @@ -989,10 +871,10 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public final boolean equals(final Object other) { @@ -1004,9 +886,9 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public final int hashCode() { @@ -1016,9 +898,9 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public final String toString() { @@ -1060,4 +942,5 @@ public class DCModuleImpl extends ModuleImpl implements DCModule { COPY_FROM_HELPER = new CopyFromHelper(DCModule.class, basePropInterfaceMap, basePropClassImplMap); } + } diff --git a/src/main/java/com/sun/syndication/feed/module/DCSubjectImpl.java b/src/main/java/com/sun/syndication/feed/module/DCSubjectImpl.java index 5244c52..d3f8099 100644 --- a/src/main/java/com/sun/syndication/feed/module/DCSubjectImpl.java +++ b/src/main/java/com/sun/syndication/feed/module/DCSubjectImpl.java @@ -28,21 +28,34 @@ import com.sun.syndication.feed.impl.ObjectBean; /** * Subject of the Dublin Core ModuleImpl, default implementation. *

- * + * * @see Dublin Core module. * @author Alejandro Abdelnur - * + * */ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { + private static final long serialVersionUID = 6276396184267118968L; + private static final CopyFromHelper COPY_FROM_HELPER; + private final ObjectBean objBean; private String taxonomyUri; private String value; + static { + final Map> basePropInterfaceMap = new HashMap>(); + basePropInterfaceMap.put("taxonomyUri", String.class); + basePropInterfaceMap.put("value", String.class); + + final Map, Class> basePropClassImplMap = Collections., Class> emptyMap(); + + COPY_FROM_HELPER = new CopyFromHelper(DCSubject.class, basePropInterfaceMap, basePropClassImplMap); + } + /** * Default constructor. All properties are set to null. *

- * + * */ public DCSubjectImpl() { objBean = new ObjectBean(this.getClass(), this); @@ -51,10 +64,10 @@ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -65,10 +78,10 @@ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -83,9 +96,9 @@ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -95,9 +108,9 @@ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -107,9 +120,9 @@ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { /** * Returns the DublinCore subject taxonomy URI. *

- * + * * @return the DublinCore subject taxonomy URI, null if none. - * + * */ @Override public String getTaxonomyUri() { @@ -119,9 +132,9 @@ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { /** * Sets the DublinCore subject taxonomy URI. *

- * + * * @param taxonomyUri the DublinCore subject taxonomy URI to set, null if none. - * + * */ @Override public void setTaxonomyUri(final String taxonomyUri) { @@ -131,9 +144,9 @@ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { /** * Returns the DublinCore subject value. *

- * + * * @return the DublinCore subject value, null if none. - * + * */ @Override public String getValue() { @@ -143,9 +156,9 @@ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { /** * Sets the DublinCore subject value. *

- * + * * @param value the DublinCore subject value to set, null if none. - * + * */ @Override public void setValue(final String value) { @@ -162,16 +175,4 @@ public class DCSubjectImpl implements Cloneable, Serializable, DCSubject { COPY_FROM_HELPER.copy(this, obj); } - private static final CopyFromHelper COPY_FROM_HELPER; - - static { - final Map> basePropInterfaceMap = new HashMap>(); - basePropInterfaceMap.put("taxonomyUri", String.class); - basePropInterfaceMap.put("value", String.class); - - final Map, Class> basePropClassImplMap = Collections., Class> emptyMap(); - - COPY_FROM_HELPER = new CopyFromHelper(DCSubject.class, basePropInterfaceMap, basePropClassImplMap); - } - } diff --git a/src/main/java/com/sun/syndication/feed/module/SyModuleImpl.java b/src/main/java/com/sun/syndication/feed/module/SyModuleImpl.java index 8c54e47..1b8629f 100644 --- a/src/main/java/com/sun/syndication/feed/module/SyModuleImpl.java +++ b/src/main/java/com/sun/syndication/feed/module/SyModuleImpl.java @@ -23,37 +23,50 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import com.rometools.utils.Dates; import com.sun.syndication.feed.CopyFrom; import com.sun.syndication.feed.impl.CopyFromHelper; /** * Syndication ModuleImpl, default implementation. *

- * + * * @see Syndication module. * @author Alejandro Abdelnur - * + * */ public class SyModuleImpl extends ModuleImpl implements SyModule { - private static final long serialVersionUID = -8345879299577437933L; - private static final Set PERIODS = new HashSet(); - static { - PERIODS.add(HOURLY); - PERIODS.add(DAILY); - PERIODS.add(WEEKLY); - PERIODS.add(MONTHLY); - PERIODS.add(YEARLY); - } + private static final long serialVersionUID = -8345879299577437933L; + + private static final Set PERIODS = new HashSet(); + private static final CopyFromHelper COPY_FROM_HELPER; private String updatePeriod; private int updateFrequency; private Date updateBase; + static { + + PERIODS.add(HOURLY); + PERIODS.add(DAILY); + PERIODS.add(WEEKLY); + PERIODS.add(MONTHLY); + PERIODS.add(YEARLY); + + final Map> basePropInterfaceMap = new HashMap>(); + basePropInterfaceMap.put("updatePeriod", String.class); + basePropInterfaceMap.put("updateFrequency", Integer.TYPE); + basePropInterfaceMap.put("updateBase", Date.class); + final Map, Class> basePropClassImplMap = Collections., Class> emptyMap(); + COPY_FROM_HELPER = new CopyFromHelper(SyModule.class, basePropInterfaceMap, basePropClassImplMap); + + } + /** * Default constructor. All properties are set to null. *

- * + * */ public SyModuleImpl() { super(SyModule.class, URI); @@ -62,9 +75,9 @@ public class SyModuleImpl extends ModuleImpl implements SyModule { /** * Returns the Syndication module update period. *

- * + * * @return the Syndication module update period, null if none. - * + * */ @Override public String getUpdatePeriod() { @@ -74,9 +87,9 @@ public class SyModuleImpl extends ModuleImpl implements SyModule { /** * Sets the Syndication module update period. *

- * + * * @param updatePeriod the Syndication module update period to set, null if none. - * + * */ @Override public void setUpdatePeriod(final String updatePeriod) { @@ -89,9 +102,9 @@ public class SyModuleImpl extends ModuleImpl implements SyModule { /** * Returns the Syndication module update frequency. *

- * + * * @return the Syndication module update frequency, null if none. - * + * */ @Override public int getUpdateFrequency() { @@ -101,9 +114,9 @@ public class SyModuleImpl extends ModuleImpl implements SyModule { /** * Sets the Syndication module update frequency. *

- * + * * @param updateFrequency the Syndication module update frequency to set, null if none. - * + * */ @Override public void setUpdateFrequency(final int updateFrequency) { @@ -113,25 +126,25 @@ public class SyModuleImpl extends ModuleImpl implements SyModule { /** * Returns the Syndication module update base date. *

- * + * * @return the Syndication module update base date, null if none. - * + * */ @Override public Date getUpdateBase() { - return updateBase != null ? new Date(updateBase.getTime()) : null; + return Dates.copy(updateBase); } /** * Sets the Syndication module update base date. *

- * + * * @param updateBase the Syndication module update base date to set, null if none. - * + * */ @Override public void setUpdateBase(final Date updateBase) { - this.updateBase = new Date(updateBase.getTime()); + this.updateBase = Dates.copy(updateBase); } @Override @@ -144,17 +157,4 @@ public class SyModuleImpl extends ModuleImpl implements SyModule { COPY_FROM_HELPER.copy(this, obj); } - private static final CopyFromHelper COPY_FROM_HELPER; - - static { - final Map> basePropInterfaceMap = new HashMap>(); - basePropInterfaceMap.put("updatePeriod", String.class); - basePropInterfaceMap.put("updateFrequency", Integer.TYPE); - basePropInterfaceMap.put("updateBase", Date.class); - - final Map, Class> basePropClassImplMap = Collections., Class> emptyMap(); - - COPY_FROM_HELPER = new CopyFromHelper(SyModule.class, basePropInterfaceMap, basePropClassImplMap); - } - } diff --git a/src/main/java/com/sun/syndication/feed/rss/Channel.java b/src/main/java/com/sun/syndication/feed/rss/Channel.java index c03f358..a239b21 100644 --- a/src/main/java/com/sun/syndication/feed/rss/Channel.java +++ b/src/main/java/com/sun/syndication/feed/rss/Channel.java @@ -17,13 +17,14 @@ */ package com.sun.syndication.feed.rss; -import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.List; 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.module.Module; import com.sun.syndication.feed.module.impl.ModuleUtils; @@ -34,12 +35,14 @@ import com.sun.syndication.feed.module.impl.ModuleUtils; * It handles all RSS versions (0.9, 0.91, 0.92, 0.93, 0.94, 1.0 and 2.0) without losing * information. *

- * + * * @author Alejandro Abdelnur - * + * */ public class Channel extends WireFeed { + private static final long serialVersionUID = 745207486449728472L; + public static final String SUNDAY = "sunday"; public static final String MONDAY = "monday"; public static final String TUESDAY = "tuesday"; @@ -87,7 +90,7 @@ public class Channel extends WireFeed { /** * Default constructor, for bean cloning purposes only. - * + * */ public Channel() { } @@ -95,9 +98,9 @@ public class Channel extends WireFeed { /** * Channel Constructor. All properties, except the type, are set to null. *

- * + * * @param type the type of the RSS feed. - * + * */ public Channel(final String type) { super(type); @@ -106,9 +109,9 @@ public class Channel extends WireFeed { /** * Returns the channel title. *

- * + * * @return the channel title, null if none. - * + * */ public String getTitle() { return title; @@ -117,9 +120,9 @@ public class Channel extends WireFeed { /** * Sets the channel title. *

- * + * * @param title the channel title to set, null if none. - * + * */ public void setTitle(final String title) { this.title = title; @@ -128,9 +131,9 @@ public class Channel extends WireFeed { /** * Returns the channel description. *

- * + * * @return the channel description, null if none. - * + * */ public String getDescription() { return description; @@ -139,9 +142,9 @@ public class Channel extends WireFeed { /** * Sets the channel description. *

- * + * * @param description the channel description to set, null if none. - * + * */ public void setDescription(final String description) { this.description = description; @@ -150,9 +153,9 @@ public class Channel extends WireFeed { /** * Returns the channel link. *

- * + * * @return the channel link, null if none. - * + * */ public String getLink() { return link; @@ -161,9 +164,9 @@ public class Channel extends WireFeed { /** * Sets the channel link. *

- * + * * @param link the channel link to set, null if none. - * + * */ public void setLink(final String link) { this.link = link; @@ -172,7 +175,7 @@ public class Channel extends WireFeed { /** * Returns the channel uri. *

- * + * * @return the channel uri, null if none. */ public String getUri() { @@ -182,7 +185,7 @@ public class Channel extends WireFeed { /** * Sets the channel uri. *

- * + * * @param uri the channel uri, null if none. */ public void setUri(final String uri) { @@ -192,9 +195,9 @@ public class Channel extends WireFeed { /** * Returns the channel image. *

- * + * * @return the channel image, null if none. - * + * */ public Image getImage() { return image; @@ -203,9 +206,9 @@ public class Channel extends WireFeed { /** * Sets the channel image. *

- * + * * @param image the channel image to set, null if none. - * + * */ public void setImage(final Image image) { this.image = image; @@ -214,24 +217,21 @@ public class Channel extends WireFeed { /** * Returns the channel items. *

- * + * * @return a list of Item elements with the channel items, an empty list if none. - * + * */ public List getItems() { - if (items == null) { - items = new ArrayList(); - } - return items; + return items = Lists.createWhenNull(items); } /** * Sets the channel items. *

- * + * * @param items the list of Item elements with the channel items to set, an empty list or * null if none. - * + * */ public void setItems(final List items) { this.items = items; @@ -240,9 +240,9 @@ public class Channel extends WireFeed { /** * Returns the channel text input. *

- * + * * @return the channel text input, null if none. - * + * */ public TextInput getTextInput() { return textInput; @@ -251,9 +251,9 @@ public class Channel extends WireFeed { /** * Sets the channel text input. *

- * + * * @param textInput the channel text input to set, null if none. - * + * */ public void setTextInput(final TextInput textInput) { this.textInput = textInput; @@ -262,9 +262,9 @@ public class Channel extends WireFeed { /** * Returns the channel language. *

- * + * * @return the channel language, null if none. - * + * */ public String getLanguage() { return language; @@ -273,9 +273,9 @@ public class Channel extends WireFeed { /** * Sets the channel language. *

- * + * * @param language the channel language to set, null if none. - * + * */ public void setLanguage(final String language) { this.language = language; @@ -284,9 +284,9 @@ public class Channel extends WireFeed { /** * Returns the channel rating. *

- * + * * @return the channel rating, null if none. - * + * */ public String getRating() { return rating; @@ -295,9 +295,9 @@ public class Channel extends WireFeed { /** * Sets the channel rating. *

- * + * * @param rating the channel rating to set, null if none. - * + * */ public void setRating(final String rating) { this.rating = rating; @@ -306,9 +306,9 @@ public class Channel extends WireFeed { /** * Returns the channel copyright. *

- * + * * @return the channel copyright, null if none. - * + * */ public String getCopyright() { return copyright; @@ -317,9 +317,9 @@ public class Channel extends WireFeed { /** * Sets the channel copyright. *

- * + * * @param copyright the channel copyright to set, null if none. - * + * */ public void setCopyright(final String copyright) { this.copyright = copyright; @@ -328,69 +328,53 @@ public class Channel extends WireFeed { /** * Returns the channel publishing date. *

- * + * * @return the channel publishing date, null if none. - * + * */ public Date getPubDate() { - if (pubDate == null) { - return null; - } else { - return new Date(pubDate.getTime()); - } + return Dates.copy(pubDate); } /** * Sets the channel publishing date. *

- * + * * @param pubDate the channel publishing date to set, null if none. - * + * */ public void setPubDate(final Date pubDate) { - if (pubDate == null) { - this.pubDate = null; - } else { - this.pubDate = new Date(pubDate.getTime()); - } + this.pubDate = Dates.copy(pubDate); } /** * Returns the channel last build date. *

- * + * * @return the channel last build date, null if none. - * + * */ public Date getLastBuildDate() { - if (lastBuildDate == null) { - return null; - } else { - return new Date(lastBuildDate.getTime()); - } + return Dates.copy(lastBuildDate); } /** * Sets the channel last build date. *

- * + * * @param lastBuildDate the channel last build date to set, null if none. - * + * */ public void setLastBuildDate(final Date lastBuildDate) { - if (lastBuildDate == null) { - this.lastBuildDate = null; - } else { - this.lastBuildDate = new Date(lastBuildDate.getTime()); - } + this.lastBuildDate = Dates.copy(lastBuildDate); } /** * Returns the channel docs. *

- * + * * @return the channel docs, null if none. - * + * */ public String getDocs() { return docs; @@ -399,9 +383,9 @@ public class Channel extends WireFeed { /** * Sets the channel docs. *

- * + * * @param docs the channel docs to set, null if none. - * + * */ public void setDocs(final String docs) { this.docs = docs; @@ -410,9 +394,9 @@ public class Channel extends WireFeed { /** * Returns the channel managing editor. *

- * + * * @return the channel managing editor, null if none. - * + * */ public String getManagingEditor() { return managingEditor; @@ -421,9 +405,9 @@ public class Channel extends WireFeed { /** * Sets the channel managing editor. *

- * + * * @param managingEditor the channel managing editor to set, null if none. - * + * */ public void setManagingEditor(final String managingEditor) { this.managingEditor = managingEditor; @@ -432,9 +416,9 @@ public class Channel extends WireFeed { /** * Returns the channel web master. *

- * + * * @return the channel web master, null if none. - * + * */ public String getWebMaster() { return webMaster; @@ -443,9 +427,9 @@ public class Channel extends WireFeed { /** * Sets the channel web master. *

- * + * * @param webMaster the channel web master to set, null if none. - * + * */ public void setWebMaster(final String webMaster) { this.webMaster = webMaster; @@ -454,25 +438,21 @@ public class Channel extends WireFeed { /** * Returns the channel skip hours. *

- * + * * @return a list of Integer elements with the channel skip hours, an empty list if none. - * + * */ public List getSkipHours() { - if (skipHours != null) { - return skipHours; - } else { - return new ArrayList(); - } + return Lists.createWhenNull(skipHours); } /** * Sets the channel skip hours. *

- * + * * @param skipHours the list of Integer elements with the channel skip hours to set, an empty * list or null if none. - * + * */ public void setSkipHours(final List skipHours) { if (skipHours != null) { @@ -494,25 +474,21 @@ public class Channel extends WireFeed { /** * Returns the channel skip days. *

- * + * * @return a list of Day elements with the channel skip days, an empty list if none. - * + * */ public List getSkipDays() { - if (skipDays != null) { - return skipDays; - } else { - return new ArrayList(); - } + return Lists.createWhenNull(skipDays); } /** * Sets the channel skip days. *

- * + * * @param skipDays the list of Day elements with the channel skip days to set, an empty list or * null if none. - * + * */ public void setSkipDays(final List skipDays) { if (skipDays != null) { @@ -535,9 +511,9 @@ public class Channel extends WireFeed { /** * Returns the channel cloud. *

- * + * * @return the channel cloud, null if none. - * + * */ public Cloud getCloud() { return cloud; @@ -546,9 +522,9 @@ public class Channel extends WireFeed { /** * Sets the channel cloud. *

- * + * * @param cloud the channel cloud to set, null if none. - * + * */ public void setCloud(final Cloud cloud) { this.cloud = cloud; @@ -557,24 +533,21 @@ public class Channel extends WireFeed { /** * Returns the channel categories. *

- * + * * @return a list of Category elements with the channel categories, an empty list if none. - * + * */ public List getCategories() { - if (categories == null) { - categories = new ArrayList(); - } - return categories; + return categories = Lists.createWhenNull(categories); } /** * Sets the channel categories. *

- * + * * @param categories the list of Category elements with the channel categories to set, an empty * list or null if none. - * + * */ public void setCategories(final List categories) { this.categories = categories; @@ -583,9 +556,9 @@ public class Channel extends WireFeed { /** * Returns the channel generator. *

- * + * * @return the channel generator, null if none. - * + * */ public String getGenerator() { return generator; @@ -594,9 +567,9 @@ public class Channel extends WireFeed { /** * Sets the channel generator. *

- * + * * @param generator the channel generator to set, null if none. - * + * */ public void setGenerator(final String generator) { this.generator = generator; @@ -605,9 +578,9 @@ public class Channel extends WireFeed { /** * Returns the channel time to live. *

- * + * * @return the channel time to live, null if none. - * + * */ public int getTtl() { return ttl; @@ -616,9 +589,9 @@ public class Channel extends WireFeed { /** * Sets the channel time to live. *

- * + * * @param ttl the channel time to live to set, null if none. - * + * */ public void setTtl(final int ttl) { this.ttl = ttl; @@ -627,25 +600,22 @@ public class Channel extends WireFeed { /** * Returns the channel modules. *

- * + * * @return a list of ModuleImpl elements with the channel modules, an empty list if none. - * + * */ @Override public List getModules() { - if (modules == null) { - modules = new ArrayList(); - } - return modules; + return modules = Lists.createWhenNull(modules); } /** * Sets the channel modules. *

- * + * * @param modules the list of ModuleImpl elements with the channel modules to set, an empty list * or null if none. - * + * */ @Override public void setModules(final List modules) { @@ -655,7 +625,7 @@ public class Channel extends WireFeed { /** * Returns the module identified by a given URI. *

- * + * * @param uri the URI of the ModuleImpl. * @return The module with the given URI, null if none. */ diff --git a/src/main/java/com/sun/syndication/feed/rss/Item.java b/src/main/java/com/sun/syndication/feed/rss/Item.java index abe5c33..3e86da8 100644 --- a/src/main/java/com/sun/syndication/feed/rss/Item.java +++ b/src/main/java/com/sun/syndication/feed/rss/Item.java @@ -18,12 +18,13 @@ package com.sun.syndication.feed.rss; import java.io.Serializable; -import java.util.ArrayList; import java.util.Date; import java.util.List; 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.module.Extendable; import com.sun.syndication.feed.module.Module; @@ -37,13 +38,16 @@ import com.sun.syndication.feed.module.impl.ModuleUtils; * For RSS1.0 it supports Dublin Core and Syndication modules. Note that those modules currently * support simple syntax format only. *

- * + * * @author Alejandro Abdelnur - * + * */ public class Item implements Cloneable, Serializable, Extendable { + private static final long serialVersionUID = 3741763947754555947L; + private final ObjectBean objBean; + private String title; private String link; private String uri; @@ -63,7 +67,7 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Default constructor. All properties are set to null. *

- * + * */ public Item() { objBean = new ObjectBean(this.getClass(), this); @@ -72,10 +76,10 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -86,10 +90,10 @@ public class Item implements Cloneable, Serializable, Extendable { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -110,9 +114,9 @@ public class Item implements Cloneable, Serializable, Extendable { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -122,9 +126,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -134,9 +138,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item title. *

- * + * * @return the item title, null if none. - * + * */ public String getTitle() { return title; @@ -145,9 +149,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Sets the item title. *

- * + * * @param title the item title to set, null if none. - * + * */ public void setTitle(final String title) { this.title = title; @@ -156,9 +160,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item link. *

- * + * * @return the item link, null if none. - * + * */ public String getLink() { return link; @@ -167,9 +171,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Sets the item link. *

- * + * * @param link the item link to set, null if none. - * + * */ public void setLink(final String link) { this.link = link; @@ -178,7 +182,7 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item uri. *

- * + * * @return the item uri, null if none. */ public String getUri() { @@ -188,7 +192,7 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Sets the item uri. *

- * + * * @param uri the item uri to set, null if none. */ public void setUri(final String uri) { @@ -198,9 +202,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item description. *

- * + * * @return the item description, null if none. - * + * */ public Description getDescription() { return description; @@ -209,9 +213,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Sets the item description. *

- * + * * @param description the item description to set, null if none. - * + * */ public void setDescription(final Description description) { this.description = description; @@ -220,9 +224,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item content. *

- * + * * @return the item content, null if none. - * + * */ public Content getContent() { return content; @@ -231,9 +235,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Sets the item content. *

- * + * * @param content the item content to set, null if none. - * + * */ public void setContent(final Content content) { this.content = content; @@ -242,9 +246,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item source. *

- * + * * @return the item source, null if none. - * + * */ public Source getSource() { return source; @@ -253,9 +257,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Sets the item source. *

- * + * * @param source the item source to set, null if none. - * + * */ public void setSource(final Source source) { this.source = source; @@ -264,24 +268,21 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item enclosures. *

- * + * * @return a list of Enclosure elements with the item enclosures, an empty list if none. - * + * */ public List getEnclosures() { - if (enclosures == null) { - enclosures = new ArrayList(); - } - return enclosures; + return enclosures = Lists.createWhenNull(enclosures); } /** * Sets the item enclosures. *

- * + * * @param enclosures the list of Enclosure elements with the item enclosures to set, an empty * list or null if none. - * + * */ public void setEnclosures(final List enclosures) { this.enclosures = enclosures; @@ -290,24 +291,21 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item categories. *

- * + * * @return a list of Category elements with the item categories, an empty list if none. - * + * */ public List getCategories() { - if (categories == null) { - categories = new ArrayList(); - } - return categories; + return categories = Lists.createWhenNull(categories); } /** * Sets the item categories. *

- * + * * @param categories the list of Categories elements with the item categories to set, an empty * list or null if none. - * + * */ public void setCategories(final List categories) { this.categories = categories; @@ -316,9 +314,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item GUID. *

- * + * * @return the item GUID, null if none. - * + * */ public Guid getGuid() { return guid; @@ -327,9 +325,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Sets the item GUID. *

- * + * * @param guid the item GUID to set, null if none. - * + * */ public void setGuid(final Guid guid) { this.guid = guid; @@ -338,9 +336,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item comments. *

- * + * * @return the item comments, null if none. - * + * */ public String getComments() { return comments; @@ -349,9 +347,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Sets the item comments. *

- * + * * @param comments the item comments to set, null if none. - * + * */ public void setComments(final String comments) { this.comments = comments; @@ -360,9 +358,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item author. *

- * + * * @return the item author, null if none. - * + * */ public String getAuthor() { return author; @@ -371,9 +369,9 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Sets the item author. *

- * + * * @param author the item author to set, null if none. - * + * */ public void setAuthor(final String author) { this.author = author; @@ -382,25 +380,22 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item modules. *

- * + * * @return a list of ModuleImpl elements with the item modules, an empty list if none. - * + * */ @Override public List getModules() { - if (modules == null) { - modules = new ArrayList(); - } - return modules; + return modules = Lists.createWhenNull(modules); } /** * Sets the item modules. *

- * + * * @param modules the list of ModuleImpl elements with the item modules to set, an empty list or * null if none. - * + * */ @Override public void setModules(final List modules) { @@ -410,7 +405,7 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the module identified by a given URI. *

- * + * * @param uri the URI of the ModuleImpl. * @return The module with the given URI, null if none. */ @@ -422,83 +417,64 @@ public class Item implements Cloneable, Serializable, Extendable { /** * Returns the item publishing date. *

- * + * * @return the item publishing date, null if none. - * + * */ public Date getPubDate() { - if (pubDate == null) { - return null; - } else { - return new Date(pubDate.getTime()); - } + return Dates.copy(pubDate); } /** * Sets the item publishing date. *

- * + * * @param pubDate the item publishing date to set, null if none. - * + * */ public void setPubDate(final Date pubDate) { - if (pubDate == null) { - this.pubDate = null; - } else { - this.pubDate = new Date(pubDate.getTime()); - } + this.pubDate = Dates.copy(pubDate); } /** * Returns the item expiration date. *

- * + * * @return the item expiration date, null if none. - * + * */ public Date getExpirationDate() { - if (expirationDate == null) { - return null; - } else { - return new Date(expirationDate.getTime()); - } + return Dates.copy(expirationDate); } /** * Sets the item expiration date. *

- * + * * @param expirationDate the item expiration date to set, null if none. - * + * */ public void setExpirationDate(final Date expirationDate) { - if (expirationDate == null) { - this.expirationDate = null; - } else { - this.expirationDate = new Date(expirationDate.getTime()); - } + this.expirationDate = Dates.copy(expirationDate); } /** * Returns foreign markup found at item level. *

- * + * * @return Opaque object to discourage use - * + * */ public List getForeignMarkup() { - if (foreignMarkup == null) { - foreignMarkup = new ArrayList(); - } - return foreignMarkup; + return foreignMarkup = Lists.createWhenNull(foreignMarkup); } /** * Sets foreign markup found at item level. *

- * + * * @param foreignMarkup Opaque object to discourage use - * + * */ public void setForeignMarkup(final List foreignMarkup) { this.foreignMarkup = foreignMarkup; diff --git a/src/main/java/com/sun/syndication/feed/synd/SyndCategoryImpl.java b/src/main/java/com/sun/syndication/feed/synd/SyndCategoryImpl.java index 58944d1..120f8d1 100644 --- a/src/main/java/com/sun/syndication/feed/synd/SyndCategoryImpl.java +++ b/src/main/java/com/sun/syndication/feed/synd/SyndCategoryImpl.java @@ -1,7 +1,7 @@ /* * Copyright 2004 Sun Microsystems, Inc. * Copyright 2011 ROME Team - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -18,11 +18,8 @@ package com.sun.syndication.feed.synd; import java.io.Serializable; -import java.util.AbstractList; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import com.sun.syndication.feed.CopyFrom; @@ -34,20 +31,32 @@ import com.sun.syndication.feed.module.DCSubjectImpl; /** * Bean for categories of SyndFeedImpl feeds and entries. *

- * + * * @author Alejandro Abdelnur - * + * */ public class SyndCategoryImpl implements Serializable, SyndCategory { + private static final long serialVersionUID = -2151815243404151131L; + + private static final CopyFromHelper COPY_FROM_HELPER; + private final ObjectBean objBean; private final DCSubject subject; + static { + final Map> basePropInterfaceMap = new HashMap>(); + basePropInterfaceMap.put("name", String.class); + basePropInterfaceMap.put("taxonomyUri", String.class); + final Map, 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 * with extended interfaces. *

- * + * * @param subject the DC subject to wrap. */ SyndCategoryImpl(final DCSubject subject) { @@ -58,10 +67,10 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -72,10 +81,10 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -90,9 +99,9 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -102,9 +111,9 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -114,9 +123,9 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { /** * Package private constructor, used by SyndCategoryListFacade. *

- * + * * @return the DC subject being wrapped. - * + * */ DCSubject getSubject() { return subject; @@ -125,7 +134,7 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { /** * Default constructor. All properties are set to null. *

- * + * */ public SyndCategoryImpl() { this(new DCSubjectImpl()); @@ -134,9 +143,9 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { /** * Returns the category name. *

- * + * * @return the category name, null if none. - * + * */ @Override public String getName() { @@ -146,9 +155,9 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { /** * Sets the category name. *

- * + * * @param name the category name to set, null if none. - * + * */ @Override public void setName(final String name) { @@ -158,9 +167,9 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { /** * Returns the category taxonomy URI. *

- * + * * @return the category taxonomy URI, null if none. - * + * */ @Override public String getTaxonomyUri() { @@ -170,9 +179,9 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { /** * Sets the category taxonomy URI. *

- * + * * @param taxonomyUri the category taxonomy URI to set, null if none. - * + * */ @Override public void setTaxonomyUri(final String taxonomyUri) { @@ -189,168 +198,4 @@ public class SyndCategoryImpl implements Serializable, SyndCategory { COPY_FROM_HELPER.copy(this, obj); } - private static final CopyFromHelper COPY_FROM_HELPER; - - static { - final Map> basePropInterfaceMap = new HashMap>(); - basePropInterfaceMap.put("name", String.class); - basePropInterfaceMap.put("taxonomyUri", String.class); - final Map, 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. - *

- * 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. - *

- * This is necessary because the SyndFeedImpl categories are just a convenience to access the - * DublinCore subjects. - *

- * All this mess to avoid making DCSubjectImpl implement SyndCategory (which it would be odd). - *

- * - * @author Alejandro Abdelnur - * - */ -class SyndCategoryListFacade extends AbstractList { - private final List subjects; - - /** - * Default constructor. Creates and empty list. - */ - public SyndCategoryListFacade() { - this(new ArrayList()); - } - - /** - * Creates a facade list of categories on top the given subject list. - *

- * - * @param subjects the list of subjects to create the facade. - * - */ - public SyndCategoryListFacade(final List subjects) { - this.subjects = subjects; - } - - /** - * Gets the category by index. - *

- * - * @param index the index position to retrieve the category. - * @return the SyndCategoryImpl in position index, null if none. - * - */ - @Override - public SyndCategory get(final int index) { - return new SyndCategoryImpl(subjects.get(index)); - } - - /** - * Returns the size of the list. - *

- * - * @return the size of the list. - * - */ - @Override - public int size() { - return subjects.size(); - } - - /** - * Sets a category in an existing position in the list. - *

- * - * @param index position to set the category. - * @param obj the SyndCategoryImpl object to set. - * @return the SyndCategoryImpl object that is being replaced, null 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. - *

- * - * @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. - *

- * - * @param index position to remove the category from. - * @return the SyndCategoryImpl being removed from position index, null 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. - *

- * - * @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 convertElementsSyndCategoryToSubject(final List cList) { - List sList = null; - if (cList != null) { - sList = new ArrayList(); - 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; - } - } diff --git a/src/main/java/com/sun/syndication/feed/synd/SyndCategoryListFacade.java b/src/main/java/com/sun/syndication/feed/synd/SyndCategoryListFacade.java new file mode 100644 index 0000000..8671bec --- /dev/null +++ b/src/main/java/com/sun/syndication/feed/synd/SyndCategoryListFacade.java @@ -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. + *

+ * 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. + *

+ * This is necessary because the SyndFeedImpl categories are just a convenience to access the + * DublinCore subjects. + *

+ * All this mess to avoid making DCSubjectImpl implement SyndCategory (which it would be odd). + *

+ * + * @author Alejandro Abdelnur + * + */ +class SyndCategoryListFacade extends AbstractList { + + private final List subjects; + + /** + * Default constructor. Creates and empty list. + */ + public SyndCategoryListFacade() { + this(new ArrayList()); + } + + /** + * Creates a facade list of categories on top the given subject list. + *

+ * + * @param subjects the list of subjects to create the facade. + * + */ + public SyndCategoryListFacade(final List subjects) { + this.subjects = subjects; + } + + /** + * Gets the category by index. + *

+ * + * @param index the index position to retrieve the category. + * @return the SyndCategoryImpl in position index, null if none. + * + */ + @Override + public SyndCategory get(final int index) { + return new SyndCategoryImpl(subjects.get(index)); + } + + /** + * Returns the size of the list. + *

+ * + * @return the size of the list. + * + */ + @Override + public int size() { + return subjects.size(); + } + + /** + * Sets a category in an existing position in the list. + *

+ * + * @param index position to set the category. + * @param obj the SyndCategoryImpl object to set. + * @return the SyndCategoryImpl object that is being replaced, null 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. + *

+ * + * @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. + *

+ * + * @param index position to remove the category from. + * @return the SyndCategoryImpl being removed from position index, null 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. + *

+ * + * @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 convertElementsSyndCategoryToSubject(final List cList) { + List sList = null; + if (cList != null) { + sList = new ArrayList(); + 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; + } + +} \ No newline at end of file diff --git a/src/main/java/com/sun/syndication/feed/synd/SyndContentImpl.java b/src/main/java/com/sun/syndication/feed/synd/SyndContentImpl.java index 6393f5d..c979e08 100644 --- a/src/main/java/com/sun/syndication/feed/synd/SyndContentImpl.java +++ b/src/main/java/com/sun/syndication/feed/synd/SyndContentImpl.java @@ -28,21 +28,36 @@ import com.sun.syndication.feed.impl.ObjectBean; /** * Bean for content of SyndFeedImpl entries. *

- * + * * @author Alejandro Abdelnur - * + * */ public class SyndContentImpl implements Serializable, SyndContent { + private static final long serialVersionUID = -8831050456661121113L; + + private static final CopyFromHelper COPY_FROM_HELPER; + private final ObjectBean objBean; + private String type; private String value; private String mode; + static { + final Map> basePropInterfaceMap = new HashMap>(); + basePropInterfaceMap.put("type", String.class); + basePropInterfaceMap.put("value", String.class); + + final Map, Class> basePropClassImplMap = Collections., Class> emptyMap(); + + COPY_FROM_HELPER = new CopyFromHelper(SyndContent.class, basePropInterfaceMap, basePropClassImplMap); + } + /** * Default constructor. All properties are set to null. *

- * + * */ public SyndContentImpl() { objBean = new ObjectBean(SyndContent.class, this); @@ -51,10 +66,10 @@ public class SyndContentImpl implements Serializable, SyndContent { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -65,10 +80,10 @@ public class SyndContentImpl implements Serializable, SyndContent { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -80,9 +95,9 @@ public class SyndContentImpl implements Serializable, SyndContent { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -92,9 +107,9 @@ public class SyndContentImpl implements Serializable, SyndContent { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -106,9 +121,9 @@ public class SyndContentImpl implements Serializable, SyndContent { *

* When used for the description of an entry, if null 'text/plain' must be assumed. *

- * + * * @return the content type, null if none. - * + * */ @Override public String getType() { @@ -120,9 +135,9 @@ public class SyndContentImpl implements Serializable, SyndContent { *

* When used for the description of an entry, if null 'text/plain' must be assumed. *

- * + * * @param type the content type to set, null if none. - * + * */ @Override public void setType(final String type) { @@ -131,9 +146,9 @@ public class SyndContentImpl implements Serializable, SyndContent { /** * Returns the content mode. - * + * * @return the content mode, null if none. - * + * */ @Override public String getMode() { @@ -142,9 +157,9 @@ public class SyndContentImpl implements Serializable, SyndContent { /** * Sets the content mode. - * + * * @param mode the content mode to set, null if none. - * + * */ @Override public void setMode(final String mode) { @@ -154,9 +169,9 @@ public class SyndContentImpl implements Serializable, SyndContent { /** * Returns the content value. *

- * + * * @return the content value, null if none. - * + * */ @Override public String getValue() { @@ -166,9 +181,9 @@ public class SyndContentImpl implements Serializable, SyndContent { /** * Sets the content value. *

- * + * * @param value the content value to set, null if none. - * + * */ @Override public void setValue(final String value) { @@ -185,16 +200,4 @@ public class SyndContentImpl implements Serializable, SyndContent { COPY_FROM_HELPER.copy(this, obj); } - private static final CopyFromHelper COPY_FROM_HELPER; - - static { - final Map> basePropInterfaceMap = new HashMap>(); - basePropInterfaceMap.put("type", String.class); - basePropInterfaceMap.put("value", String.class); - - final Map, Class> basePropClassImplMap = Collections., Class> emptyMap(); - - COPY_FROM_HELPER = new CopyFromHelper(SyndContent.class, basePropInterfaceMap, basePropClassImplMap); - } - } diff --git a/src/main/java/com/sun/syndication/feed/synd/SyndEnclosureImpl.java b/src/main/java/com/sun/syndication/feed/synd/SyndEnclosureImpl.java index 78cf425..30272cc 100644 --- a/src/main/java/com/sun/syndication/feed/synd/SyndEnclosureImpl.java +++ b/src/main/java/com/sun/syndication/feed/synd/SyndEnclosureImpl.java @@ -13,16 +13,32 @@ import com.sun.syndication.feed.impl.ObjectBean; * @author Alejandro Abdelnur */ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { + private static final long serialVersionUID = -5813049622142257411L; + + private static final CopyFromHelper COPY_FROM_HELPER; + private final ObjectBean objBean; + private String url; private String type; private long length; + static { + final Map> basePropInterfaceMap = new HashMap>(); + basePropInterfaceMap.put("url", String.class); + basePropInterfaceMap.put("type", String.class); + basePropInterfaceMap.put("length", Long.TYPE); + + final Map, Class> basePropClassImplMap = Collections., Class> emptyMap(); + + COPY_FROM_HELPER = new CopyFromHelper(SyndEnclosure.class, basePropInterfaceMap, basePropClassImplMap); + } + /** * Default constructor. All properties are set to null. *

- * + * */ public SyndEnclosureImpl() { objBean = new ObjectBean(SyndEnclosure.class, this); @@ -31,10 +47,10 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -45,10 +61,10 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -60,9 +76,9 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -72,9 +88,9 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -84,7 +100,7 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { /** * Returns the enclosure URL. *

- * + * * @return the enclosure URL, null if none. */ @Override @@ -95,7 +111,7 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { /** * Sets the enclosure URL. *

- * + * * @param url the enclosure URL to set, null if none. */ @Override @@ -106,7 +122,7 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { /** * Returns the enclosure length. *

- * + * * @return the enclosure length, null if none. */ @Override @@ -117,7 +133,7 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { /** * Sets the enclosure length. *

- * + * * @param length the enclosure length to set, null if none. */ @Override @@ -128,7 +144,7 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { /** * Returns the enclosure type. *

- * + * * @return the enclosure type, null if none. */ @Override @@ -139,7 +155,7 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { /** * Sets the enclosure type. *

- * + * * @param type the enclosure type to set, null if none. */ @Override @@ -157,17 +173,4 @@ public class SyndEnclosureImpl implements Serializable, SyndEnclosure { COPY_FROM_HELPER.copy(this, obj); } - private static final CopyFromHelper COPY_FROM_HELPER; - - static { - final Map> basePropInterfaceMap = new HashMap>(); - basePropInterfaceMap.put("url", String.class); - basePropInterfaceMap.put("type", String.class); - basePropInterfaceMap.put("length", Long.TYPE); - - final Map, Class> basePropClassImplMap = Collections., Class> emptyMap(); - - COPY_FROM_HELPER = new CopyFromHelper(SyndEnclosure.class, basePropInterfaceMap, basePropClassImplMap); - } - } diff --git a/src/main/java/com/sun/syndication/feed/synd/SyndEntryImpl.java b/src/main/java/com/sun/syndication/feed/synd/SyndEntryImpl.java index 57b7411..cc4ed3f 100644 --- a/src/main/java/com/sun/syndication/feed/synd/SyndEntryImpl.java +++ b/src/main/java/com/sun/syndication/feed/synd/SyndEntryImpl.java @@ -28,6 +28,9 @@ import java.util.Set; 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.impl.CopyFromHelper; import com.sun.syndication.feed.impl.ObjectBean; @@ -42,13 +45,18 @@ import com.sun.syndication.feed.synd.impl.URINormalizer; /** * Bean for entries of SyndFeedImpl feeds. *

- * + * * @author Alejandro Abdelnur - * + * */ public class SyndEntryImpl implements Serializable, SyndEntry { + private static final long serialVersionUID = 1944144041409866698L; + + private static final CopyFromHelper COPY_FROM_HELPER; + private final ObjectBean objBean; + private String uri; private String link; private String comments; @@ -63,8 +71,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { private List contributors; private SyndFeed source; private List 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 private List categories = new ArrayList(); @@ -80,19 +89,41 @@ public class SyndEntryImpl implements Serializable, SyndEntry { public static final Set CONVENIENCE_PROPERTIES = Collections.unmodifiableSet(IGNORE_PROPERTIES); static { + IGNORE_PROPERTIES.add("publishedDate"); IGNORE_PROPERTIES.add("author"); + + final Map> basePropInterfaceMap = new HashMap>(); + 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> basePropClassImplMap = new HashMap, 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); + } /** * For implementations extending SyndEntryImpl to be able to use the ObjectBean functionality * with extended interfaces. *

- * + * * @param beanClass * @param convenienceProperties set containing the convenience properties of the SyndEntryImpl * (the are ignored during cloning, check CloneableBean for details). - * + * */ protected SyndEntryImpl(final Class beanClass, final Set convenienceProperties) { objBean = new ObjectBean(beanClass, this, convenienceProperties); @@ -101,7 +132,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Default constructor. All properties are set to null. *

- * + * */ public SyndEntryImpl() { this(SyndEntry.class, IGNORE_PROPERTIES); @@ -110,10 +141,10 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -124,10 +155,10 @@ public class SyndEntryImpl implements Serializable, SyndEntry { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -154,9 +185,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -166,9 +197,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -185,9 +216,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { *

* The returned URI is a normalized URI as specified in RFC 2396bis. *

- * + * * @return the entry URI, null if none. - * + * */ @Override public String getUri() { @@ -202,9 +233,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { * href="http://wiki.java.net/bin/edit/Javawsxml/Rome04URIMapping">Feed and entry URI * mapping. *

- * + * * @param uri the entry URI to set, null if none. - * + * */ @Override public void setUri(final String uri) { @@ -214,9 +245,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the entry title. *

- * + * * @return the entry title, null if none. - * + * */ @Override public String getTitle() { @@ -229,9 +260,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Sets the entry title. *

- * + * * @param title the entry title to set, null if none. - * + * */ @Override public void setTitle(final String title) { @@ -244,9 +275,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the entry title as a text construct. *

- * + * * @return the entry title, null if none. - * + * */ @Override public SyndContent getTitleEx() { @@ -256,9 +287,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Sets the entry title as a text construct. *

- * + * * @param title the entry title to set, null if none. - * + * */ @Override public void setTitleEx(final SyndContent title) { @@ -268,9 +299,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the entry link. *

- * + * * @return the entry link, null if none. - * + * */ @Override public String getLink() { @@ -280,9 +311,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Sets the entry link. *

- * + * * @param link the entry link to set, null if none. - * + * */ @Override public void setLink(final String link) { @@ -292,9 +323,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the entry description. *

- * + * * @return the entry description, null if none. - * + * */ @Override public SyndContent getDescription() { @@ -304,9 +335,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Sets the entry description. *

- * + * * @param description the entry description to set, null if none. - * + * */ @Override public void setDescription(final SyndContent description) { @@ -316,25 +347,22 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the entry contents. *

- * + * * @return a list of SyndContentImpl elements with the entry contents, an empty list if none. - * + * */ @Override public List getContents() { - if (contents == null) { - contents = new ArrayList(); - } - return contents; + return contents = Lists.createWhenNull(contents); } /** * Sets the entry contents. *

- * + * * @param contents the list of SyndContentImpl elements with the entry contents to set, an empty * list or null if none. - * + * */ @Override public void setContents(final List contents) { @@ -344,25 +372,22 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the entry enclosures. *

- * + * * @return a list of SyndEnclosure elements with the entry enclosures, an empty list if none. - * + * */ @Override public List getEnclosures() { - if (enclosures == null) { - enclosures = new ArrayList(); - } - return enclosures; + return enclosures = Lists.createWhenNull(enclosures); } /** * Sets the entry enclosures. *

- * + * * @param enclosures the list of SyndEnclosure elements with the entry enclosures to set, an * empty list or null if none. - * + * */ @Override public void setEnclosures(final List enclosures) { @@ -374,9 +399,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { *

* This method is a convenience method, it maps to the Dublin Core module date. *

- * + * * @return the entry published date, null if none. - * + * */ @Override public Date getPublishedDate() { @@ -388,9 +413,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { *

* This method is a convenience method, it maps to the Dublin Core module date. *

- * + * * @param publishedDate the entry published date to set, null if none. - * + * */ @Override public void setPublishedDate(final Date publishedDate) { @@ -400,9 +425,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the entry categories. *

- * + * * @return a list of SyndCategoryImpl elements with the entry categories, an empty list if none. - * + * */ @Override public List getCategories() { @@ -414,10 +439,10 @@ public class SyndEntryImpl implements Serializable, SyndEntry { *

* This method is a convenience method, it maps to the Dublin Core module subjects. *

- * + * * @param categories the list of SyndCategoryImpl elements with the entry categories to set, an * empty list or null if none. - * + * */ @Override public void setCategories(final List categories) { @@ -427,15 +452,13 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the entry modules. *

- * + * * @return a list of ModuleImpl elements with the entry modules, an empty list if none. - * + * */ @Override public List getModules() { - if (modules == null) { - modules = new ArrayList(); - } + modules = Lists.createWhenNull(modules); if (ModuleUtils.getModule(modules, DCModule.URI) == null) { modules.add(new DCModuleImpl()); } @@ -445,10 +468,10 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Sets the entry modules. *

- * + * * @param modules the list of ModuleImpl elements with the entry modules to set, an empty list * or null if none. - * + * */ @Override public void setModules(final List modules) { @@ -458,7 +481,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the module identified by a given URI. *

- * + * * @param uri the URI of the ModuleImpl. * @return The module with the given URI, null if none. */ @@ -469,9 +492,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the Dublin Core module of the feed. - * + * * @return the DC module, it's never null - * + * */ private DCModule getDCModule() { return (DCModule) getModule(DCModule.URI); @@ -487,48 +510,21 @@ public class SyndEntryImpl implements Serializable, SyndEntry { COPY_FROM_HELPER.copy(this, obj); } - private static final CopyFromHelper COPY_FROM_HELPER; - - static { - final Map> basePropInterfaceMap = new HashMap>(); - 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> basePropClassImplMap = new HashMap, 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 *

- * + * * @return Returns the links. */ @Override public List getLinks() { - if (links == null) { - links = new ArrayList(); - } - return links; + return links = Lists.createWhenNull(links); } /** * Set the links *

- * + * * @param links The links to set. */ @Override @@ -539,22 +535,18 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns the updatedDate *

- * + * * @return Returns the updatedDate. */ @Override public Date getUpdatedDate() { - if (updatedDate == null) { - return null; - } else { - return new Date(updatedDate.getTime()); - } + return Dates.copy(updatedDate); } /** * Set the updatedDate *

- * + * * @param updatedDate The updatedDate to set. */ @Override @@ -564,16 +556,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry { @Override public List getAuthors() { - if (authors == null) { - authors = new ArrayList(); - } - return authors; + return authors = Lists.createWhenNull(authors); } - /* - * (non-Javadoc) - * @see com.sun.syndication.feed.synd.SyndEntry#setAuthors(java.util.List) - */ @Override public void setAuthors(final List authors) { this.authors = authors; @@ -584,26 +569,29 @@ public class SyndEntryImpl implements Serializable, SyndEntry { *

* This method is a convenience method, it maps to the Dublin Core module creator. *

- * + * * @return the entry author, null if none. - * + * */ @Override public String getAuthor() { + String author; // Start out looking for one or more authors in authors. For non-Atom // feeds, authors may actually be null. - if (authors != null && !authors.isEmpty()) { + if (Lists.isNotEmpty(authors)) { author = authors.get(0).getName(); } else { author = getDCModule().getCreator(); } + if (author == null) { author = ""; } return author; + } /** @@ -611,28 +599,24 @@ public class SyndEntryImpl implements Serializable, SyndEntry { *

* This method is a convenience method, it maps to the Dublin Core module creator. *

- * + * * @param author the entry author to set, null if none. - * + * */ @Override public void setAuthor(final String author) { - // Get the DCModule so that we can check to see if "creator" is already - // set. + // Get the DCModule so that we can check to see if "creator" is already set. final DCModule dcModule = getDCModule(); final String currentValue = dcModule.getCreator(); - if (currentValue == null || currentValue.length() == 0) { + if (Strings.isEmpty(currentValue)) { getDCModule().setCreator(author); } } @Override public List getContributors() { - if (contributors == null) { - contributors = new ArrayList(); - } - return contributors; + return contributors = Lists.createWhenNull(contributors); } @Override @@ -653,42 +637,33 @@ public class SyndEntryImpl implements Serializable, SyndEntry { /** * Returns foreign markup found at channel level. *

- * + * * @return list of JDOM nodes containing channel-level foreign markup, an empty list if none. - * + * */ @Override public List getForeignMarkup() { - if (foreignMarkup == null) { - foreignMarkup = new ArrayList(); - } - return foreignMarkup; + return foreignMarkup = Lists.createWhenNull(foreignMarkup); } /** * Sets foreign markup found at channel level. *

- * + * * @param foreignMarkup list of JDOM nodes containing channel-level foreign markup, an empty * list if none. - * + * */ @Override public void setForeignMarkup(final List foreignMarkup) { this.foreignMarkup = foreignMarkup; } - /** - * {@inheritDoc} - */ @Override public String getComments() { return comments; } - /** - * {@inheritDoc} - */ @Override public void setComments(final String comments) { this.comments = comments; @@ -705,11 +680,13 @@ public class SyndEntryImpl implements Serializable, SyndEntry { @Override public SyndLink findRelatedLink(final String relation) { - for (final SyndLink l : getLinks()) { - if (relation.equals(l.getRel())) { - return l; + final List syndLinks = getLinks(); + for (final SyndLink syndLink : syndLinks) { + if (relation.equals(syndLink.getRel())) { + return syndLink; } } return null; } + } diff --git a/src/main/java/com/sun/syndication/feed/synd/SyndFeedImpl.java b/src/main/java/com/sun/syndication/feed/synd/SyndFeedImpl.java index 4bbcf35..d49d32e 100644 --- a/src/main/java/com/sun/syndication/feed/synd/SyndFeedImpl.java +++ b/src/main/java/com/sun/syndication/feed/synd/SyndFeedImpl.java @@ -17,7 +17,6 @@ package com.sun.syndication.feed.synd; import java.io.Serializable; -import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -28,6 +27,7 @@ import java.util.Set; import org.jdom2.Element; +import com.rometools.utils.Lists; import com.sun.syndication.feed.CopyFrom; import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.impl.CopyFromHelper; @@ -47,13 +47,16 @@ import com.sun.syndication.feed.synd.impl.URINormalizer; * It handles all RSS versions, Atom 0.3 and Atom 1.0, it normalizes all info, it may lose * information. *

- * + * * @author Alejandro Abdelnur - * + * */ public class SyndFeedImpl implements Serializable, SyndFeed { + private static final long serialVersionUID = -2529165503200548045L; + private static final CopyFromHelper COPY_FROM_HELPER; + private final ObjectBean objBean; private String encoding; @@ -92,17 +95,40 @@ public class SyndFeedImpl implements Serializable, SyndFeed { public static final Set CONVENIENCE_PROPERTIES = Collections.unmodifiableSet(IGNORE_PROPERTIES); static { + IGNORE_PROPERTIES.add("publishedDate"); IGNORE_PROPERTIES.add("author"); IGNORE_PROPERTIES.add("copyright"); IGNORE_PROPERTIES.add("categories"); IGNORE_PROPERTIES.add("language"); + + final Map> basePropInterfaceMap = new HashMap>(); + 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> basePropClassImplMap = new HashMap, 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 real feed types the SyndFeedImpl supports when converting from and to. *

- * + * * @return the real feed type supported. */ @Override @@ -114,11 +140,11 @@ public class SyndFeedImpl implements Serializable, SyndFeed { * For implementations extending SyndFeedImpl to be able to use the ObjectBean functionality * with extended interfaces. *

- * + * * @param beanClass * @param convenienceProperties set containing the convenience properties of the SyndEntryImpl * (the are ignored during cloning, check CloneableBean for details). - * + * */ protected SyndFeedImpl(final Class beanClass, final Set convenienceProperties) { objBean = new ObjectBean(beanClass, this, convenienceProperties); @@ -127,7 +153,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Default constructor. All properties are set to null. *

- * + * */ public SyndFeedImpl() { this(null); @@ -137,9 +163,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { * Creates a SyndFeedImpl and populates all its properties out of the given RSS Channel or Atom * Feed properties. *

- * + * * @param feed the RSS Channel or the Atom Feed to populate the properties from. - * + * */ public SyndFeedImpl(final WireFeed feed) { this(feed, false); @@ -149,7 +175,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed { * Creates a SyndFeedImpl and populates all its properties out of the given RSS Channel or Atom * Feed properties, while optionally preserving the WireFeed for access via the * orignalWireFeed() method. - * + * * @param feed * @param preserveWireFeed */ @@ -175,10 +201,10 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -189,10 +215,10 @@ public class SyndFeedImpl implements Serializable, SyndFeed { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -212,9 +238,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -224,9 +250,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -238,9 +264,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* The feed type of the created WireFeed is taken from the SyndFeedImpl feedType property. *

- * + * * @return the real feed. - * + * */ @Override public WireFeed createWireFeed() { @@ -250,21 +276,25 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Creates a real feed containing the information of the SyndFeedImpl. *

- * + * * @param feedType the feed type for the WireFeed to be created. * @return the real feed. - * + * */ @Override public WireFeed createWireFeed(final String feedType) { + if (feedType == null) { throw new IllegalArgumentException("Feed type cannot be null"); } + final Converter converter = CONVERTERS.getConverter(feedType); if (converter == null) { throw new IllegalArgumentException("Invalid feed type [" + feedType + "]"); } + return converter.createRealFeed(this); + } /** @@ -273,9 +303,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { * Note: The wire feed returned here will NOT contain any modifications done on this SyndFeed * since it was created. That is in contrast to the createWireFeed method, which will reflect * the current state of the SyndFeed - * + * * @return The WireFeed this was created from, or null - * + * */ @Override public WireFeed originalWireFeed() { @@ -285,9 +315,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the wire feed type the feed had/will-have when coverted from/to a WireFeed. *

- * + * * @return the feed type, null if none. - * + * */ @Override public String getFeedType() { @@ -297,9 +327,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Sets the wire feed type the feed will-have when coverted to a WireFeed. *

- * + * * @param feedType the feed type to set, null if none. - * + * */ @Override public void setFeedType(final String feedType) { @@ -309,9 +339,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the charset encoding of a the feed. This is not set by Rome parsers. *

- * + * * @return the charset encoding of the feed. - * + * */ @Override public String getEncoding() { @@ -321,9 +351,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Sets the charset encoding of a the feed. This is not set by Rome parsers. *

- * + * * @param encoding the charset encoding of the feed. - * + * */ @Override public void setEncoding(final String encoding) { @@ -350,9 +380,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { * be treated as distinct entities. In the RSS 1.0 case the URI must be a valid RDF URI * reference. *

- * + * * @return the feed URI, null if none. - * + * */ @Override public String getUri() { @@ -377,9 +407,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { * be treated as distinct entities. In the RSS 1.0 case the URI must be a valid RDF URI * reference. *

- * + * * @param uri the feed URI to set, null if none. - * + * */ @Override public void setUri(final String uri) { @@ -389,9 +419,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the feed title. *

- * + * * @return the feed title, null if none. - * + * */ @Override public String getTitle() { @@ -404,9 +434,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Sets the feed title. *

- * + * * @param title the feed title to set, null if none. - * + * */ @Override public void setTitle(final String title) { @@ -419,9 +449,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the feed title as a text construct. *

- * + * * @return the feed title, null if none. - * + * */ @Override public SyndContent getTitleEx() { @@ -431,9 +461,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Sets the feed title as a text construct. *

- * + * * @param title the feed title to set, null if none. - * + * */ @Override public void setTitleEx(final SyndContent title) { @@ -453,9 +483,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { * be treated as distinct entities. In the RSS 1.0 case the URI must be a valid RDF URI * reference. *

- * + * * @return the feed link, null if none. - * + * */ @Override public String getLink() { @@ -475,9 +505,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { * be treated as distinct entities. In the RSS 1.0 case the URI must be a valid RDF URI * reference. *

- * + * * @param link the feed link to set, null if none. - * + * */ @Override public void setLink(final String link) { @@ -487,9 +517,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the feed description. *

- * + * * @return the feed description, null if none. - * + * */ @Override public String getDescription() { @@ -502,9 +532,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Sets the feed description. *

- * + * * @param description the feed description to set, null if none. - * + * */ @Override public void setDescription(final String description) { @@ -517,9 +547,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the feed description as a text construct. *

- * + * * @return the feed description, null if none. - * + * */ @Override public SyndContent getDescriptionEx() { @@ -529,9 +559,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Sets the feed description as a text construct. *

- * + * * @param description the feed description to set, null if none. - * + * */ @Override public void setDescriptionEx(final SyndContent description) { @@ -543,9 +573,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* This method is a convenience method, it maps to the Dublin Core module date. *

- * + * * @return the feed published date, null if none. - * + * */ @Override public Date getPublishedDate() { @@ -557,9 +587,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* This method is a convenience method, it maps to the Dublin Core module date. *

- * + * * @param publishedDate the feed published date to set, null if none. - * + * */ @Override public void setPublishedDate(final Date publishedDate) { @@ -571,9 +601,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* This method is a convenience method, it maps to the Dublin Core module rights. *

- * + * * @return the feed copyright, null if none. - * + * */ @Override public String getCopyright() { @@ -585,9 +615,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* This method is a convenience method, it maps to the Dublin Core module rights. *

- * + * * @param copyright the feed copyright to set, null if none. - * + * */ @Override public void setCopyright(final String copyright) { @@ -597,9 +627,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the feed image. *

- * + * * @return the feed image, null if none. - * + * */ @Override public SyndImage getImage() { @@ -609,9 +639,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Sets the feed image. *

- * + * * @param image the feed image to set, null if none. - * + * */ @Override public void setImage(final SyndImage image) { @@ -623,9 +653,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* This method is a convenience method, it maps to the Dublin Core module subjects. *

- * + * * @return a list of SyndCategoryImpl elements with the feed categories, an empty list if none. - * + * */ @Override public List getCategories() { @@ -637,10 +667,10 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* This method is a convenience method, it maps to the Dublin Core module subjects. *

- * + * * @param categories the list of SyndCategoryImpl elements with the feed categories to set, an * empty list or null if none. - * + * */ @Override public void setCategories(final List categories) { @@ -650,25 +680,22 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the feed entries. *

- * + * * @return a list of SyndEntryImpl elements with the feed entries, an empty list if none. - * + * */ @Override public List getEntries() { - if (entries == null) { - entries = new ArrayList(); - } - return entries; + return entries = Lists.createWhenNull(entries); } /** * Sets the feed entries. *

- * + * * @param entries the list of SyndEntryImpl elements with the feed entries to set, an empty list * or null if none. - * + * */ @Override public void setEntries(final List entries) { @@ -680,9 +707,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* This method is a convenience method, it maps to the Dublin Core module language. *

- * + * * @return the feed language, null if none. - * + * */ @Override public String getLanguage() { @@ -694,9 +721,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* This method is a convenience method, it maps to the Dublin Core module language. *

- * + * * @param language the feed language to set, null if none. - * + * */ @Override public void setLanguage(final String language) { @@ -706,15 +733,13 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the feed modules. *

- * + * * @return a list of ModuleImpl elements with the feed modules, an empty list if none. - * + * */ @Override public List getModules() { - if (modules == null) { - modules = new ArrayList(); - } + modules = Lists.createWhenNull(modules); if (ModuleUtils.getModule(modules, DCModule.URI) == null) { modules.add(new DCModuleImpl()); } @@ -724,10 +749,10 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Sets the feed modules. *

- * + * * @param modules the list of ModuleImpl elements with the feed modules to set, an empty list or * null if none. - * + * */ @Override public void setModules(final List modules) { @@ -737,7 +762,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the module identified by a given URI. *

- * + * * @param uri the URI of the ModuleImpl. * @return The module with the given URI, null if none. */ @@ -748,9 +773,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns the Dublin Core module of the feed. - * + * * @return the DC module, it's never null - * + * */ private DCModule getDCModule() { return (DCModule) getModule(DCModule.URI); @@ -766,51 +791,21 @@ public class SyndFeedImpl implements Serializable, SyndFeed { 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> basePropInterfaceMap = new HashMap>(); - 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> basePropClassImplMap = new HashMap, 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 *

- * + * * @return Returns the links. */ @Override public List getLinks() { - if (links == null) { - links = new ArrayList(); - } - return links; + return links = Lists.createWhenNull(links); } /** * Set the links *

- * + * * @param links The links to set. */ @Override @@ -820,10 +815,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed { @Override public List getAuthors() { - if (authors == null) { - authors = new ArrayList(); - } - return authors; + return authors = Lists.createWhenNull(authors); } @Override @@ -836,9 +828,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* This method is a convenience method, it maps to the Dublin Core module creator. *

- * + * * @return the feed author, null if none. - * + * */ @Override public String getAuthor() { @@ -850,9 +842,9 @@ public class SyndFeedImpl implements Serializable, SyndFeed { *

* This method is a convenience method, it maps to the Dublin Core module creator. *

- * + * * @param author the feed author to set, null if none. - * + * */ @Override public void setAuthor(final String author) { @@ -861,10 +853,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed { @Override public List getContributors() { - if (contributors == null) { - contributors = new ArrayList(); - } - return contributors; + return contributors = Lists.createWhenNull(contributors); } @Override @@ -875,24 +864,21 @@ public class SyndFeedImpl implements Serializable, SyndFeed { /** * Returns foreign markup found at channel level. *

- * + * * @return Opaque object to discourage use - * + * */ @Override public List getForeignMarkup() { - if (foreignMarkup == null) { - foreignMarkup = new ArrayList(); - } - return foreignMarkup; + return foreignMarkup = Lists.createWhenNull(foreignMarkup); } /** * Sets foreign markup found at channel level. *

- * + * * @param foreignMarkup Opaque object to discourage use - * + * */ @Override public void setForeignMarkup(final List foreignMarkup) { @@ -904,83 +890,54 @@ public class SyndFeedImpl implements Serializable, SyndFeed { return preserveWireFeed; } - /** - * {@inheritDoc} - */ @Override public String getDocs() { return docs; } - /** - * {@inheritDoc} - */ @Override public void setDocs(final String docs) { this.docs = docs; } - /** - * {@inheritDoc} - */ @Override public String getGenerator() { return generator; } - /** - * {@inheritDoc} - */ @Override public void setGenerator(final String generator) { this.generator = generator; } - /** - * {@inheritDoc} - */ @Override public String getManagingEditor() { return managingEditor; } - /** - * {@inheritDoc} - */ @Override public void setManagingEditor(final String managingEditor) { this.managingEditor = managingEditor; } - /** - * {@inheritDoc} - */ @Override public String getWebMaster() { return webMaster; } - /** - * {@inheritDoc} - */ @Override public void setWebMaster(final String webMaster) { this.webMaster = webMaster; } - /** - * {@inheritDoc} - */ @Override public String getStyleSheet() { return styleSheet; } - /** - * {@inheritDoc} - */ @Override public void setStyleSheet(final String styleSheet) { this.styleSheet = styleSheet; } + } diff --git a/src/main/java/com/sun/syndication/feed/synd/SyndImageImpl.java b/src/main/java/com/sun/syndication/feed/synd/SyndImageImpl.java index c283fbe..c4db769 100644 --- a/src/main/java/com/sun/syndication/feed/synd/SyndImageImpl.java +++ b/src/main/java/com/sun/syndication/feed/synd/SyndImageImpl.java @@ -28,22 +28,39 @@ import com.sun.syndication.feed.impl.ObjectBean; /** * Bean for images of SyndFeedImpl feeds. *

- * + * * @author Alejandro Abdelnur - * + * */ public class SyndImageImpl implements Serializable, SyndImage { + private static final long serialVersionUID = 5078981553559513247L; + + private static final CopyFromHelper COPY_FROM_HELPER; + private final ObjectBean objBean; + private String title; private String url; private String link; private String description; + static { + final Map> basePropInterfaceMap = new HashMap>(); + basePropInterfaceMap.put("title", String.class); + basePropInterfaceMap.put("url", String.class); + basePropInterfaceMap.put("link", String.class); + basePropInterfaceMap.put("description", String.class); + + final Map, Class> basePropClassImplMap = Collections., Class> emptyMap(); + + COPY_FROM_HELPER = new CopyFromHelper(SyndImage.class, basePropInterfaceMap, basePropClassImplMap); + } + /** * Default constructor. All properties are set to null. *

- * + * */ public SyndImageImpl() { objBean = new ObjectBean(SyndImage.class, this); @@ -52,10 +69,10 @@ public class SyndImageImpl implements Serializable, SyndImage { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -66,10 +83,10 @@ public class SyndImageImpl implements Serializable, SyndImage { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -81,9 +98,9 @@ public class SyndImageImpl implements Serializable, SyndImage { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -93,9 +110,9 @@ public class SyndImageImpl implements Serializable, SyndImage { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -105,9 +122,9 @@ public class SyndImageImpl implements Serializable, SyndImage { /** * Returns the image title. *

- * + * * @return the image title, null if none. - * + * */ @Override public String getTitle() { @@ -117,9 +134,9 @@ public class SyndImageImpl implements Serializable, SyndImage { /** * Sets the image title. *

- * + * * @param title the image title to set, null if none. - * + * */ @Override public void setTitle(final String title) { @@ -129,9 +146,9 @@ public class SyndImageImpl implements Serializable, SyndImage { /** * Returns the image URL. *

- * + * * @return the image URL, null if none. - * + * */ @Override public String getUrl() { @@ -141,9 +158,9 @@ public class SyndImageImpl implements Serializable, SyndImage { /** * Sets the image URL. *

- * + * * @param url the image URL to set, null if none. - * + * */ @Override public void setUrl(final String url) { @@ -153,9 +170,9 @@ public class SyndImageImpl implements Serializable, SyndImage { /** * Returns the image link. *

- * + * * @return the image link, null if none. - * + * */ @Override public String getLink() { @@ -165,9 +182,9 @@ public class SyndImageImpl implements Serializable, SyndImage { /** * Sets the image link. *

- * + * * @param link the image link to set, null if none. - * + * */ @Override public void setLink(final String link) { @@ -177,9 +194,9 @@ public class SyndImageImpl implements Serializable, SyndImage { /** * Returns the image description. *

- * + * * @return the image description, null if none. - * + * */ @Override public String getDescription() { @@ -189,9 +206,9 @@ public class SyndImageImpl implements Serializable, SyndImage { /** * Sets the image description. *

- * + * * @param description the image description to set, null if none. - * + * */ @Override public void setDescription(final String description) { @@ -208,18 +225,4 @@ public class SyndImageImpl implements Serializable, SyndImage { COPY_FROM_HELPER.copy(this, syndImage); } - private static final CopyFromHelper COPY_FROM_HELPER; - - static { - final Map> basePropInterfaceMap = new HashMap>(); - basePropInterfaceMap.put("title", String.class); - basePropInterfaceMap.put("url", String.class); - basePropInterfaceMap.put("link", String.class); - basePropInterfaceMap.put("description", String.class); - - final Map, Class> basePropClassImplMap = Collections., Class> emptyMap(); - - COPY_FROM_HELPER = new CopyFromHelper(SyndImage.class, basePropInterfaceMap, basePropClassImplMap); - } - } diff --git a/src/main/java/com/sun/syndication/feed/synd/SyndPersonImpl.java b/src/main/java/com/sun/syndication/feed/synd/SyndPersonImpl.java index 7882d23..fa6346c 100644 --- a/src/main/java/com/sun/syndication/feed/synd/SyndPersonImpl.java +++ b/src/main/java/com/sun/syndication/feed/synd/SyndPersonImpl.java @@ -18,9 +18,9 @@ package com.sun.syndication.feed.synd; import java.io.Serializable; -import java.util.ArrayList; import java.util.List; +import com.rometools.utils.Lists; import com.sun.syndication.feed.impl.ObjectBean; import com.sun.syndication.feed.module.Module; import com.sun.syndication.feed.module.impl.ModuleUtils; @@ -28,13 +28,16 @@ import com.sun.syndication.feed.module.impl.ModuleUtils; /** * Bean for authors and contributors of SyndFeedImpl feeds and entries. *

- * + * * @author Dave Johnson - * + * */ public class SyndPersonImpl implements Serializable, SyndPerson { + private static final long serialVersionUID = 8523373264589239335L; + private final ObjectBean objBean; + private String name; private String uri; private String email; @@ -51,10 +54,10 @@ public class SyndPersonImpl implements Serializable, SyndPerson { /** * Creates a deep 'bean' clone of the object. *

- * + * * @return a clone of the object. * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned. - * + * */ @Override public Object clone() throws CloneNotSupportedException { @@ -65,10 +68,10 @@ public class SyndPersonImpl implements Serializable, SyndPerson { * Indicates whether some other object is "equal to" this one as defined by the Object equals() * method. *

- * + * * @param other he reference object with which to compare. * @return true if 'this' object is equal to the 'other' object. - * + * */ @Override public boolean equals(final Object other) { @@ -83,9 +86,9 @@ public class SyndPersonImpl implements Serializable, SyndPerson { *

* It follows the contract defined by the Object hashCode() method. *

- * + * * @return the hashcode of the bean object. - * + * */ @Override public int hashCode() { @@ -95,9 +98,9 @@ public class SyndPersonImpl implements Serializable, SyndPerson { /** * Returns the String representation for the object. *

- * + * * @return String representation for the object. - * + * */ @Override public String toString() { @@ -107,9 +110,9 @@ public class SyndPersonImpl implements Serializable, SyndPerson { /** * Returns the person name. *

- * + * * @return the person name, null if none. - * + * */ @Override public String getName() { @@ -119,9 +122,9 @@ public class SyndPersonImpl implements Serializable, SyndPerson { /** * Sets the category name. *

- * + * * @param name the category name to set, null if none. - * + * */ @Override public void setName(final String name) { @@ -131,9 +134,9 @@ public class SyndPersonImpl implements Serializable, SyndPerson { /** * Returns the person's e-mail address. *

- * + * * @return the person's e-mail address, null if none. - * + * */ @Override public String getEmail() { @@ -143,9 +146,9 @@ public class SyndPersonImpl implements Serializable, SyndPerson { /** * Sets the person's e-mail address. *

- * + * * @param email The person's e-mail address to set, null if none. - * + * */ @Override public void setEmail(final String email) { @@ -155,9 +158,9 @@ public class SyndPersonImpl implements Serializable, SyndPerson { /** * Returns the person's URI. *

- * + * * @return the person's URI, null if none. - * + * */ @Override public String getUri() { @@ -167,9 +170,9 @@ public class SyndPersonImpl implements Serializable, SyndPerson { /** * Sets the person's URI. *

- * + * * @param uri the peron's URI to set, null if none. - * + * */ @Override public void setUri(final String uri) { @@ -179,24 +182,21 @@ public class SyndPersonImpl implements Serializable, SyndPerson { /** * Returns the person modules. *

- * + * * @return a list of ModuleImpl elements with the person modules, an empty list if none. */ @Override public List getModules() { - if (modules == null) { - modules = new ArrayList(); - } - return modules; + return modules = Lists.createWhenNull(modules); } /** * Sets the person modules. *

- * + * * @param modules the list of ModuleImpl elements with the person modules to set, an empty list * or null if none. - * + * */ @Override public void setModules(final List modules) { @@ -206,7 +206,7 @@ public class SyndPersonImpl implements Serializable, SyndPerson { /** * Returns the module identified by a given URI. *

- * + * * @param uri the URI of the ModuleImpl. * @return The module with the given URI, null if none. */ @@ -214,4 +214,5 @@ public class SyndPersonImpl implements Serializable, SyndPerson { public Module getModule(final String uri) { return ModuleUtils.getModule(getModules(), uri); } + } diff --git a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForAtom03.java b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForAtom03.java index 4740aed..b54104b 100644 --- a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForAtom03.java +++ b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForAtom03.java @@ -21,6 +21,11 @@ import java.util.ArrayList; import java.util.Date; 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.atom.Content; import com.sun.syndication.feed.atom.Entry; @@ -46,6 +51,7 @@ import com.sun.syndication.feed.synd.SyndPersonImpl; /** */ public class ConverterForAtom03 implements Converter { + private final String type; public ConverterForAtom03() { @@ -63,24 +69,29 @@ public class ConverterForAtom03 implements Converter { @Override public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { + final Feed aFeed = (Feed) feed; syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules())); - if (!feed.getForeignMarkup().isEmpty()) { - syndFeed.setForeignMarkup(feed.getForeignMarkup()); + final List foreignMarkup = feed.getForeignMarkup(); + if (Lists.isNotEmpty(foreignMarkup)) { + syndFeed.setForeignMarkup(foreignMarkup); } syndFeed.setEncoding(aFeed.getEncoding()); 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(); - image.setUrl(aFeed.getLogo()); + image.setUrl(logo); syndFeed.setImage(image); - } else if (aFeed.getIcon() != null) { + } else if (icon != null) { final SyndImage image = new SyndImageImpl(); - image.setUrl(aFeed.getIcon()); + image.setUrl(icon); syndFeed.setImage(image); } @@ -89,17 +100,19 @@ public class ConverterForAtom03 implements Converter { syndFeed.setTitle(aFeed.getTitle()); // use first alternate links as THE link - if (aFeed.getAlternateLinks() != null && !aFeed.getAlternateLinks().isEmpty()) { - final Link theLink = aFeed.getAlternateLinks().get(0); - syndFeed.setLink(theLink.getHrefResolved()); + final List alternateLinks = aFeed.getAlternateLinks(); + if (Lists.isNotEmpty(alternateLinks)) { + final Link link = alternateLinks.get(0); + syndFeed.setLink(link.getHrefResolved()); } // lump alternate and other links together final List syndLinks = new ArrayList(); - 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 otherLinks = aFeed.getOtherLinks(); + if (Lists.isNotEmpty(otherLinks)) { + syndLinks.addAll(createSyndLinks(otherLinks)); } syndFeed.setLinks(syndLinks); @@ -109,7 +122,7 @@ public class ConverterForAtom03 implements Converter { } final List aEntries = aFeed.getEntries(); - if (aEntries != null) { + if (Lists.isNotEmpty(aEntries)) { syndFeed.setEntries(createSyndEntries(aEntries, syndFeed.isPreservingWireFeed())); } @@ -122,7 +135,7 @@ public class ConverterForAtom03 implements Converter { } final List authors = aFeed.getAuthors(); - if (authors != null && !authors.isEmpty()) { + if (Lists.isNotEmpty(authors)) { syndFeed.setAuthors(createSyndPersons(authors)); } @@ -138,16 +151,16 @@ public class ConverterForAtom03 implements Converter { } - protected List createSyndLinks(final List aLinks) { - final ArrayList sLinks = new ArrayList(); - for (final Link link2 : aLinks) { - final Link link = link2; + protected List createSyndLinks(final List atomLinks) { + final ArrayList syndLinks = new ArrayList(); + for (final Link atomLink : atomLinks) { + final Link link = atomLink; if (!link.getRel().equals("enclosure")) { - final SyndLink sLink = createSyndLink(link); - sLinks.add(sLink); + final SyndLink syndLink = createSyndLink(link); + syndLinks.add(syndLink); } } - return sLinks; + return syndLinks; } public SyndLink createSyndLink(final Link link) { @@ -161,38 +174,42 @@ public class ConverterForAtom03 implements Converter { protected List createSyndEntries(final List atomEntries, final boolean preserveWireItems) { final List syndEntries = new ArrayList(); - for (int i = 0; i < atomEntries.size(); i++) { - syndEntries.add(createSyndEntry(atomEntries.get(i), preserveWireItems)); + for (final Entry atomEntry : atomEntries) { + syndEntries.add(createSyndEntry(atomEntry, preserveWireItems)); } return syndEntries; } protected SyndEntry createSyndEntry(final Entry entry, final boolean preserveWireItem) { + final SyndEntryImpl syndEntry = new SyndEntryImpl(); + if (preserveWireItem) { syndEntry.setWireEntry(entry); } syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules())); - if (!entry.getForeignMarkup().isEmpty()) { - syndEntry.setForeignMarkup(entry.getForeignMarkup()); + final List foreignMarkup = entry.getForeignMarkup(); + if (Lists.isNotEmpty(foreignMarkup)) { + syndEntry.setForeignMarkup(foreignMarkup); } syndEntry.setTitle(entry.getTitle()); // if there is exactly one alternate link, use that as THE link - if (entry.getAlternateLinks() != null && entry.getAlternateLinks().size() == 1) { - final Link theLink = entry.getAlternateLinks().get(0); + final List alternateLinks = entry.getAlternateLinks(); + if (Lists.sizeIs(alternateLinks, 1)) { + final Link theLink = alternateLinks.get(0); syndEntry.setLink(theLink.getHrefResolved()); } // Create synd enclosures from enclosure links final List syndEnclosures = new ArrayList(); - if (entry.getOtherLinks() != null && !entry.getOtherLinks().isEmpty()) { - final List oLinks = entry.getOtherLinks(); - for (final Link link : oLinks) { - final Link thisLink = link; + final List otherLinks = entry.getOtherLinks(); + if (Lists.isNotEmpty(otherLinks)) { + for (final Link otherLink : otherLinks) { + final Link thisLink = otherLink; if ("enclosure".equals(thisLink.getRel())) { syndEnclosures.add(createSyndEnclosure(entry, thisLink)); } @@ -202,63 +219,64 @@ public class ConverterForAtom03 implements Converter { // lump alternate and other links together final List syndLinks = new ArrayList(); - 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); final String id = entry.getId(); if (id != null) { - syndEntry.setUri(entry.getId()); + syndEntry.setUri(id); } else { - syndEntry.setUri(syndEntry.getLink()); + final String link = syndEntry.getLink(); + syndEntry.setUri(link); } - Content content = entry.getSummary(); - if (content == null) { + Content summary = entry.getSummary(); + if (summary == null) { final List contents = entry.getContents(); - if (contents != null && !contents.isEmpty()) { - content = contents.get(0); + if (Lists.isNotEmpty(contents)) { + summary = contents.get(0); } - } - if (content != null) { + } else { final SyndContent sContent = new SyndContentImpl(); - sContent.setType(content.getType()); - sContent.setValue(content.getValue()); + sContent.setType(summary.getType()); + sContent.setValue(summary.getValue()); syndEntry.setDescription(sContent); } final List contents = entry.getContents(); - if (!contents.isEmpty()) { + if (Lists.isNotEmpty(contents)) { final List sContents = new ArrayList(); - for (int i = 0; i < contents.size(); i++) { - content = contents.get(i); + for (final Content content : contents) { final SyndContent sContent = new SyndContentImpl(); sContent.setType(content.getType()); sContent.setValue(content.getValue()); sContent.setMode(content.getMode()); sContents.add(sContent); + } syndEntry.setContents(sContents); } final List authors = entry.getAuthors(); - if (authors != null && !authors.isEmpty()) { + if (Lists.isNotEmpty(authors)) { syndEntry.setAuthors(createSyndPersons(authors)); - final SyndPerson person0 = syndEntry.getAuthors().get(0); - syndEntry.setAuthor(person0.getName()); + final SyndPerson firstPerson = syndEntry.getAuthors().get(0); + syndEntry.setAuthor(firstPerson.getName()); } Date date = entry.getModified(); if (date == null) { - date = entry.getIssued(); - if (date == null) { - date = entry.getCreated(); - } + date = Alternatives.firstNotNull(entry.getIssued(), entry.getCreated()); } + if (date != null) { syndEntry.setPublishedDate(date); } @@ -286,13 +304,16 @@ public class ConverterForAtom03 implements Converter { final SyndContent sTitle = syndFeed.getTitleEx(); if (sTitle != null) { + final Content title = new Content(); - if (sTitle.getType() != null) { - title.setType(sTitle.getType()); + final String type = sTitle.getType(); + if (type != null) { + title.setType(type); } - if (sTitle.getMode() != null) { - title.setMode(sTitle.getMode()); + final String mode = sTitle.getMode(); + if (mode != null) { + title.setMode(mode); } title.setValue(sTitle.getValue()); @@ -302,12 +323,14 @@ public class ConverterForAtom03 implements Converter { // separate SyndEntry's links collection into alternate and other links final List alternateLinks = new ArrayList(); final List otherLinks = new ArrayList(); + final List slinks = syndFeed.getLinks(); if (slinks != null) { for (final SyndLink syndLink2 : slinks) { final SyndLink syndLink = syndLink2; 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); } else { otherLinks.add(link); @@ -315,10 +338,11 @@ public class ConverterForAtom03 implements Converter { } } // 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(); link.setRel("alternate"); - link.setHref(syndFeed.getLink()); + link.setHref(sLink); alternateLinks.add(link); } @@ -339,7 +363,7 @@ public class ConverterForAtom03 implements Converter { aFeed.setLanguage(syndFeed.getLanguage()); final List authors = syndFeed.getAuthors(); - if (authors != null && !authors.isEmpty()) { + if (Lists.isNotEmpty(authors)) { aFeed.setAuthors(createAtomPersons(authors)); } @@ -384,8 +408,8 @@ public class ConverterForAtom03 implements Converter { protected List createAtomEntries(final List syndEntries) { final List atomEntries = new ArrayList(); - for (int i = 0; i < syndEntries.size(); i++) { - atomEntries.add(createAtomEntry(syndEntries.get(i))); + for (final SyndEntry syndEntry : syndEntries) { + atomEntries.add(createAtomEntry(syndEntry)); } return atomEntries; } @@ -399,12 +423,14 @@ public class ConverterForAtom03 implements Converter { final SyndContent sTitle = sEntry.getTitleEx(); if (sTitle != null) { final Content title = new Content(); - if (sTitle.getType() != null) { - title.setType(sTitle.getType()); + final String type = sTitle.getType(); + if (type != null) { + title.setType(type); } - if (sTitle.getMode() != null) { - title.setMode(sTitle.getMode()); + final String mode = sTitle.getMode(); + if (mode != null) { + title.setMode(mode); } title.setValue(sTitle.getValue()); @@ -414,12 +440,13 @@ public class ConverterForAtom03 implements Converter { // separate SyndEntry's links collection into alternate and other links final List alternateLinks = new ArrayList(); final List otherLinks = new ArrayList(); - final List slinks = sEntry.getLinks(); - if (slinks != null) { - for (final SyndLink syndLink2 : slinks) { - final SyndLink syndLink = syndLink2; + final List syndLinks = sEntry.getLinks(); + + if (syndLinks != null) { + for (final SyndLink syndLink : syndLinks) { 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); } else { otherLinks.add(link); @@ -427,17 +454,17 @@ public class ConverterForAtom03 implements Converter { } } // 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(); link.setRel("alternate"); - link.setHref(sEntry.getLink()); + link.setHref(sLink); alternateLinks.add(link); } final List sEnclosures = sEntry.getEnclosures(); if (sEnclosures != null) { - for (final SyndEnclosure syndEnclosure2 : sEnclosures) { - final SyndEnclosure syndEnclosure = syndEnclosure2; + for (final SyndEnclosure syndEnclosure : sEnclosures) { final Link link = createAtomEnclosure(syndEnclosure); otherLinks.add(link); } @@ -450,7 +477,7 @@ public class ConverterForAtom03 implements Converter { aEntry.setOtherLinks(otherLinks); } - SyndContent sContent = sEntry.getDescription(); + final SyndContent sContent = sEntry.getDescription(); if (sContent != null) { final Content content = new Content(); content.setType(sContent.getType()); @@ -462,24 +489,23 @@ public class ConverterForAtom03 implements Converter { final List contents = sEntry.getContents(); if (!contents.isEmpty()) { final List aContents = new ArrayList(); - for (int i = 0; i < contents.size(); i++) { - sContent = contents.get(i); + for (final SyndContent syndContent : contents) { final Content content = new Content(); - content.setType(sContent.getType()); - content.setValue(sContent.getValue()); - content.setMode(sContent.getMode()); + content.setType(syndContent.getType()); + content.setValue(syndContent.getValue()); + content.setMode(syndContent.getMode()); aContents.add(content); - } aEntry.setContents(aContents); } final List sAuthors = sEntry.getAuthors(); - if (sAuthors != null && !sAuthors.isEmpty()) { + final String author = sEntry.getAuthor(); + if (Lists.isNotEmpty(sAuthors)) { aEntry.setAuthors(createAtomPersons(sAuthors)); - } else if (sEntry.getAuthor() != null) { + } else if (author != null) { final Person person = new Person(); - person.setName(sEntry.getAuthor()); + person.setName(author); final List authors = new ArrayList(); authors.add(person); aEntry.setAuthors(authors); diff --git a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForAtom10.java b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForAtom10.java index 2d5d396..d055564 100644 --- a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForAtom10.java +++ b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForAtom10.java @@ -20,6 +20,10 @@ import java.util.ArrayList; import java.util.Date; 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.atom.Category; import com.sun.syndication.feed.atom.Content; @@ -48,6 +52,7 @@ import com.sun.syndication.feed.synd.SyndPerson; /** */ public class ConverterForAtom10 implements Converter { + private final String type; public ConverterForAtom10() { @@ -65,24 +70,29 @@ public class ConverterForAtom10 implements Converter { @Override public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { + final Feed aFeed = (Feed) feed; syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules())); - if (!feed.getForeignMarkup().isEmpty()) { - syndFeed.setForeignMarkup(feed.getForeignMarkup()); + final List foreignMarkup = feed.getForeignMarkup(); + if (!foreignMarkup.isEmpty()) { + syndFeed.setForeignMarkup(foreignMarkup); } syndFeed.setEncoding(aFeed.getEncoding()); 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(); - image.setUrl(aFeed.getLogo()); + image.setUrl(logo); syndFeed.setImage(image); - } else if (aFeed.getIcon() != null) { + } else if (icon != null) { final SyndImage image = new SyndImageImpl(); - image.setUrl(aFeed.getIcon()); + image.setUrl(icon); syndFeed.setImage(image); } @@ -105,18 +115,24 @@ public class ConverterForAtom10 implements Converter { } // use first alternate links as THE link - if (aFeed.getAlternateLinks() != null && !aFeed.getAlternateLinks().isEmpty()) { - final Link theLink = aFeed.getAlternateLinks().get(0); + final List alternateLinks = aFeed.getAlternateLinks(); + if (Lists.isNotEmpty(alternateLinks)) { + final Link theLink = alternateLinks.get(0); syndFeed.setLink(theLink.getHrefResolved()); } + // lump alternate and other links together final List syndLinks = new ArrayList(); - 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 otherLinks = aFeed.getOtherLinks(); + if (Lists.isNotEmpty(otherLinks)) { + syndLinks.addAll(createSyndLinks(otherLinks)); } + syndFeed.setLinks(syndLinks); final List aEntries = aFeed.getEntries(); @@ -128,12 +144,12 @@ public class ConverterForAtom10 implements Converter { // over DC equivalent info. final List authors = aFeed.getAuthors(); - if (authors != null && !authors.isEmpty()) { + if (Lists.isNotEmpty(authors)) { syndFeed.setAuthors(ConverterForAtom03.createSyndPersons(authors)); } final List contributors = aFeed.getContributors(); - if (contributors != null && !contributors.isEmpty()) { + if (Lists.isNotEmpty(contributors)) { syndFeed.setContributors(ConverterForAtom03.createSyndPersons(contributors)); } @@ -149,20 +165,19 @@ public class ConverterForAtom10 implements Converter { } - protected List createSyndLinks(final List aLinks) { - final ArrayList sLinks = new ArrayList(); - for (final Link link2 : aLinks) { - final Link link = link2; - final SyndLink sLink = createSyndLink(link); - sLinks.add(sLink); + protected List createSyndLinks(final List atomLinks) { + final ArrayList syndLinks = new ArrayList(); + for (final Link atomLink : atomLinks) { + final SyndLink syndLink = createSyndLink(atomLink); + syndLinks.add(syndLink); } - return sLinks; + return syndLinks; } protected List createSyndEntries(final Feed feed, final List atomEntries, final boolean preserveWireItems) { final List syndEntries = new ArrayList(); - for (int i = 0; i < atomEntries.size(); i++) { - syndEntries.add(createSyndEntry(feed, atomEntries.get(i), preserveWireItems)); + for (final Entry atomEntry : atomEntries) { + syndEntries.add(createSyndEntry(feed, atomEntry, preserveWireItems)); } return syndEntries; } @@ -174,8 +189,9 @@ public class ConverterForAtom10 implements Converter { } syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules())); - if (!entry.getForeignMarkup().isEmpty()) { - syndEntry.setForeignMarkup(entry.getForeignMarkup()); + final List foreignMarkup = entry.getForeignMarkup(); + if (!foreignMarkup.isEmpty()) { + syndEntry.setForeignMarkup(foreignMarkup); } final Content eTitle = entry.getTitleEx(); @@ -189,24 +205,23 @@ public class ConverterForAtom10 implements Converter { } final List contents = entry.getContents(); - if (contents != null && !contents.isEmpty()) { + if (Lists.isNotEmpty(contents)) { final List sContents = new ArrayList(); - for (final Content content2 : contents) { - final Content content = content2; + for (final Content content : contents) { sContents.add(createSyndContent(content)); } syndEntry.setContents(sContents); } final List authors = entry.getAuthors(); - if (authors != null && !authors.isEmpty()) { + if (Lists.isNotEmpty(authors)) { syndEntry.setAuthors(ConverterForAtom03.createSyndPersons(authors)); final SyndPerson person0 = syndEntry.getAuthors().get(0); syndEntry.setAuthor(person0.getName()); } final List contributors = entry.getContributors(); - if (contributors != null && !contributors.isEmpty()) { + if (Lists.isNotEmpty(contributors)) { syndEntry.setContributors(ConverterForAtom03.createSyndPersons(contributors)); } @@ -224,10 +239,9 @@ public class ConverterForAtom10 implements Converter { if (categories != null) { final List syndCategories = new ArrayList(); for (final Category category : categories) { - final Category c = category; final SyndCategory syndCategory = new SyndCategoryImpl(); - syndCategory.setName(c.getTerm()); - syndCategory.setTaxonomyUri(c.getSchemeResolved()); + syndCategory.setName(category.getTerm()); + syndCategory.setTaxonomyUri(category.getSchemeResolved()); // TODO: categories MAY have labels // syndCategory.setLabel(c.getLabel()); syndCategories.add(syndCategory); @@ -236,19 +250,20 @@ public class ConverterForAtom10 implements Converter { } // use first alternate link as THE link - if (entry.getAlternateLinks() != null && !entry.getAlternateLinks().isEmpty()) { - final Link theLink = entry.getAlternateLinks().get(0); + final List alternateLinks = entry.getAlternateLinks(); + if (Lists.isNotEmpty(alternateLinks)) { + final Link theLink = alternateLinks.get(0); syndEntry.setLink(theLink.getHrefResolved()); } // Create synd enclosures from enclosure links final List syndEnclosures = new ArrayList(); - if (entry.getOtherLinks() != null && !entry.getOtherLinks().isEmpty()) { - final List oLinks = entry.getOtherLinks(); + final List otherLinks = entry.getOtherLinks(); + if (Lists.isNotEmpty(otherLinks)) { + final List oLinks = otherLinks; for (final Link link : oLinks) { - final Link thisLink = link; - if ("enclosure".equals(thisLink.getRel())) { - syndEnclosures.add(createSyndEnclosure(feed, entry, thisLink)); + if ("enclosure".equals(link.getRel())) { + syndEnclosures.add(createSyndEnclosure(feed, entry, link)); } } } @@ -256,11 +271,11 @@ public class ConverterForAtom10 implements Converter { // lump alternate and other links together final List syndLinks = new ArrayList(); - 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); @@ -352,10 +367,10 @@ public class ConverterForAtom10 implements Converter { final List otherLinks = new ArrayList(); final List slinks = syndFeed.getLinks(); if (slinks != null) { - for (final SyndLink syndLink2 : slinks) { - final SyndLink syndLink = syndLink2; + for (final SyndLink syndLink : slinks) { 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); } else { otherLinks.add(link); @@ -379,8 +394,7 @@ public class ConverterForAtom10 implements Converter { final List sCats = syndFeed.getCategories(); final List aCats = new ArrayList(); if (sCats != null) { - for (final SyndCategory syndCategory : sCats) { - final SyndCategory sCat = syndCategory; + for (final SyndCategory sCat : sCats) { final Category aCat = new Category(); aCat.setTerm(sCat.getName()); // TODO: aCat.setLabel(sCat.getLabel()); @@ -393,12 +407,12 @@ public class ConverterForAtom10 implements Converter { } final List authors = syndFeed.getAuthors(); - if (authors != null && !authors.isEmpty()) { + if (Lists.isNotEmpty(authors)) { aFeed.setAuthors(ConverterForAtom03.createAtomPersons(authors)); } final List contributors = syndFeed.getContributors(); - if (contributors != null && !contributors.isEmpty()) { + if (Lists.isNotEmpty(contributors)) { aFeed.setContributors(ConverterForAtom03.createAtomPersons(contributors)); } @@ -411,10 +425,13 @@ public class ConverterForAtom10 implements Converter { aFeed.setEntries(createAtomEntries(sEntries)); } - if (!syndFeed.getForeignMarkup().isEmpty()) { - aFeed.setForeignMarkup(syndFeed.getForeignMarkup()); + final List foreignMarkup = syndFeed.getForeignMarkup(); + if (!foreignMarkup.isEmpty()) { + aFeed.setForeignMarkup(foreignMarkup); } + return aFeed; + } protected SyndContent createSyndContent(final Content content) { @@ -426,8 +443,8 @@ public class ConverterForAtom10 implements Converter { protected List createAtomEntries(final List syndEntries) { final List atomEntries = new ArrayList(); - for (int i = 0; i < syndEntries.size(); i++) { - atomEntries.add(createAtomEntry(syndEntries.get(i))); + for (final SyndEntry syndEntry : syndEntries) { + atomEntries.add(createAtomEntry(syndEntry)); } return atomEntries; } @@ -441,14 +458,16 @@ public class ConverterForAtom10 implements Converter { protected List createAtomContents(final List syndContents) { final List atomContents = new ArrayList(); - for (int i = 0; i < syndContents.size(); i++) { - atomContents.add(createAtomContent(syndContents.get(i))); + for (final SyndContent syndContent : syndContents) { + atomContents.add(createAtomContent(syndContent)); } return atomContents; } protected Entry createAtomEntry(final SyndEntry sEntry) { + final Entry aEntry = new Entry(); + aEntry.setModules(ModuleUtils.cloneModules(sEntry.getModules())); aEntry.setId(sEntry.getUri()); @@ -472,26 +491,31 @@ public class ConverterForAtom10 implements Converter { // separate SyndEntry's links collection into alternate and other links final List alternateLinks = new ArrayList(); final List otherLinks = new ArrayList(); + boolean linkRelEnclosureExists = false; + final List slinks = sEntry.getLinks(); final List enclosures = sEntry.getEnclosures(); - boolean linkRelEnclosureExists = false; + if (slinks != null) { - for (final SyndLink syndLink2 : slinks) { - final SyndLink syndLink = syndLink2; + for (final SyndLink syndLink : slinks) { final Link link = createAtomLink(syndLink); // Set this flag if there's a link of rel = enclosure so that // enclosures won't be duplicated when pulled from // SyndEntry.getEnclosures() - if (syndLink.getRel() != null && "enclosure".equals(syndLink.getRel())) { + final String sRel = syndLink.getRel(); + if (sRel != null && "enclosure".equals(sRel)) { 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); } else { otherLinks.add(link); } } } + // no alternate link? then use THE link if there is one if (alternateLinks.isEmpty() && sEntry.getLink() != null) { final Link link = new Link(); @@ -499,6 +523,7 @@ public class ConverterForAtom10 implements Converter { link.setHref(sEntry.getLink()); alternateLinks.add(link); } + // add SyndEnclosures as links with rel="enclosure" ONLY if // there are no SyndEntry.getLinks() with rel="enclosure" if (enclosures != null && linkRelEnclosureExists == false) { @@ -508,9 +533,11 @@ public class ConverterForAtom10 implements Converter { otherLinks.add(link); } } + if (!alternateLinks.isEmpty()) { aEntry.setAlternateLinks(alternateLinks); } + if (!otherLinks.isEmpty()) { aEntry.setOtherLinks(otherLinks); } @@ -518,8 +545,7 @@ public class ConverterForAtom10 implements Converter { final List sCats = sEntry.getCategories(); final List aCats = new ArrayList(); if (sCats != null) { - for (final SyndCategory syndCategory : sCats) { - final SyndCategory sCat = syndCategory; + for (final SyndCategory sCat : sCats) { final Category aCat = new Category(); aCat.setTerm(sCat.getName()); // TODO: aCat.setLabel(sCat.getLabel()); @@ -527,6 +553,7 @@ public class ConverterForAtom10 implements Converter { aCats.add(aCat); } } + if (!aCats.isEmpty()) { aEntry.setCategories(aCats); } @@ -535,18 +562,19 @@ public class ConverterForAtom10 implements Converter { aEntry.setContents(createAtomContents(syndContents)); List authors = sEntry.getAuthors(); - if (authors != null && !authors.isEmpty()) { + final String author = sEntry.getAuthor(); + if (Lists.isNotEmpty(authors)) { aEntry.setAuthors(ConverterForAtom03.createAtomPersons(authors)); - } else if (sEntry.getAuthor() != null) { + } else if (author != null) { final Person person = new Person(); - person.setName(sEntry.getAuthor()); + person.setName(author); authors = new ArrayList(); authors.add(person); aEntry.setAuthors(authors); } final List contributors = sEntry.getContributors(); - if (contributors != null && !contributors.isEmpty()) { + if (Lists.isNotEmpty(contributors)) { aEntry.setContributors(ConverterForAtom03.createAtomPersons(contributors)); } @@ -561,8 +589,9 @@ public class ConverterForAtom10 implements Converter { aEntry.setUpdated(sEntry.getPublishedDate()); } - if (!sEntry.getForeignMarkup().isEmpty()) { - aEntry.setForeignMarkup(sEntry.getForeignMarkup()); + final List foreignMarkup = sEntry.getForeignMarkup(); + if (!foreignMarkup.isEmpty()) { + aEntry.setForeignMarkup(foreignMarkup); } final SyndFeed sSource = sEntry.getSource(); diff --git a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS090.java b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS090.java index 2898534..e71bbab 100644 --- a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS090.java +++ b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS090.java @@ -19,6 +19,8 @@ package com.sun.syndication.feed.synd.impl; import java.util.ArrayList; import java.util.List; +import org.jdom2.Element; + import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.module.impl.ModuleUtils; 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.SyndImage; import com.sun.syndication.feed.synd.SyndImageImpl; +import com.sun.syndication.feed.synd.SyndLink; /** */ @@ -53,13 +56,20 @@ public class ConverterForRSS090 implements Converter { @Override public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { + syndFeed.setModules(ModuleUtils.cloneModules(feed.getModules())); - if (!feed.getForeignMarkup().isEmpty()) { - syndFeed.setForeignMarkup(feed.getForeignMarkup()); + + final List foreignMarkup = feed.getForeignMarkup(); + if (!foreignMarkup.isEmpty()) { + syndFeed.setForeignMarkup(foreignMarkup); } + syndFeed.setStyleSheet(feed.getStyleSheet()); + syndFeed.setEncoding(feed.getEncoding()); + final Channel channel = (Channel) feed; + syndFeed.setTitle(channel.getTitle()); syndFeed.setLink(channel.getLink()); syndFeed.setDescription(channel.getDescription()); @@ -85,22 +95,25 @@ public class ConverterForRSS090 implements Converter { protected List createSyndEntries(final List rssItems, final boolean preserveWireItems) { final List syndEntries = new ArrayList(); - for (int i = 0; i < rssItems.size(); i++) { - syndEntries.add(createSyndEntry(rssItems.get(i), preserveWireItems)); + for (final Item item : rssItems) { + syndEntries.add(createSyndEntry(item, preserveWireItems)); } return syndEntries; } protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { + final SyndEntryImpl syndEntry = new SyndEntryImpl(); + if (preserveWireItem) { syndEntry.setWireEntry(item); } syndEntry.setModules(ModuleUtils.cloneModules(item.getModules())); - if (!item.getForeignMarkup().isEmpty()) { - syndEntry.setForeignMarkup(item.getForeignMarkup()); + final List foreignMarkup = item.getForeignMarkup(); + if (!foreignMarkup.isEmpty()) { + syndEntry.setForeignMarkup(foreignMarkup); } syndEntry.setUri(item.getUri()); @@ -134,12 +147,16 @@ public class ConverterForRSS090 implements Converter { channel.setEncoding(syndFeed.getEncoding()); channel.setTitle(syndFeed.getTitle()); - if (syndFeed.getLink() != null) { - channel.setLink(syndFeed.getLink()); - } else if (!syndFeed.getLinks().isEmpty()) { - channel.setLink(syndFeed.getLinks().get(0).getHref()); + final String link = syndFeed.getLink(); + final List links = syndFeed.getLinks(); + if (link != null) { + channel.setLink(link); + } else if (!links.isEmpty()) { + channel.setLink(links.get(0).getHref()); } + channel.setDescription(syndFeed.getDescription()); + final SyndImage sImage = syndFeed.getImage(); if (sImage != null) { channel.setImage(createRSSImage(sImage)); @@ -150,9 +167,11 @@ public class ConverterForRSS090 implements Converter { channel.setItems(createRSSItems(sEntries)); } - if (!syndFeed.getForeignMarkup().isEmpty()) { - channel.setForeignMarkup(syndFeed.getForeignMarkup()); + final List foreignMarkup = syndFeed.getForeignMarkup(); + if (!foreignMarkup.isEmpty()) { + channel.setForeignMarkup(foreignMarkup); } + return channel; } @@ -166,22 +185,29 @@ public class ConverterForRSS090 implements Converter { protected List createRSSItems(final List sEntries) { final List list = new ArrayList(); - for (int i = 0; i < sEntries.size(); i++) { - list.add(createRSSItem(sEntries.get(i))); + for (final SyndEntry syndEntry : sEntries) { + list.add(createRSSItem(syndEntry)); } return list; } protected Item createRSSItem(final SyndEntry sEntry) { + final Item item = new Item(); + item.setModules(ModuleUtils.cloneModules(sEntry.getModules())); + item.setTitle(sEntry.getTitle()); + item.setLink(sEntry.getLink()); - if (!sEntry.getForeignMarkup().isEmpty()) { - item.setForeignMarkup(sEntry.getForeignMarkup()); + + final List foreignMarkup = sEntry.getForeignMarkup(); + if (!foreignMarkup.isEmpty()) { + item.setForeignMarkup(foreignMarkup); } item.setSource(createSource(sEntry.getSource())); + final String uri = sEntry.getUri(); if (uri != null) { item.setUri(uri); diff --git a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS091Userland.java b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS091Userland.java index bc01cb2..2f41c14 100644 --- a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS091Userland.java +++ b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS091Userland.java @@ -22,6 +22,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import com.rometools.utils.Lists; import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.module.DCModule; 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.SyndPerson; -/** - */ public class ConverterForRSS091Userland extends ConverterForRSS090 { + public ConverterForRSS091Userland() { this("rss_0.91U"); } @@ -49,13 +49,21 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 { @Override public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { + final Channel channel = (Channel) feed; + super.copyInto(channel, syndFeed); + syndFeed.setLanguage(channel.getLanguage()); // c + syndFeed.setCopyright(channel.getCopyright()); // c + syndFeed.setDocs(channel.getDocs()); + syndFeed.setManagingEditor(channel.getManagingEditor()); + syndFeed.setWebMaster(channel.getWebMaster()); + syndFeed.setGenerator(channel.getGenerator()); final Date pubDate = channel.getPubDate(); @@ -69,12 +77,12 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 { final String author = channel.getManagingEditor(); if (author != null) { + final List creators = ((DCModule) syndFeed.getModule(DCModule.URI)).getCreators(); if (!creators.contains(author)) { - final Set s = new LinkedHashSet(); // using a set to - // remove - // duplicates + // using a set to remove duplicates + final Set s = new LinkedHashSet(); s.addAll(creators); // DC creators s.add(author); // feed native author creators.clear(); @@ -87,7 +95,6 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 { final Description desc = new Description(); desc.setValue(sContent.getValue()); desc.setType(sContent.getType()); - return desc; } @@ -95,7 +102,6 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 { protected Image createRSSImage(final SyndImage sImage) { final Image image = super.createRSSImage(sImage); image.setDescription(sImage.getDescription()); - return image; } @@ -104,8 +110,11 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 { // synd.description -> rss.description @Override protected Item createRSSItem(final SyndEntry sEntry) { + final Item item = super.createRSSItem(sEntry); + item.setComments(sEntry.getComments()); + final SyndContent sContent = sEntry.getDescription(); if (sContent != null) { @@ -114,7 +123,7 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 { final List contents = sEntry.getContents(); - if (contents != null && !contents.isEmpty()) { + if (Lists.isNotEmpty(contents)) { final SyndContent syndContent = contents.get(0); final Content cont = new Content(); cont.setValue(syndContent.getValue()); @@ -136,8 +145,9 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 { channel.setWebMaster(syndFeed.getWebMaster()); channel.setGenerator(syndFeed.getGenerator()); - if (syndFeed.getAuthors() != null && !syndFeed.getAuthors().isEmpty()) { - final SyndPerson author = syndFeed.getAuthors().get(0); + final List authors = syndFeed.getAuthors(); + if (Lists.isNotEmpty(authors)) { + final SyndPerson author = authors.get(0); channel.setManagingEditor(author.getName()); } @@ -149,8 +159,11 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 { // rss.description -> synd.description @Override protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { + final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem); + final Description desc = item.getDescription(); + syndEntry.setComments(item.getComments()); if (desc != null) { @@ -179,7 +192,7 @@ public class ConverterForRSS091Userland extends ConverterForRSS090 { protected SyndImage createSyndImage(final Image rssImage) { final SyndImage syndImage = super.createSyndImage(rssImage); syndImage.setDescription(rssImage.getDescription()); - return syndImage; } + } diff --git a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS092.java b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS092.java index e542c03..ff41d99 100644 --- a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS092.java +++ b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS092.java @@ -44,40 +44,38 @@ public class ConverterForRSS092 extends ConverterForRSS091Userland { @Override protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { + final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem); + final List cats = item.getCategories(); + if (!cats.isEmpty()) { - final Set s = new LinkedHashSet(); // using - // a - // set to - // remove - // duplicates - // and use - // a - // LinkedHashSet - // to try - // to - // retain - // the - // document - // order - s.addAll(createSyndCategories(cats)); // feed native categories - // (as - // syndcat) - s.addAll(syndEntry.getCategories()); // DC subjects (as syndcat) + + // using a set to remove duplicates and use a LinkedHashSet to try to retain the + // document order + final Set s = new LinkedHashSet(); + + // feed native categories (as syndcat) + s.addAll(createSyndCategories(cats)); + + // DC subjects (as syndcat) + s.addAll(syndEntry.getCategories()); + syndEntry.setCategories(new ArrayList(s)); // c } + final List enclosures = item.getEnclosures(); if (!enclosures.isEmpty()) { syndEntry.setEnclosures(createSyndEnclosures(enclosures)); } + return syndEntry; + } protected List createSyndCategories(final List rssCats) { final List syndCats = new ArrayList(); - for (int i = 0; i < rssCats.size(); i++) { - final Category rssCat = rssCats.get(i); + for (final Category rssCat : rssCats) { final SyndCategory sCat = new SyndCategoryImpl(); sCat.setTaxonomyUri(rssCat.getDomain()); sCat.setName(rssCat.getValue()); @@ -88,8 +86,7 @@ public class ConverterForRSS092 extends ConverterForRSS091Userland { protected List createSyndEnclosures(final List enclosures) { final List sEnclosures = new ArrayList(); - for (int i = 0; i < enclosures.size(); i++) { - final Enclosure enc = enclosures.get(i); + for (final Enclosure enc : enclosures) { final SyndEnclosure sEnc = new SyndEnclosureImpl(); sEnc.setUrl(enc.getUrl()); sEnc.setType(enc.getType()); @@ -101,23 +98,26 @@ public class ConverterForRSS092 extends ConverterForRSS091Userland { @Override protected Item createRSSItem(final SyndEntry sEntry) { + final Item item = super.createRSSItem(sEntry); final List sCats = sEntry.getCategories(); // c if (!sCats.isEmpty()) { item.setCategories(createRSSCategories(sCats)); } + final List sEnclosures = sEntry.getEnclosures(); if (!sEnclosures.isEmpty()) { item.setEnclosures(createEnclosures(sEnclosures)); } + return item; + } protected List createRSSCategories(final List sCats) { final List cats = new ArrayList(); - for (int i = 0; i < sCats.size(); i++) { - final SyndCategory sCat = sCats.get(i); + for (final SyndCategory sCat : sCats) { final Category cat = new Category(); cat.setDomain(sCat.getTaxonomyUri()); cat.setValue(sCat.getName()); @@ -128,8 +128,7 @@ public class ConverterForRSS092 extends ConverterForRSS091Userland { protected List createEnclosures(final List sEnclosures) { final List enclosures = new ArrayList(); - for (int i = 0; i < sEnclosures.size(); i++) { - final SyndEnclosure sEnc = sEnclosures.get(i); + for (final SyndEnclosure sEnc : sEnclosures) { final Enclosure enc = new Enclosure(); enc.setUrl(sEnc.getUrl()); enc.setType(sEnc.getType()); diff --git a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS093.java b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS093.java index 2630cd6..c5fdf44 100644 --- a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS093.java +++ b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS093.java @@ -21,8 +21,6 @@ import java.util.Date; import com.sun.syndication.feed.rss.Item; import com.sun.syndication.feed.synd.SyndEntry; -/** - */ public class ConverterForRSS093 extends ConverterForRSS092 { public ConverterForRSS093() { @@ -35,11 +33,15 @@ public class ConverterForRSS093 extends ConverterForRSS092 { @Override protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { + final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem); + 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 } + return syndEntry; } diff --git a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS094.java b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS094.java index 420d159..d1ca318 100644 --- a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS094.java +++ b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS094.java @@ -21,6 +21,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import com.rometools.utils.Lists; import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.module.DCModule; 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.SyndPerson; -/** - */ public class ConverterForRSS094 extends ConverterForRSS093 { public ConverterForRSS094() { @@ -48,24 +47,32 @@ public class ConverterForRSS094 extends ConverterForRSS093 { @Override public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { + final Channel channel = (Channel) feed; + super.copyInto(channel, syndFeed); + final List cats = channel.getCategories(); // c if (!cats.isEmpty()) { - final Set s = new LinkedHashSet(); // using a - // set to - // remove - // duplicates - s.addAll(createSyndCategories(cats)); // feed native categories - // (as - // syndcat) - s.addAll(syndFeed.getCategories()); // DC subjects (as syndcat) + + // using a set to remove duplicates + final Set s = new LinkedHashSet(); + + // feed native categories (as syndcat) + s.addAll(createSyndCategories(cats)); + + // DC subjects (as syndcat) + s.addAll(syndFeed.getCategories()); + syndFeed.setCategories(new ArrayList(s)); + } + } @Override protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { + final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem); // adding native feed author to DC creators list @@ -73,32 +80,42 @@ public class ConverterForRSS094 extends ConverterForRSS093 { if (author != null) { final List creators = ((DCModule) syndEntry.getModule(DCModule.URI)).getCreators(); if (!creators.contains(author)) { - final Set s = new LinkedHashSet(); // using a set to - // remove - // duplicates - s.addAll(creators); // DC creators - s.add(author); // feed native author + + // using a set to remove duplicates + final Set s = new LinkedHashSet(); + + // DC creators + s.addAll(creators); + + // feed native author + s.add(author); + creators.clear(); creators.addAll(s); } } final Guid guid = item.getGuid(); + final String itemLink = item.getLink(); if (guid != null) { - syndEntry.setUri(guid.getValue()); - if (item.getLink() == null && guid.isPermaLink()) { - syndEntry.setLink(guid.getValue()); + final String guidValue = guid.getValue(); + syndEntry.setUri(guidValue); + if (itemLink == null && guid.isPermaLink()) { + syndEntry.setLink(guidValue); } } else { - syndEntry.setUri(item.getLink()); + syndEntry.setUri(itemLink); } + if (item.getComments() != null) { final SyndLinkImpl comments = new SyndLinkImpl(); comments.setRel("comments"); comments.setHref(item.getComments()); comments.setType("text/html"); } + return syndEntry; + } @Override @@ -113,32 +130,36 @@ public class ConverterForRSS094 extends ConverterForRSS093 { @Override protected Item createRSSItem(final SyndEntry sEntry) { + final Item item = super.createRSSItem(sEntry); - if (sEntry.getAuthors() != null && !sEntry.getAuthors().isEmpty()) { - final SyndPerson author = sEntry.getAuthors().get(0); + + final List authors = sEntry.getAuthors(); + if (Lists.isNotEmpty(authors)) { + final SyndPerson author = authors.get(0); item.setAuthor(author.getEmail()); } Guid guid = null; final String uri = sEntry.getUri(); + final String link = sEntry.getLink(); if (uri != null) { guid = new Guid(); guid.setPermaLink(false); guid.setValue(uri); - } else { - final String link = sEntry.getLink(); - if (link != null) { - guid = new Guid(); - guid.setPermaLink(true); - guid.setValue(link); - } + } else if (link != null) { + guid = new Guid(); + guid.setPermaLink(true); + guid.setValue(link); + } item.setGuid(guid); + final SyndLink comments = sEntry.findRelatedLink("comments"); if (comments != null && (comments.getType() == null || comments.getType().endsWith("html"))) { item.setComments(comments.getHref()); } return item; + } } diff --git a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS10.java b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS10.java index b8df996..87330e2 100644 --- a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS10.java +++ b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS10.java @@ -19,6 +19,7 @@ package com.sun.syndication.feed.synd.impl; import java.util.ArrayList; import java.util.List; +import com.rometools.utils.Lists; import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.rss.Channel; 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.SyndFeed; -/** - */ public class ConverterForRSS10 extends ConverterForRSS090 { public ConverterForRSS10() { @@ -43,13 +42,18 @@ public class ConverterForRSS10 extends ConverterForRSS090 { @Override public void copyInto(final WireFeed feed, final SyndFeed syndFeed) { + final Channel channel = (Channel) feed; + super.copyInto(channel, syndFeed); - if (channel.getUri() != null) { - syndFeed.setUri(channel.getUri()); + + final String uri = channel.getUri(); + if (uri != null) { + syndFeed.setUri(uri); } else { // 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 protected SyndEntry createSyndEntry(final Item item, final boolean preserveWireItem) { + final SyndEntry syndEntry = super.createSyndEntry(item, preserveWireItem); final Description desc = item.getDescription(); @@ -68,27 +73,36 @@ public class ConverterForRSS10 extends ConverterForRSS090 { descContent.setValue(desc.getValue()); syndEntry.setDescription(descContent); } + final Content cont = item.getContent(); if (cont != null) { + final SyndContent contContent = new SyndContentImpl(); contContent.setType(cont.getType()); contContent.setValue(cont.getValue()); + final List contents = new ArrayList(); contents.add(contContent); syndEntry.setContents(contents); + } return syndEntry; + } @Override protected WireFeed createRealFeed(final String type, final SyndFeed 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 { // if URI is not set use the value for link - channel.setUri(syndFeed.getLink()); + final String link = syndFeed.getLink(); + channel.setUri(link); } return channel; @@ -100,14 +114,16 @@ public class ConverterForRSS10 extends ConverterForRSS090 { @Override protected Item createRSSItem(final SyndEntry sEntry) { + final Item item = super.createRSSItem(sEntry); final SyndContent desc = sEntry.getDescription(); if (desc != null) { item.setDescription(createItemDescription(desc)); } + final List contents = sEntry.getContents(); - if (contents != null && !contents.isEmpty()) { + if (Lists.isNotEmpty(contents)) { item.setContent(createItemContent(contents.get(0))); } diff --git a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS20.java b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS20.java index f28b9d3..c72e5b6 100644 --- a/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS20.java +++ b/src/main/java/com/sun/syndication/feed/synd/impl/ConverterForRSS20.java @@ -16,8 +16,6 @@ */ package com.sun.syndication.feed.synd.impl; -/** - */ public class ConverterForRSS20 extends ConverterForRSS094 { public ConverterForRSS20() { diff --git a/src/main/java/com/sun/syndication/io/FeedException.java b/src/main/java/com/sun/syndication/io/FeedException.java index af3be4a..6b6d87b 100644 --- a/src/main/java/com/sun/syndication/io/FeedException.java +++ b/src/main/java/com/sun/syndication/io/FeedException.java @@ -19,10 +19,9 @@ package com.sun.syndication.io; /** * Exception thrown by WireFeedInput, WireFeedOutput, WireFeedParser and WireFeedGenerator instances * if they can not parse or generate a feed. - *

- * + * * @author Alejandro Abdelnur - * + * */ public class FeedException extends Exception { @@ -30,10 +29,9 @@ public class FeedException extends Exception { /** * Creates a FeedException with a message. - *

- * + * * @param msg exception message. - * + * */ public FeedException(final String msg) { super(msg); @@ -41,11 +39,10 @@ public class FeedException extends Exception { /** * Creates a FeedException with a message and a root cause exception. - *

- * + * * @param msg exception message. * @param rootCause root cause exception. - * + * */ public FeedException(final String msg, final Throwable rootCause) { super(msg, rootCause); diff --git a/src/main/java/com/sun/syndication/io/ParsingFeedException.java b/src/main/java/com/sun/syndication/io/ParsingFeedException.java index 841c40c..8088fea 100644 --- a/src/main/java/com/sun/syndication/io/ParsingFeedException.java +++ b/src/main/java/com/sun/syndication/io/ParsingFeedException.java @@ -20,10 +20,9 @@ import org.jdom2.input.JDOMParseException; /** * Exception thrown by WireFeedInput instance if it can not parse a feed. - *

- * + * * @author Elaine Chien - * + * */ public class ParsingFeedException extends FeedException { @@ -31,10 +30,9 @@ public class ParsingFeedException extends FeedException { /** * Creates a FeedException with a message. - *

- * + * * @param msg exception message. - * + * */ public ParsingFeedException(final String msg) { super(msg); @@ -42,11 +40,10 @@ public class ParsingFeedException extends FeedException { /** * Creates a FeedException with a message and a root cause exception. - *

- * + * * @param msg exception message. * @param rootCause root cause exception. - * + * */ public ParsingFeedException(final String msg, final Throwable rootCause) { super(msg, rootCause); @@ -57,7 +54,7 @@ public class ParsingFeedException extends FeedException { *

* The first line in the document is line 1. *

- * + * * @return an integer representing the line number, or -1 if the information is not available. */ public int getLineNumber() { @@ -73,7 +70,7 @@ public class ParsingFeedException extends FeedException { *

* The first column in a line is position 1. *

- * + * * @return an integer representing the column number, or -1 if the information is not available. */ public int getColumnNumber() { diff --git a/src/main/java/com/sun/syndication/io/SyndFeedOutput.java b/src/main/java/com/sun/syndication/io/SyndFeedOutput.java index ccce3b6..ecf243e 100644 --- a/src/main/java/com/sun/syndication/io/SyndFeedOutput.java +++ b/src/main/java/com/sun/syndication/io/SyndFeedOutput.java @@ -30,21 +30,13 @@ import com.sun.syndication.feed.synd.SyndFeed; *

* It delegates to a WireFeedOutput to generate all feed types. *

- * + * * @author Alejandro Abdelnur - * + * */ public class SyndFeedOutput { - private final WireFeedOutput feedOutput; - /** - * Creates a SyndFeedOutput instance. - *

- * - */ - public SyndFeedOutput() { - feedOutput = new WireFeedOutput(); - } + private final WireFeedOutput feedOutput = new WireFeedOutput(); /** * Creates a String with the XML representation for the given SyndFeedImpl. @@ -53,12 +45,12 @@ public class SyndFeedOutput { * the responsibility of the developer to ensure that if the String is written to a character * stream the stream charset is the same as the feed encoding property. *

- * + * * @param feed Abstract feed to create XML representation from. The type of the SyndFeedImpl * must match the type given to the FeedOuptut constructor. * @return a String with the XML representation for the given SyndFeedImpl. * @throws FeedException thrown if the XML representation for the feed could not be created. - * + * */ public String outputString(final SyndFeed feed) throws FeedException { return feedOutput.outputString(feed.createWireFeed()); @@ -71,13 +63,13 @@ public class SyndFeedOutput { * the responsibility of the developer to ensure that if the String is written to a character * stream the stream charset is the same as the feed encoding property. *

- * + * * @param feed Abstract feed to create XML representation from. The type of the SyndFeedImpl * must match the type given to the FeedOuptut constructor. * @param prettyPrint pretty-print XML (true) oder collapsed * @return a String with the XML representation for the given SyndFeedImpl. * @throws FeedException thrown if the XML representation for the feed could not be created. - * + * */ public String outputString(final SyndFeed feed, final boolean prettyPrint) throws FeedException { return feedOutput.outputString(feed.createWireFeed(), prettyPrint); @@ -91,13 +83,13 @@ public class SyndFeedOutput { * responsibility of the developer to ensure the feed encoding is set to the platform charset * encoding. *

- * + * * @param feed Abstract feed to create XML representation from. The type of the SyndFeedImpl * must match the type given to the FeedOuptut constructor. * @param file the file where to write the XML representation for the given SyndFeedImpl. * @throws IOException thrown if there was some problem writing to the File. * @throws FeedException thrown if the XML representation for the feed could not be created. - * + * */ public void output(final SyndFeed feed, final File file) throws IOException, FeedException { feedOutput.output(feed.createWireFeed(), file); @@ -111,14 +103,14 @@ public class SyndFeedOutput { * responsibility of the developer to ensure the feed encoding is set to the platform charset * encoding. *

- * + * * @param feed Abstract feed to create XML representation from. The type of the SyndFeedImpl * must match the type given to the FeedOuptut constructor. * @param prettyPrint pretty-print XML (true) oder collapsed * @param file the file where to write the XML representation for the given SyndFeedImpl. * @throws IOException thrown if there was some problem writing to the File. * @throws FeedException thrown if the XML representation for the feed could not be created. - * + * */ public void output(final SyndFeed feed, final File file, final boolean prettyPrint) throws IOException, FeedException { feedOutput.output(feed.createWireFeed(), file, prettyPrint); @@ -131,13 +123,13 @@ public class SyndFeedOutput { * the responsibility of the developer to ensure that if the String is written to a character * stream the stream charset is the same as the feed encoding property. *

- * + * * @param feed Abstract feed to create XML representation from. The type of the SyndFeedImpl * must match the type given to the FeedOuptut constructor. * @param writer Writer to write the XML representation for the given SyndFeedImpl. * @throws IOException thrown if there was some problem writing to the Writer. * @throws FeedException thrown if the XML representation for the feed could not be created. - * + * */ public void output(final SyndFeed feed, final Writer writer) throws IOException, FeedException { feedOutput.output(feed.createWireFeed(), writer); @@ -150,14 +142,14 @@ public class SyndFeedOutput { * the responsibility of the developer to ensure that if the String is written to a character * stream the stream charset is the same as the feed encoding property. *

- * + * * @param feed Abstract feed to create XML representation from. The type of the SyndFeedImpl * must match the type given to the FeedOuptut constructor. * @param prettyPrint pretty-print XML (true) oder collapsed * @param writer Writer to write the XML representation for the given SyndFeedImpl. * @throws IOException thrown if there was some problem writing to the Writer. * @throws FeedException thrown if the XML representation for the feed could not be created. - * + * */ public void output(final SyndFeed feed, final Writer writer, final boolean prettyPrint) throws IOException, FeedException { feedOutput.output(feed.createWireFeed(), writer, prettyPrint); @@ -168,12 +160,12 @@ public class SyndFeedOutput { *

* This method does not use the feed encoding property. *

- * + * * @param feed Abstract feed to create W3C DOM document from. The type of the SyndFeedImpl must * match the type given to the FeedOuptut constructor. * @return the W3C DOM document for the given SyndFeedImpl. * @throws FeedException thrown if the W3C DOM document for the feed could not be created. - * + * */ public org.w3c.dom.Document outputW3CDom(final SyndFeed feed) throws FeedException { return feedOutput.outputW3CDom(feed.createWireFeed()); @@ -184,12 +176,12 @@ public class SyndFeedOutput { *

* This method does not use the feed encoding property. *

- * + * * @param feed Abstract feed to create JDOM document from. The type of the SyndFeedImpl must * match the type given to the FeedOuptut constructor. * @return the JDOM document for the given SyndFeedImpl. * @throws FeedException thrown if the JDOM document for the feed could not be created. - * + * */ public Document outputJDom(final SyndFeed feed) throws FeedException { return feedOutput.outputJDom(feed.createWireFeed()); diff --git a/src/main/java/com/sun/syndication/io/WireFeedInput.java b/src/main/java/com/sun/syndication/io/WireFeedInput.java index 138c671..4e1ebf4 100644 --- a/src/main/java/com/sun/syndication/io/WireFeedInput.java +++ b/src/main/java/com/sun/syndication/io/WireFeedInput.java @@ -52,14 +52,22 @@ import com.sun.syndication.io.impl.XmlFixerReader; *

* The WireFeedInput useds liberal parsers. *

- * + * * @author Alejandro Abdelnur - * + * */ 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 clMap = new WeakHashMap(); + private final boolean validate; + private final Locale locale; + + private boolean xmlHealerOn; + private static FeedParsers getFeedParsers() { synchronized (WireFeedInput.class) { 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 { @Override public InputSource resolveEntity(final String publicId, final String systemId) { @@ -85,19 +90,14 @@ public class WireFeedInput { } } - private final boolean validate; - - private boolean xmlHealerOn; - private final Locale locale; - /** * Returns the list of supported input feed types. *

- * + * * @see WireFeed for details on the format of these strings. *

* @return a list of String elements with the supported input feed types. - * + * */ public static List getSupportedFeedTypes() { return getFeedParsers().getSupportedFeedTypes(); @@ -106,7 +106,7 @@ public class WireFeedInput { /** * Creates a WireFeedInput instance with input validation turned off. *

- * + * */ public WireFeedInput() { this(false, Locale.US); @@ -115,10 +115,10 @@ public class WireFeedInput { /** * Creates a WireFeedInput instance. *

- * + * * @param validate indicates if the input should be validated. NOT IMPLEMENTED YET (validation * does not happen) - * + * */ public WireFeedInput(final boolean validate, final Locale locale) { this.validate = false; // TODO FIX THIS THINGY @@ -137,9 +137,9 @@ public class WireFeedInput { *

* By default is TRUE. *

- * + * * @param heals TRUE enables stream healing, FALSE disables it. - * + * */ public void setXmlHealerOn(final boolean heals) { xmlHealerOn = heals; @@ -156,9 +156,9 @@ public class WireFeedInput { *

* By default is TRUE. *

- * + * * @return TRUE if healing is enabled, FALSE if not. - * + * */ public boolean getXmlHealerOn() { return xmlHealerOn; @@ -169,7 +169,7 @@ public class WireFeedInput { *

* NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom2.Document)'. *

- * + * * @param file file to read to create the WireFeed. * @return the WireFeed read from the file. * @throws FileNotFoundException thrown if the file could not be found. @@ -177,7 +177,7 @@ public class WireFeedInput { * @throws IllegalArgumentException thrown if feed type could not be understood by any of the * underlying parsers. * @throws FeedException if the feed could not be parsed - * + * */ public WireFeed build(final File file) throws FileNotFoundException, IOException, IllegalArgumentException, FeedException { WireFeed feed; @@ -195,13 +195,13 @@ public class WireFeedInput { *

* NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom2.Document)'. *

- * + * * @param reader Reader to read to create the WireFeed. * @return the WireFeed read from the Reader. * @throws IllegalArgumentException thrown if feed type could not be understood by any of the * underlying parsers. * @throws FeedException if the feed could not be parsed - * + * */ public WireFeed build(Reader reader) throws IllegalArgumentException, FeedException { final SAXBuilder saxBuilder = createSAXBuilder(); @@ -225,13 +225,13 @@ public class WireFeedInput { *

* NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom2.Document)'. *

- * + * * @param is W3C SAX InputSource to read to create the WireFeed. * @return the WireFeed read from the W3C SAX InputSource. * @throws IllegalArgumentException thrown if feed type could not be understood by any of the * underlying parsers. * @throws FeedException if the feed could not be parsed - * + * */ public WireFeed build(final InputSource is) throws IllegalArgumentException, FeedException { final SAXBuilder saxBuilder = createSAXBuilder(); @@ -252,13 +252,13 @@ public class WireFeedInput { *

* NOTE: This method delages to the 'AsbtractFeed WireFeedInput#build(org.jdom2.Document)'. *

- * + * * @param document W3C DOM document to read to create the WireFeed. * @return the WireFeed read from the W3C DOM document. * @throws IllegalArgumentException thrown if feed type could not be understood by any of the * underlying parsers. * @throws FeedException if the feed could not be parsed - * + * */ public WireFeed build(final org.w3c.dom.Document document) throws IllegalArgumentException, FeedException { final DOMBuilder domBuilder = new DOMBuilder(); @@ -277,13 +277,13 @@ public class WireFeedInput { *

* NOTE: All other build methods delegate to this method. *

- * + * * @param document JDOM document to read to create the WireFeed. * @return the WireFeed read from the JDOM document. * @throws IllegalArgumentException thrown if feed type could not be understood by any of the * underlying parsers. * @throws FeedException if the feed could not be parsed - * + * */ public WireFeed build(final Document document) throws IllegalArgumentException, FeedException { final WireFeedParser parser = getFeedParsers().getParserFor(document); @@ -295,7 +295,7 @@ public class WireFeedInput { /** * Creates and sets up a org.jdom2.input.SAXBuilder for parsing. - * + * * @return a new org.jdom2.input.SAXBuilder object */ protected SAXBuilder createSAXBuilder() { @@ -357,4 +357,5 @@ public class WireFeedInput { saxBuilder.setExpandEntities(false); return saxBuilder; } + } diff --git a/src/main/java/com/sun/syndication/io/impl/Atom03Generator.java b/src/main/java/com/sun/syndication/io/impl/Atom03Generator.java index 23d599a..09fb028 100644 --- a/src/main/java/com/sun/syndication/io/impl/Atom03Generator.java +++ b/src/main/java/com/sun/syndication/io/impl/Atom03Generator.java @@ -17,6 +17,7 @@ package com.sun.syndication.io.impl; import java.io.StringReader; +import java.util.Date; import java.util.List; import java.util.Locale; @@ -26,6 +27,7 @@ import org.jdom2.Element; import org.jdom2.Namespace; import org.jdom2.input.SAXBuilder; +import com.rometools.utils.Lists; import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.atom.Content; import com.sun.syndication.feed.atom.Entry; @@ -44,6 +46,7 @@ import com.sun.syndication.io.FeedException; */ public class Atom03Generator extends BaseWireFeedGenerator { + private static final String ATOM_03_URI = "http://purl.org/atom/ns#"; 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 { - final List items = feed.getEntries(); - for (int i = 0; i < items.size(); i++) { - addEntry(items.get(i), parent); + final List entries = feed.getEntries(); + for (final Entry entry : entries) { + addEntry(entry, parent); } checkEntriesConstraints(parent); } @@ -118,129 +121,151 @@ public class Atom03Generator extends BaseWireFeedGenerator { } 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()); - fillContentElement(titleElement, feed.getTitleEx()); + fillContentElement(titleElement, titleEx); eFeed.addContent(titleElement); } List links = feed.getAlternateLinks(); - for (int i = 0; i < links.size(); i++) { - eFeed.addContent(generateLinkElement(links.get(i))); + for (final Link link : links) { + eFeed.addContent(generateLinkElement(link)); } links = feed.getOtherLinks(); - for (int i = 0; i < links.size(); i++) { - eFeed.addContent(generateLinkElement(links.get(i))); + for (final Link link : links) { + eFeed.addContent(generateLinkElement(link)); } - if (feed.getAuthors() != null && !feed.getAuthors().isEmpty()) { + + final List authors = feed.getAuthors(); + if (Lists.isNotEmpty(authors)) { final Element authorElement = new Element("author", getFeedNamespace()); - fillPersonElement(authorElement, feed.getAuthors().get(0)); + fillPersonElement(authorElement, authors.get(0)); eFeed.addContent(authorElement); } final List contributors = feed.getContributors(); - for (int i = 0; i < contributors.size(); i++) { + for (final SyndPerson contributor : contributors) { final Element contributorElement = new Element("contributor", getFeedNamespace()); - fillPersonElement(contributorElement, contributors.get(i)); + fillPersonElement(contributorElement, contributor); eFeed.addContent(contributorElement); } - if (feed.getTagline() != null) { + final Content tagline = feed.getTagline(); + if (tagline != null) { final Element taglineElement = new Element("tagline", getFeedNamespace()); - fillContentElement(taglineElement, feed.getTagline()); + fillContentElement(taglineElement, tagline); eFeed.addContent(taglineElement); } - if (feed.getId() != null) { - eFeed.addContent(generateSimpleElement("id", feed.getId())); + final String id = feed.getId(); + if (id != null) { + eFeed.addContent(generateSimpleElement("id", id)); } - if (feed.getGenerator() != null) { - eFeed.addContent(generateGeneratorElement(feed.getGenerator())); + final Generator generator = feed.getGenerator(); + if (generator != null) { + eFeed.addContent(generateGeneratorElement(generator)); } - if (feed.getCopyright() != null) { - eFeed.addContent(generateSimpleElement("copyright", feed.getCopyright())); + final String 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()); - fillContentElement(infoElement, feed.getInfo()); + fillContentElement(infoElement, info); eFeed.addContent(infoElement); } - if (feed.getModified() != null) { + final Date modified = feed.getModified(); + if (modified != null) { 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); } + } 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()); - fillContentElement(titleElement, entry.getTitleEx()); + fillContentElement(titleElement, titleEx); eEntry.addContent(titleElement); } - List links = entry.getAlternateLinks(); - for (int i = 0; i < links.size(); i++) { - eEntry.addContent(generateLinkElement(links.get(i))); + + final List alternateLinks = entry.getAlternateLinks(); + for (final Link link : alternateLinks) { + eEntry.addContent(generateLinkElement(link)); } - links = entry.getOtherLinks(); - for (int i = 0; i < links.size(); i++) { - eEntry.addContent(generateLinkElement(links.get(i))); + final List otherLinks = entry.getOtherLinks(); + for (final Link link : otherLinks) { + eEntry.addContent(generateLinkElement(link)); } - if (entry.getAuthors() != null && !entry.getAuthors().isEmpty()) { + final List authors = entry.getAuthors(); + if (Lists.isNotEmpty(authors)) { final Element authorElement = new Element("author", getFeedNamespace()); - fillPersonElement(authorElement, entry.getAuthors().get(0)); + fillPersonElement(authorElement, authors.get(0)); eEntry.addContent(authorElement); } final List contributors = entry.getContributors(); - for (int i = 0; i < contributors.size(); i++) { + for (final SyndPerson contributor : contributors) { final Element contributorElement = new Element("contributor", getFeedNamespace()); - fillPersonElement(contributorElement, contributors.get(i)); + fillPersonElement(contributorElement, contributor); 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()); - modifiedElement.addContent(DateParser.formatW3CDateTime(entry.getModified(), Locale.US)); + modifiedElement.addContent(DateParser.formatW3CDateTime(modified, Locale.US)); eEntry.addContent(modifiedElement); } - if (entry.getIssued() != null) { + final Date issued = entry.getIssued(); + if (issued != null) { 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); } - if (entry.getCreated() != null) { + final Date created = entry.getCreated(); + if (created != null) { 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); } - if (entry.getSummary() != null) { + final Content summary = entry.getSummary(); + if (summary != null) { final Element summaryElement = new Element("summary", getFeedNamespace()); - fillContentElement(summaryElement, entry.getSummary()); + fillContentElement(summaryElement, summary); eEntry.addContent(summaryElement); } final List contents = entry.getContents(); - for (int i = 0; i < contents.size(); i++) { + for (final Content content : contents) { final Element contentElement = new Element("content", getFeedNamespace()); - fillContentElement(contentElement, contents.get(i)); + fillContentElement(contentElement, content); eEntry.addContent(contentElement); } generateForeignMarkup(eEntry, entry.getForeignMarkup()); + } protected void checkFeedHeaderConstraints(final Element eFeed) throws FeedException { @@ -253,75 +278,98 @@ public class Atom03Generator extends BaseWireFeedGenerator { } protected Element generateLinkElement(final Link link) { + final Element linkElement = new Element("link", getFeedNamespace()); - if (link.getRel() != null) { - final Attribute relAttribute = new Attribute("rel", link.getRel().toString()); + final String rel = link.getRel(); + if (rel != null) { + final Attribute relAttribute = new Attribute("rel", rel.toString()); linkElement.setAttribute(relAttribute); } - if (link.getType() != null) { - final Attribute typeAttribute = new Attribute("type", link.getType()); + final String type = link.getType(); + if (type != null) { + final Attribute typeAttribute = new Attribute("type", type); linkElement.setAttribute(typeAttribute); } - if (link.getHref() != null) { - final Attribute hrefAttribute = new Attribute("href", link.getHref()); + final String href = link.getHref(); + if (href != null) { + final Attribute hrefAttribute = new Attribute("href", href); linkElement.setAttribute(hrefAttribute); } + return linkElement; + } protected void fillPersonElement(final Element element, final SyndPerson person) { - if (person.getName() != null) { - element.addContent(generateSimpleElement("name", person.getName())); - } - if (person.getUri() != null) { - element.addContent(generateSimpleElement("url", person.getUri())); + + final String name = person.getName(); + if (name != null) { + element.addContent(generateSimpleElement("name", name)); } - if (person.getEmail() != null) { - element.addContent(generateSimpleElement("email", person.getEmail())); + final String uri = person.getUri(); + 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) { + final Element taglineElement = new Element("tagline", getFeedNamespace()); - if (tagline.getType() != null) { - final Attribute typeAttribute = new Attribute("type", tagline.getType()); + final String type = tagline.getType(); + if (type != null) { + final Attribute typeAttribute = new Attribute("type", type); taglineElement.setAttribute(typeAttribute); } - if (tagline.getValue() != null) { - taglineElement.addContent(tagline.getValue()); + final String value = tagline.getValue(); + if (value != null) { + taglineElement.addContent(value); } + return taglineElement; + } protected void fillContentElement(final Element contentElement, final Content content) throws FeedException { - if (content.getType() != null) { - final Attribute typeAttribute = new Attribute("type", content.getType()); + final String type = content.getType(); + if (type != null) { + final Attribute typeAttribute = new Attribute("type", type); contentElement.setAttribute(typeAttribute); } final String mode = content.getMode(); if (mode != null) { - final Attribute modeAttribute = new Attribute("mode", content.getMode().toString()); + final Attribute modeAttribute = new Attribute("mode", mode.toString()); contentElement.setAttribute(modeAttribute); } - if (content.getValue() != null) { + final String value = content.getValue(); + if (value != null) { if (mode == null || mode.equals(Content.ESCAPED)) { - contentElement.addContent(content.getValue()); + + contentElement.addContent(value); + } else if (mode.equals(Content.BASE64)) { - contentElement.addContent(Base64.encode(content.getValue())); + + contentElement.addContent(Base64.encode(value)); + } else if (mode.equals(Content.XML)) { final StringBuffer tmpDocString = new StringBuffer(""); - tmpDocString.append(content.getValue()); + tmpDocString.append(value); tmpDocString.append(""); final StringReader tmpDocReader = new StringReader(tmpDocString.toString()); Document tmpDoc; @@ -336,24 +384,29 @@ public class Atom03Generator extends BaseWireFeedGenerator { final List children = tmpDoc.getRootElement().removeContent(); contentElement.addContent(children); } + } } protected Element generateGeneratorElement(final Generator generator) { + final Element generatorElement = new Element("generator", getFeedNamespace()); - if (generator.getUrl() != null) { - final Attribute urlAttribute = new Attribute("url", generator.getUrl()); + final String url = generator.getUrl(); + if (url != null) { + final Attribute urlAttribute = new Attribute("url", url); generatorElement.setAttribute(urlAttribute); } - if (generator.getVersion() != null) { - final Attribute versionAttribute = new Attribute("version", generator.getVersion()); + final String version = generator.getVersion(); + if (version != null) { + final Attribute versionAttribute = new Attribute("version", version); generatorElement.setAttribute(versionAttribute); } - if (generator.getValue() != null) { - generatorElement.addContent(generator.getValue()); + final String value = generator.getValue(); + if (value != null) { + generatorElement.addContent(value); } return generatorElement; diff --git a/src/main/java/com/sun/syndication/io/impl/Atom03Parser.java b/src/main/java/com/sun/syndication/io/impl/Atom03Parser.java index 83ffcba..9e127d2 100644 --- a/src/main/java/com/sun/syndication/io/impl/Atom03Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/Atom03Parser.java @@ -25,6 +25,7 @@ import org.jdom2.Element; import org.jdom2.Namespace; import org.jdom2.output.XMLOutputter; +import com.rometools.utils.Lists; import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.atom.Content; 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.io.FeedException; -/** - */ public class Atom03Parser extends BaseWireFeedParser { + private static final String ATOM_03_URI = "http://purl.org/atom/ns#"; private static final Namespace ATOM_03_NS = Namespace.getNamespace(ATOM_03_URI); @@ -62,125 +62,137 @@ public class Atom03Parser extends BaseWireFeedParser { @Override public WireFeed parse(final Document document, final boolean validate, final Locale locale) throws IllegalArgumentException, FeedException { + if (validate) { validateFeed(document); } + final Element rssRoot = document.getRootElement(); + return parseFeed(rssRoot, locale); + } protected void validateFeed(final Document document) throws FeedException { - // TBD - // here we have to validate the Feed against a schema or whatever - // not sure how to do it - // 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. + // TODO here we have to validate the Feed against a schema or whatever not sure how to do it + // 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) { - final Feed feed = new Feed(getType()); - feed.setStyleSheet(getStyleSheet(eFeed.getDocument())); + final String type = getType(); + final Document document = eFeed.getDocument(); + final String styleSheet = getStyleSheet(document); - Element e = eFeed.getChild("title", getAtomNamespace()); - if (e != null) { - feed.setTitleEx(parseContent(e)); + final Feed feed = new Feed(type); + feed.setStyleSheet(styleSheet); + + final Element title = eFeed.getChild("title", getAtomNamespace()); + if (title != null) { + feed.setTitleEx(parseContent(title)); } - List eList = eFeed.getChildren("link", getAtomNamespace()); - feed.setAlternateLinks(parseAlternateLinks(eList)); - feed.setOtherLinks(parseOtherLinks(eList)); + final List links = eFeed.getChildren("link", getAtomNamespace()); + feed.setAlternateLinks(parseAlternateLinks(links)); + feed.setOtherLinks(parseOtherLinks(links)); - e = eFeed.getChild("author", getAtomNamespace()); - if (e != null) { + final Element author = eFeed.getChild("author", getAtomNamespace()); + if (author != null) { final List authors = new ArrayList(); - authors.add(parsePerson(e)); + authors.add(parsePerson(author)); feed.setAuthors(authors); } - eList = eFeed.getChildren("contributor", getAtomNamespace()); - if (!eList.isEmpty()) { - feed.setContributors(parsePersons(eList)); + final List contributors = eFeed.getChildren("contributor", getAtomNamespace()); + if (!contributors.isEmpty()) { + feed.setContributors(parsePersons(contributors)); } - e = eFeed.getChild("tagline", getAtomNamespace()); - if (e != null) { - feed.setTagline(parseContent(e)); + final Element tagline = eFeed.getChild("tagline", getAtomNamespace()); + if (tagline != null) { + feed.setTagline(parseContent(tagline)); } - e = eFeed.getChild("id", getAtomNamespace()); - if (e != null) { - feed.setId(e.getText()); + final Element id = eFeed.getChild("id", getAtomNamespace()); + if (id != null) { + feed.setId(id.getText()); } - e = eFeed.getChild("generator", getAtomNamespace()); - if (e != null) { + final Element generator = eFeed.getChild("generator", getAtomNamespace()); + if (generator != null) { final Generator gen = new Generator(); - gen.setValue(e.getText()); - String att = getAttributeValue(e, "url"); + gen.setValue(generator.getText()); + String att = getAttributeValue(generator, "url"); if (att != null) { gen.setUrl(att); } - att = getAttributeValue(e, "version"); + att = getAttributeValue(generator, "version"); if (att != null) { gen.setVersion(att); } feed.setGenerator(gen); } - e = eFeed.getChild("copyright", getAtomNamespace()); - if (e != null) { - feed.setCopyright(e.getText()); + final Element copyright = eFeed.getChild("copyright", getAtomNamespace()); + if (copyright != null) { + feed.setCopyright(copyright.getText()); } - e = eFeed.getChild("info", getAtomNamespace()); - if (e != null) { - feed.setInfo(parseContent(e)); + final Element info = eFeed.getChild("info", getAtomNamespace()); + if (info != null) { + feed.setInfo(parseContent(info)); } - e = eFeed.getChild("modified", getAtomNamespace()); - if (e != null) { - feed.setModified(DateParser.parseDate(e.getText(), locale)); + final Element modified = eFeed.getChild("modified", getAtomNamespace()); + if (modified != null) { + feed.setModified(DateParser.parseDate(modified.getText(), locale)); } feed.setModules(parseFeedModules(eFeed, locale)); - eList = eFeed.getChildren("entry", getAtomNamespace()); - if (!eList.isEmpty()) { - feed.setEntries(parseEntries(eList, locale)); + final List entries = eFeed.getChildren("entry", getAtomNamespace()); + if (!entries.isEmpty()) { + feed.setEntries(parseEntries(entries, locale)); } final List foreignMarkup = extractForeignMarkup(eFeed, feed, getAtomNamespace()); if (!foreignMarkup.isEmpty()) { feed.setForeignMarkup(foreignMarkup); } + return feed; + } private Link parseLink(final Element eLink) { + final Link link = new Link(); - String att = getAttributeValue(eLink, "rel"); - if (att != null) { - link.setRel(att); + + final String rel = getAttributeValue(eLink, "rel"); + if (rel != null) { + link.setRel(rel); } - att = getAttributeValue(eLink, "type"); - if (att != null) { - link.setType(att); + + final String type = getAttributeValue(eLink, "type"); + if (type != null) { + link.setType(type); } - att = getAttributeValue(eLink, "href"); - if (att != null) { - link.setHref(att); + + final String href = getAttributeValue(eLink, "href"); + if (href != null) { + link.setHref(href); } + return link; + } - // List(Elements) -> List(Link) private List parseLinks(final List eLinks, final boolean alternate) { + final List links = new ArrayList(); - 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"); if (alternate) { if ("alternate".equals(rel)) { @@ -192,11 +204,9 @@ public class Atom03Parser extends BaseWireFeedParser { } } } - if (!links.isEmpty()) { - return links; - } else { - return null; - } + + return Lists.emptyToNull(links); + } // List(Elements) -> List(Link) @@ -210,51 +220,67 @@ public class Atom03Parser extends BaseWireFeedParser { } private Person parsePerson(final Element ePerson) { + final Person person = new Person(); - Element e = ePerson.getChild("name", getAtomNamespace()); - if (e != null) { - person.setName(e.getText()); + + final Element name = ePerson.getChild("name", getAtomNamespace()); + + if (name != null) { + person.setName(name.getText()); } - e = ePerson.getChild("url", getAtomNamespace()); - if (e != null) { - person.setUrl(e.getText()); + + final Element url = ePerson.getChild("url", getAtomNamespace()); + if (url != null) { + person.setUrl(url.getText()); } - e = ePerson.getChild("email", getAtomNamespace()); - if (e != null) { - person.setEmail(e.getText()); + + final Element email = ePerson.getChild("email", getAtomNamespace()); + if (email != null) { + person.setEmail(email.getText()); } + return person; + } // List(Elements) -> List(Persons) private List parsePersons(final List ePersons) { + final List persons = new ArrayList(); - for (int i = 0; i < ePersons.size(); i++) { - persons.add(parsePerson(ePersons.get(i))); - } - if (!persons.isEmpty()) { - return persons; - } else { - return null; + + for (final Element person : ePersons) { + persons.add(parsePerson(person)); } + + return Lists.emptyToNull(persons); + } private Content parseContent(final Element e) { + String value = null; + String type = getAttributeValue(e, "type"); if (type == null) { type = "text/plain"; } + String mode = getAttributeValue(e, "mode"); if (mode == null) { mode = Content.XML; // default to xml content } + if (mode.equals(Content.ESCAPED)) { + // do nothing XML Parser took care of this value = e.getText(); + } else if (mode.equals(Content.BASE64)) { + value = Base64.decode(e.getText()); + } else if (mode.equals(Content.XML)) { + final XMLOutputter outputter = new XMLOutputter(); final List contents = e.getContent(); for (final org.jdom2.Content content : contents) { @@ -267,6 +293,7 @@ public class Atom03Parser extends BaseWireFeedParser { } value = outputter.outputString(contents); + } final Content content = new Content(); @@ -278,71 +305,71 @@ public class Atom03Parser extends BaseWireFeedParser { // List(Elements) -> List(Entries) private List parseEntries(final List eEntries, final Locale locale) { + final List entries = new ArrayList(); - for (int i = 0; i < eEntries.size(); i++) { - entries.add(parseEntry(eEntries.get(i), locale)); - } - if (!entries.isEmpty()) { - return entries; - } else { - return null; + for (final Element entry : eEntries) { + entries.add(parseEntry(entry, locale)); } + + return Lists.emptyToNull(entries); + } private Entry parseEntry(final Element eEntry, final Locale locale) { + final Entry entry = new Entry(); - Element e = eEntry.getChild("title", getAtomNamespace()); - if (e != null) { - entry.setTitleEx(parseContent(e)); + final Element title = eEntry.getChild("title", getAtomNamespace()); + if (title != null) { + entry.setTitleEx(parseContent(title)); } - List eList = eEntry.getChildren("link", getAtomNamespace()); - entry.setAlternateLinks(parseAlternateLinks(eList)); - entry.setOtherLinks(parseOtherLinks(eList)); + final List links = eEntry.getChildren("link", getAtomNamespace()); + entry.setAlternateLinks(parseAlternateLinks(links)); + entry.setOtherLinks(parseOtherLinks(links)); - e = eEntry.getChild("author", getAtomNamespace()); - if (e != null) { + final Element author = eEntry.getChild("author", getAtomNamespace()); + if (author != null) { final List authors = new ArrayList(); - authors.add(parsePerson(e)); + authors.add(parsePerson(author)); entry.setAuthors(authors); } - eList = eEntry.getChildren("contributor", getAtomNamespace()); - if (!eList.isEmpty()) { - entry.setContributors(parsePersons(eList)); + final List contributors = eEntry.getChildren("contributor", getAtomNamespace()); + if (!contributors.isEmpty()) { + entry.setContributors(parsePersons(contributors)); } - e = eEntry.getChild("id", getAtomNamespace()); - if (e != null) { - entry.setId(e.getText()); + final Element id = eEntry.getChild("id", getAtomNamespace()); + if (id != null) { + entry.setId(id.getText()); } - e = eEntry.getChild("modified", getAtomNamespace()); - if (e != null) { - entry.setModified(DateParser.parseDate(e.getText(), locale)); + final Element modified = eEntry.getChild("modified", getAtomNamespace()); + if (modified != null) { + entry.setModified(DateParser.parseDate(modified.getText(), locale)); } - e = eEntry.getChild("issued", getAtomNamespace()); - if (e != null) { - entry.setIssued(DateParser.parseDate(e.getText(), locale)); + final Element issued = eEntry.getChild("issued", getAtomNamespace()); + if (issued != null) { + entry.setIssued(DateParser.parseDate(issued.getText(), locale)); } - e = eEntry.getChild("created", getAtomNamespace()); - if (e != null) { - entry.setCreated(DateParser.parseDate(e.getText(), locale)); + final Element created = eEntry.getChild("created", getAtomNamespace()); + if (created != null) { + entry.setCreated(DateParser.parseDate(created.getText(), locale)); } - e = eEntry.getChild("summary", getAtomNamespace()); - if (e != null) { - entry.setSummary(parseContent(e)); + final Element summary = eEntry.getChild("summary", getAtomNamespace()); + if (summary != null) { + entry.setSummary(parseContent(summary)); } - eList = eEntry.getChildren("content", getAtomNamespace()); - if (!eList.isEmpty()) { + final List contents = eEntry.getChildren("content", getAtomNamespace()); + if (!contents.isEmpty()) { final List content = new ArrayList(); - for (int i = 0; i < eList.size(); i++) { - content.add(parseContent(eList.get(i))); + for (final Element eContent : contents) { + content.add(parseContent(eContent)); } entry.setContents(content); } @@ -353,7 +380,9 @@ public class Atom03Parser extends BaseWireFeedParser { if (!foreignMarkup.isEmpty()) { entry.setForeignMarkup(foreignMarkup); } + return entry; + } } diff --git a/src/main/java/com/sun/syndication/io/impl/Atom10Generator.java b/src/main/java/com/sun/syndication/io/impl/Atom10Generator.java index ad4788b..6087a70 100644 --- a/src/main/java/com/sun/syndication/io/impl/Atom10Generator.java +++ b/src/main/java/com/sun/syndication/io/impl/Atom10Generator.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.StringReader; import java.io.Writer; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Locale; @@ -30,6 +31,7 @@ import org.jdom2.Namespace; import org.jdom2.input.SAXBuilder; import org.jdom2.output.XMLOutputter; +import com.rometools.utils.Lists; import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.atom.Category; 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) * */ - public class Atom10Generator extends BaseWireFeedGenerator { + private static final String ATOM_10_URI = "http://www.w3.org/2005/Atom"; 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) { + final Element root = new Element("feed", getFeedNamespace()); + root.addNamespaceDeclaration(getFeedNamespace()); + // Attribute version = new Attribute("version", getVersion()); // 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); + return root; + } 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 { final List items = feed.getEntries(); - for (int i = 0; i < items.size(); i++) { - addEntry(items.get(i), parent); + for (final Entry entry : items) { + addEntry(entry, parent); } checkEntriesConstraints(parent); } protected void addEntry(final Entry entry, final Element parent) throws FeedException { + 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); generateForeignMarkup(eEntry, entry.getForeignMarkup()); checkEntryConstraints(eEntry); generateItemModules(entry.getModules(), eEntry); parent.addContent(eEntry); + } 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()); - fillContentElement(titleElement, feed.getTitleEx()); + fillContentElement(titleElement, titleEx); eFeed.addContent(titleElement); } - List links = feed.getAlternateLinks(); - if (links != null) { - for (int i = 0; i < links.size(); i++) { - eFeed.addContent(generateLinkElement(links.get(i))); + final List alternateLinks = feed.getAlternateLinks(); + if (alternateLinks != null) { + for (final Link link : alternateLinks) { + eFeed.addContent(generateLinkElement(link)); } } - links = feed.getOtherLinks(); - if (links != null) { - for (int j = 0; j < links.size(); j++) { - eFeed.addContent(generateLinkElement(links.get(j))); + + final List otherLinks = feed.getOtherLinks(); + if (otherLinks != null) { + for (final Link link : otherLinks) { + eFeed.addContent(generateLinkElement(link)); } } @@ -159,133 +177,153 @@ public class Atom10Generator extends BaseWireFeedGenerator { } final List authors = feed.getAuthors(); - if (authors != null && !authors.isEmpty()) { - for (int i = 0; i < authors.size(); i++) { + if (Lists.isNotEmpty(authors)) { + for (final SyndPerson author : authors) { final Element authorElement = new Element("author", getFeedNamespace()); - fillPersonElement(authorElement, feed.getAuthors().get(i)); + fillPersonElement(authorElement, author); eFeed.addContent(authorElement); } } final List contributors = feed.getContributors(); - if (contributors != null && !contributors.isEmpty()) { - for (int i = 0; i < contributors.size(); i++) { + if (Lists.isNotEmpty(contributors)) { + for (final SyndPerson contributor : contributors) { final Element contributorElement = new Element("contributor", getFeedNamespace()); - fillPersonElement(contributorElement, contributors.get(i)); + fillPersonElement(contributorElement, contributor); eFeed.addContent(contributorElement); } } - if (feed.getSubtitle() != null) { + final Content subtitle = feed.getSubtitle(); + if (subtitle != null) { final Element subtitleElement = new Element("subtitle", getFeedNamespace()); - fillContentElement(subtitleElement, feed.getSubtitle()); + fillContentElement(subtitleElement, subtitle); eFeed.addContent(subtitleElement); } - if (feed.getId() != null) { - eFeed.addContent(generateSimpleElement("id", feed.getId())); + final String id = feed.getId(); + if (id != null) { + eFeed.addContent(generateSimpleElement("id", id)); } - if (feed.getGenerator() != null) { - eFeed.addContent(generateGeneratorElement(feed.getGenerator())); + final Generator generator = feed.getGenerator(); + if (generator != null) { + eFeed.addContent(generateGeneratorElement(generator)); } - if (feed.getRights() != null) { - eFeed.addContent(generateSimpleElement("rights", feed.getRights())); + final String rights = feed.getRights(); + if (rights != null) { + eFeed.addContent(generateSimpleElement("rights", rights)); } - if (feed.getIcon() != null) { - eFeed.addContent(generateSimpleElement("icon", feed.getIcon())); + final String icon = feed.getIcon(); + if (icon != null) { + eFeed.addContent(generateSimpleElement("icon", icon)); } - if (feed.getLogo() != null) { - eFeed.addContent(generateSimpleElement("logo", feed.getLogo())); + final String 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()); - updatedElement.addContent(DateParser.formatW3CDateTime(feed.getUpdated(), Locale.US)); + updatedElement.addContent(DateParser.formatW3CDateTime(updated, Locale.US)); eFeed.addContent(updatedElement); } + } 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()); - fillContentElement(titleElement, entry.getTitleEx()); + fillContentElement(titleElement, titleEx); eEntry.addContent(titleElement); } - List links = entry.getAlternateLinks(); - if (links != null) { - for (int i = 0; i < links.size(); i++) { - eEntry.addContent(generateLinkElement(links.get(i))); + + final List alternateLinks = entry.getAlternateLinks(); + if (alternateLinks != null) { + for (final Link link : alternateLinks) { + eEntry.addContent(generateLinkElement(link)); } } - links = entry.getOtherLinks(); - if (links != null) { - for (int i = 0; i < links.size(); i++) { - eEntry.addContent(generateLinkElement(links.get(i))); + + final List otherLinks = entry.getOtherLinks(); + if (otherLinks != null) { + for (final Link link : otherLinks) { + eEntry.addContent(generateLinkElement(link)); } } final List cats = entry.getCategories(); if (cats != null) { - for (int i = 0; i < cats.size(); i++) { - eEntry.addContent(generateCategoryElement(cats.get(i))); + for (final Category category : cats) { + eEntry.addContent(generateCategoryElement(category)); } } final List authors = entry.getAuthors(); - if (authors != null && !authors.isEmpty()) { - for (int i = 0; i < authors.size(); i++) { + if (Lists.isNotEmpty(authors)) { + for (final SyndPerson author : authors) { final Element authorElement = new Element("author", getFeedNamespace()); - fillPersonElement(authorElement, entry.getAuthors().get(i)); + fillPersonElement(authorElement, author); eEntry.addContent(authorElement); } } final List contributors = entry.getContributors(); - if (contributors != null && !contributors.isEmpty()) { - for (int i = 0; i < contributors.size(); i++) { + if (Lists.isNotEmpty(contributors)) { + for (final SyndPerson contributor : contributors) { final Element contributorElement = new Element("contributor", getFeedNamespace()); - fillPersonElement(contributorElement, contributors.get(i)); + fillPersonElement(contributorElement, contributor); 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()); - updatedElement.addContent(DateParser.formatW3CDateTime(entry.getUpdated(), Locale.US)); + updatedElement.addContent(DateParser.formatW3CDateTime(updated, Locale.US)); eEntry.addContent(updatedElement); } - if (entry.getPublished() != null) { + final Date published = entry.getPublished(); + if (published != null) { 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); } - if (entry.getContents() != null && !entry.getContents().isEmpty()) { + final List contents = entry.getContents(); + if (Lists.isNotEmpty(contents)) { final Element contentElement = new Element("content", getFeedNamespace()); - final Content content = entry.getContents().get(0); + final Content content = contents.get(0); fillContentElement(contentElement, content); eEntry.addContent(contentElement); } - if (entry.getSummary() != null) { + final Content summary = entry.getSummary(); + if (summary != null) { final Element summaryElement = new Element("summary", getFeedNamespace()); - fillContentElement(summaryElement, entry.getSummary()); + fillContentElement(summaryElement, summary); eEntry.addContent(summaryElement); } - if (entry.getSource() != null) { + final Feed source = entry.getSource(); + if (source != null) { final Element sourceElement = new Element("source", getFeedNamespace()); - populateFeedHeader(entry.getSource(), sourceElement); + populateFeedHeader(source, sourceElement); eEntry.addContent(sourceElement); } + } protected void checkFeedHeaderConstraints(final Element eFeed) throws FeedException { @@ -298,90 +336,122 @@ public class Atom10Generator extends BaseWireFeedGenerator { } protected Element generateCategoryElement(final Category cat) { - final Element catElement = new Element("category", getFeedNamespace()); - if (cat.getTerm() != null) { - final Attribute termAttribute = new Attribute("term", cat.getTerm()); + final Namespace namespace = getFeedNamespace(); + 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); } - if (cat.getLabel() != null) { - final Attribute labelAttribute = new Attribute("label", cat.getLabel()); + final String label = cat.getLabel(); + if (label != null) { + final Attribute labelAttribute = new Attribute("label", label); catElement.setAttribute(labelAttribute); } - if (cat.getScheme() != null) { - final Attribute schemeAttribute = new Attribute("scheme", cat.getScheme()); + final String scheme = cat.getScheme(); + if (scheme != null) { + final Attribute schemeAttribute = new Attribute("scheme", scheme); catElement.setAttribute(schemeAttribute); } + return catElement; + } protected Element generateLinkElement(final Link link) { - final Element linkElement = new Element("link", getFeedNamespace()); - if (link.getRel() != null) { - final Attribute relAttribute = new Attribute("rel", link.getRel()); + final Namespace namespace = getFeedNamespace(); + 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); } - if (link.getType() != null) { - final Attribute typeAttribute = new Attribute("type", link.getType()); + final String type = link.getType(); + if (type != null) { + final Attribute typeAttribute = new Attribute("type", type); linkElement.setAttribute(typeAttribute); } - if (link.getHref() != null) { - final Attribute hrefAttribute = new Attribute("href", link.getHref()); + final String href = link.getHref(); + if (href != null) { + final Attribute hrefAttribute = new Attribute("href", href); linkElement.setAttribute(hrefAttribute); } - if (link.getHreflang() != null) { - final Attribute hreflangAttribute = new Attribute("hreflang", link.getHreflang()); + final String hreflang = link.getHreflang(); + if (hreflang != null) { + final Attribute hreflangAttribute = new Attribute("hreflang", hreflang); 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); } + if (link.getLength() != 0) { final Attribute lenght = new Attribute("length", Long.toString(link.getLength())); linkElement.setAttribute(lenght); } + return linkElement; + } protected void fillPersonElement(final Element element, final SyndPerson person) { - if (person.getName() != null) { - element.addContent(generateSimpleElement("name", person.getName())); - } - if (person.getUri() != null) { - element.addContent(generateSimpleElement("uri", person.getUri())); + + final String name = person.getName(); + if (name != null) { + element.addContent(generateSimpleElement("name", name)); } - if (person.getEmail() != null) { - element.addContent(generateSimpleElement("email", person.getEmail())); + final String uri = person.getUri(); + 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); + } protected Element generateTagLineElement(final Content tagline) { + final Element taglineElement = new Element("subtitle", getFeedNamespace()); - if (tagline.getType() != null) { - final Attribute typeAttribute = new Attribute("type", tagline.getType()); + final String type = tagline.getType(); + if (type != null) { + final Attribute typeAttribute = new Attribute("type", type); taglineElement.setAttribute(typeAttribute); } - if (tagline.getValue() != null) { - taglineElement.addContent(tagline.getValue()); + final String value = tagline.getValue(); + if (value != null) { + taglineElement.addContent(value); } + return taglineElement; + } protected void fillContentElement(final Element contentElement, final Content content) throws FeedException { final String type = content.getType(); + String atomType = type; + if (type != null) { // 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 @@ -396,16 +466,20 @@ public class Atom10Generator extends BaseWireFeedGenerator { final Attribute typeAttribute = new Attribute("type", atomType); contentElement.setAttribute(typeAttribute); } + final String href = content.getSrc(); if (href != null) { final Attribute srcAttribute = new Attribute("src", href); 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)) { final StringBuffer tmpDocString = new StringBuffer(""); - tmpDocString.append(content.getValue()); + tmpDocString.append(value); tmpDocString.append(""); final StringReader tmpDocReader = new StringReader(tmpDocString.toString()); Document tmpDoc; @@ -417,29 +491,37 @@ public class Atom10Generator extends BaseWireFeedGenerator { } final List children = tmpDoc.getRootElement().removeContent(); contentElement.addContent(children); + } else { + // must be type html, text or some other non-XML format // JDOM will escape property for XML - contentElement.addContent(content.getValue()); + contentElement.addContent(value); + } + } } protected Element generateGeneratorElement(final Generator generator) { + final Element generatorElement = new Element("generator", getFeedNamespace()); - if (generator.getUrl() != null) { - final Attribute urlAttribute = new Attribute("uri", generator.getUrl()); + final String url = generator.getUrl(); + if (url != null) { + final Attribute urlAttribute = new Attribute("uri", url); generatorElement.setAttribute(urlAttribute); } - if (generator.getVersion() != null) { - final Attribute versionAttribute = new Attribute("version", generator.getVersion()); + final String version2 = generator.getVersion(); + if (version2 != null) { + final Attribute versionAttribute = new Attribute("version", version2); generatorElement.setAttribute(versionAttribute); } - if (generator.getValue() != null) { - generatorElement.addContent(generator.getValue()); + final String value = generator.getValue(); + if (value != null) { + generatorElement.addContent(value); } return generatorElement; diff --git a/src/main/java/com/sun/syndication/io/impl/Atom10Parser.java b/src/main/java/com/sun/syndication/io/impl/Atom10Parser.java index 713e481..1943bea 100644 --- a/src/main/java/com/sun/syndication/io/impl/Atom10Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/Atom10Parser.java @@ -33,6 +33,7 @@ import org.jdom2.Parent; import org.jdom2.input.SAXBuilder; import org.jdom2.output.XMLOutputter; +import com.rometools.utils.Lists; import com.sun.syndication.feed.WireFeed; import com.sun.syndication.feed.atom.Category; import com.sun.syndication.feed.atom.Content; @@ -52,6 +53,7 @@ import com.sun.syndication.io.WireFeedOutput; * @author Dave Johnson */ public class Atom10Parser extends BaseWireFeedParser { + 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); @@ -94,13 +96,9 @@ public class Atom10Parser extends BaseWireFeedParser { } protected void validateFeed(final Document document) throws FeedException { - // TBD - // here we have to validate the Feed against a schema or whatever - // not sure how to do it - // 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. + // TBD here we have to validate the Feed against a schema or whatever not sure how to do it + // 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 { @@ -135,203 +133,225 @@ public class Atom10Parser extends BaseWireFeedParser { } 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()); - Element e = eFeed.getChild("title", getAtomNamespace()); - if (e != null) { + final Element title = eFeed.getChild("title", getAtomNamespace()); + if (title != null) { final Content c = new Content(); - c.setValue(parseTextConstructToString(e)); - c.setType(getAttributeValue(e, "type")); + c.setValue(parseTextConstructToString(title)); + c.setType(getAttributeValue(title, "type")); feed.setTitleEx(c); } - List eList = eFeed.getChildren("link", getAtomNamespace()); - feed.setAlternateLinks(parseAlternateLinks(feed, null, baseURI, eList)); - feed.setOtherLinks(parseOtherLinks(feed, null, baseURI, eList)); + final List links = eFeed.getChildren("link", getAtomNamespace()); + feed.setAlternateLinks(parseAlternateLinks(feed, null, baseURI, links)); + feed.setOtherLinks(parseOtherLinks(feed, null, baseURI, links)); - final List cList = eFeed.getChildren("category", getAtomNamespace()); - feed.setCategories(parseCategories(baseURI, cList)); + final List categories = eFeed.getChildren("category", getAtomNamespace()); + feed.setCategories(parseCategories(baseURI, categories)); - eList = eFeed.getChildren("author", getAtomNamespace()); - if (!eList.isEmpty()) { - feed.setAuthors(parsePersons(baseURI, eList, locale)); + final List authors = eFeed.getChildren("author", getAtomNamespace()); + if (!authors.isEmpty()) { + feed.setAuthors(parsePersons(baseURI, authors, locale)); } - eList = eFeed.getChildren("contributor", getAtomNamespace()); - if (!eList.isEmpty()) { - feed.setContributors(parsePersons(baseURI, eList, locale)); + final List contributors = eFeed.getChildren("contributor", getAtomNamespace()); + if (!contributors.isEmpty()) { + feed.setContributors(parsePersons(baseURI, contributors, locale)); } - e = eFeed.getChild("subtitle", getAtomNamespace()); - if (e != null) { - final Content subtitle = new Content(); - subtitle.setValue(parseTextConstructToString(e)); - subtitle.setType(getAttributeValue(e, "type")); - feed.setSubtitle(subtitle); + final Element subtitle = eFeed.getChild("subtitle", getAtomNamespace()); + if (subtitle != null) { + final Content content = new Content(); + content.setValue(parseTextConstructToString(subtitle)); + content.setType(getAttributeValue(subtitle, "type")); + feed.setSubtitle(content); } - e = eFeed.getChild("id", getAtomNamespace()); - if (e != null) { - feed.setId(e.getText()); + final Element id = eFeed.getChild("id", getAtomNamespace()); + if (id != null) { + feed.setId(id.getText()); } - e = eFeed.getChild("generator", getAtomNamespace()); - if (e != null) { + final Element generator = eFeed.getChild("generator", getAtomNamespace()); + if (generator != null) { + final Generator gen = new Generator(); - gen.setValue(e.getText()); - String att = getAttributeValue(e, "uri"); - if (att != null) { - gen.setUrl(att); + gen.setValue(generator.getText()); + + final String uri = getAttributeValue(generator, "uri"); + if (uri != null) { + gen.setUrl(uri); } - att = getAttributeValue(e, "version"); - if (att != null) { - gen.setVersion(att); + + final String version = getAttributeValue(generator, "version"); + if (version != null) { + gen.setVersion(version); } + feed.setGenerator(gen); + } - e = eFeed.getChild("rights", getAtomNamespace()); - if (e != null) { - feed.setRights(parseTextConstructToString(e)); + final Element rights = eFeed.getChild("rights", getAtomNamespace()); + if (rights != null) { + feed.setRights(parseTextConstructToString(rights)); } - e = eFeed.getChild("icon", getAtomNamespace()); - if (e != null) { - feed.setIcon(e.getText()); + final Element icon = eFeed.getChild("icon", getAtomNamespace()); + if (icon != null) { + feed.setIcon(icon.getText()); } - e = eFeed.getChild("logo", getAtomNamespace()); - if (e != null) { - feed.setLogo(e.getText()); + final Element logo = eFeed.getChild("logo", getAtomNamespace()); + if (logo != null) { + feed.setLogo(logo.getText()); } - e = eFeed.getChild("updated", getAtomNamespace()); - if (e != null) { - feed.setUpdated(DateParser.parseDate(e.getText(), locale)); + final Element updated = eFeed.getChild("updated", getAtomNamespace()); + if (updated != null) { + feed.setUpdated(DateParser.parseDate(updated.getText(), locale)); } return feed; + } private Link parseLink(final Feed feed, final Entry entry, final String baseURI, final Element eLink) { + final Link link = new Link(); - String att = getAttributeValue(eLink, "rel"); - if (att != null) { - link.setRel(att); + + final String rel = getAttributeValue(eLink, "rel"); + if (rel != null) { + link.setRel(rel); } - att = getAttributeValue(eLink, "type"); - if (att != null) { - link.setType(att); + + final String type = getAttributeValue(eLink, "type"); + if (type != null) { + link.setType(type); } - att = getAttributeValue(eLink, "href"); - if (att != null) { - link.setHref(att); - if (isRelativeURI(att)) { - link.setHrefResolved(resolveURI(baseURI, eLink, att)); + + final String href = getAttributeValue(eLink, "href"); + if (href != null) { + link.setHref(href); + if (isRelativeURI(href)) { + link.setHrefResolved(resolveURI(baseURI, eLink, href)); } } - att = getAttributeValue(eLink, "title"); - if (att != null) { - link.setTitle(att); + + final String title = getAttributeValue(eLink, "title"); + if (title != null) { + link.setTitle(title); } - att = getAttributeValue(eLink, "hreflang"); - if (att != null) { - link.setHreflang(att); + + final String hrefLang = getAttributeValue(eLink, "hreflang"); + if (hrefLang != null) { + link.setHreflang(hrefLang); } - att = getAttributeValue(eLink, "length"); - if (att != null) { - final Long val = NumberParser.parseLong(att); + + final String length = getAttributeValue(eLink, "length"); + if (length != null) { + final Long val = NumberParser.parseLong(length); if (val != null) { link.setLength(val.longValue()); } } + return link; + } // List(Elements) -> List(Link) private List parseAlternateLinks(final Feed feed, final Entry entry, final String baseURI, final List eLinks) { + final List links = new ArrayList(); - for (int i = 0; i < eLinks.size(); i++) { - final Element eLink = eLinks.get(i); + for (final Element eLink : eLinks) { final Link link = parseLink(feed, entry, baseURI, eLink); if (link.getRel() == null || "".equals(link.getRel().trim()) || "alternate".equals(link.getRel())) { links.add(link); } } - if (!links.isEmpty()) { - return links; - } else { - return null; - } + + return Lists.emptyToNull(links); + } private List parseOtherLinks(final Feed feed, final Entry entry, final String baseURI, final List eLinks) { + final List links = new ArrayList(); - for (int i = 0; i < eLinks.size(); i++) { - final Element eLink = eLinks.get(i); + for (final Element eLink : eLinks) { final Link link = parseLink(feed, entry, baseURI, eLink); if (!"alternate".equals(link.getRel())) { links.add(link); } } - if (!links.isEmpty()) { - return links; - } else { - return null; - } + + return Lists.emptyToNull(links); + } private Person parsePerson(final String baseURI, final Element ePerson, final Locale locale) { + final Person person = new Person(); - Element e = ePerson.getChild("name", getAtomNamespace()); - if (e != null) { - person.setName(e.getText()); + + final Element name = ePerson.getChild("name", getAtomNamespace()); + if (name != null) { + person.setName(name.getText()); } - e = ePerson.getChild("uri", getAtomNamespace()); - if (e != null) { - person.setUri(e.getText()); - if (isRelativeURI(e.getText())) { - person.setUriResolved(resolveURI(baseURI, ePerson, e.getText())); + + final Element uri = ePerson.getChild("uri", getAtomNamespace()); + if (uri != null) { + person.setUri(uri.getText()); + if (isRelativeURI(uri.getText())) { + person.setUriResolved(resolveURI(baseURI, ePerson, uri.getText())); } } - e = ePerson.getChild("email", getAtomNamespace()); - if (e != null) { - person.setEmail(e.getText()); + + final Element email = ePerson.getChild("email", getAtomNamespace()); + if (email != null) { + person.setEmail(email.getText()); } + person.setModules(parsePersonModules(ePerson, locale)); + return person; } // List(Elements) -> List(Persons) private List parsePersons(final String baseURI, final List ePersons, final Locale locale) { + final List persons = new ArrayList(); - for (int i = 0; i < ePersons.size(); i++) { - persons.add(parsePerson(baseURI, ePersons.get(i), locale)); - } - if (!persons.isEmpty()) { - return persons; - } else { - return null; + for (final Element ePerson : ePersons) { + persons.add(parsePerson(baseURI, ePerson, locale)); } + + return Lists.emptyToNull(persons); + } private Content parseContent(final Element e) { + final String value = parseTextConstructToString(e); final String src = getAttributeValue(e, "src"); final String type = getAttributeValue(e, "type"); + final Content content = new Content(); content.setSrc(src); content.setType(type); content.setValue(value); return content; + } private String parseTextConstructToString(final Element e) { - String value = null; + String type = getAttributeValue(e, "type"); if (type == null) { type = Content.TEXT; } + + String value = null; if (type.equals(Content.XHTML) || type.indexOf("/xml") != -1 || type.indexOf("+xml") != -1) { // XHTML content needs special handling final XMLOutputter outputter = new XMLOutputter(); @@ -349,23 +369,25 @@ public class Atom10Parser extends BaseWireFeedParser { // Everything else comes in verbatim value = e.getText(); } + return value; + } // List(Elements) -> List(Entries) protected List parseEntries(final Feed feed, final String baseURI, final List eEntries, final Locale locale) { + final List entries = new ArrayList(); - for (int i = 0; i < eEntries.size(); i++) { - entries.add(this.parseEntry(feed, eEntries.get(i), baseURI, locale)); - } - if (!entries.isEmpty()) { - return entries; - } else { - return null; + for (final Element entry : eEntries) { + entries.add(this.parseEntry(feed, entry, baseURI, locale)); } + + return Lists.emptyToNull(entries); + } protected Entry parseEntry(final Feed feed, final Element eEntry, final String baseURI, final Locale locale) { + final Entry entry = new Entry(); final String xmlBase = eEntry.getAttributeValue("base", Namespace.XML_NAMESPACE); @@ -373,67 +395,67 @@ public class Atom10Parser extends BaseWireFeedParser { entry.setXmlBase(xmlBase); } - Element e = eEntry.getChild("title", getAtomNamespace()); - if (e != null) { + final Element title = eEntry.getChild("title", getAtomNamespace()); + if (title != null) { final Content c = new Content(); - c.setValue(parseTextConstructToString(e)); - c.setType(getAttributeValue(e, "type")); + c.setValue(parseTextConstructToString(title)); + c.setType(getAttributeValue(title, "type")); entry.setTitleEx(c); } - List eList = eEntry.getChildren("link", getAtomNamespace()); - entry.setAlternateLinks(parseAlternateLinks(feed, entry, baseURI, eList)); - entry.setOtherLinks(parseOtherLinks(feed, entry, baseURI, eList)); + final List links = eEntry.getChildren("link", getAtomNamespace()); + entry.setAlternateLinks(parseAlternateLinks(feed, entry, baseURI, links)); + entry.setOtherLinks(parseOtherLinks(feed, entry, baseURI, links)); - eList = eEntry.getChildren("author", getAtomNamespace()); - if (!eList.isEmpty()) { - entry.setAuthors(parsePersons(baseURI, eList, locale)); + final List authors = eEntry.getChildren("author", getAtomNamespace()); + if (!authors.isEmpty()) { + entry.setAuthors(parsePersons(baseURI, authors, locale)); } - eList = eEntry.getChildren("contributor", getAtomNamespace()); - if (!eList.isEmpty()) { - entry.setContributors(parsePersons(baseURI, eList, locale)); + final List contributors = eEntry.getChildren("contributor", getAtomNamespace()); + if (!contributors.isEmpty()) { + entry.setContributors(parsePersons(baseURI, contributors, locale)); } - e = eEntry.getChild("id", getAtomNamespace()); - if (e != null) { - entry.setId(e.getText()); + final Element id = eEntry.getChild("id", getAtomNamespace()); + if (id != null) { + entry.setId(id.getText()); } - e = eEntry.getChild("updated", getAtomNamespace()); - if (e != null) { - entry.setUpdated(DateParser.parseDate(e.getText(), locale)); + final Element updated = eEntry.getChild("updated", getAtomNamespace()); + if (updated != null) { + entry.setUpdated(DateParser.parseDate(updated.getText(), locale)); } - e = eEntry.getChild("published", getAtomNamespace()); - if (e != null) { - entry.setPublished(DateParser.parseDate(e.getText(), locale)); + final Element published = eEntry.getChild("published", getAtomNamespace()); + if (published != null) { + entry.setPublished(DateParser.parseDate(published.getText(), locale)); } - e = eEntry.getChild("summary", getAtomNamespace()); - if (e != null) { - entry.setSummary(parseContent(e)); + final Element summary = eEntry.getChild("summary", getAtomNamespace()); + if (summary != null) { + entry.setSummary(parseContent(summary)); } - e = eEntry.getChild("content", getAtomNamespace()); - if (e != null) { + final Element content = eEntry.getChild("content", getAtomNamespace()); + if (content != null) { final List contents = new ArrayList(); - contents.add(parseContent(e)); + contents.add(parseContent(content)); entry.setContents(contents); } - e = eEntry.getChild("rights", getAtomNamespace()); - if (e != null) { - entry.setRights(e.getText()); + final Element rights = eEntry.getChild("rights", getAtomNamespace()); + if (rights != null) { + entry.setRights(rights.getText()); } - final List cList = eEntry.getChildren("category", getAtomNamespace()); - entry.setCategories(parseCategories(baseURI, cList)); + final List categories = eEntry.getChildren("category", getAtomNamespace()); + entry.setCategories(parseCategories(baseURI, categories)); // TODO: SHOULD handle Atom entry source element - e = eEntry.getChild("source", getAtomNamespace()); - if (e != null) { - entry.setSource(parseFeedMetadata(baseURI, e, locale)); + final Element source = eEntry.getChild("source", getAtomNamespace()); + if (source != null) { + entry.setSource(parseFeedMetadata(baseURI, source, locale)); } entry.setModules(parseItemModules(eEntry, locale)); @@ -442,39 +464,43 @@ public class Atom10Parser extends BaseWireFeedParser { if (!foreignMarkup.isEmpty()) { entry.setForeignMarkup(foreignMarkup); } + return entry; } private List parseCategories(final String baseURI, final List eCategories) { + final List cats = new ArrayList(); - for (int i = 0; i < eCategories.size(); i++) { - final Element eCategory = eCategories.get(i); + for (final Element eCategory : eCategories) { cats.add(parseCategory(baseURI, eCategory)); } - if (!cats.isEmpty()) { - return cats; - } else { - return null; - } + + return Lists.emptyToNull(cats); + } private Category parseCategory(final String baseURI, final Element eCategory) { + final Category category = new Category(); - String att = getAttributeValue(eCategory, "term"); - if (att != null) { - category.setTerm(att); + + final String term = getAttributeValue(eCategory, "term"); + if (term != null) { + category.setTerm(term); } - att = getAttributeValue(eCategory, "scheme"); - if (att != null) { - category.setScheme(att); - if (isRelativeURI(att)) { - category.setSchemeResolved(resolveURI(baseURI, eCategory, att)); + + final String scheme = getAttributeValue(eCategory, "scheme"); + if (scheme != null) { + category.setScheme(scheme); + if (isRelativeURI(scheme)) { + category.setSchemeResolved(resolveURI(baseURI, eCategory, scheme)); } } - att = getAttributeValue(eCategory, "label"); - if (att != null) { - category.setLabel(att); + + final String label = getAttributeValue(eCategory, "label"); + if (label != null) { + category.setLabel(label); } + return category; } @@ -506,10 +532,13 @@ public class Atom10Parser extends BaseWireFeedParser { * @param url URL to be resolved */ public static String resolveURI(final String baseURI, final Parent parent, String url) { + if (!resolveURIs) { return url; } + if (isRelativeURI(url)) { + if (".".equals(url) || "./".equals(url)) { url = ""; } @@ -560,7 +589,9 @@ public class Atom10Parser extends BaseWireFeedParser { return formURI(baseURI, url); } } + return url; + } /** @@ -658,7 +689,8 @@ public class Atom10Parser extends BaseWireFeedParser { * Parse entry from reader. */ public static Entry parseEntry(final Reader rd, final String baseURI, final Locale locale) throws JDOMException, IOException, IllegalArgumentException, - FeedException { + FeedException { + // Parse entry into JDOM tree final SAXBuilder builder = new SAXBuilder(); final Document entryDoc = builder.build(rd); @@ -681,4 +713,5 @@ public class Atom10Parser extends BaseWireFeedParser { final Feed parsedFeed = (Feed) input.build(feedDoc); return parsedFeed.getEntries().get(0); } + } diff --git a/src/main/java/com/sun/syndication/io/impl/BaseWireFeedGenerator.java b/src/main/java/com/sun/syndication/io/impl/BaseWireFeedGenerator.java index 283b113..b93ec8c 100644 --- a/src/main/java/com/sun/syndication/io/impl/BaseWireFeedGenerator.java +++ b/src/main/java/com/sun/syndication/io/impl/BaseWireFeedGenerator.java @@ -122,25 +122,28 @@ public abstract class BaseWireFeedGenerator implements WireFeedGenerator { // ConcurrentModificationException // below - for (int i = 0; i < additionalNamespaces.size(); i++) { - final Namespace ns = additionalNamespaces.get(i); + for (final Namespace ns : additionalNamespaces) { final String prefix = ns.getPrefix(); if (prefix != null && prefix.length() > 0 && !usedPrefixes.contains(prefix)) { root.removeNamespaceDeclaration(ns); } } + } private static void collectUsedPrefixes(final Element el, final Set collector) { + final String prefix = el.getNamespacePrefix(); if (prefix != null && prefix.length() > 0 && !collector.contains(prefix)) { collector.add(prefix); } + final List kids = el.getChildren(); - for (int i = 0; i < kids.size(); i++) { - collectUsedPrefixes(kids.get(i), collector); // recursion - // - worth it + for (final Element kid : kids) { + // recursion- worth it + collectUsedPrefixes(kid, collector); } + } } diff --git a/src/main/java/com/sun/syndication/io/impl/DCModuleGenerator.java b/src/main/java/com/sun/syndication/io/impl/DCModuleGenerator.java index 2000364..c5f2afa 100644 --- a/src/main/java/com/sun/syndication/io/impl/DCModuleGenerator.java +++ b/src/main/java/com/sun/syndication/io/impl/DCModuleGenerator.java @@ -36,9 +36,9 @@ import com.sun.syndication.io.ModuleGenerator; /** * Feed Generator for DublinCore Module. *

- * + * * @author Elaine Chien - * + * */ public class DCModuleGenerator implements ModuleGenerator { @@ -83,7 +83,7 @@ public class DCModuleGenerator implements ModuleGenerator { * It is used by the the feed generators to add their namespace definition in the root element * of the generated document (forward-missing of Java 5.0 Generics). *

- * + * * @return a set with all the URIs this module generator uses. */ @Override @@ -94,97 +94,137 @@ public class DCModuleGenerator implements ModuleGenerator { /** * Populate an element tree with elements for a module. *

- * + * * @param module the module to populate from. * @param element the root element to attach child elements to. */ @Override public final void generate(final Module module, final Element element) { + final DCModule dcModule = (DCModule) module; - if (dcModule.getTitle() != null) { + final String title = dcModule.getTitle(); + if (title != null) { element.addContent(generateSimpleElementList("title", dcModule.getTitles())); } - if (dcModule.getCreator() != null) { + + final String creator = dcModule.getCreator(); + if (creator != null) { element.addContent(generateSimpleElementList("creator", dcModule.getCreators())); } + final List subjects = dcModule.getSubjects(); - for (int i = 0; i < subjects.size(); i++) { - element.addContent(generateSubjectElement(subjects.get(i))); + for (final DCSubject dcSubject : subjects) { + element.addContent(generateSubjectElement(dcSubject)); } - if (dcModule.getDescription() != null) { + + final String description = dcModule.getDescription(); + if (description != null) { element.addContent(generateSimpleElementList("description", dcModule.getDescriptions())); } - if (dcModule.getPublisher() != null) { + + final String publisher = dcModule.getPublisher(); + if (publisher != null) { element.addContent(generateSimpleElementList("publisher", dcModule.getPublishers())); } - if (dcModule.getContributors() != null) { - element.addContent(generateSimpleElementList("contributor", dcModule.getContributors())); + + final List 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()) { 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())); } - if (dcModule.getFormat() != null) { + + final String format = dcModule.getFormat(); + if (format != null) { element.addContent(generateSimpleElementList("format", dcModule.getFormats())); } - if (dcModule.getIdentifier() != null) { + + final String identifier = dcModule.getIdentifier(); + if (identifier != null) { element.addContent(generateSimpleElementList("identifier", dcModule.getIdentifiers())); } - if (dcModule.getSource() != null) { + + final String source = dcModule.getSource(); + if (source != null) { element.addContent(generateSimpleElementList("source", dcModule.getSources())); } - if (dcModule.getLanguage() != null) { + + final String language = dcModule.getLanguage(); + if (language != null) { element.addContent(generateSimpleElementList("language", dcModule.getLanguages())); } - if (dcModule.getRelation() != null) { + + final String relation = dcModule.getRelation(); + if (relation != null) { element.addContent(generateSimpleElementList("relation", dcModule.getRelations())); } - if (dcModule.getCoverage() != null) { + + final String coverage = dcModule.getCoverage(); + if (coverage != null) { element.addContent(generateSimpleElementList("coverage", dcModule.getCoverages())); } - if (dcModule.getRights() != null) { + + final String rights = dcModule.getRights(); + if (rights != null) { element.addContent(generateSimpleElementList("rights", dcModule.getRightsList())); } + } /** * Utility method to generate an element for a subject. *

- * + * * @param subject the subject to generate an element for. * @return the element for the subject. */ protected final Element generateSubjectElement(final DCSubject subject) { + final Element subjectElement = new Element("subject", getDCNamespace()); - if (subject.getTaxonomyUri() != null) { - final Element descriptionElement = new Element("Description", getRDFNamespace()); + final String taxonomyUri = subject.getTaxonomyUri(); + final String value = subject.getValue(); + + if (taxonomyUri != null) { + + final Attribute resourceAttribute = new Attribute("resource", taxonomyUri, getRDFNamespace()); + final Element topicElement = new Element("topic", getTaxonomyNamespace()); - final Attribute resourceAttribute = new Attribute("resource", subject.getTaxonomyUri(), getRDFNamespace()); topicElement.setAttribute(resourceAttribute); + + final Element descriptionElement = new Element("Description", getRDFNamespace()); descriptionElement.addContent(topicElement); - if (subject.getValue() != null) { + if (value != null) { final Element valueElement = new Element("value", getRDFNamespace()); - valueElement.addContent(subject.getValue()); + valueElement.addContent(value); descriptionElement.addContent(valueElement); } + subjectElement.addContent(descriptionElement); + } else { - subjectElement.addContent(subject.getValue()); + subjectElement.addContent(value); } + return subjectElement; } /** * Utility method to generate a single element containing a string. *

- * + * * @param name the name of the elment to generate. * @param value the value of the text in the element. * @return the element generated. @@ -192,24 +232,22 @@ public class DCModuleGenerator implements ModuleGenerator { protected final Element generateSimpleElement(final String name, final String value) { final Element element = new Element(name, getDCNamespace()); element.addContent(value); - return element; } /** * Utility method to generate a list of simple elements. *

- * + * * @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. */ - protected final List generateSimpleElementList(final String name, final List value) { + protected final List generateSimpleElementList(final String name, final List values) { final List elements = new ArrayList(); - for (final String string : value) { - elements.add(generateSimpleElement(name, string)); + for (final String value : values) { + elements.add(generateSimpleElement(name, value)); } - return elements; } } diff --git a/src/main/java/com/sun/syndication/io/impl/DCModuleParser.java b/src/main/java/com/sun/syndication/io/impl/DCModuleParser.java index 40d24ea..038eb90 100644 --- a/src/main/java/com/sun/syndication/io/impl/DCModuleParser.java +++ b/src/main/java/com/sun/syndication/io/impl/DCModuleParser.java @@ -64,139 +64,163 @@ public class DCModuleParser implements ModuleParser { /** * Parse an element tree and return the module found in it. *

- * + * * @param dcRoot the root element containing the module elements. * @param locale for date/time parsing * @return the module parsed from the element tree, null if none. */ @Override public Module parse(final Element dcRoot, final Locale locale) { + boolean foundSomething = false; final DCModule dcm = new DCModuleImpl(); - List eList = dcRoot.getChildren("title", getDCNamespace()); - if (!eList.isEmpty()) { + final List titles = dcRoot.getChildren("title", getDCNamespace()); + if (!titles.isEmpty()) { foundSomething = true; - dcm.setTitles(parseElementList(eList)); + dcm.setTitles(parseElementList(titles)); } - eList = dcRoot.getChildren("creator", getDCNamespace()); - if (!eList.isEmpty()) { + + final List creators = dcRoot.getChildren("creator", getDCNamespace()); + if (!creators.isEmpty()) { foundSomething = true; - dcm.setCreators(parseElementList(eList)); + dcm.setCreators(parseElementList(creators)); } - eList = dcRoot.getChildren("subject", getDCNamespace()); - if (!eList.isEmpty()) { + + final List subjects = dcRoot.getChildren("subject", getDCNamespace()); + if (!subjects.isEmpty()) { foundSomething = true; - dcm.setSubjects(parseSubjects(eList)); + dcm.setSubjects(parseSubjects(subjects)); } - eList = dcRoot.getChildren("description", getDCNamespace()); - if (!eList.isEmpty()) { + + final List descriptions = dcRoot.getChildren("description", getDCNamespace()); + if (!descriptions.isEmpty()) { foundSomething = true; - dcm.setDescriptions(parseElementList(eList)); + dcm.setDescriptions(parseElementList(descriptions)); } - eList = dcRoot.getChildren("publisher", getDCNamespace()); - if (!eList.isEmpty()) { + + final List publishers = dcRoot.getChildren("publisher", getDCNamespace()); + if (!publishers.isEmpty()) { foundSomething = true; - dcm.setPublishers(parseElementList(eList)); + dcm.setPublishers(parseElementList(publishers)); } - eList = dcRoot.getChildren("contributor", getDCNamespace()); - if (!eList.isEmpty()) { + + final List contributors = dcRoot.getChildren("contributor", getDCNamespace()); + if (!contributors.isEmpty()) { foundSomething = true; - dcm.setContributors(parseElementList(eList)); + dcm.setContributors(parseElementList(contributors)); } - eList = dcRoot.getChildren("date", getDCNamespace()); - if (!eList.isEmpty()) { + + final List dates = dcRoot.getChildren("date", getDCNamespace()); + if (!dates.isEmpty()) { foundSomething = true; - dcm.setDates(parseElementListDate(eList, locale)); + dcm.setDates(parseElementListDate(dates, locale)); } - eList = dcRoot.getChildren("type", getDCNamespace()); - if (!eList.isEmpty()) { + + final List types = dcRoot.getChildren("type", getDCNamespace()); + if (!types.isEmpty()) { foundSomething = true; - dcm.setTypes(parseElementList(eList)); + dcm.setTypes(parseElementList(types)); } - eList = dcRoot.getChildren("format", getDCNamespace()); - if (!eList.isEmpty()) { + + final List formats = dcRoot.getChildren("format", getDCNamespace()); + if (!formats.isEmpty()) { foundSomething = true; - dcm.setFormats(parseElementList(eList)); + dcm.setFormats(parseElementList(formats)); } - eList = dcRoot.getChildren("identifier", getDCNamespace()); - if (!eList.isEmpty()) { + + final List identifiers = dcRoot.getChildren("identifier", getDCNamespace()); + if (!identifiers.isEmpty()) { foundSomething = true; - dcm.setIdentifiers(parseElementList(eList)); + dcm.setIdentifiers(parseElementList(identifiers)); } - eList = dcRoot.getChildren("source", getDCNamespace()); - if (!eList.isEmpty()) { + + final List sources = dcRoot.getChildren("source", getDCNamespace()); + if (!sources.isEmpty()) { foundSomething = true; - dcm.setSources(parseElementList(eList)); + dcm.setSources(parseElementList(sources)); } - eList = dcRoot.getChildren("language", getDCNamespace()); - if (!eList.isEmpty()) { + + final List languages = dcRoot.getChildren("language", getDCNamespace()); + if (!languages.isEmpty()) { foundSomething = true; - dcm.setLanguages(parseElementList(eList)); + dcm.setLanguages(parseElementList(languages)); } - eList = dcRoot.getChildren("relation", getDCNamespace()); - if (!eList.isEmpty()) { + + final List relations = dcRoot.getChildren("relation", getDCNamespace()); + if (!relations.isEmpty()) { foundSomething = true; - dcm.setRelations(parseElementList(eList)); + dcm.setRelations(parseElementList(relations)); } - eList = dcRoot.getChildren("coverage", getDCNamespace()); - if (!eList.isEmpty()) { + + final List coverages = dcRoot.getChildren("coverage", getDCNamespace()); + if (!coverages.isEmpty()) { foundSomething = true; - dcm.setCoverages(parseElementList(eList)); + dcm.setCoverages(parseElementList(coverages)); } - eList = dcRoot.getChildren("rights", getDCNamespace()); - if (!eList.isEmpty()) { + + final List rights = dcRoot.getChildren("rights", getDCNamespace()); + if (!rights.isEmpty()) { foundSomething = true; - dcm.setRightsList(parseElementList(eList)); + dcm.setRightsList(parseElementList(rights)); } + if (foundSomething) { return dcm; } else { return null; } + } /** * Utility method to parse a taxonomy from an element. *

- * + * * @param desc the taxonomy description element. * @return the string contained in the resource of the element. */ protected final String getTaxonomy(final Element desc) { - String d = null; - final Element taxo = desc.getChild("topic", getTaxonomyNamespace()); - if (taxo != null) { - final Attribute a = taxo.getAttribute("resource", getRDFNamespace()); - if (a != null) { - d = a.getValue(); + String taxonomy = null; + final Element topic = desc.getChild("topic", getTaxonomyNamespace()); + if (topic != null) { + final Attribute resource = topic.getAttribute("resource", getRDFNamespace()); + if (resource != null) { + taxonomy = resource.getValue(); } } - return d; + return taxonomy; } /** * Utility method to parse a list of subjects out of a list of elements. *

- * + * * @param eList the element list to parse. * @return a list of subjects parsed from the elements. */ protected final List parseSubjects(final List eList) { + final List subjects = new ArrayList(); - for (final Element element : eList) { - final Element eSubject = element; - final Element eDesc = eSubject.getChild("Description", getRDFNamespace()); - if (eDesc != null) { - final String taxonomy = getTaxonomy(eDesc); - final List eValues = eDesc.getChildren("value", getRDFNamespace()); - for (final Element element2 : eValues) { - final Element eValue = element2; + + for (final Element eSubject : eList) { + + final Element description = eSubject.getChild("Description", getRDFNamespace()); + + if (description != null) { + + final String taxonomy = getTaxonomy(description); + + final List values = description.getChildren("value", getRDFNamespace()); + for (final Element value : values) { + final DCSubject subject = new DCSubjectImpl(); subject.setTaxonomyUri(taxonomy); - subject.setValue(eValue.getText()); + subject.setValue(value.getText()); subjects.add(subject); + } + } else { final DCSubject subject = new DCSubjectImpl(); subject.setValue(eSubject.getText()); @@ -210,34 +234,31 @@ public class DCModuleParser implements ModuleParser { /** * Utility method to parse a list of strings out of a list of elements. *

- * - * @param eList the list of elements to parse. + * + * @param elements the list of elements to parse. * @return the list of strings */ - protected final List parseElementList(final List eList) { + protected final List parseElementList(final List elements) { final List values = new ArrayList(); - for (final Element element : eList) { - final Element e = element; - values.add(e.getText()); + for (final Element element : elements) { + values.add(element.getText()); } - return values; } /** * Utility method to parse a list of dates out of a list of elements. *

- * - * @param eList the list of elements to parse. + * + * @param elements the list of elements to parse. * @return the list of dates. */ - protected final List parseElementListDate(final List eList, final Locale locale) { + protected final List parseElementListDate(final List elements, final Locale locale) { final List values = new ArrayList(); - for (final Element element : eList) { - final Element e = element; - values.add(DateParser.parseDate(e.getText(), locale)); + for (final Element element : elements) { + values.add(DateParser.parseDate(element.getText(), locale)); } - return values; } + } diff --git a/src/main/java/com/sun/syndication/io/impl/DateParser.java b/src/main/java/com/sun/syndication/io/impl/DateParser.java index 1372aef..607e80a 100644 --- a/src/main/java/com/sun/syndication/io/impl/DateParser.java +++ b/src/main/java/com/sun/syndication/io/impl/DateParser.java @@ -31,44 +31,34 @@ import java.util.TimeZone; * It uses the JDK java.text.SimpleDateFormat class attemtping the parse using a mask for each one * of the possible formats. *

- * + * * @author Alejandro Abdelnur - * + * */ public class DateParser { private static String[] ADDITIONAL_MASKS; - static { - ADDITIONAL_MASKS = PropertiesLoader.getPropertiesLoader().getTokenizedProperty("datetime.extra.masks", "|"); - } - - // 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 + // 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" }; - // 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 + // 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[] 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'Z'", "yyyy-MM-dd't'HH:mm:ss'z'", "yyyy-MM-dd'T'HH:mmz", // together - // with - // logic - // in - // the - // parseW3CDateTime - // they - "yyyy-MM'T'HH:mmz", // handle W3C dates without time forcing them to - // 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-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 + // with + // logic + // in + // the + // parseW3CDateTime + // they + "yyyy-MM'T'HH:mmz", // handle W3C dates without time forcing them to + // 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" }; /** * 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") 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: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: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", "yyyy-MM", "yyyy" }; + "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: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:mm'Z'", // invalid + "yyyy-MM-dd't'HH:mm'z'", // invalid + "yyyy-MM-dd", "yyyy-MM", "yyyy" }; + + static { + ADDITIONAL_MASKS = PropertiesLoader.getPropertiesLoader().getTokenizedProperty("datetime.extra.masks", "|"); + } /** * Private constructor to avoid DateParser instances creation. @@ -97,12 +91,12 @@ public class DateParser { *

* It uses the masks in order until one of them succedes or all fail. *

- * + * * @param masks array of masks to use for parsing the string * @param sDate string to parse for a date. * @return the Date represented by the given string using one of the given masks. It returns * null if it was not possible to parse the the string with any of the masks. - * + * */ private static Date parseUsingMask(final String[] masks, String sDate, final Locale locale) { if (sDate != null) { @@ -145,11 +139,11 @@ public class DateParser { *

* Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element. *

- * + * * @param sDate string to parse for a date. * @return the Date represented by the given RFC822 string. It returns null if it was not * possible to parse the given string into a Date. - * + * */ public static Date parseRFC822(String sDate, final Locale locale) { final int utIndex = sDate.indexOf(" UT"); @@ -175,16 +169,15 @@ public class DateParser { *

* Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element. *

- * + * * @param sDate string to parse for a date. * @return the Date represented by the given W3C date-time string. It returns null if it * was not possible to parse the given string into a Date. - * + * */ public static Date parseW3CDateTime(String sDate, final Locale locale) { - // if sDate has time on it, it injects 'GTM' before de TZ displacement - // to - // allow the SimpleDateFormat parser to parse it properly + // if sDate has time on it, it injects 'GTM' before de TZ displacement to allow the + // SimpleDateFormat parser to parse it properly final int tIndex = sDate.indexOf("T"); if (tIndex > -1) { if (sDate.endsWith("Z")) { @@ -212,21 +205,21 @@ public class DateParser { /** * Parses a Date out of a String with a date in W3C date-time format or in a RFC822 format. *

- * + * * @param sDate string to parse for a date. * @return the Date represented by the given W3C date-time string. It returns null if it * was not possible to parse the given string into a Date. - * + * * */ public static Date parseDate(final String sDate, final Locale locale) { - Date d = parseW3CDateTime(sDate, locale); - if (d == null) { - d = parseRFC822(sDate, locale); - if (d == null && ADDITIONAL_MASKS.length > 0) { - d = parseUsingMask(ADDITIONAL_MASKS, sDate, locale); + Date date = parseW3CDateTime(sDate, locale); + if (date == null) { + date = parseRFC822(sDate, locale); + if (date == null && ADDITIONAL_MASKS.length > 0) { + date = parseUsingMask(ADDITIONAL_MASKS, sDate, locale); } } - return d; + return date; } /** @@ -234,11 +227,11 @@ public class DateParser { *

* Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element. *

- * + * * @param date Date to parse * @return the RFC822 represented by the given Date It returns null if it was not * possible to parse the date. - * + * */ public static String formatRFC822(final Date date, final Locale locale) { final SimpleDateFormat dateFormater = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", locale); @@ -251,11 +244,11 @@ public class DateParser { *

* Refer to the java.text.SimpleDateFormat javadocs for details on the format of each element. *

- * + * * @param date Date to parse * @return the W3C Date Time represented by the given Date It returns null if it was not * possible to parse the date. - * + * */ public static String formatW3CDateTime(final Date date, final Locale locale) { final SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", locale); diff --git a/src/main/java/com/sun/syndication/io/impl/FeedParsers.java b/src/main/java/com/sun/syndication/io/impl/FeedParsers.java index 8190070..51d5800 100644 --- a/src/main/java/com/sun/syndication/io/impl/FeedParsers.java +++ b/src/main/java/com/sun/syndication/io/impl/FeedParsers.java @@ -34,22 +34,22 @@ import com.sun.syndication.io.WireFeedParser; * Parsers for a specific type must extend this class and register in the parser list. (Right now * registration is hardcoded in the WireFeedParser constructor). *

- * + * * @author Alejandro Abdelnur - * + * */ public class FeedParsers extends PluginManager { /** * WireFeedParser.classes= [className] ... - * + * */ public static final String FEED_PARSERS_KEY = "WireFeedParser.classes"; /** * Creates a parser instance. *

- * + * */ public FeedParsers() { super(FEED_PARSERS_KEY); @@ -62,22 +62,20 @@ public class FeedParsers extends PluginManager { /** * Finds the real parser type for the given document feed. *

- * + * * @param document document feed to find the parser for. * @return the parser for the given document or null if there is no parser for that * document. - * + * */ public WireFeedParser getParserFor(final Document document) { final List parsers = getPlugins(); - WireFeedParser parser = null; - for (int i = 0; parser == null && i < parsers.size(); i++) { - parser = parsers.get(i); - if (!parser.isMyType(document)) { - parser = null; + for (final WireFeedParser parser : parsers) { + if (parser.isMyType(document)) { + return parser; } } - return parser; + return null; } @Override diff --git a/src/main/java/com/sun/syndication/io/impl/ModuleGenerators.java b/src/main/java/com/sun/syndication/io/impl/ModuleGenerators.java index aa91f74..c498efd 100644 --- a/src/main/java/com/sun/syndication/io/impl/ModuleGenerators.java +++ b/src/main/java/com/sun/syndication/io/impl/ModuleGenerators.java @@ -27,9 +27,8 @@ import org.jdom2.Namespace; import com.sun.syndication.feed.module.Module; import com.sun.syndication.io.ModuleGenerator; -/** - */ public class ModuleGenerators extends PluginManager { + private Set allNamespaces; public ModuleGenerators(final String propertyKey, final BaseWireFeedGenerator parentGenerator) { @@ -51,8 +50,7 @@ public class ModuleGenerators extends PluginManager { public void generateModules(final List modules, final Element element) { final Map generators = getPluginMap(); - for (int i = 0; i < modules.size(); i++) { - final Module module = modules.get(i); + for (final Module module : modules) { final String namespaceUri = module.getUri(); final ModuleGenerator generator = generators.get(namespaceUri); if (generator != null) { @@ -65,11 +63,12 @@ public class ModuleGenerators extends PluginManager { if (allNamespaces == null) { allNamespaces = new HashSet(); final List mUris = getModuleNamespaces(); - for (int i = 0; i < mUris.size(); i++) { - final ModuleGenerator mGen = getGenerator(mUris.get(i)); + for (final String mUri : mUris) { + final ModuleGenerator mGen = getGenerator(mUri); allNamespaces.addAll(mGen.getNamespaces()); } } return allNamespaces; } + } diff --git a/src/main/java/com/sun/syndication/io/impl/ModuleParsers.java b/src/main/java/com/sun/syndication/io/impl/ModuleParsers.java index 84bb1d7..2e658a9 100644 --- a/src/main/java/com/sun/syndication/io/impl/ModuleParsers.java +++ b/src/main/java/com/sun/syndication/io/impl/ModuleParsers.java @@ -16,13 +16,13 @@ */ package com.sun.syndication.io.impl; -import java.util.ArrayList; import java.util.List; import java.util.Locale; import org.jdom2.Element; import org.jdom2.Namespace; +import com.rometools.utils.Lists; import com.sun.syndication.feed.module.Module; import com.sun.syndication.io.ModuleParser; import com.sun.syndication.io.WireFeedParser; @@ -30,6 +30,7 @@ import com.sun.syndication.io.WireFeedParser; /** */ public class ModuleParsers extends PluginManager { + public ModuleParsers(final String propertyKey, final WireFeedParser parentParser) { super(propertyKey, parentParser, null); } @@ -46,16 +47,13 @@ public class ModuleParsers extends PluginManager { public List parseModules(final Element root, final Locale locale) { final List parsers = getPlugins(); List modules = null; - for (int i = 0; i < parsers.size(); i++) { - final ModuleParser parser = parsers.get(i); + for (final ModuleParser parser : parsers) { final String namespaceUri = parser.getNamespaceUri(); final Namespace namespace = Namespace.getNamespace(namespaceUri); if (hasElementsFrom(root, namespace)) { final Module module = parser.parse(root, locale); if (module != null) { - if (modules == null) { - modules = new ArrayList(); - } + modules = Lists.createWhenNull(modules); modules.add(module); } } @@ -65,15 +63,14 @@ public class ModuleParsers extends PluginManager { private boolean hasElementsFrom(final Element root, final Namespace namespace) { boolean hasElements = false; - // boolean hasElements = namespace.equals(root.getNamespace()); - - if (!hasElements) { - final List children = root.getChildren(); - for (int i = 0; !hasElements && i < children.size(); i++) { - final Element child = children.get(i); - hasElements = namespace.equals(child.getNamespace()); + for (final Element child : root.getChildren()) { + final Namespace childNamespace = child.getNamespace(); + if (namespace.equals(childNamespace)) { + hasElements = true; + break; } } return hasElements; } + } diff --git a/src/main/java/com/sun/syndication/io/impl/PluginManager.java b/src/main/java/com/sun/syndication/io/impl/PluginManager.java index eb4f84d..ac14525 100644 --- a/src/main/java/com/sun/syndication/io/impl/PluginManager.java +++ b/src/main/java/com/sun/syndication/io/impl/PluginManager.java @@ -37,16 +37,17 @@ import com.sun.syndication.io.WireFeedParser; * */ public abstract class PluginManager { + private final String[] propertyValues; - private Map pluginsMap; - private List pluginsList; private final List keys; private final WireFeedParser parentParser; private final WireFeedGenerator parentGenerator; + private Map pluginsMap; + private List pluginsList; + /** * Creates a PluginManager - *

* * @param propertyKey property key defining the plugins classes * @@ -86,25 +87,32 @@ public abstract class PluginManager { // PRIVATE - LOADER PART private void loadPlugins() { + final List finalPluginsList = new ArrayList(); pluginsList = new ArrayList(); pluginsMap = new HashMap(); String className = null; + try { final Class[] classes = getClasses(); - for (final Class classe : classes) { - className = classe.getName(); - final T plugin = classe.newInstance(); + for (final Class clazz : classes) { + + className = clazz.getName(); + final T plugin = clazz.newInstance(); + if (plugin instanceof DelegatingModuleParser) { ((DelegatingModuleParser) plugin).setFeedParser(parentParser); } + if (plugin instanceof DelegatingModuleGenerator) { ((DelegatingModuleGenerator) plugin).setFeedGenerator(parentGenerator); } pluginsMap.put(getKey(plugin), plugin); + // to preserve the order of definition in the rome.properties files pluginsList.add(plugin); + } final Collection plugins = pluginsMap.values(); @@ -143,9 +151,13 @@ public abstract class PluginManager { */ @SuppressWarnings("unchecked") private Class[] getClasses() throws ClassNotFoundException { + final ClassLoader classLoader = ConfigurableClassLoader.INSTANCE.getClassLoader(); + final List> classes = new ArrayList>(); + final boolean useLoadClass = Boolean.valueOf(System.getProperty("rome.pluginmanager.useloadclass", "false")).booleanValue(); + for (final String propertyValue : propertyValues) { final Class mClass; if (useLoadClass) { @@ -155,6 +167,7 @@ public abstract class PluginManager { } classes.add(mClass); } + final Class[] array = new Class[classes.size()]; classes.toArray(array); return array; diff --git a/src/main/java/com/sun/syndication/io/impl/RSS090Parser.java b/src/main/java/com/sun/syndication/io/impl/RSS090Parser.java index 24d9bb1..1c331f3 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS090Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS090Parser.java @@ -17,7 +17,6 @@ package com.sun.syndication.io.impl; import java.util.ArrayList; -import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Locale; @@ -56,44 +55,40 @@ public class RSS090Parser extends BaseWireFeedParser { @Override public boolean isMyType(final Document document) { - boolean ok = false; final Element rssRoot = document.getRootElement(); final Namespace defaultNS = rssRoot.getNamespace(); final List additionalNSs = rssRoot.getAdditionalNamespaces(); - ok = defaultNS != null && defaultNS.equals(getRDFNamespace()); - if (ok) { - if (additionalNSs == null) { - ok = false; - } else { - ok = false; - for (int i = 0; !ok && i < additionalNSs.size(); i++) { - ok = getRSSNamespace().equals(additionalNSs.get(i)); + boolean myType = false; + if (defaultNS != null && defaultNS.equals(getRDFNamespace()) && additionalNSs != null) { + for (final Namespace namespace : additionalNSs) { + if (getRSSNamespace().equals(namespace)) { + myType = true; + break; } } } - return ok; + return myType; + } @Override public WireFeed parse(final Document document, final boolean validate, final Locale locale) throws IllegalArgumentException, FeedException { + if (validate) { validateFeed(document); } + final Element rssRoot = document.getRootElement(); return parseChannel(rssRoot, locale); } protected void validateFeed(final Document document) throws FeedException { - // TBD - // here we have to validate the Feed against a schema or whatever - // not sure how to do it - // one posibility would be to inject our own schema for the feed (they - // 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. + // TODO here we have to validate the Feed against a schema or whatever not sure how to do it + // one posibility would be to inject our own schema for the feed (they 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. */ protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { - final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); final Channel channel = new Channel(getType()); channel.setStyleSheet(getStyleSheet(rssRoot.getDocument())); - Element e = eChannel.getChild("title", getRSSNamespace()); - if (e != null) { - channel.setTitle(e.getText()); + final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); + + final Element title = eChannel.getChild("title", getRSSNamespace()); + if (title != null) { + channel.setTitle(title.getText()); } - e = eChannel.getChild("link", getRSSNamespace()); - if (e != null) { - channel.setLink(e.getText()); + + final Element link = eChannel.getChild("link", getRSSNamespace()); + if (link != null) { + channel.setLink(link.getText()); } - e = eChannel.getChild("description", getRSSNamespace()); - if (e != null) { - channel.setDescription(e.getText()); + + final Element description = eChannel.getChild("description", getRSSNamespace()); + if (description != null) { + channel.setDescription(description.getText()); } channel.setImage(parseImage(rssRoot)); channel.setTextInput(parseTextInput(rssRoot)); - // Unfortunately Microsoft's SSE extension has a special case of - // effectively putting the sharing channel module inside the RSS tag - // and not inside the channel itself. So we also need to look for - // channel modules from the root RSS element. + // Unfortunately Microsoft's SSE extension has a special case of effectively putting the + // sharing channel module inside the RSS tag and not inside the channel itself. So we also + // need to look for channel modules from the root RSS element. final List allFeedModules = new ArrayList(); final List rootModules = parseFeedModules(rssRoot, locale); final List channelModules = parseFeedModules(eChannel, locale); + if (rootModules != null) { allFeedModules.addAll(rootModules); } + if (channelModules != null) { allFeedModules.addAll(channelModules); } + channel.setModules(allFeedModules); channel.setItems(parseItems(rssRoot, locale)); @@ -189,7 +189,9 @@ public class RSS090Parser extends BaseWireFeedParser { if (!foreignMarkup.isEmpty()) { channel.setForeignMarkup(foreignMarkup); } + return channel; + } /** @@ -232,25 +234,33 @@ public class RSS090Parser extends BaseWireFeedParser { * @return the parsed image bean. */ protected Image parseImage(final Element rssRoot) { + Image image = null; + final Element eImage = getImage(rssRoot); if (eImage != null) { + image = new Image(); - Element e = eImage.getChild("title", getRSSNamespace()); - if (e != null) { - image.setTitle(e.getText()); + final Element title = eImage.getChild("title", getRSSNamespace()); + if (title != null) { + image.setTitle(title.getText()); } - e = eImage.getChild("url", getRSSNamespace()); - if (e != null) { - image.setUrl(e.getText()); + + final Element url = eImage.getChild("url", getRSSNamespace()); + if (url != null) { + image.setUrl(url.getText()); } - e = eImage.getChild("link", getRSSNamespace()); - if (e != null) { - image.setLink(e.getText()); + + final Element link = eImage.getChild("link", getRSSNamespace()); + if (link != null) { + image.setLink(link.getText()); } + } + return image; + } /** @@ -265,12 +275,9 @@ public class RSS090Parser extends BaseWireFeedParser { * @return a list with all the parsed RSSItem beans. */ protected List parseItems(final Element rssRoot, final Locale locale) { - final Collection eItems = getItems(rssRoot); - final List items = new ArrayList(); - for (final Element element : eItems) { - final Element eItem = element; - items.add(parseItem(rssRoot, eItem, locale)); + for (final Element item : getItems(rssRoot)) { + items.add(parseItem(rssRoot, item, locale)); } return items; } @@ -286,29 +293,32 @@ public class RSS090Parser extends BaseWireFeedParser { * @return the parsed RSSItem bean. */ protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { + final Item item = new Item(); - Element e = eItem.getChild("title", getRSSNamespace()); - if (e != null) { - item.setTitle(e.getText()); + + final Element title = eItem.getChild("title", getRSSNamespace()); + if (title != null) { + item.setTitle(title.getText()); } - e = eItem.getChild("link", getRSSNamespace()); - if (e != null) { - item.setLink(e.getText()); - item.setUri(e.getText()); + + final Element link = eItem.getChild("link", getRSSNamespace()); + if (link != null) { + item.setLink(link.getText()); + item.setUri(link.getText()); } item.setModules(parseItemModules(eItem, locale)); final List foreignMarkup = extractForeignMarkup(eItem, item, getRSSNamespace()); - // content:encoded elements are treated special, without a module, they - // have to be removed from the foreign - // markup to avoid duplication in case of read/write. Note that this fix - // will break if a content module is - // used + // content:encoded elements are treated special, without a module, they have to be removed + // from the foreign markup to avoid duplication in case of read/write. Note that this fix + // will break if a content module is used final Iterator iterator = foreignMarkup.iterator(); while (iterator.hasNext()) { 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(); } } @@ -330,28 +340,38 @@ public class RSS090Parser extends BaseWireFeedParser { * @return the parsed RSSTextInput bean. */ protected TextInput parseTextInput(final Element rssRoot) { + TextInput textInput = null; + final Element eTextInput = getTextInput(rssRoot); if (eTextInput != null) { + textInput = new TextInput(); - Element e = eTextInput.getChild("title", getRSSNamespace()); - if (e != null) { - textInput.setTitle(e.getText()); + + final Element title = eTextInput.getChild("title", getRSSNamespace()); + if (title != null) { + textInput.setTitle(title.getText()); } - e = eTextInput.getChild("description", getRSSNamespace()); - if (e != null) { - textInput.setDescription(e.getText()); + + final Element description = eTextInput.getChild("description", getRSSNamespace()); + if (description != null) { + textInput.setDescription(description.getText()); } - e = eTextInput.getChild("name", getRSSNamespace()); - if (e != null) { - textInput.setName(e.getText()); + + final Element name = eTextInput.getChild("name", getRSSNamespace()); + if (name != null) { + textInput.setName(name.getText()); } - e = eTextInput.getChild("link", getRSSNamespace()); - if (e != null) { - textInput.setLink(e.getText()); + + final Element link = eTextInput.getChild("link", getRSSNamespace()); + if (link != null) { + textInput.setLink(link.getText()); } + } + return textInput; + } } diff --git a/src/main/java/com/sun/syndication/io/impl/RSS091NetscapeParser.java b/src/main/java/com/sun/syndication/io/impl/RSS091NetscapeParser.java index 7e6c3f5..b02eb9e 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS091NetscapeParser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS091NetscapeParser.java @@ -21,8 +21,6 @@ import org.jdom2.DocType; import org.jdom2.Document; import org.jdom2.Element; -/** - */ public class RSS091NetscapeParser extends RSS091UserlandParser { public RSS091NetscapeParser() { @@ -39,27 +37,15 @@ public class RSS091NetscapeParser extends RSS091UserlandParser { @Override 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) { - ok = ELEMENT_NAME.equals(docType.getElementName()); - ok = ok && PUBLIC_ID.equals(docType.getPublicID()); - ok = ok && SYSTEM_ID.equals(docType.getSystemID()); - } - } - } - } - return ok; + final Element rssRoot = document.getRootElement(); + final String name = rssRoot.getName(); + final Attribute version = rssRoot.getAttribute("version"); + 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()); + } @Override diff --git a/src/main/java/com/sun/syndication/io/impl/RSS091UserlandGenerator.java b/src/main/java/com/sun/syndication/io/impl/RSS091UserlandGenerator.java index c2a179f..28dd7c3 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS091UserlandGenerator.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS091UserlandGenerator.java @@ -26,6 +26,7 @@ import org.jdom2.Element; import org.jdom2.Namespace; 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.Image; import com.sun.syndication.feed.rss.Item; @@ -34,11 +35,12 @@ import com.sun.syndication.io.FeedException; /** * Feed Generator for RSS 0.91 *

- * + * * @author Elaine Chien - * + * */ public class RSS091UserlandGenerator extends RSS090Generator { + private final String version; public RSS091UserlandGenerator() { @@ -68,6 +70,7 @@ public class RSS091UserlandGenerator extends RSS090Generator { @Override protected void addChannel(final Channel channel, final Element parent) throws FeedException { + super.addChannel(channel, parent); final Element eChannel = parent.getChild("channel", getFeedNamespace()); @@ -75,15 +78,16 @@ public class RSS091UserlandGenerator extends RSS090Generator { addImage(channel, eChannel); addTextInput(channel, eChannel); addItems(channel, eChannel); + } @Override protected void checkChannelConstraints(final Element eChannel) throws FeedException { + checkNotNullAndLength(eChannel, "title", 1, 100); checkNotNullAndLength(eChannel, "description", 1, 500); checkNotNullAndLength(eChannel, "link", 1, 500); checkNotNullAndLength(eChannel, "language", 2, 5); - checkLength(eChannel, "rating", 20, 500); checkLength(eChannel, "copyright", 1, 100); checkLength(eChannel, "pubDate", 1, 100); @@ -95,10 +99,10 @@ public class RSS091UserlandGenerator extends RSS090Generator { final Element skipHours = eChannel.getChild("skipHours"); if (skipHours != null) { - final List hours = skipHours.getChildren(); - for (int i = 0; i < hours.size(); i++) { - final Element hour = hours.get(i); + final List hours = skipHours.getChildren(); + for (final Element hour : hours) { + final int value = Integer.parseInt(hour.getText().trim()); 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"); } } + } + } + } @Override protected void checkImageConstraints(final Element eImage) throws FeedException { checkNotNullAndLength(eImage, "title", 1, 100); checkNotNullAndLength(eImage, "url", 1, 500); - checkLength(eImage, "link", 1, 500); 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 { checkNotNullAndLength(eItem, "title", 1, 100); checkNotNullAndLength(eItem, "link", 1, 500); - checkLength(eItem, "description", 1, 500); } @@ -153,93 +158,80 @@ public class RSS091UserlandGenerator extends RSS090Generator { root.setAttribute(version); root.addNamespaceDeclaration(getContentNamespace()); generateModuleNamespaceDefs(root); - return root; } protected Element generateSkipDaysElement(final List days) { final Element skipDaysElement = new Element("skipDays"); - - for (int i = 0; i < days.size(); i++) { - skipDaysElement.addContent(generateSimpleElement("day", days.get(i).toString())); + for (final String day : days) { + skipDaysElement.addContent(generateSimpleElement("day", day.toString())); } - return skipDaysElement; } protected Element generateSkipHoursElement(final List hours) { final Element skipHoursElement = new Element("skipHours", getFeedNamespace()); - - for (int i = 0; i < hours.size(); i++) { - skipHoursElement.addContent(generateSimpleElement("hour", hours.get(i).toString())); + for (final Integer hour : hours) { + skipHoursElement.addContent(generateSimpleElement("hour", hour.toString())); } - return skipHoursElement; } @Override protected void populateChannel(final Channel channel, final Element eChannel) { + super.populateChannel(channel, eChannel); final String language = channel.getLanguage(); - if (language != null) { eChannel.addContent(generateSimpleElement("language", language)); } final String rating = channel.getRating(); - if (rating != null) { eChannel.addContent(generateSimpleElement("rating", rating)); } final String copyright = channel.getCopyright(); - if (copyright != null) { eChannel.addContent(generateSimpleElement("copyright", copyright)); } final Date pubDate = channel.getPubDate(); - if (pubDate != null) { eChannel.addContent(generateSimpleElement("pubDate", DateParser.formatRFC822(pubDate, Locale.US))); } final Date lastBuildDate = channel.getLastBuildDate(); - if (lastBuildDate != null) { eChannel.addContent(generateSimpleElement("lastBuildDate", DateParser.formatRFC822(lastBuildDate, Locale.US))); } final String docs = channel.getDocs(); - if (docs != null) { eChannel.addContent(generateSimpleElement("docs", docs)); } final String managingEditor = channel.getManagingEditor(); - if (managingEditor != null) { eChannel.addContent(generateSimpleElement("managingEditor", managingEditor)); } final String webMaster = channel.getWebMaster(); - if (webMaster != null) { eChannel.addContent(generateSimpleElement("webMaster", webMaster)); } final List skipHours = channel.getSkipHours(); - if (skipHours != null && !skipHours.isEmpty()) { eChannel.addContent(generateSkipHoursElement(skipHours)); } final List skipDays = channel.getSkipDays(); - if (skipDays != null && !skipDays.isEmpty()) { eChannel.addContent(generateSkipDaysElement(skipDays)); } + } @Override @@ -249,41 +241,44 @@ public class RSS091UserlandGenerator extends RSS090Generator { @Override protected void populateImage(final Image image, final Element eImage) { + super.populateImage(image, eImage); final Integer width = image.getWidth(); - if (width != null) { eImage.addContent(generateSimpleElement("width", String.valueOf(width))); } final Integer height = image.getHeight(); - if (height != null) { eImage.addContent(generateSimpleElement("height", String.valueOf(height))); } final String description = image.getDescription(); - if (description != null) { eImage.addContent(generateSimpleElement("description", description)); } + } @Override protected void populateItem(final Item item, final Element eItem, final int index) { + super.populateItem(item, eItem, index); final Description description = item.getDescription(); - if (description != null) { eItem.addContent(generateSimpleElement("description", description.getValue())); } - if (item.getModule(getContentNamespace().getURI()) == null && item.getContent() != null) { - final Element elem = new Element("encoded", getContentNamespace()); - elem.addContent(item.getContent().getValue()); + final Namespace contentNamespace = getContentNamespace(); + final Content content = item.getContent(); + if (item.getModule(contentNamespace.getURI()) == null && content != null) { + final Element elem = new Element("encoded", contentNamespace); + elem.addContent(content.getValue()); eItem.addContent(elem); } + } + } diff --git a/src/main/java/com/sun/syndication/io/impl/RSS091UserlandParser.java b/src/main/java/com/sun/syndication/io/impl/RSS091UserlandParser.java index bafe211..6b26f73 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS091UserlandParser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS091UserlandParser.java @@ -47,17 +47,9 @@ public class RSS091UserlandParser extends RSS090Parser { @Override public boolean isMyType(final Document document) { - boolean ok; 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()); - } - } - return ok; + final Attribute version = rssRoot.getAttribute("version"); + return rssRoot.getName().equals("rss") && version != null && version.getValue().equals(getRSSVersion()); } protected String getRSSVersion() { @@ -82,73 +74,82 @@ public class RSS091UserlandParser extends RSS090Parser { * It first invokes super.parseChannel and then parses and injects the following properties if * present: language, pubDate, rating and copyright. *

- * + * * @param rssRoot the root element of the RSS document to parse. * @return the parsed Channel bean. */ @Override protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { + final Channel channel = (Channel) super.parseChannel(rssRoot, locale); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); - Element e = eChannel.getChild("language", getRSSNamespace()); - if (e != null) { - channel.setLanguage(e.getText()); + final Element language = eChannel.getChild("language", getRSSNamespace()); + if (language != null) { + channel.setLanguage(language.getText()); } - e = eChannel.getChild("rating", getRSSNamespace()); - if (e != null) { - channel.setRating(e.getText()); + + final Element atinge = eChannel.getChild("rating", getRSSNamespace()); + if (atinge != null) { + channel.setRating(atinge.getText()); } - e = eChannel.getChild("copyright", getRSSNamespace()); - if (e != null) { - channel.setCopyright(e.getText()); + + final Element copyright = eChannel.getChild("copyright", getRSSNamespace()); + if (copyright != null) { + channel.setCopyright(copyright.getText()); } - e = eChannel.getChild("pubDate", getRSSNamespace()); - if (e != null) { - channel.setPubDate(DateParser.parseDate(e.getText(), locale)); + + final Element pubDate = eChannel.getChild("pubDate", getRSSNamespace()); + if (pubDate != null) { + channel.setPubDate(DateParser.parseDate(pubDate.getText(), locale)); } - e = eChannel.getChild("lastBuildDate", getRSSNamespace()); - if (e != null) { - channel.setLastBuildDate(DateParser.parseDate(e.getText(), locale)); + + final Element lastBuildDate = eChannel.getChild("lastBuildDate", getRSSNamespace()); + if (lastBuildDate != null) { + channel.setLastBuildDate(DateParser.parseDate(lastBuildDate.getText(), locale)); } - e = eChannel.getChild("docs", getRSSNamespace()); - if (e != null) { - channel.setDocs(e.getText()); + + final Element docs = eChannel.getChild("docs", getRSSNamespace()); + if (docs != null) { + channel.setDocs(docs.getText()); } - e = eChannel.getChild("generator", getRSSNamespace()); - if (e != null) { - channel.setGenerator(e.getText()); + + final Element generator = eChannel.getChild("generator", getRSSNamespace()); + if (generator != null) { + channel.setGenerator(generator.getText()); } - e = eChannel.getChild("managingEditor", getRSSNamespace()); - if (e != null) { - channel.setManagingEditor(e.getText()); + + final Element managingEditor = eChannel.getChild("managingEditor", getRSSNamespace()); + if (managingEditor != null) { + channel.setManagingEditor(managingEditor.getText()); } - e = eChannel.getChild("webMaster", getRSSNamespace()); - if (e != null) { - channel.setWebMaster(e.getText()); + + final Element webMaster = eChannel.getChild("webMaster", getRSSNamespace()); + 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 skipHours = new ArrayList(); - final List eHours = e.getChildren("hour", getRSSNamespace()); - for (int i = 0; i < eHours.size(); i++) { - final Element eHour = eHours.get(i); + final List eHours = eSkipHours.getChildren("hour", getRSSNamespace()); + for (final Element eHour : eHours) { skipHours.add(new Integer(eHour.getText().trim())); } channel.setSkipHours(skipHours); } - e = eChannel.getChild("skipDays"); - if (e != null) { + final Element eSkipDays = eChannel.getChild("skipDays"); + if (eSkipDays != null) { final List skipDays = new ArrayList(); - final List eDays = e.getChildren("day", getRSSNamespace()); - for (int i = 0; i < eDays.size(); i++) { - final Element eDay = eDays.get(i); + final List eDays = eSkipDays.getChildren("day", getRSSNamespace()); + for (final Element eDay : eDays) { skipDays.add(eDay.getText().trim()); } channel.setSkipDays(skipDays); } + return channel; } @@ -158,35 +159,43 @@ public class RSS091UserlandParser extends RSS090Parser { * It first invokes super.parseImage and then parses and injects the following properties if * present: url, link, width, height and description. *

- * + * * @param rssRoot the root element of the RSS document to parse for image information. * @return the parsed RSSImage bean. */ @Override protected Image parseImage(final Element rssRoot) { + final Image image = super.parseImage(rssRoot); if (image != null) { + final Element eImage = getImage(rssRoot); - Element e = eImage.getChild("width", getRSSNamespace()); - if (e != null) { - final Integer val = NumberParser.parseInt(e.getText()); + + final Element width = eImage.getChild("width", getRSSNamespace()); + if (width != null) { + final Integer val = NumberParser.parseInt(width.getText()); if (val != null) { - image.setWidth(val.intValue()); + image.setWidth(val); } } - e = eImage.getChild("height", getRSSNamespace()); - if (e != null) { - final Integer val = NumberParser.parseInt(e.getText()); + + final Element height = eImage.getChild("height", getRSSNamespace()); + if (height != null) { + final Integer val = NumberParser.parseInt(height.getText()); if (val != null) { - image.setHeight(val.intValue()); + image.setHeight(val); } } - e = eImage.getChild("description", getRSSNamespace()); - if (e != null) { - image.setDescription(e.getText()); + + final Element description = eImage.getChild("description", getRSSNamespace()); + if (description != null) { + image.setDescription(description.getText()); } + } + return image; + } /** @@ -194,13 +203,15 @@ public class RSS091UserlandParser extends RSS090Parser { */ @Override protected List getItems(final Element rssRoot) { + final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); - final List emptyList = Collections.emptyList(); + if (eChannel != null) { return eChannel.getChildren("item", getRSSNamespace()); } else { - return emptyList; + return Collections.emptyList(); } + } /** @@ -208,12 +219,15 @@ public class RSS091UserlandParser extends RSS090Parser { */ @Override protected Element getImage(final Element rssRoot) { + final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); + if (eChannel != null) { return eChannel.getChild("image", getRSSNamespace()); } else { return null; } + } /** @@ -228,13 +242,16 @@ public class RSS091UserlandParser extends RSS090Parser { */ @Override protected Element getTextInput(final Element rssRoot) { + final String elementName = getTextInputLabel(); + final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); if (eChannel != null) { return eChannel.getChild(elementName, getRSSNamespace()); } else { return null; } + } /** @@ -243,26 +260,31 @@ public class RSS091UserlandParser extends RSS090Parser { * It first invokes super.parseItem and then parses and injects the description property if * present. *

- * + * * @param rssRoot the root element of the RSS document in case it's needed for context. * @param eItem the item element to parse. * @return the parsed RSSItem bean. */ @Override protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { + final Item item = super.parseItem(rssRoot, eItem, locale); - final Element e = eItem.getChild("description", getRSSNamespace()); - if (e != null) { - item.setDescription(parseItemDescription(rssRoot, e)); + + final Element description = eItem.getChild("description", getRSSNamespace()); + 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(); content.setType(Content.HTML); - content.setValue(ce.getText()); + content.setValue(encoded.getText()); item.setContent(content); } + return item; + } protected Description parseItemDescription(final Element rssRoot, final Element eDesc) { diff --git a/src/main/java/com/sun/syndication/io/impl/RSS092Generator.java b/src/main/java/com/sun/syndication/io/impl/RSS092Generator.java index 6ec0b4a..b986a8d 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS092Generator.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS092Generator.java @@ -31,12 +31,10 @@ import com.sun.syndication.io.FeedException; /** * Feed Generator for RSS 0.92 - *

- * + * * @author Elaine Chien - * + * */ - public class RSS092Generator extends RSS091UserlandGenerator { public RSS092Generator() { @@ -49,37 +47,47 @@ public class RSS092Generator extends RSS091UserlandGenerator { @Override protected void populateChannel(final Channel channel, final Element eChannel) { + super.populateChannel(channel, eChannel); final Cloud cloud = channel.getCloud(); if (cloud != null) { eChannel.addContent(generateCloud(cloud)); } + } protected Element generateCloud(final Cloud cloud) { + final Element eCloud = new Element("cloud", getFeedNamespace()); - if (cloud.getDomain() != null) { - eCloud.setAttribute(new Attribute("domain", cloud.getDomain())); + final String domain = cloud.getDomain(); + if (domain != null) { + eCloud.setAttribute(new Attribute("domain", domain)); } - if (cloud.getPort() != 0) { - eCloud.setAttribute(new Attribute("port", String.valueOf(cloud.getPort()))); + final int port = cloud.getPort(); + if (port != 0) { + eCloud.setAttribute(new Attribute("port", String.valueOf(port))); } - if (cloud.getPath() != null) { - eCloud.setAttribute(new Attribute("path", cloud.getPath())); + final String path = cloud.getPath(); + if (path != null) { + eCloud.setAttribute(new Attribute("path", path)); } - if (cloud.getRegisterProcedure() != null) { - eCloud.setAttribute(new Attribute("registerProcedure", cloud.getRegisterProcedure())); + final String registerProcedure = cloud.getRegisterProcedure(); + if (registerProcedure != null) { + eCloud.setAttribute(new Attribute("registerProcedure", registerProcedure)); } - if (cloud.getProtocol() != null) { - eCloud.setAttribute(new Attribute("protocol", cloud.getProtocol())); + final String protocol = cloud.getProtocol(); + if (protocol != null) { + eCloud.setAttribute(new Attribute("protocol", protocol)); } + return eCloud; + } // Another one to thanks DW for @@ -93,6 +101,7 @@ public class RSS092Generator extends RSS091UserlandGenerator { @Override protected void populateItem(final Item item, final Element eItem, final int index) { + super.populateItem(item, eItem, index); final Source source = item.getSource(); @@ -106,40 +115,59 @@ public class RSS092Generator extends RSS091UserlandGenerator { } final List categories = item.getCategories(); - for (int i = 0; i < categories.size(); i++) { - eItem.addContent(generateCategoryElement(categories.get(i))); + for (final Category category : categories) { + eItem.addContent(generateCategoryElement(category)); } + } protected Element generateSourceElement(final Source source) { + 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()); + return sourceElement; } protected Element generateEnclosure(final Enclosure enclosure) { + 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; } protected Element generateCategoryElement(final Category category) { + 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()); + return categoryElement; } diff --git a/src/main/java/com/sun/syndication/io/impl/RSS092Parser.java b/src/main/java/com/sun/syndication/io/impl/RSS092Parser.java index a8d066c..336f606 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS092Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS092Parser.java @@ -56,41 +56,47 @@ public class RSS092Parser extends RSS091UserlandParser { @Override protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { + final Channel channel = (Channel) super.parseChannel(rssRoot, locale); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); + final Element eCloud = eChannel.getChild("cloud", getRSSNamespace()); + if (eCloud != null) { final Cloud cloud = new Cloud(); - String att = eCloud.getAttributeValue("domain");// getRSSNamespace()); - // DONT KNOW WHY - // DOESN'T WORK - if (att != null) { - cloud.setDomain(att); + + final String domain = eCloud.getAttributeValue("domain"); + if (domain != null) { + cloud.setDomain(domain); } - att = eCloud.getAttributeValue("port");// getRSSNamespace()); DONT - // KNOW WHY DOESN'T WORK - if (att != null) { - cloud.setPort(Integer.parseInt(att.trim())); + + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final String port = eCloud.getAttributeValue("port"); + if (port != null) { + cloud.setPort(Integer.parseInt(port.trim())); } - att = eCloud.getAttributeValue("path");// getRSSNamespace()); DONT - // KNOW WHY DOESN'T WORK - if (att != null) { - cloud.setPath(att); + + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final String path = eCloud.getAttributeValue("path"); + if (path != null) { + cloud.setPath(path); } - att = eCloud.getAttributeValue("registerProcedure");// getRSSNamespace()); - // DONT KNOW WHY - // DOESN'T WORK - if (att != null) { - cloud.setRegisterProcedure(att); + + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final String registerProcedure = eCloud.getAttributeValue("registerProcedure"); + if (registerProcedure != null) { + cloud.setRegisterProcedure(registerProcedure); } - att = eCloud.getAttributeValue("protocol");// getRSSNamespace()); - // DONT KNOW WHY DOESN'T - // WORK - if (att != null) { - cloud.setProtocol(att); + + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final String protocol = eCloud.getAttributeValue("protocol"); + if (protocol != null) { + cloud.setProtocol(protocol); } + channel.setCloud(cloud); + } return channel; } @@ -99,76 +105,83 @@ public class RSS092Parser extends RSS091UserlandParser { protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { final Item item = super.parseItem(rssRoot, eItem, locale); - Element e = eItem.getChild("source", getRSSNamespace()); - if (e != null) { + final Element eSource = eItem.getChild("source", getRSSNamespace()); + if (eSource != null) { final Source source = new Source(); - final String url = e.getAttributeValue("url");// getRSSNamespace()); - // DONT - // KNOW WHY DOESN'T WORK + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final String url = eSource.getAttributeValue("url"); source.setUrl(url); - source.setValue(e.getText()); + source.setValue(eSource.getText()); item.setSource(source); } - // 0.92 allows one enclosure occurrence, 0.93 multiple - // just saving to write some code. - final List eEnclosures = eItem.getChildren("enclosure");// getRSSNamespace()); - // DONT KNOW - // WHY - // DOESN'T - // WORK + // 0.92 allows one enclosure occurrence, 0.93 multiple just saving to write some code. + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final List eEnclosures = eItem.getChildren("enclosure"); + if (!eEnclosures.isEmpty()) { + final List enclosures = new ArrayList(); - for (int i = 0; i < eEnclosures.size(); i++) { - e = eEnclosures.get(i); + + for (final Element eEnclosure : eEnclosures) { final Enclosure enclosure = new Enclosure(); - String att = e.getAttributeValue("url");// getRSSNamespace()); - // DONT KNOW WHY DOESN'T - // WORK - if (att != null) { - enclosure.setUrl(att); + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final String url = eEnclosure.getAttributeValue("url"); + if (url != null) { + enclosure.setUrl(url); } - att = e.getAttributeValue("length");// getRSSNamespace()); DONT - // KNOW WHY DOESN'T WORK - enclosure.setLength(NumberParser.parseLong(att, 0L)); - att = e.getAttributeValue("type");// getRSSNamespace()); DONT - // KNOW WHY DOESN'T WORK - if (att != null) { - enclosure.setType(att); + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final String length = eEnclosure.getAttributeValue("length"); + enclosure.setLength(NumberParser.parseLong(length, 0L)); + + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final String type = eEnclosure.getAttributeValue("type"); + if (type != null) { + enclosure.setType(type); } + enclosures.add(enclosure); + } + item.setEnclosures(enclosures); } - final List eCats = eItem.getChildren("category");// getRSSNamespace()); - // DONT KNOW WHY - // DOESN'T WORK - item.setCategories(parseCategories(eCats)); + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final List categories = eItem.getChildren("category"); + item.setCategories(parseCategories(categories)); return item; } protected List parseCategories(final List eCats) { + List cats = null; + if (!eCats.isEmpty()) { + cats = new ArrayList(); - for (int i = 0; i < eCats.size(); i++) { + for (final Element eCat : eCats) { + final Category cat = new Category(); - final Element e = eCats.get(i); - final String att = e.getAttributeValue("domain");// getRSSNamespace()); - // DONT KNOW WHY - // DOESN'T WORK - if (att != null) { - cat.setDomain(att); + + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final String domain = eCat.getAttributeValue("domain"); + if (domain != null) { + cat.setDomain(domain); } - cat.setValue(e.getText()); + + cat.setValue(eCat.getText()); + cats.add(cat); + } } + return cats; + } @Override diff --git a/src/main/java/com/sun/syndication/io/impl/RSS093Parser.java b/src/main/java/com/sun/syndication/io/impl/RSS093Parser.java index 7e8c3e4..9a41697 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS093Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS093Parser.java @@ -41,23 +41,29 @@ public class RSS093Parser extends RSS092Parser { @Override protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { + final Item item = super.parseItem(rssRoot, eItem, locale); - Element e = eItem.getChild("pubDate", getRSSNamespace()); - if (e != null) { - item.setPubDate(DateParser.parseDate(e.getText(), locale)); + + final Element pubDate = eItem.getChild("pubDate", getRSSNamespace()); + if (pubDate != null) { + item.setPubDate(DateParser.parseDate(pubDate.getText(), locale)); } - e = eItem.getChild("expirationDate", getRSSNamespace()); - if (e != null) { - item.setExpirationDate(DateParser.parseDate(e.getText(), locale)); + + final Element expirationDate = eItem.getChild("expirationDate", getRSSNamespace()); + if (expirationDate != null) { + item.setExpirationDate(DateParser.parseDate(expirationDate.getText(), locale)); } - e = eItem.getChild("description", getRSSNamespace()); - if (e != null) { - final String type = e.getAttributeValue("type"); + + final Element description = eItem.getChild("description", getRSSNamespace()); + if (description != null) { + final String type = description.getAttributeValue("type"); if (type != null) { item.getDescription().setType(type); } } + return item; + } } diff --git a/src/main/java/com/sun/syndication/io/impl/RSS094Parser.java b/src/main/java/com/sun/syndication/io/impl/RSS094Parser.java index 95131ce..9585c4c 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS094Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS094Parser.java @@ -26,8 +26,6 @@ import com.sun.syndication.feed.rss.Channel; import com.sun.syndication.feed.rss.Guid; import com.sun.syndication.feed.rss.Item; -/** - */ public class RSS094Parser extends RSS093Parser { public RSS094Parser() { @@ -45,22 +43,19 @@ public class RSS094Parser extends RSS093Parser { @Override protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { + final Channel channel = (Channel) super.parseChannel(rssRoot, locale); + final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); - final List eCats = eChannel.getChildren("category", getRSSNamespace()); - channel.setCategories(parseCategories(eCats)); + final List categories = eChannel.getChildren("category", getRSSNamespace()); + channel.setCategories(parseCategories(categories)); - final Element eTtl = eChannel.getChild("ttl", getRSSNamespace()); - if (eTtl != null && eTtl.getText() != null) { - Integer ttlValue = null; - try { - ttlValue = new Integer(eTtl.getText()); - } catch (final NumberFormatException nfe) { - ; // let it go by - } + final Element ttl = eChannel.getChild("ttl", getRSSNamespace()); + if (ttl != null && ttl.getText() != null) { + final Integer ttlValue = NumberParser.parseInt(ttl.getText()); if (ttlValue != null) { - channel.setTtl(ttlValue.intValue()); + channel.setTtl(ttlValue); } } @@ -69,33 +64,40 @@ public class RSS094Parser extends RSS093Parser { @Override public Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { + final Item item = super.parseItem(rssRoot, eItem, locale); + item.setExpirationDate(null); - Element e = eItem.getChild("author", getRSSNamespace()); - if (e != null) { - item.setAuthor(e.getText()); + final Element author = eItem.getChild("author", getRSSNamespace()); + if (author != null) { + item.setAuthor(author.getText()); } - e = eItem.getChild("guid", getRSSNamespace()); - if (e != null) { + final Element eGuid = eItem.getChild("guid", getRSSNamespace()); + if (eGuid != null) { + final Guid guid = new Guid(); - final String att = e.getAttributeValue("isPermaLink");// getRSSNamespace()); - // DONT KNOW WHY - // DOESN'T WORK + + // getRSSNamespace()); DONT KNOW WHY DOESN'T WORK + final String att = eGuid.getAttributeValue("isPermaLink"); if (att != null) { guid.setPermaLink(att.equalsIgnoreCase("true")); } - guid.setValue(e.getText()); + + guid.setValue(eGuid.getText()); + item.setGuid(guid); + } - e = eItem.getChild("comments", getRSSNamespace()); - if (e != null) { - item.setComments(e.getText()); + final Element comments = eItem.getChild("comments", getRSSNamespace()); + if (comments != null) { + item.setComments(comments.getText()); } return item; + } } diff --git a/src/main/java/com/sun/syndication/io/impl/RSS10Generator.java b/src/main/java/com/sun/syndication/io/impl/RSS10Generator.java index a43d222..e0fe2b4 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS10Generator.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS10Generator.java @@ -29,9 +29,9 @@ import com.sun.syndication.io.FeedException; /** * Feed Generator for RSS 1.0 *

- * + * * @author Elaine Chien - * + * */ public class RSS10Generator extends RSS090Generator { @@ -54,22 +54,25 @@ public class RSS10Generator extends RSS090Generator { @Override protected void populateChannel(final Channel channel, final Element 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 items = channel.getItems(); if (!items.isEmpty()) { final Element eItems = new Element("items", getFeedNamespace()); final Element eSeq = new Element("Seq", getRDFNamespace()); - for (int i = 0; i < items.size(); i++) { - final Item item = items.get(i); - final Element eLi = new Element("li", getRDFNamespace()); + for (final Item item : items) { + final Element lis = new Element("li", getRDFNamespace()); final String uri = item.getUri(); if (uri != null) { - eLi.setAttribute("resource", uri, getRDFNamespace()); + lis.setAttribute("resource", uri, getRDFNamespace()); } - eSeq.addContent(eLi); + eSeq.addContent(lis); } eItems.addContent(eSeq); eChannel.addContent(eItems); @@ -78,10 +81,11 @@ public class RSS10Generator extends RSS090Generator { @Override protected void populateItem(final Item item, final Element eItem, final int index) { + super.populateItem(item, eItem, index); + final String link = item.getLink(); final String uri = item.getUri(); - if (uri != null) { eItem.setAttribute("about", uri, getRDFNamespace()); } else if (link != null) { @@ -92,11 +96,13 @@ public class RSS10Generator extends RSS090Generator { if (description != null) { eItem.addContent(generateSimpleElement("description", description.getValue())); } + if (item.getModule(getContentNamespace().getURI()) == null && item.getContent() != null) { final Element elem = new Element("encoded", getContentNamespace()); elem.addContent(item.getContent().getValue()); eItem.addContent(elem); } + } @Override diff --git a/src/main/java/com/sun/syndication/io/impl/RSS10Parser.java b/src/main/java/com/sun/syndication/io/impl/RSS10Parser.java index cef3172..4b191c6 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS10Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS10Parser.java @@ -49,33 +49,21 @@ public class RSS10Parser extends RSS090Parser { * It checks for RDF ("http://www.w3.org/1999/02/22-rdf-syntax-ns#") namespace being defined in * the root element and for the RSS 1.0 ("http://purl.org/rss/1.0/") namespace in the channel * element. - * + * * @param document document to check if it can be parsed with this parser implementation. * @return true if the document is RSS1., false otherwise. */ @Override public boolean isMyType(final Document document) { - boolean ok = false; - final Element rssRoot = document.getRootElement(); final Namespace defaultNS = rssRoot.getNamespace(); - - 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; + return defaultNS != null && defaultNS.equals(getRDFNamespace()) && rssRoot.getChild("channel", getRSSNamespace()) != null; } /** * Returns the namespace used by RSS elements in document of the RSS 1.0 *

- * + * * @return returns "http://purl.org/rss/1.0/". */ @Override @@ -89,29 +77,32 @@ public class RSS10Parser extends RSS090Parser { * It first invokes super.parseItem and then parses and injects the description property if * present. *

- * + * * @param rssRoot the root element of the RSS document in case it's needed for context. * @param eItem the item element to parse. * @return the parsed RSSItem bean. */ @Override protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) { + final Item item = super.parseItem(rssRoot, eItem, locale); - final Element e = eItem.getChild("description", getRSSNamespace()); - if (e != null) { - item.setDescription(parseItemDescription(rssRoot, e)); + + final Element description = eItem.getChild("description", getRSSNamespace()); + 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(); content.setType(Content.HTML); - content.setValue(ce.getText()); + content.setValue(encoded.getText()); item.setContent(content); } - final String uri = eItem.getAttributeValue("about", getRDFNamespace()); - if (uri != null) { - item.setUri(uri); + final String about = eItem.getAttributeValue("about", getRDFNamespace()); + if (about != null) { + item.setUri(about); } return item; @@ -119,6 +110,7 @@ public class RSS10Parser extends RSS090Parser { @Override protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { + final Channel channel = (Channel) super.parseChannel(rssRoot, locale); final Element eChannel = rssRoot.getChild("channel", getRSSNamespace()); diff --git a/src/main/java/com/sun/syndication/io/impl/RSS20Generator.java b/src/main/java/com/sun/syndication/io/impl/RSS20Generator.java index fff29ed..ac58a35 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS20Generator.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS20Generator.java @@ -28,11 +28,10 @@ import com.sun.syndication.feed.rss.Item; /** * Feed Generator for RSS 2.0 *

- * + * * @author Elaine Chien - * + * */ - public class RSS20Generator extends RSS094Generator { public RSS20Generator() { @@ -45,6 +44,7 @@ public class RSS20Generator extends RSS094Generator { @Override protected void populateChannel(final Channel channel, final Element eChannel) { + super.populateChannel(channel, eChannel); final String generator = channel.getGenerator(); @@ -58,19 +58,20 @@ public class RSS20Generator extends RSS094Generator { } final List categories = channel.getCategories(); - for (int i = 0; i < categories.size(); i++) { - eChannel.addContent(generateCategoryElement(categories.get(i))); + for (final Category category : categories) { + eChannel.addContent(generateCategoryElement(category)); } } @Override public void populateItem(final Item item, final Element eItem, final int index) { + super.populateItem(item, eItem, index); - final Element eDescription = eItem.getChild("description", getFeedNamespace()); - if (eDescription != null) { - eDescription.removeAttribute("type"); + final Element description = eItem.getChild("description", getFeedNamespace()); + if (description != null) { + description.removeAttribute("type"); } final String author = item.getAuthor(); diff --git a/src/main/java/com/sun/syndication/io/impl/RSS20Parser.java b/src/main/java/com/sun/syndication/io/impl/RSS20Parser.java index 51fcf60..168ae57 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS20Parser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS20Parser.java @@ -22,8 +22,6 @@ import org.jdom2.Element; import com.sun.syndication.feed.rss.Description; -/** - */ public class RSS20Parser extends RSS094Parser { public RSS20Parser() { @@ -52,19 +50,11 @@ public class RSS20Parser extends RSS094Parser { @Override public boolean isMyType(final Document document) { - boolean ok; final Element rssRoot = document.getRootElement(); - ok = rssRoot.getName().equals("rss"); - if (ok) { - ok = false; - final Attribute version = rssRoot.getAttribute("version"); - 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; + final Attribute version = rssRoot.getAttribute("version"); + // 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. + return rssRoot.getName().equals("rss") && version != null && version.getValue().startsWith(getRSSVersion()); } } diff --git a/src/main/java/com/sun/syndication/io/impl/RSS20wNSParser.java b/src/main/java/com/sun/syndication/io/impl/RSS20wNSParser.java index 98578c3..1189327 100644 --- a/src/main/java/com/sun/syndication/io/impl/RSS20wNSParser.java +++ b/src/main/java/com/sun/syndication/io/impl/RSS20wNSParser.java @@ -26,16 +26,17 @@ import com.sun.syndication.feed.WireFeed; /** * To address issue with certain feeds (brought up by Charles Miller): - * + * * "During the debacle that was the rollout of RSS2.0, this namespace was tried, and even appeared * in Dave Winer's Scripting News feed for a while. It was then withdrawn, but the wonderful thing * about standards is the moment you roll one out, even if it's marked as unfinished and subject to * change, someone will end up stuck with it forever." - * + * * Note that there is not counter part on the generator, we only generate the final RSS2 - * + * */ public class RSS20wNSParser extends RSS20Parser { + private static String RSS20_URI = "http://backend.userland.com/rss2"; public RSS20wNSParser() { @@ -50,11 +51,7 @@ public class RSS20wNSParser extends RSS20Parser { public boolean isMyType(final Document document) { final Element rssRoot = document.getRootElement(); final Namespace defaultNS = rssRoot.getNamespace(); - boolean ok = defaultNS != null && defaultNS.equals(getRSSNamespace()); - if (ok) { - ok = super.isMyType(document); - } - return ok; + return defaultNS != null && defaultNS.equals(getRSSNamespace()) && super.isMyType(document); } @Override @@ -65,7 +62,7 @@ public class RSS20wNSParser extends RSS20Parser { /** * After we parse the feed we put "rss_2.0" in it (so converters and generators work) this * parser is a phantom. - * + * */ @Override protected WireFeed parseChannel(final Element rssRoot, final Locale locale) { diff --git a/src/main/java/com/sun/syndication/io/impl/SyModuleGenerator.java b/src/main/java/com/sun/syndication/io/impl/SyModuleGenerator.java index c0c32fd..4e8189a 100644 --- a/src/main/java/com/sun/syndication/io/impl/SyModuleGenerator.java +++ b/src/main/java/com/sun/syndication/io/impl/SyModuleGenerator.java @@ -17,6 +17,7 @@ package com.sun.syndication.io.impl; import java.util.Collections; +import java.util.Date; import java.util.HashSet; import java.util.Locale; import java.util.Set; @@ -31,11 +32,10 @@ import com.sun.syndication.io.ModuleGenerator; /** * Feed Generator for SY ModuleImpl *

- * + * * @author Elaine Chien - * + * */ - public class SyModuleGenerator implements ModuleGenerator { private static final String SY_URI = "http://purl.org/rss/1.0/modules/syndication/"; @@ -60,7 +60,7 @@ public class SyModuleGenerator implements ModuleGenerator { * It is used by the the feed generators to add their namespace definition in the root element * of the generated document (forward-missing of Java 5.0 Generics). *

- * + * * @return a set with all the URIs (JDOM Namespace elements) this module generator uses. */ @Override @@ -73,9 +73,10 @@ public class SyModuleGenerator implements ModuleGenerator { 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); - updatePeriodElement.addContent(syModule.getUpdatePeriod()); + updatePeriodElement.addContent(updatePeriod); element.addContent(updatePeriodElement); } @@ -83,10 +84,13 @@ public class SyModuleGenerator implements ModuleGenerator { updateFrequencyElement.addContent(String.valueOf(syModule.getUpdateFrequency())); element.addContent(updateFrequencyElement); - if (syModule.getUpdateBase() != null) { + final Date updateBase = syModule.getUpdateBase(); + if (updateBase != null) { 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); } + } + } diff --git a/src/main/java/com/sun/syndication/io/impl/SyModuleParser.java b/src/main/java/com/sun/syndication/io/impl/SyModuleParser.java index 03bd74e..9fd9ec2 100644 --- a/src/main/java/com/sun/syndication/io/impl/SyModuleParser.java +++ b/src/main/java/com/sun/syndication/io/impl/SyModuleParser.java @@ -26,9 +26,8 @@ import com.sun.syndication.feed.module.SyModule; import com.sun.syndication.feed.module.SyModuleImpl; import com.sun.syndication.io.ModuleParser; -/** - */ public class SyModuleParser implements ModuleParser { + @Override public String getNamespaceUri() { return SyModule.URI; @@ -40,29 +39,35 @@ public class SyModuleParser implements ModuleParser { @Override public Module parse(final Element syndRoot, final Locale locale) { + boolean foundSomething = false; + final SyModule sm = new SyModuleImpl(); - Element e = syndRoot.getChild("updatePeriod", getDCNamespace()); - if (e != null) { + final Element updatePeriod = syndRoot.getChild("updatePeriod", getDCNamespace()); + if (updatePeriod != null) { 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; - 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; - sm.setUpdateBase(DateParser.parseDate(e.getText(), locale)); + sm.setUpdateBase(DateParser.parseDate(updateBase.getText(), locale)); } + if (foundSomething) { return sm; } else { return null; } + } }