Refactored code

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

View file

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

View file

@ -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;
@ -43,8 +43,11 @@ import com.sun.syndication.feed.module.impl.ModuleUtils;
*
*/
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.
* <p>
*
*/
protected WireFeed() {
objBean = new ObjectBean(this.getClass(), this);
@ -62,7 +63,6 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
/**
* Creates a feed for a given type.
* <p>
*
* @param type of the feed to create.
*
@ -74,7 +74,6 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
/**
* Creates a deep 'bean' clone of the object.
* <p>
*
* @return a clone of the object.
* @throws CloneNotSupportedException thrown if an element of the object cannot be cloned.
@ -88,7 +87,6 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
/**
* Indicates whether some other object is "equal to" this one as defined by the Object equals()
* method.
* <p>
*
* @param other he reference object with which to compare.
* @return <b>true</b> if 'this' object is equal to the 'other' object.
@ -96,12 +94,15 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
*/
@Override
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<Element> 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;
}
/**
@ -194,10 +196,7 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
*/
@Override
public List<Module> getModules() {
if (modules == null) {
modules = new ArrayList<Module>();
}
return modules;
return modules = Lists.createWhenNull(modules);
}
/**
@ -233,10 +232,7 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
*
*/
public List<Element> getForeignMarkup() {
if (foreignMarkup == null) {
foreignMarkup = new ArrayList<Element>();
}
return foreignMarkup;
return foreignMarkup = Lists.createWhenNull(foreignMarkup);
}
/**
@ -269,4 +265,5 @@ public abstract class WireFeed implements Cloneable, Serializable, Extendable {
public void setStyleSheet(final String styleSheet) {
this.styleSheet = styleSheet;
}
}

View file

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

View file

@ -20,6 +20,7 @@ import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import com.rometools.utils.Strings;
import com.sun.syndication.feed.impl.ObjectBean;
/**
@ -30,6 +31,7 @@ import com.sun.syndication.feed.impl.ObjectBean;
* @author Dave Johnson (updated for Atom 1.0)
*/
public class Content implements Cloneable, Serializable {
private static final long serialVersionUID = 2036205883043031310L;
private final ObjectBean objBean;
@ -173,14 +175,11 @@ public class Content implements Cloneable, Serializable {
*
* @param mode the content mode, <b>null</b> 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;
}
/**

View file

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

View file

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

View file

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

View file

@ -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;
@ -156,11 +157,7 @@ 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);
}
/**
@ -220,10 +217,7 @@ public class Person implements SyndPerson, Cloneable, Serializable, Extendable {
*/
@Override
public List<Module> getModules() {
if (modules == null) {
modules = new ArrayList<Module>();
}
return modules;
return modules = Lists.createWhenNull(modules);
}
/**

View file

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

View file

@ -34,11 +34,24 @@ import com.sun.syndication.feed.impl.ObjectBean;
*
*/
public class DCSubjectImpl implements Cloneable, Serializable, DCSubject {
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<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("taxonomyUri", String.class);
basePropInterfaceMap.put("value", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(DCSubject.class, basePropInterfaceMap, basePropClassImplMap);
}
/**
* Default constructor. All properties are set to <b>null</b>.
* <p>
@ -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<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("taxonomyUri", String.class);
basePropInterfaceMap.put("value", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(DCSubject.class, basePropInterfaceMap, basePropClassImplMap);
}
}

View file

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

View file

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

View file

@ -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;
@ -42,8 +43,11 @@ import com.sun.syndication.feed.module.impl.ModuleUtils;
*
*/
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;
@ -269,10 +273,7 @@ public class Item implements Cloneable, Serializable, Extendable {
*
*/
public List<Enclosure> getEnclosures() {
if (enclosures == null) {
enclosures = new ArrayList<Enclosure>();
}
return enclosures;
return enclosures = Lists.createWhenNull(enclosures);
}
/**
@ -295,10 +296,7 @@ public class Item implements Cloneable, Serializable, Extendable {
*
*/
public List<Category> getCategories() {
if (categories == null) {
categories = new ArrayList<Category>();
}
return categories;
return categories = Lists.createWhenNull(categories);
}
/**
@ -388,10 +386,7 @@ public class Item implements Cloneable, Serializable, Extendable {
*/
@Override
public List<Module> getModules() {
if (modules == null) {
modules = new ArrayList<Module>();
}
return modules;
return modules = Lists.createWhenNull(modules);
}
/**
@ -427,11 +422,7 @@ public class Item implements Cloneable, Serializable, Extendable {
*
*/
public Date getPubDate() {
if (pubDate == null) {
return null;
} else {
return new Date(pubDate.getTime());
}
return Dates.copy(pubDate);
}
/**
@ -442,11 +433,7 @@ public class Item implements Cloneable, Serializable, Extendable {
*
*/
public void setPubDate(final Date pubDate) {
if (pubDate == null) {
this.pubDate = null;
} else {
this.pubDate = new Date(pubDate.getTime());
}
this.pubDate = Dates.copy(pubDate);
}
/**
@ -457,11 +444,7 @@ public class Item implements Cloneable, Serializable, Extendable {
*
*/
public Date getExpirationDate() {
if (expirationDate == null) {
return null;
} else {
return new Date(expirationDate.getTime());
}
return Dates.copy(expirationDate);
}
/**
@ -472,11 +455,7 @@ public class Item implements Cloneable, Serializable, Extendable {
*
*/
public void setExpirationDate(final Date expirationDate) {
if (expirationDate == null) {
this.expirationDate = null;
} else {
this.expirationDate = new Date(expirationDate.getTime());
}
this.expirationDate = Dates.copy(expirationDate);
}
/**
@ -487,10 +466,7 @@ public class Item implements Cloneable, Serializable, Extendable {
*
*/
public List<Element> getForeignMarkup() {
if (foreignMarkup == null) {
foreignMarkup = new ArrayList<Element>();
}
return foreignMarkup;
return foreignMarkup = Lists.createWhenNull(foreignMarkup);
}
/**

View file

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

View file

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

View file

@ -33,12 +33,27 @@ import com.sun.syndication.feed.impl.ObjectBean;
*
*/
public class SyndContentImpl implements Serializable, SyndContent {
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<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("type", String.class);
basePropInterfaceMap.put("value", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndContent.class, basePropInterfaceMap, basePropClassImplMap);
}
/**
* Default constructor. All properties are set to <b>null</b>.
* <p>
@ -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<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("type", String.class);
basePropInterfaceMap.put("value", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndContent.class, basePropInterfaceMap, basePropClassImplMap);
}
}

View file

@ -13,12 +13,28 @@ import com.sun.syndication.feed.impl.ObjectBean;
* @author Alejandro Abdelnur
*/
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<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("url", String.class);
basePropInterfaceMap.put("type", String.class);
basePropInterfaceMap.put("length", Long.TYPE);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndEnclosure.class, basePropInterfaceMap, basePropClassImplMap);
}
/**
* Default constructor. All properties are set to <b>null</b>.
* <p>
@ -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<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("url", String.class);
basePropInterfaceMap.put("type", String.class);
basePropInterfaceMap.put("length", Long.TYPE);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndEnclosure.class, basePropInterfaceMap, basePropClassImplMap);
}
}

View file

@ -28,6 +28,9 @@ import java.util.Set;
import org.jdom2.Element;
import 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;
@ -47,8 +50,13 @@ import com.sun.syndication.feed.synd.impl.URINormalizer;
*
*/
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<SyndPerson> contributors;
private SyndFeed source;
private List<Element> foreignMarkup;
private Object wireEntry; // com.sun.syndication.feed.atom.Entry or
// com.sun.syndication.feed.rss.Item
// com.sun.syndication.feed.atom.Entry or com.sun.syndication.feed.rss.Item
private Object wireEntry;
// ISSUE: some converters assume this is never null
private List<SyndCategory> categories = new ArrayList<SyndCategory>();
@ -80,8 +89,30 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
public static final Set<String> CONVENIENCE_PROPERTIES = Collections.unmodifiableSet(IGNORE_PROPERTIES);
static {
IGNORE_PROPERTIES.add("publishedDate");
IGNORE_PROPERTIES.add("author");
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("description", SyndContent.class);
basePropInterfaceMap.put("contents", SyndContent.class);
basePropInterfaceMap.put("enclosures", SyndEnclosure.class);
basePropInterfaceMap.put("modules", Module.class);
basePropInterfaceMap.put("categories", SyndCategory.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = new HashMap<Class<? extends CopyFrom>, Class<?>>();
basePropClassImplMap.put(SyndContent.class, SyndContentImpl.class);
basePropClassImplMap.put(SyndEnclosure.class, SyndEnclosureImpl.class);
basePropClassImplMap.put(SyndCategory.class, SyndCategoryImpl.class);
basePropClassImplMap.put(DCModule.class, DCModuleImpl.class);
basePropClassImplMap.put(SyModule.class, SyModuleImpl.class);
COPY_FROM_HELPER = new CopyFromHelper(SyndEntry.class, basePropInterfaceMap, basePropClassImplMap);
}
/**
@ -322,10 +353,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/
@Override
public List<SyndContent> getContents() {
if (contents == null) {
contents = new ArrayList<SyndContent>();
}
return contents;
return contents = Lists.createWhenNull(contents);
}
/**
@ -350,10 +378,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/
@Override
public List<SyndEnclosure> getEnclosures() {
if (enclosures == null) {
enclosures = new ArrayList<SyndEnclosure>();
}
return enclosures;
return enclosures = Lists.createWhenNull(enclosures);
}
/**
@ -433,9 +458,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/
@Override
public List<Module> getModules() {
if (modules == null) {
modules = new ArrayList<Module>();
}
modules = Lists.createWhenNull(modules);
if (ModuleUtils.getModule(modules, DCModule.URI) == null) {
modules.add(new DCModuleImpl());
}
@ -487,30 +510,6 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
COPY_FROM_HELPER.copy(this, obj);
}
private static final CopyFromHelper COPY_FROM_HELPER;
static {
final Map<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("description", SyndContent.class);
basePropInterfaceMap.put("contents", SyndContent.class);
basePropInterfaceMap.put("enclosures", SyndEnclosure.class);
basePropInterfaceMap.put("modules", Module.class);
basePropInterfaceMap.put("categories", SyndCategory.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = new HashMap<Class<? extends CopyFrom>, Class<?>>();
basePropClassImplMap.put(SyndContent.class, SyndContentImpl.class);
basePropClassImplMap.put(SyndEnclosure.class, SyndEnclosureImpl.class);
basePropClassImplMap.put(SyndCategory.class, SyndCategoryImpl.class);
basePropClassImplMap.put(DCModule.class, DCModuleImpl.class);
basePropClassImplMap.put(SyModule.class, SyModuleImpl.class);
COPY_FROM_HELPER = new CopyFromHelper(SyndEntry.class, basePropInterfaceMap, basePropClassImplMap);
}
/**
* Returns the links
* <p>
@ -519,10 +518,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/
@Override
public List<SyndLink> getLinks() {
if (links == null) {
links = new ArrayList<SyndLink>();
}
return links;
return links = Lists.createWhenNull(links);
}
/**
@ -544,11 +540,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/
@Override
public Date getUpdatedDate() {
if (updatedDate == null) {
return null;
} else {
return new Date(updatedDate.getTime());
}
return Dates.copy(updatedDate);
}
/**
@ -564,16 +556,9 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
@Override
public List<SyndPerson> getAuthors() {
if (authors == null) {
authors = new ArrayList<SyndPerson>();
}
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<SyndPerson> authors) {
this.authors = authors;
@ -590,20 +575,23 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/
@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;
}
/**
@ -617,22 +605,18 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/
@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<SyndPerson> getContributors() {
if (contributors == null) {
contributors = new ArrayList<SyndPerson>();
}
return contributors;
return contributors = Lists.createWhenNull(contributors);
}
@Override
@ -659,10 +643,7 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
*/
@Override
public List<Element> getForeignMarkup() {
if (foreignMarkup == null) {
foreignMarkup = new ArrayList<Element>();
}
return foreignMarkup;
return foreignMarkup = Lists.createWhenNull(foreignMarkup);
}
/**
@ -678,17 +659,11 @@ public class SyndEntryImpl implements Serializable, SyndEntry {
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<SyndLink> syndLinks = getLinks();
for (final SyndLink syndLink : syndLinks) {
if (relation.equals(syndLink.getRel())) {
return syndLink;
}
}
return null;
}
}

View file

@ -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;
@ -52,8 +52,11 @@ import com.sun.syndication.feed.synd.impl.URINormalizer;
*
*/
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,11 +95,34 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
public static final Set<String> 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<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("feedType", String.class);
basePropInterfaceMap.put("encoding", String.class);
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("description", String.class);
basePropInterfaceMap.put("image", SyndImage.class);
basePropInterfaceMap.put("entries", SyndEntry.class);
basePropInterfaceMap.put("modules", Module.class);
basePropInterfaceMap.put("categories", SyndCategory.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = new HashMap<Class<? extends CopyFrom>, Class<?>>();
basePropClassImplMap.put(SyndEntry.class, SyndEntryImpl.class);
basePropClassImplMap.put(SyndImage.class, SyndImageImpl.class);
basePropClassImplMap.put(SyndCategory.class, SyndCategoryImpl.class);
basePropClassImplMap.put(DCModule.class, DCModuleImpl.class);
basePropClassImplMap.put(SyModule.class, SyModuleImpl.class);
COPY_FROM_HELPER = new CopyFromHelper(SyndFeed.class, basePropInterfaceMap, basePropClassImplMap);
}
/**
@ -257,14 +283,18 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
*/
@Override
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);
}
/**
@ -656,10 +686,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
*/
@Override
public List<SyndEntry> getEntries() {
if (entries == null) {
entries = new ArrayList<SyndEntry>();
}
return entries;
return entries = Lists.createWhenNull(entries);
}
/**
@ -712,9 +739,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
*/
@Override
public List<Module> getModules() {
if (modules == null) {
modules = new ArrayList<Module>();
}
modules = Lists.createWhenNull(modules);
if (ModuleUtils.getModule(modules, DCModule.URI) == null) {
modules.add(new DCModuleImpl());
}
@ -766,33 +791,6 @@ 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<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("feedType", String.class);
basePropInterfaceMap.put("encoding", String.class);
basePropInterfaceMap.put("uri", String.class);
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("description", String.class);
basePropInterfaceMap.put("image", SyndImage.class);
basePropInterfaceMap.put("entries", SyndEntry.class);
basePropInterfaceMap.put("modules", Module.class);
basePropInterfaceMap.put("categories", SyndCategory.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = new HashMap<Class<? extends CopyFrom>, Class<?>>();
basePropClassImplMap.put(SyndEntry.class, SyndEntryImpl.class);
basePropClassImplMap.put(SyndImage.class, SyndImageImpl.class);
basePropClassImplMap.put(SyndCategory.class, SyndCategoryImpl.class);
basePropClassImplMap.put(DCModule.class, DCModuleImpl.class);
basePropClassImplMap.put(SyModule.class, SyModuleImpl.class);
COPY_FROM_HELPER = new CopyFromHelper(SyndFeed.class, basePropInterfaceMap, basePropClassImplMap);
}
/**
* Returns the links
* <p>
@ -801,10 +799,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
*/
@Override
public List<SyndLink> getLinks() {
if (links == null) {
links = new ArrayList<SyndLink>();
}
return links;
return links = Lists.createWhenNull(links);
}
/**
@ -820,10 +815,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
@Override
public List<SyndPerson> getAuthors() {
if (authors == null) {
authors = new ArrayList<SyndPerson>();
}
return authors;
return authors = Lists.createWhenNull(authors);
}
@Override
@ -861,10 +853,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
@Override
public List<SyndPerson> getContributors() {
if (contributors == null) {
contributors = new ArrayList<SyndPerson>();
}
return contributors;
return contributors = Lists.createWhenNull(contributors);
}
@Override
@ -881,10 +870,7 @@ public class SyndFeedImpl implements Serializable, SyndFeed {
*/
@Override
public List<Element> getForeignMarkup() {
if (foreignMarkup == null) {
foreignMarkup = new ArrayList<Element>();
}
return foreignMarkup;
return foreignMarkup = Lists.createWhenNull(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;
}
}

View file

@ -33,13 +33,30 @@ import com.sun.syndication.feed.impl.ObjectBean;
*
*/
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<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("url", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("description", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndImage.class, basePropInterfaceMap, basePropClassImplMap);
}
/**
* Default constructor. All properties are set to <b>null</b>.
* <p>
@ -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<String, Class<?>> basePropInterfaceMap = new HashMap<String, Class<?>>();
basePropInterfaceMap.put("title", String.class);
basePropInterfaceMap.put("url", String.class);
basePropInterfaceMap.put("link", String.class);
basePropInterfaceMap.put("description", String.class);
final Map<Class<? extends CopyFrom>, Class<?>> basePropClassImplMap = Collections.<Class<? extends CopyFrom>, Class<?>> emptyMap();
COPY_FROM_HELPER = new CopyFromHelper(SyndImage.class, basePropInterfaceMap, basePropClassImplMap);
}
}

View file

@ -18,9 +18,9 @@
package com.sun.syndication.feed.synd;
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;
@ -33,8 +33,11 @@ import com.sun.syndication.feed.module.impl.ModuleUtils;
*
*/
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;
@ -184,10 +187,7 @@ public class SyndPersonImpl implements Serializable, SyndPerson {
*/
@Override
public List<Module> getModules() {
if (modules == null) {
modules = new ArrayList<Module>();
}
return modules;
return modules = Lists.createWhenNull(modules);
}
/**
@ -214,4 +214,5 @@ public class SyndPersonImpl implements Serializable, SyndPerson {
public Module getModule(final String uri) {
return ModuleUtils.getModule(getModules(), uri);
}
}

View file

@ -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<Element> 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<Link> 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<SyndLink> syndLinks = new ArrayList<SyndLink>();
if (aFeed.getAlternateLinks() != null && !aFeed.getAlternateLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(aFeed.getAlternateLinks()));
if (Lists.isNotEmpty(alternateLinks)) {
syndLinks.addAll(createSyndLinks(alternateLinks));
}
if (aFeed.getOtherLinks() != null && !aFeed.getOtherLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(aFeed.getOtherLinks()));
final List<Link> otherLinks = aFeed.getOtherLinks();
if (Lists.isNotEmpty(otherLinks)) {
syndLinks.addAll(createSyndLinks(otherLinks));
}
syndFeed.setLinks(syndLinks);
@ -109,7 +122,7 @@ public class ConverterForAtom03 implements Converter {
}
final List<Entry> 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<SyndPerson> 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<SyndLink> createSyndLinks(final List<Link> aLinks) {
final ArrayList<SyndLink> sLinks = new ArrayList<SyndLink>();
for (final Link link2 : aLinks) {
final Link link = link2;
protected List<SyndLink> createSyndLinks(final List<Link> atomLinks) {
final ArrayList<SyndLink> syndLinks = new ArrayList<SyndLink>();
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<SyndEntry> createSyndEntries(final List<Entry> atomEntries, final boolean preserveWireItems) {
final List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
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<Element> 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<Link> 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<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>();
if (entry.getOtherLinks() != null && !entry.getOtherLinks().isEmpty()) {
final List<Link> oLinks = entry.getOtherLinks();
for (final Link link : oLinks) {
final Link thisLink = link;
final List<Link> 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<SyndLink> syndLinks = new ArrayList<SyndLink>();
if (entry.getAlternateLinks() != null && !entry.getAlternateLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(entry.getAlternateLinks()));
if (Lists.isNotEmpty(alternateLinks)) {
syndLinks.addAll(createSyndLinks(alternateLinks));
}
if (entry.getOtherLinks() != null && !entry.getOtherLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(entry.getOtherLinks()));
if (Lists.isNotEmpty(otherLinks)) {
syndLinks.addAll(createSyndLinks(otherLinks));
}
syndEntry.setLinks(syndLinks);
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<Content> 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<Content> contents = entry.getContents();
if (!contents.isEmpty()) {
if (Lists.isNotEmpty(contents)) {
final List<SyndContent> sContents = new ArrayList<SyndContent>();
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<SyndPerson> 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<Link> alternateLinks = new ArrayList<Link>();
final List<Link> otherLinks = new ArrayList<Link>();
final List<SyndLink> 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<SyndPerson> 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<Entry> createAtomEntries(final List<SyndEntry> syndEntries) {
final List<Entry> atomEntries = new ArrayList<Entry>();
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<Link> alternateLinks = new ArrayList<Link>();
final List<Link> otherLinks = new ArrayList<Link>();
final List<SyndLink> slinks = sEntry.getLinks();
if (slinks != null) {
for (final SyndLink syndLink2 : slinks) {
final SyndLink syndLink = syndLink2;
final List<SyndLink> 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<SyndEnclosure> 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<SyndContent> contents = sEntry.getContents();
if (!contents.isEmpty()) {
final List<Content> aContents = new ArrayList<Content>();
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<SyndPerson> 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<SyndPerson> authors = new ArrayList<SyndPerson>();
authors.add(person);
aEntry.setAuthors(authors);

View file

@ -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<Element> 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<Link> 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<SyndLink> syndLinks = new ArrayList<SyndLink>();
if (aFeed.getAlternateLinks() != null && !aFeed.getAlternateLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(aFeed.getAlternateLinks()));
if (Lists.isNotEmpty(alternateLinks)) {
syndLinks.addAll(createSyndLinks(alternateLinks));
}
if (aFeed.getOtherLinks() != null && !aFeed.getOtherLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(aFeed.getOtherLinks()));
final List<Link> otherLinks = aFeed.getOtherLinks();
if (Lists.isNotEmpty(otherLinks)) {
syndLinks.addAll(createSyndLinks(otherLinks));
}
syndFeed.setLinks(syndLinks);
final List<Entry> aEntries = aFeed.getEntries();
@ -128,12 +144,12 @@ public class ConverterForAtom10 implements Converter {
// over DC equivalent info.
final List<SyndPerson> authors = aFeed.getAuthors();
if (authors != null && !authors.isEmpty()) {
if (Lists.isNotEmpty(authors)) {
syndFeed.setAuthors(ConverterForAtom03.createSyndPersons(authors));
}
final List<SyndPerson> 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<SyndLink> createSyndLinks(final List<Link> aLinks) {
final ArrayList<SyndLink> sLinks = new ArrayList<SyndLink>();
for (final Link link2 : aLinks) {
final Link link = link2;
final SyndLink sLink = createSyndLink(link);
sLinks.add(sLink);
protected List<SyndLink> createSyndLinks(final List<Link> atomLinks) {
final ArrayList<SyndLink> syndLinks = new ArrayList<SyndLink>();
for (final Link atomLink : atomLinks) {
final SyndLink syndLink = createSyndLink(atomLink);
syndLinks.add(syndLink);
}
return sLinks;
return syndLinks;
}
protected List<SyndEntry> createSyndEntries(final Feed feed, final List<Entry> atomEntries, final boolean preserveWireItems) {
final List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
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<Element> 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<Content> contents = entry.getContents();
if (contents != null && !contents.isEmpty()) {
if (Lists.isNotEmpty(contents)) {
final List<SyndContent> sContents = new ArrayList<SyndContent>();
for (final Content content2 : contents) {
final Content content = content2;
for (final Content content : contents) {
sContents.add(createSyndContent(content));
}
syndEntry.setContents(sContents);
}
final List<SyndPerson> 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<SyndPerson> 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<SyndCategory> syndCategories = new ArrayList<SyndCategory>();
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<Link> 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<SyndEnclosure> syndEnclosures = new ArrayList<SyndEnclosure>();
if (entry.getOtherLinks() != null && !entry.getOtherLinks().isEmpty()) {
final List<Link> oLinks = entry.getOtherLinks();
final List<Link> otherLinks = entry.getOtherLinks();
if (Lists.isNotEmpty(otherLinks)) {
final List<Link> 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<SyndLink> syndLinks = new ArrayList<SyndLink>();
if (entry.getAlternateLinks() != null && !entry.getAlternateLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(entry.getAlternateLinks()));
if (Lists.isNotEmpty(alternateLinks)) {
syndLinks.addAll(createSyndLinks(alternateLinks));
}
if (entry.getOtherLinks() != null && !entry.getOtherLinks().isEmpty()) {
syndLinks.addAll(createSyndLinks(entry.getOtherLinks()));
if (Lists.isNotEmpty(otherLinks)) {
syndLinks.addAll(createSyndLinks(otherLinks));
}
syndEntry.setLinks(syndLinks);
@ -352,10 +367,10 @@ public class ConverterForAtom10 implements Converter {
final List<Link> otherLinks = new ArrayList<Link>();
final List<SyndLink> 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<SyndCategory> sCats = syndFeed.getCategories();
final List<Category> aCats = new ArrayList<Category>();
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<SyndPerson> authors = syndFeed.getAuthors();
if (authors != null && !authors.isEmpty()) {
if (Lists.isNotEmpty(authors)) {
aFeed.setAuthors(ConverterForAtom03.createAtomPersons(authors));
}
final List<SyndPerson> 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<Element> 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<Entry> createAtomEntries(final List<SyndEntry> syndEntries) {
final List<Entry> atomEntries = new ArrayList<Entry>();
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<Content> createAtomContents(final List<SyndContent> syndContents) {
final List<Content> atomContents = new ArrayList<Content>();
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<Link> alternateLinks = new ArrayList<Link>();
final List<Link> otherLinks = new ArrayList<Link>();
boolean linkRelEnclosureExists = false;
final List<SyndLink> slinks = sEntry.getLinks();
final List<SyndEnclosure> 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<SyndCategory> sCats = sEntry.getCategories();
final List<Category> aCats = new ArrayList<Category>();
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<SyndPerson> 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<SyndPerson>();
authors.add(person);
aEntry.setAuthors(authors);
}
final List<SyndPerson> 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<Element> foreignMarkup = sEntry.getForeignMarkup();
if (!foreignMarkup.isEmpty()) {
aEntry.setForeignMarkup(foreignMarkup);
}
final SyndFeed sSource = sEntry.getSource();

View file

@ -19,6 +19,8 @@ package com.sun.syndication.feed.synd.impl;
import java.util.ArrayList;
import java.util.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<Element> 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<SyndEntry> createSyndEntries(final List<Item> rssItems, final boolean preserveWireItems) {
final List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
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<Element> 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<SyndLink> 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<Element> foreignMarkup = syndFeed.getForeignMarkup();
if (!foreignMarkup.isEmpty()) {
channel.setForeignMarkup(foreignMarkup);
}
return channel;
}
@ -166,22 +185,29 @@ public class ConverterForRSS090 implements Converter {
protected List<Item> createRSSItems(final List<SyndEntry> sEntries) {
final List<Item> list = new ArrayList<Item>();
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<Element> 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);

View file

@ -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<String> creators = ((DCModule) syndFeed.getModule(DCModule.URI)).getCreators();
if (!creators.contains(author)) {
final Set<String> s = new LinkedHashSet<String>(); // using a set to
// remove
// duplicates
// using a set to remove duplicates
final Set<String> s = new LinkedHashSet<String>();
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<SyndContent> 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<SyndPerson> 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;
}
}

View file

@ -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<Category> cats = item.getCategories();
if (!cats.isEmpty()) {
final Set<SyndCategory> s = new LinkedHashSet<SyndCategory>(); // 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<SyndCategory> s = new LinkedHashSet<SyndCategory>();
// feed native categories (as syndcat)
s.addAll(createSyndCategories(cats));
// DC subjects (as syndcat)
s.addAll(syndEntry.getCategories());
syndEntry.setCategories(new ArrayList<SyndCategory>(s)); // c
}
final List<Enclosure> enclosures = item.getEnclosures();
if (!enclosures.isEmpty()) {
syndEntry.setEnclosures(createSyndEnclosures(enclosures));
}
return syndEntry;
}
protected List<SyndCategory> createSyndCategories(final List<Category> rssCats) {
final List<SyndCategory> syndCats = new ArrayList<SyndCategory>();
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<SyndEnclosure> createSyndEnclosures(final List<Enclosure> enclosures) {
final List<SyndEnclosure> sEnclosures = new ArrayList<SyndEnclosure>();
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<SyndCategory> sCats = sEntry.getCategories(); // c
if (!sCats.isEmpty()) {
item.setCategories(createRSSCategories(sCats));
}
final List<SyndEnclosure> sEnclosures = sEntry.getEnclosures();
if (!sEnclosures.isEmpty()) {
item.setEnclosures(createEnclosures(sEnclosures));
}
return item;
}
protected List<Category> createRSSCategories(final List<SyndCategory> sCats) {
final List<Category> cats = new ArrayList<Category>();
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<Enclosure> createEnclosures(final List<SyndEnclosure> sEnclosures) {
final List<Enclosure> enclosures = new ArrayList<Enclosure>();
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());

View file

@ -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;
}

View file

@ -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<Category> cats = channel.getCategories(); // c
if (!cats.isEmpty()) {
final Set<SyndCategory> s = new LinkedHashSet<SyndCategory>(); // 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<SyndCategory> s = new LinkedHashSet<SyndCategory>();
// feed native categories (as syndcat)
s.addAll(createSyndCategories(cats));
// DC subjects (as syndcat)
s.addAll(syndFeed.getCategories());
syndFeed.setCategories(new ArrayList<SyndCategory>(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<String> creators = ((DCModule) syndEntry.getModule(DCModule.URI)).getCreators();
if (!creators.contains(author)) {
final Set<String> s = new LinkedHashSet<String>(); // 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<String> s = new LinkedHashSet<String>();
// 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<SyndPerson> 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) {
} 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;
}
}

View file

@ -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<SyndContent> contents = new ArrayList<SyndContent>();
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<SyndContent> contents = sEntry.getContents();
if (contents != null && !contents.isEmpty()) {
if (Lists.isNotEmpty(contents)) {
item.setContent(createItemContent(contents.get(0)));
}

View file

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

View file

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

View file

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

View file

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

View file

@ -58,8 +58,16 @@ import com.sun.syndication.io.impl.XmlFixerReader;
*/
public class WireFeedInput {
private static final InputSource EMPTY_INPUTSOURCE = new InputSource(new ByteArrayInputStream(new byte[0]));
private static final EntityResolver RESOLVER = new EmptyEntityResolver();
private static Map<ClassLoader, FeedParsers> clMap = new WeakHashMap<ClassLoader, FeedParsers>();
private 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,11 +90,6 @@ public class WireFeedInput {
}
}
private final boolean validate;
private boolean xmlHealerOn;
private final Locale locale;
/**
* Returns the list of supported input feed types.
* <p>
@ -357,4 +357,5 @@ public class WireFeedInput {
saxBuilder.setExpandEntities(false);
return saxBuilder;
}
}

View file

@ -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<Entry> items = feed.getEntries();
for (int i = 0; i < items.size(); i++) {
addEntry(items.get(i), parent);
final List<Entry> 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<Link> 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<SyndPerson> 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<SyndPerson> 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<Link> links = entry.getAlternateLinks();
for (int i = 0; i < links.size(); i++) {
eEntry.addContent(generateLinkElement(links.get(i)));
final List<Link> 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<Link> otherLinks = entry.getOtherLinks();
for (final Link link : otherLinks) {
eEntry.addContent(generateLinkElement(link));
}
if (entry.getAuthors() != null && !entry.getAuthors().isEmpty()) {
final List<SyndPerson> authors = entry.getAuthors();
if (Lists.isNotEmpty(authors)) {
final Element authorElement = new Element("author", getFeedNamespace());
fillPersonElement(authorElement, entry.getAuthors().get(0));
fillPersonElement(authorElement, authors.get(0));
eEntry.addContent(authorElement);
}
final List<SyndPerson> contributors = entry.getContributors();
for (int i = 0; i < contributors.size(); i++) {
for (final SyndPerson contributor : contributors) {
final Element contributorElement = new Element("contributor", getFeedNamespace());
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<Content> 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("<tmpdoc>");
tmpDocString.append(content.getValue());
tmpDocString.append(value);
tmpDocString.append("</tmpdoc>");
final StringReader tmpDocReader = new StringReader(tmpDocString.toString());
Document tmpDoc;
@ -336,24 +384,29 @@ public class Atom03Generator extends BaseWireFeedGenerator {
final List<org.jdom2.Content> 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;

View file

@ -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<Element> eList = eFeed.getChildren("link", getAtomNamespace());
feed.setAlternateLinks(parseAlternateLinks(eList));
feed.setOtherLinks(parseOtherLinks(eList));
final List<Element> 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<SyndPerson> authors = new ArrayList<SyndPerson>();
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<Element> 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<Element> entries = eFeed.getChildren("entry", getAtomNamespace());
if (!entries.isEmpty()) {
feed.setEntries(parseEntries(entries, locale));
}
final List<Element> 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);
}
att = getAttributeValue(eLink, "type");
if (att != null) {
link.setType(att);
}
att = getAttributeValue(eLink, "href");
if (att != null) {
link.setHref(att);
}
return link;
final String rel = getAttributeValue(eLink, "rel");
if (rel != null) {
link.setRel(rel);
}
final String type = getAttributeValue(eLink, "type");
if (type != null) {
link.setType(type);
}
final String href = getAttributeValue(eLink, "href");
if (href != null) {
link.setHref(href);
}
return link;
}
// List(Elements) -> List(Link)
private List<Link> parseLinks(final List<Element> eLinks, final boolean alternate) {
final List<Link> links = new ArrayList<Link>();
for (int i = 0; i < eLinks.size(); i++) {
final Element eLink = eLinks.get(i);
for (final Element eLink : eLinks) {
final String rel = getAttributeValue(eLink, "rel");
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<SyndPerson> parsePersons(final List<Element> ePersons) {
final List<SyndPerson> persons = new ArrayList<SyndPerson>();
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<org.jdom2.Content> 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<Entry> parseEntries(final List<Element> eEntries, final Locale locale) {
final List<Entry> entries = new ArrayList<Entry>();
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<Element> eList = eEntry.getChildren("link", getAtomNamespace());
entry.setAlternateLinks(parseAlternateLinks(eList));
entry.setOtherLinks(parseOtherLinks(eList));
final List<Element> 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<SyndPerson> authors = new ArrayList<SyndPerson>();
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<Element> 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<Element> contents = eEntry.getChildren("content", getAtomNamespace());
if (!contents.isEmpty()) {
final List<Content> content = new ArrayList<Content>();
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;
}
}

View file

@ -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<Entry> 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<Link> links = feed.getAlternateLinks();
if (links != null) {
for (int i = 0; i < links.size(); i++) {
eFeed.addContent(generateLinkElement(links.get(i)));
final List<Link> 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<Link> 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<SyndPerson> 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<SyndPerson> 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<Link> links = entry.getAlternateLinks();
if (links != null) {
for (int i = 0; i < links.size(); i++) {
eEntry.addContent(generateLinkElement(links.get(i)));
final List<Link> 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<Link> otherLinks = entry.getOtherLinks();
if (otherLinks != null) {
for (final Link link : otherLinks) {
eEntry.addContent(generateLinkElement(link));
}
}
final List<Category> 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<SyndPerson> 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<SyndPerson> 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<Content> 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("<tmpdoc>");
tmpDocString.append(content.getValue());
tmpDocString.append(value);
tmpDocString.append("</tmpdoc>");
final StringReader tmpDocReader = new StringReader(tmpDocString.toString());
Document tmpDoc;
@ -417,29 +491,37 @@ public class Atom10Generator extends BaseWireFeedGenerator {
}
final List<org.jdom2.Content> 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;

View file

@ -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<Element> eList = eFeed.getChildren("link", getAtomNamespace());
feed.setAlternateLinks(parseAlternateLinks(feed, null, baseURI, eList));
feed.setOtherLinks(parseOtherLinks(feed, null, baseURI, eList));
final List<Element> links = eFeed.getChildren("link", getAtomNamespace());
feed.setAlternateLinks(parseAlternateLinks(feed, null, baseURI, links));
feed.setOtherLinks(parseOtherLinks(feed, null, baseURI, links));
final List<Element> cList = eFeed.getChildren("category", getAtomNamespace());
feed.setCategories(parseCategories(baseURI, cList));
final List<Element> 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<Element> 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<Element> 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<Link> parseAlternateLinks(final Feed feed, final Entry entry, final String baseURI, final List<Element> eLinks) {
final List<Link> links = new ArrayList<Link>();
for (int i = 0; i < eLinks.size(); i++) {
final Element eLink = eLinks.get(i);
for (final Element eLink : eLinks) {
final 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<Link> parseOtherLinks(final Feed feed, final Entry entry, final String baseURI, final List<Element> eLinks) {
final List<Link> links = new ArrayList<Link>();
for (int i = 0; i < eLinks.size(); i++) {
final Element eLink = eLinks.get(i);
for (final Element eLink : eLinks) {
final 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<SyndPerson> parsePersons(final String baseURI, final List<Element> ePersons, final Locale locale) {
final List<SyndPerson> persons = new ArrayList<SyndPerson>();
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<Entry> parseEntries(final Feed feed, final String baseURI, final List<Element> eEntries, final Locale locale) {
final List<Entry> entries = new ArrayList<Entry>();
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<Element> eList = eEntry.getChildren("link", getAtomNamespace());
entry.setAlternateLinks(parseAlternateLinks(feed, entry, baseURI, eList));
entry.setOtherLinks(parseOtherLinks(feed, entry, baseURI, eList));
final List<Element> 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<Element> 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<Element> 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<Content> contents = new ArrayList<Content>();
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<Element> cList = eEntry.getChildren("category", getAtomNamespace());
entry.setCategories(parseCategories(baseURI, cList));
final List<Element> 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<Category> parseCategories(final String baseURI, final List<Element> eCategories) {
final List<Category> cats = new ArrayList<Category>();
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;
}
/**
@ -659,6 +690,7 @@ public class Atom10Parser extends BaseWireFeedParser {
*/
public static Entry parseEntry(final Reader rd, final String baseURI, final Locale locale) throws JDOMException, IOException, IllegalArgumentException,
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);
}
}

