Updated rome-opml to use generics

This commit is contained in:
Patrick Gotthard 2014-04-12 16:51:04 +02:00
parent a8a0290187
commit 1f1a2786f8
7 changed files with 104 additions and 101 deletions

View file

@ -29,9 +29,9 @@ import com.sun.syndication.feed.WireFeed;
* @author <a href="mailto:cooper@screaming-penguin.com"> Robert "kebernet" Cooper</a>
*/
public class Opml extends WireFeed {
private static final long serialVersionUID = 1L;
private Date _created;
private Date _modified;
private Integer _verticalScrollState;
@ -39,7 +39,7 @@ public class Opml extends WireFeed {
private Integer _windowLeft;
private Integer _windowRight;
private Integer _windowTop;
private List _outlines;
private List<Outline> _outlines;
private String _docs;
private String _ownerEmail;
private String _ownerId;
@ -135,7 +135,7 @@ public class Opml extends WireFeed {
*
* @param outlines Root level Outline object that should appear in the &lt;body&gt;
*/
public void setOutlines(final List outlines) {
public void setOutlines(final List<Outline> outlines) {
_outlines = outlines;
}
@ -144,9 +144,9 @@ public class Opml extends WireFeed {
*
* @return Root level Outline object that should appear in the &lt;body&gt;
*/
public List getOutlines() {
public List<Outline> getOutlines() {
if (_outlines == null) {
_outlines = new ArrayList();
_outlines = new ArrayList<Outline>();
}
return _outlines;

View file

@ -26,6 +26,7 @@ import java.util.List;
import com.sun.syndication.feed.impl.EqualsBean;
import com.sun.syndication.feed.impl.ToStringBean;
import com.sun.syndication.feed.module.Module;
/**
* This class represents an OPML outline element.
@ -33,14 +34,14 @@ import com.sun.syndication.feed.impl.ToStringBean;
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/
public class Outline implements Cloneable, Serializable {
private static final long serialVersionUID = 1L;
private Date _created;
private List _attributes;
private List _categories;
private List _children;
private List _modules;
private List<Attribute> _attributes;
private List<String> _categories;
private List<Outline> _children;
private List<Module> _modules;
private String _text;
private String _title;
private String _type;
@ -75,7 +76,7 @@ public class Outline implements Cloneable, Serializable {
super();
setType("rss");
setTitle(title);
setAttributes(new ArrayList());
setAttributes(new ArrayList<Attribute>());
if (xmlUrl != null) {
getAttributes().add(new Attribute("xmlUrl", xmlUrl.toString()));
@ -91,7 +92,7 @@ public class Outline implements Cloneable, Serializable {
*
* @param attributes List of attributes on this outline.
*/
public void setAttributes(final List attributes) {
public void setAttributes(final List<Attribute> attributes) {
_attributes = attributes;
}
@ -100,9 +101,9 @@ public class Outline implements Cloneable, Serializable {
*
* @return List of attributes on this outline.
*/
public List getAttributes() {
public List<Attribute> getAttributes() {
if (_attributes == null) {
_attributes = new ArrayList();
_attributes = new ArrayList<Attribute>();
}
return _attributes;
@ -133,7 +134,7 @@ public class Outline implements Cloneable, Serializable {
*
* @param categories (OPML 2) A List of Strings indicating values in the category attribute.
*/
public void setCategories(final List categories) {
public void setCategories(final List<String> categories) {
_categories = categories;
}
@ -142,9 +143,9 @@ public class Outline implements Cloneable, Serializable {
*
* @return (OPML 2) A List of Strings indicating values in the category attribute.
*/
public List getCategories() {
public List<String> getCategories() {
if (_categories == null) {
_categories = new ArrayList();
_categories = new ArrayList<String>();
}
return _categories;
@ -155,7 +156,7 @@ public class Outline implements Cloneable, Serializable {
*
* @param children A list of sub-outlines for this entry.
*/
public void setChildren(final List children) {
public void setChildren(final List<Outline> children) {
_children = children;
}
@ -164,9 +165,9 @@ public class Outline implements Cloneable, Serializable {
*
* @return A list of sub-outlines for this entry.
*/
public List getChildren() {
public List<Outline> getChildren() {
if (_children == null) {
_children = new ArrayList();
_children = new ArrayList<Outline>();
}
return _children;
@ -228,13 +229,13 @@ public class Outline implements Cloneable, Serializable {
return getAttributeValue("htmlUrl");
}
public void setModules(final List modules) {
public void setModules(final List<Module> modules) {
_modules = modules;
}
public List getModules() {
public List<Module> getModules() {
if (_modules == null) {
_modules = new ArrayList();
_modules = new ArrayList<Module>();
}
return _modules;
@ -309,45 +310,40 @@ public class Outline implements Cloneable, Serializable {
* @param name name of the attribute.
*/
public String getAttributeValue(final String name) {
final List attributes = Collections.synchronizedList(getAttributes());
final List<Attribute> attributes = Collections.synchronizedList(getAttributes());
for (int i = 0; i < attributes.size(); i++) {
final Attribute a = (Attribute) attributes.get(i);
final Attribute a = attributes.get(i);
if (a.getName() != null && a.getName().equals(name)) {
return a.getValue();
}
}
return null;
}
@Override
public Object clone() {
final Outline o = new Outline();
o.setBreakpoint(isBreakpoint());
o.setCategories(new ArrayList(getCategories()));
o.setCategories(new ArrayList<String>(getCategories()));
o.setComment(isComment());
o.setCreated(_created != null ? (Date) _created.clone() : null);
o.setModules(new ArrayList(getModules()));
o.setModules(new ArrayList<Module>(getModules()));
o.setText(getText());
o.setTitle(getTitle());
o.setType(getType());
final ArrayList children = new ArrayList();
final ArrayList<Outline> children = new ArrayList<Outline>();
for (int i = 0; i < getChildren().size(); i++) {
children.add(((Outline) _children.get(i)).clone());
children.add((Outline) _children.get(i).clone());
}
o.setChildren(children);
final ArrayList attributes = new ArrayList();
final ArrayList<Attribute> attributes = new ArrayList<Attribute>();
for (int i = 0; i < getAttributes().size(); i++) {
attributes.add(((Attribute) _attributes.get(i)).clone());
attributes.add((Attribute) _attributes.get(i).clone());
}
o.setAttributes(attributes);
return o;

View file

@ -57,7 +57,7 @@ public class ConverterForOPML10 implements Converter {
protected void addOwner(final Opml opml, final SyndFeed syndFeed) {
if (opml.getOwnerEmail() != null || opml.getOwnerName() != null) {
final List authors = new ArrayList();
final List<SyndPerson> authors = new ArrayList<SyndPerson>();
final SyndPerson person = new SyndPersonImpl();
person.setEmail(opml.getOwnerEmail());
person.setName(opml.getOwnerName());
@ -85,19 +85,18 @@ public class ConverterForOPML10 implements Converter {
syndFeed.setModules(opml.getModules());
syndFeed.setFeedType(getType());
final ArrayList entries = new ArrayList();
createEntries(new TreeContext(), syndFeed.getEntries(), opml.getOutlines());
}
protected void createEntries(final TreeContext context, final List allEntries, final List outlines) {
final List so = Collections.synchronizedList(outlines);
protected void createEntries(final TreeContext context, final List<SyndEntry> allEntries, final List<Outline> outlines) {
final List<Outline> so = Collections.synchronizedList(outlines);
for (int i = 0; i < so.size(); i++) {
createEntry(context, allEntries, (Outline) so.get(i));
createEntry(context, allEntries, so.get(i));
}
}
protected SyndEntry createEntry(final TreeContext context, final List allEntries, final Outline outline) {
protected SyndEntry createEntry(final TreeContext context, final List<SyndEntry> allEntries, final Outline outline) {
final SyndEntry entry = new SyndEntryImpl();
if (outline.getType() != null && outline.getType().equals("rss")) {
@ -161,17 +160,17 @@ public class ConverterForOPML10 implements Converter {
entry.getCategories().add(cat);
if (context.size() > 0) {
final Integer parent = (Integer) context.peek();
final Integer parent = context.peek();
final SyndCategory pcat = new TreeCategoryImpl();
pcat.setTaxonomyUri(URI_TREE);
pcat.setName("parent." + parent);
entry.getCategories().add(pcat);
}
final List attributes = Collections.synchronizedList(outline.getAttributes());
final List<Attribute> attributes = Collections.synchronizedList(outline.getAttributes());
for (int i = 0; i < attributes.size(); i++) {
final Attribute a = (Attribute) attributes.get(i);
final Attribute a = attributes.get(i);
final SyndCategory acat = new SyndCategoryImpl();
acat.setName(a.getValue());
acat.setTaxonomyUri(URI_ATTRIBUTE + a.getName());
@ -197,22 +196,23 @@ public class ConverterForOPML10 implements Converter {
*/
@Override
public WireFeed createRealFeed(final SyndFeed syndFeed) {
final List entries = Collections.synchronizedList(syndFeed.getEntries());
final HashMap entriesByNode = new HashMap();
final ArrayList doAfterPass = new ArrayList(); // this will hold entries that we can't parent the first time.
final ArrayList root = new ArrayList(); // this holds root level outlines;
final List<SyndEntry> entries = Collections.synchronizedList(syndFeed.getEntries());
final HashMap<String, Outline> entriesByNode = new HashMap<String, Outline>();
final ArrayList<OutlineHolder> doAfterPass = new ArrayList<OutlineHolder>(); // this will hold entries that we can't parent the first time.
final ArrayList<Outline> root = new ArrayList<Outline>(); // this holds root level outlines;
for (int i = 0; i < entries.size(); i++) {
final SyndEntry entry = (SyndEntry) entries.get(i);
final SyndEntry entry = entries.get(i);
final Outline o = new Outline();
final List cats = Collections.synchronizedList(entry.getCategories());
final List<SyndCategory> cats = Collections.synchronizedList(entry.getCategories());
boolean parentFound = false;
final StringBuffer category = new StringBuffer();
for (int j = 0; j < cats.size(); j++) {
final SyndCategory cat = (SyndCategory) cats.get(j);
final SyndCategory cat = cats.get(j);
if (cat.getTaxonomyUri() != null && cat.getTaxonomyUri().equals(URI_TREE)) {
final String nodeVal = cat.getName().substring(cat.getName().lastIndexOf("."), cat.getName().length());
@ -222,7 +222,7 @@ public class ConverterForOPML10 implements Converter {
} else if (cat.getName().startsWith("parent.")) {
parentFound = true;
final Outline parent = (Outline) entriesByNode.get(nodeVal);
final Outline parent = entriesByNode.get(nodeVal);
if (parent != null) {
parent.getChildren().add(o);
@ -250,11 +250,11 @@ public class ConverterForOPML10 implements Converter {
o.getAttributes().add(new Attribute("category", category.toString()));
}
final List links = Collections.synchronizedList(entry.getLinks());
final String entryLink = entry.getLink();
final List<SyndLink> links = Collections.synchronizedList(entry.getLinks());
// final String entryLink = entry.getLink();
for (int j = 0; j < links.size(); j++) {
final SyndLink link = (SyndLink) links.get(j);
final SyndLink link = links.get(j);
// if(link.getHref().equals(entryLink)) {
if (link.getType() != null && link.getRel() != null && link.getRel().equals("alternate")
@ -289,8 +289,8 @@ public class ConverterForOPML10 implements Converter {
// Do back and parenting for things we missed.
for (int i = 0; i < doAfterPass.size(); i++) {
final OutlineHolder o = (OutlineHolder) doAfterPass.get(i);
final Outline parent = (Outline) entriesByNode.get(o.parent);
final OutlineHolder o = doAfterPass.get(i);
final Outline parent = entriesByNode.get(o.parent);
if (parent == null) {
root.add(o.outline);
@ -305,10 +305,10 @@ public class ConverterForOPML10 implements Converter {
opml.setCreated(syndFeed.getPublishedDate());
opml.setTitle(syndFeed.getTitle());
final List authors = Collections.synchronizedList(syndFeed.getAuthors());
final List<SyndPerson> authors = Collections.synchronizedList(syndFeed.getAuthors());
for (int i = 0; i < authors.size(); i++) {
final SyndPerson p = (SyndPerson) authors.get(i);
final SyndPerson p = authors.get(i);
if (syndFeed.getAuthor() == null || syndFeed.getAuthor().equals(p.getName())) {
opml.setOwnerName(p.getName());
@ -345,8 +345,8 @@ public class ConverterForOPML10 implements Converter {
}
}
private static class TreeContext extends Stack {
private static class TreeContext extends Stack<Integer> {
private static final long serialVersionUID = 1L;
TreeContext() {

View file

@ -33,7 +33,7 @@ import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.WireFeedGenerator;
/**
*
*
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/
public class OPML10Generator extends BaseWireFeedGenerator implements WireFeedGenerator {
@ -49,7 +49,7 @@ public class OPML10Generator extends BaseWireFeedGenerator implements WireFeedGe
/**
* Creates an XML document (JDOM) for the given feed bean.
* <p>
*
*
* @param feed the feed bean to generate the XML document from.
* @return the generated XML document (JDOM).
* @throws IllegalArgumentException thrown if the type of the given feed bean does not match with the type of the WireFeedGenerator.
@ -146,10 +146,10 @@ public class OPML10Generator extends BaseWireFeedGenerator implements WireFeedGe
addNotNullAttribute(e, "isComment", "true");
}
final List atts = Collections.synchronizedList(outline.getAttributes());
final List<Attribute> atts = Collections.synchronizedList(outline.getAttributes());
for (int i = 0; i < atts.size(); i++) {
final Attribute att = (Attribute) atts.get(i);
final Attribute att = atts.get(i);
addNotNullAttribute(e, att.getName(), att.getValue());
}
@ -159,13 +159,11 @@ public class OPML10Generator extends BaseWireFeedGenerator implements WireFeedGe
return e;
}
protected List generateOutlines(final List outlines) {
final ArrayList elements = new ArrayList();
protected List<Element> generateOutlines(final List<Outline> outlines) {
final ArrayList<Element> elements = new ArrayList<Element>();
for (int i = 0; outlines != null && i < outlines.size(); i++) {
elements.add(generateOutline((Outline) outlines.get(i)));
elements.add(generateOutline(outlines.get(i)));
}
return elements;
}

View file

@ -35,7 +35,7 @@ import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.WireFeedParser;
/**
*
*
* @author <a href="mailto:cooper@screaming-penguin.com">Robert "kebernet" Cooper</a>
*/
public class OPML10Parser extends BaseWireFeedParser implements WireFeedParser {
@ -55,7 +55,7 @@ public class OPML10Parser extends BaseWireFeedParser implements WireFeedParser {
* <p>
* It checks if the given document if the type of feeds the parser understands.
* <p>
*
*
* @param document XML Document (JDOM) to check if it can be parsed by this parser.
* @return <b>true</b> if the parser know how to parser this feed, <b>false</b> otherwise.
*/
@ -74,7 +74,7 @@ public class OPML10Parser extends BaseWireFeedParser implements WireFeedParser {
/**
* Parses an XML document (JDOM Document) into a feed bean.
* <p>
*
*
* @param document XML document (JDOM) to parse.
* @param validate indicates if the feed should be strictly validated (NOT YET IMPLEMENTED).
* @return the resulting feed bean.
@ -177,11 +177,11 @@ public class OPML10Parser extends BaseWireFeedParser implements WireFeedParser {
outline.setType(e.getAttributeValue("type"));
outline.setTitle(e.getAttributeValue("title"));
final List jAttributes = e.getAttributes();
final ArrayList attributes = new ArrayList();
final List<org.jdom2.Attribute> jAttributes = e.getAttributes();
final ArrayList<Attribute> attributes = new ArrayList<Attribute>();
for (int i = 0; i < jAttributes.size(); i++) {
final org.jdom2.Attribute a = (org.jdom2.Attribute) jAttributes.get(i);
final org.jdom2.Attribute a = jAttributes.get(i);
if (!a.getName().equals("isBreakpoint") && !a.getName().equals("isComment") && !a.getName().equals("title") && !a.getName().equals("text")
&& !a.getName().equals("type")) {
@ -211,20 +211,18 @@ public class OPML10Parser extends BaseWireFeedParser implements WireFeedParser {
}
}
final List children = e.getChildren("outline");
final List<Element> children = e.getChildren("outline");
outline.setModules(parseItemModules(e, locale));
outline.setChildren(parseOutlines(children, validate, locale));
return outline;
}
protected List parseOutlines(final List elements, final boolean validate, final Locale locale) throws FeedException {
final ArrayList results = new ArrayList();
protected List<Outline> parseOutlines(final List<Element> elements, final boolean validate, final Locale locale) throws FeedException {
final ArrayList<Outline> results = new ArrayList<Outline>();
for (int i = 0; i < elements.size(); i++) {
results.add(parseOutline((Element) elements.get(i), validate, locale));
results.add(parseOutline(elements.get(i), validate, locale));
}
return results;
}

View file

@ -30,7 +30,7 @@ import com.sun.syndication.feed.opml.Outline;
import com.sun.syndication.io.FeedException;
/**
*
*
* @author cooper
*/
public class OPML20Parser extends OPML10Parser {
@ -44,7 +44,7 @@ public class OPML20Parser extends OPML10Parser {
* <p>
* It checks if the given document if the type of feeds the parser understands.
* <p>
*
*
* @param document XML Document (JDOM) to check if it can be parsed by this parser.
* @return <b>true</b> if the parser know how to parser this feed, <b>false</b> otherwise.
*/
@ -54,7 +54,7 @@ public class OPML20Parser extends OPML10Parser {
if (e.getName().equals("opml")
&& (e.getChild("head") != null && e.getChild("head").getChild("docs") != null || e.getAttributeValue("version") != null
&& e.getAttributeValue("version").equals("2.0") || e.getChild("head") != null && e.getChild("head").getChild("ownerId") != null)) {
&& e.getAttributeValue("version").equals("2.0") || e.getChild("head") != null && e.getChild("head").getChild("ownerId") != null)) {
return true;
}
@ -64,7 +64,7 @@ public class OPML20Parser extends OPML10Parser {
/**
* Parses an XML document (JDOM Document) into a feed bean.
* <p>
*
*
* @param document XML document (JDOM) to parse.
* @param validate indicates if the feed should be strictly validated (NOT YET IMPLEMENTED).
* @return the resulting feed bean.
@ -102,10 +102,10 @@ public class OPML20Parser extends OPML10Parser {
retValue.setCreated(DateParser.parseRFC822(e.getAttributeValue("created"), locale));
}
final List atts = retValue.getAttributes();
final List<Attribute> atts = retValue.getAttributes();
for (int i = 0; i < atts.size(); i++) {
final Attribute a = (Attribute) atts.get(i);
final Attribute a = atts.get(i);
if (a.getName().equals("created")) {
retValue.getAttributes().remove(a);

View file

@ -32,7 +32,7 @@ import com.sun.syndication.io.XmlReader;
/**
* @author pat, tucu
*
*
*/
public class TestXmlReader extends TestCase {
@ -44,17 +44,22 @@ public class TestXmlReader extends TestCase {
}
protected void _testRawNoBomValid(final String encoding) throws Exception {
InputStream is = getXmlStream("no-bom", "xml", encoding, encoding);
XmlReader xmlReader = new XmlReader(is, false);
final XmlReader xmlReader = new XmlReader(is, false);
assertEquals(xmlReader.getEncoding(), "UTF-8");
xmlReader.close();
is = getXmlStream("no-bom", "xml-prolog", encoding, encoding);
xmlReader = new XmlReader(is);
final XmlReader xmlReader2 = new XmlReader(is);
assertEquals(xmlReader.getEncoding(), "UTF-8");
xmlReader2.close();
is = getXmlStream("no-bom", "xml-prolog-encoding", encoding, encoding);
xmlReader = new XmlReader(is);
final XmlReader xmlReader3 = new XmlReader(is);
assertEquals(xmlReader.getEncoding(), encoding);
xmlReader3.close();
}
protected void _testRawNoBomInvalid(final String encoding) throws Exception {
@ -62,6 +67,7 @@ public class TestXmlReader extends TestCase {
try {
final XmlReader xmlReader = new XmlReader(is, false);
fail("It should have failed");
xmlReader.close();
} catch (final IOException ex) {
assertTrue(ex.getMessage().indexOf("Invalid encoding,") > -1);
}
@ -81,6 +87,7 @@ public class TestXmlReader extends TestCase {
} else {
assertEquals(xmlReader.getEncoding().substring(0, encoding.length()), encoding);
}
xmlReader.close();
}
protected void _testRawBomInvalid(final String bomEnc, final String streamEnc, final String prologEnc) throws Exception {
@ -88,6 +95,7 @@ public class TestXmlReader extends TestCase {
try {
final XmlReader xmlReader = new XmlReader(is, false);
fail("It should have failed for BOM " + bomEnc + ", streamEnc " + streamEnc + " and prologEnc " + prologEnc);
xmlReader.close();
} catch (final IOException ex) {
assertTrue(ex.getMessage().indexOf("Invalid encoding,") > -1);
}
@ -169,6 +177,7 @@ public class TestXmlReader extends TestCase {
} else {
assertEquals(xmlReader.getEncoding().substring(0, streamEnc.length()), streamEnc);
}
xmlReader.close();
}
protected void _testHttpInvalid(final String cT, final String bomEnc, final String streamEnc, final String prologEnc) throws Exception {
@ -176,6 +185,7 @@ public class TestXmlReader extends TestCase {
try {
final XmlReader xmlReader = new XmlReader(is, cT, false);
fail("It should have failed for HTTP Content-type " + cT + ", BOM " + bomEnc + ", streamEnc " + streamEnc + " and prologEnc " + prologEnc);
xmlReader.close();
} catch (final IOException ex) {
assertTrue(ex.getMessage().indexOf("Invalid encoding,") > -1);
}
@ -186,6 +196,7 @@ public class TestXmlReader extends TestCase {
final InputStream is = getXmlStream(bomEnc, prologEnc == null ? "xml-prolog" : "xml-prolog-encoding", streamEnc, prologEnc);
final XmlReader xmlReader = new XmlReader(is, cT, true);
assertEquals(xmlReader.getEncoding(), shouldbe);
xmlReader.close();
}
// XML Stream generator
@ -195,7 +206,7 @@ public class TestXmlReader extends TestCase {
private static final int[] UTF_16LE_BOM_BYTES = { 0xFF, 0XFE };
private static final int[] UTF_8_BOM_BYTES = { 0xEF, 0xBB, 0xBF };
private static final Map BOMs = new HashMap();
private static final Map<String, int[]> BOMs = new HashMap<String, int[]>();
static {
BOMs.put("no-bom", NO_BOM_BYTES);
@ -211,7 +222,7 @@ public class TestXmlReader extends TestCase {
private static final MessageFormat INFO = new MessageFormat("\nBOM : {0}\nDoc : {1}\nStream Enc : {2}\nProlog Enc : {3}\n");
private static final Map XMLs = new HashMap();
private static final Map<String, MessageFormat> XMLs = new HashMap<String, MessageFormat>();
static {
XMLs.put("xml", XML);
@ -220,18 +231,18 @@ public class TestXmlReader extends TestCase {
}
/**
*
*
* @param bomType no-bom, UTF-16BE-bom, UTF-16LE-bom, UTF-8-bom
* @param xmlType xml, xml-prolog, xml-prolog-charset
* @return XML stream
*/
protected InputStream getXmlStream(final String bomType, final String xmlType, final String streamEnc, final String prologEnc) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
int[] bom = (int[]) BOMs.get(bomType);
int[] bom = BOMs.get(bomType);
if (bom == null) {
bom = new int[0];
}
final MessageFormat xml = (MessageFormat) XMLs.get(xmlType);
final MessageFormat xml = XMLs.get(xmlType);
for (final int element : bom) {
baos.write(element);
}