View file

@ -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<String> collector) {
final String prefix = el.getNamespacePrefix();
if (prefix != null && prefix.length() > 0 && !collector.contains(prefix)) {
collector.add(prefix);
}
final List<Element> 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);
}
}
}

View file

@ -100,56 +100,86 @@ public class DCModuleGenerator implements ModuleGenerator {
*/
@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<DCSubject> 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<String> contributors = dcModule.getContributors();
if (contributors != null) {
element.addContent(generateSimpleElementList("contributor", contributors));
}
if (dcModule.getDate() != null) {
final Date dcDate = dcModule.getDate();
if (dcDate != null) {
for (final Date date : dcModule.getDates()) {
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()));
}
}
/**
@ -160,24 +190,34 @@ public class DCModuleGenerator implements ModuleGenerator {
* @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;
}
@ -192,7 +232,6 @@ 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;
}
@ -201,15 +240,14 @@ public class DCModuleGenerator implements ModuleGenerator {
* <p>
*
* @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<Element> generateSimpleElementList(final String name, final List<String> value) {
protected final List<Element> generateSimpleElementList(final String name, final List<String> values) {
final List<Element> elements = new ArrayList<Element>();
for (final String string : value) {
elements.add(generateSimpleElement(name, string));
for (final String value : values) {
elements.add(generateSimpleElement(name, value));
}
return elements;
}
}

View file

@ -71,89 +71,106 @@ public class DCModuleParser implements ModuleParser {
*/
@Override
public Module parse(final Element dcRoot, final Locale locale) {
boolean foundSomething = false;
final DCModule dcm = new DCModuleImpl();
List<Element> eList = dcRoot.getChildren("title", getDCNamespace());
if (!eList.isEmpty()) {
final List<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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<Element> 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;
}
}
/**
@ -164,15 +181,15 @@ public class DCModuleParser implements ModuleParser {
* @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;
}
/**
@ -183,20 +200,27 @@ public class DCModuleParser implements ModuleParser {
* @return a list of subjects parsed from the elements.
*/
protected final List<DCSubject> parseSubjects(final List<Element> eList) {
final List<DCSubject> subjects = new ArrayList<DCSubject>();
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<Element> 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<Element> 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());
@ -211,16 +235,14 @@ public class DCModuleParser implements ModuleParser {
* Utility method to parse a list of strings out of a list of elements.
* <p>
*
* @param eList the list of elements to parse.
* @param elements the list of elements to parse.
* @return the list of strings
*/
protected final List<String> parseElementList(final List<Element> eList) {
protected final List<String> parseElementList(final List<Element> elements) {
final List<String> values = new ArrayList<String>();
for (final Element element : eList) {
final Element e = element;
values.add(e.getText());
for (final Element element : elements) {
values.add(element.getText());
}
return values;
}
@ -228,16 +250,15 @@ public class DCModuleParser implements ModuleParser {
* Utility method to parse a list of dates out of a list of elements.
* <p>
*
* @param eList the list of elements to parse.
* @param elements the list of elements to parse.
* @return the list of dates.
*/
protected final List<Date> parseElementListDate(final List<Element> eList, final Locale locale) {
protected final List<Date> parseElementListDate(final List<Element> elements, final Locale locale) {
final List<Date> values = new ArrayList<Date>();
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;
}
}

View file

@ -39,24 +39,14 @@ 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
@ -86,6 +76,10 @@ public class DateParser {
"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.
*/
@ -182,9 +176,8 @@ public class DateParser {
*
*/
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")) {
@ -219,14 +212,14 @@ public class DateParser {
*
* */
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;
}
/**

View file

@ -70,15 +70,13 @@ public class FeedParsers extends PluginManager<WireFeedParser> {
*/
public WireFeedParser getParserFor(final Document document) {
final List<WireFeedParser> 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 null;
}
@Override
protected String getKey(final WireFeedParser obj) {

View file

@ -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<ModuleGenerator> {
private Set<Namespace> allNamespaces;
public ModuleGenerators(final String propertyKey, final BaseWireFeedGenerator parentGenerator) {
@ -51,8 +50,7 @@ public class ModuleGenerators extends PluginManager<ModuleGenerator> {
public void generateModules(final List<Module> modules, final Element element) {
final Map<String, ModuleGenerator> 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<ModuleGenerator> {
if (allNamespaces == null) {
allNamespaces = new HashSet<Namespace>();
final List<String> 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;
}
}

View file

@ -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<ModuleParser> {
public ModuleParsers(final String propertyKey, final WireFeedParser parentParser) {
super(propertyKey, parentParser, null);
}
@ -46,16 +47,13 @@ public class ModuleParsers extends PluginManager<ModuleParser> {
public List<Module> parseModules(final Element root, final Locale locale) {
final List<ModuleParser> parsers = getPlugins();
List<Module> 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<Module>();
}
modules = Lists.createWhenNull(modules);
modules.add(module);
}
}
@ -65,15 +63,14 @@ public class ModuleParsers extends PluginManager<ModuleParser> {
private boolean hasElementsFrom(final Element root, final Namespace namespace) {
boolean hasElements = false;
// boolean hasElements = namespace.equals(root.getNamespace());
if (!hasElements) {
final List<Element> 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;
}
}

View file

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

View file

@ -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<Namespace> 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<Module> allFeedModules = new ArrayList<Module>();
final List<Module> rootModules = parseFeedModules(rssRoot, locale);
final List<Module> 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<Item> parseItems(final Element rssRoot, final Locale locale) {
final Collection<Element> eItems = getItems(rssRoot);
final List<Item> items = new ArrayList<Item>();
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<Element> 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<Element> 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;
}
}

View file

@ -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 String name = rssRoot.getName();
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;
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

View file

@ -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;
@ -39,6 +40,7 @@ import com.sun.syndication.io.FeedException;
*
*/
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<Element> hours = skipHours.getChildren();
for (int i = 0; i < hours.size(); i++) {
final Element hour = hours.get(i);
final List<Element> 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<String> 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<Integer> 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<Integer> skipHours = channel.getSkipHours();
if (skipHours != null && !skipHours.isEmpty()) {
eChannel.addContent(generateSkipHoursElement(skipHours));
}
final List<String> 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);
}
}
}

View file

@ -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;
return rssRoot.getName().equals("rss") && version != null && version.getValue().equals(getRSSVersion());
}
protected String getRSSVersion() {
@ -88,67 +80,76 @@ public class RSS091UserlandParser 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());
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<Integer> skipHours = new ArrayList<Integer>();
final List<Element> eHours = e.getChildren("hour", getRSSNamespace());
for (int i = 0; i < eHours.size(); i++) {
final Element eHour = eHours.get(i);
final List<Element> 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<String> skipDays = new ArrayList<String>();
final List<Element> eDays = e.getChildren("day", getRSSNamespace());
for (int i = 0; i < eDays.size(); i++) {
final Element eDay = eDays.get(i);
final List<Element> eDays = eSkipDays.getChildren("day", getRSSNamespace());
for (final Element eDay : eDays) {
skipDays.add(eDay.getText().trim());
}
channel.setSkipDays(skipDays);
}
return channel;
}
@ -164,29 +165,37 @@ public class RSS091UserlandParser extends RSS090Parser {
*/
@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<Element> getItems(final Element rssRoot) {
final Element eChannel = rssRoot.getChild("channel", getRSSNamespace());
final List<Element> 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;
}
}
/**
@ -250,19 +267,24 @@ public class RSS091UserlandParser extends RSS090Parser {
*/
@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) {

View file

@ -31,12 +31,10 @@ import com.sun.syndication.io.FeedException;
/**
* Feed Generator for RSS 0.92
* <p/>
*
* @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<Category> 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;
}

View file

@ -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<Element> 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<Element> eEnclosures = eItem.getChildren("enclosure");
if (!eEnclosures.isEmpty()) {
final List<Enclosure> enclosures = new ArrayList<Enclosure>();
for (int i = 0; i < eEnclosures.size(); i++) {
e = eEnclosures.get(i);
for (final Element eEnclosure : eEnclosures) {
final Enclosure enclosure = new Enclosure();
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<Element> eCats = eItem.getChildren("category");// getRSSNamespace());
// DONT KNOW WHY
// DOESN'T WORK
item.setCategories(parseCategories(eCats));
// getRSSNamespace()); DONT KNOW WHY DOESN'T WORK
final List<Element> categories = eItem.getChildren("category");
item.setCategories(parseCategories(categories));
return item;
}
protected List<Category> parseCategories(final List<Element> eCats) {
List<Category> cats = null;
if (!eCats.isEmpty()) {
cats = new ArrayList<Category>();
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

View file

@ -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;
}
}

View file

@ -26,8 +26,6 @@ import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Guid;
import com.sun.syndication.feed.rss.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<Element> eCats = eChannel.getChildren("category", getRSSNamespace());
channel.setCategories(parseCategories(eCats));
final List<Element> 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;
}
}

View file

@ -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<Item> 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

View file

@ -55,21 +55,9 @@ public class RSS10Parser extends RSS090Parser {
*/
@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;
}
/**
@ -96,22 +84,25 @@ public class RSS10Parser extends RSS090Parser {
*/
@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());

View file

@ -32,7 +32,6 @@ import com.sun.syndication.feed.rss.Item;
* @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<Category> 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();

View file

@ -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;
// 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());
}
}

View file

@ -36,6 +36,7 @@ import com.sun.syndication.feed.WireFeed;
*
*/
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

View file

@ -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;
@ -35,7 +36,6 @@ import com.sun.syndication.io.ModuleGenerator;
* @author Elaine Chien
*
*/
public class SyModuleGenerator implements ModuleGenerator {
private static final String SY_URI = "http://purl.org/rss/1.0/modules/syndication/";
@ -73,9 +73,10 @@ public class SyModuleGenerator implements ModuleGenerator {
final SyModule syModule = (SyModule) module;
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);
}
}
}

View file

@ -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;
}
}
}