catched NPEs in OpenSearchModule (fixes #23 and fixes #18) and cleaned

up source format
This commit is contained in:
Martin Kurz 2013-10-11 17:18:31 +02:00
parent 80ab8834b2
commit e7a81c4444
6 changed files with 144 additions and 93 deletions

View file

@ -29,20 +29,24 @@ import com.sun.syndication.feed.atom.Link;
public interface OpenSearchResponse {
/**
* # totalResults the maximum number of results available for these search terms
* # totalResults the maximum number of results available for these search
* terms
*
* * Restrictions: An integer greater than or equal to 0. * Default: The number of items that were returned in this set of results. * Requirements: May
* appear zero or one time.
* * Restrictions: An integer greater than or equal to 0. * Default: The
* number of items that were returned in this set of results. *
* Requirements: May appear zero or one time.
*
* @param totalResults A positive integer value.
*/
public void setTotalResults(int totalResults);
/**
* # totalResults the maximum number of results available for these search terms
* # totalResults the maximum number of results available for these search
* terms
*
* * Restrictions: An integer greater than or equal to 0. * Default: The number of items that were returned in this set of results. * Requirements: May
* appear zero or one time.
* * Restrictions: An integer greater than or equal to 0. * Default: The
* number of items that were returned in this set of results. *
* Requirements: May appear zero or one time.
*
* @return a positive integer value.
*/
@ -51,7 +55,8 @@ public interface OpenSearchResponse {
/**
* # startIndex the index of the first item returned in the result.
*
* * Restrictions: An integer greater than or equal to 1. * Note: The first result is 1. * Default: 1 * Requirements: May appear zero or one time.
* * Restrictions: An integer greater than or equal to 1. * Note: The first
* result is 1. * Default: 1 * Requirements: May appear zero or one time.
*
* @param startIndex int value >= 1.
*/
@ -60,27 +65,32 @@ public interface OpenSearchResponse {
/**
* # startIndex the index of the first item returned in the result.
*
* * Restrictions: An integer greater than or equal to 1. * Note: The first result is 1. * Default: 1 * Requirements: May appear zero or one time.
* * Restrictions: An integer greater than or equal to 1. * Note: The first
* result is 1. * Default: 1 * Requirements: May appear zero or one time.
*
* @return int value >= 1.
*/
public int getStartIndex();
/**
* # itemsPerPage the maximum number of items that can appear in one page of results.
* # itemsPerPage the maximum number of items that can appear in one page
* of results.
*
* * Restrictions: An integer greater than or equal to 1. * Default: The number of items that were returned in this set of results. * Requirements: May
* appear zero or one time.
* * Restrictions: An integer greater than or equal to 1. * Default: The
* number of items that were returned in this set of results. *
* Requirements: May appear zero or one time.
*
* @param itemsPerPage int value >= 1.
*/
public void setItemsPerPage(int itemsPerPage);
/**
* # itemsPerPage the maximum number of items that can appear in one page of results.
* # itemsPerPage the maximum number of items that can appear in one page
* of results.
*
* * Restrictions: An integer greater than or equal to 1. * Default: The number of items that were returned in this set of results. * Requirements: May
* appear zero or one time.
* * Restrictions: An integer greater than or equal to 1. * Default: The
* number of items that were returned in this set of results. *
* Requirements: May appear zero or one time.
*
* @return int value >= 1
*/
@ -89,8 +99,10 @@ public interface OpenSearchResponse {
/**
* # link a reference back to the OpenSearch Description file
*
* * Attributes: This is a clone of the link element in Atom, including href, hreflang, rel, and type attributes. * Restrictions: The rel attribute must
* equal search. * Note: New in version 1.1. * Requirements: May appear zero or one time.
* * Attributes: This is a clone of the link element in Atom, including
* href, hreflang, rel, and type attributes. * Restrictions: The rel
* attribute must equal search. * Note: New in version 1.1. * Requirements:
* May appear zero or one time.
*
* @param link link to the open search spec.
*/
@ -99,8 +111,10 @@ public interface OpenSearchResponse {
/**
* # link a reference back to the OpenSearch Description file
*
* * Attributes: This is a clone of the link element in Atom, including href, hreflang, rel, and type attributes. * Restrictions: The rel attribute must
* equal search. * Note: New in version 1.1. * Requirements: May appear zero or one time.
* * Attributes: This is a clone of the link element in Atom, including
* href, hreflang, rel, and type attributes. * Restrictions: The rel
* attribute must equal search. * Note: New in version 1.1. * Requirements:
* May appear zero or one time.
*
* @return link to the opensearch spec.
*/
@ -108,26 +122,30 @@ public interface OpenSearchResponse {
// list of OSResponseQuery interfaces
/**
* <code>Query</code> in an OpenSearch Response, can be used both to echo back the original query and to suggest new searches. Please see the <a
* <code>Query</code> in an OpenSearch Response, can be used both to echo
* back the original query and to suggest new searches. Please see the <a
* href="../query/">OpenSearch Query specification</a> for more information.
* <ul>
* <li>Note: <em>New in version 1.1.</em></li>
* <li>Requirements: May appear zero or more times. Note that the Q is capitalized.</li>
* <li>Requirements: May appear zero or more times. Note that the Q is
* capitalized.</li>
*
* @param query List of OSQuery objects.
*/
public void setQueries(List query);
public void setQueries(List<OSQuery> query);
/**
* <code>Query</code> in an OpenSearch Response, can be used both to echo back the original query and to suggest new searches. Please see the <a
* <code>Query</code> in an OpenSearch Response, can be used both to echo
* back the original query and to suggest new searches. Please see the <a
* href="../query/">OpenSearch Query specification</a> for more information.
* <ul>
* <li>Note: <em>New in version 1.1.</em></li>
* <li>Requirements: May appear zero or more times. Note that the Q is capitalized.</li>
* <li>Requirements: May appear zero or more times. Note that the Q is
* capitalized.</li>
*
* @return A list of OSQuery objects.
*/
public List getQueries();
public List<OSQuery> getQueries();
// convenience method
/**

View file

@ -19,20 +19,18 @@ package org.rometools.feed.module.opensearch;
* @author Michael W. Nassif (enrouteinc@gmail.com)
*/
public class RequiredAttributeMissingException extends RuntimeException {
private static final long serialVersionUID = -7678987346283611492L;
public RequiredAttributeMissingException(final String arg0, final Throwable arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
}
public RequiredAttributeMissingException(final String arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public RequiredAttributeMissingException(final Throwable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
}

View file

@ -20,9 +20,11 @@ import java.io.Serializable;
import com.sun.syndication.feed.impl.ObjectBean;
/**
* @author Michael W. Nassif (enrouteinc@gmail.com) Class representation of the Query object (Response portion so far)
* @author Michael W. Nassif (enrouteinc@gmail.com) Class representation of the
* Query object (Response portion so far)
*/
public class OSQuery implements Cloneable, Serializable {
private static final long serialVersionUID = 3818092699440612477L;
ObjectBean _objBean = null;
@ -47,7 +49,8 @@ public class OSQuery implements Cloneable, Serializable {
* <p>
*
* @return a clone of the object.
* @throws CloneNotSupportedException thrown if an element of the object cannot be cloned.
* @throws CloneNotSupportedException thrown if an element of the object
* cannot be cloned.
*
*/
@Override
@ -56,7 +59,8 @@ public class OSQuery implements Cloneable, Serializable {
}
/**
* Indicates whether some other object is "equal to" this one as defined by the Object equals() method.
* 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.

View file

@ -17,7 +17,6 @@ package org.rometools.feed.module.opensearch.impl;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
@ -33,37 +32,44 @@ import com.sun.syndication.feed.module.Module;
import com.sun.syndication.io.ModuleGenerator;
/**
* @author Michael W. Nassif (enrouteinc@gmail.com) OpenSearch implementation of ModuleGenerator
* @author Michael W. Nassif (enrouteinc@gmail.com) OpenSearch implementation of
* ModuleGenerator
*/
public class OpenSearchModuleGenerator implements ModuleGenerator {
private static final Namespace OS_NS = Namespace.getNamespace("opensearch", OpenSearchModule.URI);
@Override
public String getNamespaceUri() {
return OpenSearchModule.URI;
}
private static final Set NAMESPACES;
private static final Set<Namespace> NAMESPACES;
static {
final Set nss = new HashSet();
final Set<Namespace> nss = new HashSet<Namespace>();
nss.add(OS_NS);
NAMESPACES = Collections.unmodifiableSet(nss);
}
/**
* Returns a set with all the URIs (JDOM Namespace elements) this module generator uses.
* Returns a set with all the URIs (JDOM Namespace elements) this module
* generator uses.
* <p/>
* It is used by the the feed generators to add their namespace definition in the root element of the generated document (forward-missing of Java 5.0
* Generics).
* It is used by the the feed generators to add their namespace definition
* in the root element of the generated document (forward-missing of Java
* 5.0 Generics).
* <p/>
*
* @return a set with all the URIs (JDOM Namespace elements) this module generator uses.
* @return a set with all the URIs (JDOM Namespace elements) this module
* generator uses.
*/
public Set getNamespaces() {
@Override
public Set<Namespace> getNamespaces() {
return NAMESPACES;
}
@Override
public void generate(final Module module, final Element element) {
final OpenSearchModule osm = (OpenSearchModule) module;
@ -81,10 +87,9 @@ public class OpenSearchModuleGenerator implements ModuleGenerator {
if (osm.getQueries() != null) {
final List queries = osm.getQueries();
final List<OSQuery> queries = osm.getQueries();
for (final Iterator iter = queries.iterator(); iter.hasNext();) {
final OSQuery query = (OSQuery) iter.next();
for (OSQuery query : queries) {
if (query != null) {
element.addContent(generateQueryElement(query));
}

View file

@ -23,17 +23,20 @@ import org.rometools.feed.module.opensearch.entity.OSQuery;
import com.sun.syndication.feed.CopyFrom;
import com.sun.syndication.feed.atom.Link;
import com.sun.syndication.feed.module.Module;
import com.sun.syndication.feed.module.ModuleImpl;
/**
* @author Michael W. Nassif (enrouteinc@gmail.com) OpenSearch Module implementation
* @author Michael W. Nassif (enrouteinc@gmail.com) OpenSearch Module
* implementation
*/
public class OpenSearchModuleImpl extends ModuleImpl implements OpenSearchModule, Serializable {
private static final long serialVersionUID = -5257107642509731500L;
private int totalResults = -1;
private int startIndex = 1;
private int itemsPerPage = -1;
private Link link;
private List queries;
private List<OSQuery> queries;
public OpenSearchModuleImpl() {
super(OpenSearchModuleImpl.class, OpenSearchModule.URI);
@ -42,6 +45,7 @@ public class OpenSearchModuleImpl extends ModuleImpl implements OpenSearchModule
/**
* @return Returns the itemsPerPage.
*/
@Override
public int getItemsPerPage() {
return itemsPerPage;
}
@ -49,6 +53,7 @@ public class OpenSearchModuleImpl extends ModuleImpl implements OpenSearchModule
/**
* @param itemsPerPage The itemsPerPage to set.
*/
@Override
public void setItemsPerPage(final int itemsPerPage) {
this.itemsPerPage = itemsPerPage;
}
@ -56,6 +61,7 @@ public class OpenSearchModuleImpl extends ModuleImpl implements OpenSearchModule
/**
* @return Returns the link.
*/
@Override
public Link getLink() {
return link;
}
@ -63,6 +69,7 @@ public class OpenSearchModuleImpl extends ModuleImpl implements OpenSearchModule
/**
* @param link The link to set.
*/
@Override
public void setLink(final Link link) {
this.link = link;
}
@ -70,31 +77,31 @@ public class OpenSearchModuleImpl extends ModuleImpl implements OpenSearchModule
/**
* @return Returns the queries.
*/
public List getQueries() {
queries = queries == null ? new LinkedList() : queries;
@Override
public List<OSQuery> getQueries() {
if (queries == null) {
queries = new LinkedList<OSQuery>();
}
return queries;
}
/**
* @param queries The queries to set.
*/
public void setQueries(final List queries) {
@Override
public void setQueries(final List<OSQuery> queries) {
this.queries = queries;
}
@Override
public void addQuery(final OSQuery query) {
if (queries != null) {
queries.add(query);
} else {
queries = new LinkedList();
queries.add(query);
}
getQueries().add(query);
}
/**
* @return Returns the startIndex.
*/
@Override
public int getStartIndex() {
return startIndex;
}
@ -102,6 +109,7 @@ public class OpenSearchModuleImpl extends ModuleImpl implements OpenSearchModule
/**
* @param startIndex The startIndex to set.
*/
@Override
public void setStartIndex(final int startIndex) {
this.startIndex = startIndex;
}
@ -109,6 +117,7 @@ public class OpenSearchModuleImpl extends ModuleImpl implements OpenSearchModule
/**
* @return Returns the totalResults.
*/
@Override
public int getTotalResults() {
return totalResults;
}
@ -116,33 +125,37 @@ public class OpenSearchModuleImpl extends ModuleImpl implements OpenSearchModule
/**
* @param totalResults The totalResults to set.
*/
@Override
public void setTotalResults(final int totalResults) {
this.totalResults = totalResults;
}
/*
* (non-Javadoc)
*
* @see com.sun.syndication.feed.CopyFrom#copyFrom(java.lang.Object)
*/
public void copyFrom(final CopyFrom obj) {
final OpenSearchModule osm = (OpenSearchModuleImpl) obj;
@Override
public void copyFrom(final CopyFrom<? extends Module> obj) {
final OpenSearchModule osm = (OpenSearchModule) obj;
setTotalResults(osm.getTotalResults());
setItemsPerPage(osm.getItemsPerPage());
setStartIndex(osm.getStartIndex());
setLink(osm.getLink());
// setQueries(osm.getQueries());
for (final OSQuery q : osm.getQueries()) {
try {
getQueries().add((OSQuery) q.clone());
} catch (final CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
/*
* (non-Javadoc)
*
* @see com.sun.syndication.feed.CopyFrom#getInterface()
*/
public Class getInterface() {
// TODO Auto-generated method stub
@Override
public Class<OpenSearchModule> getInterface() {
return OpenSearchModule.class;
}
}

View file

@ -33,16 +33,19 @@ import com.sun.syndication.feed.module.Module;
import com.sun.syndication.io.ModuleParser;
/**
* @author Michael W. Nassif (enrouteinc@gmail.com) OpenSearch implementation of the ModuleParser class
* @author Michael W. Nassif (enrouteinc@gmail.com) OpenSearch implementation of
* the ModuleParser class
*/
public class OpenSearchModuleParser implements ModuleParser {
private static final Namespace OS_NS = Namespace.getNamespace("opensearch", OpenSearchModule.URI);
@Override
public String getNamespaceUri() {
return OpenSearchModule.URI;
}
@Override
public Module parse(final Element dcRoot) {
final URL baseURI = findBaseURI(dcRoot);
@ -53,11 +56,9 @@ public class OpenSearchModuleParser implements ModuleParser {
Element e = dcRoot.getChild("totalResults", OS_NS);
if (e != null) {
foundSomething = true;
try {
osm.setTotalResults(Integer.parseInt(e.getText()));
foundSomething = true;
} catch (final NumberFormatException ex) {
// Ignore setting the field and post a warning
System.err.println("Warning: The element totalResults must be an integer value: " + ex.getMessage());
@ -65,32 +66,36 @@ public class OpenSearchModuleParser implements ModuleParser {
}
e = dcRoot.getChild("itemsPerPage", OS_NS);
try {
osm.setItemsPerPage(Integer.parseInt(e.getText()));
} catch (final NumberFormatException ex) {
// Ignore setting the field and post a warning
System.err.println("Warning: The element itemsPerPage must be an integer value: " + ex.getMessage());
if (e != null) {
try {
osm.setItemsPerPage(Integer.parseInt(e.getText()));
foundSomething = true;
} catch (final NumberFormatException ex) {
// Ignore setting the field and post a warning
System.err.println("Warning: The element itemsPerPage must be an integer value: " + ex.getMessage());
}
}
e = dcRoot.getChild("startIndex", OS_NS);
try {
osm.setStartIndex(Integer.parseInt(e.getText()));
} catch (final NumberFormatException ex) {
// Ignore setting the field and post a warning
System.err.println("Warning: The element startIndex must be an integer value: " + ex.getMessage());
if (e != null) {
try {
osm.setStartIndex(Integer.parseInt(e.getText()));
foundSomething = true;
} catch (final NumberFormatException ex) {
// Ignore setting the field and post a warning
System.err.println("Warning: The element startIndex must be an integer value: " + ex.getMessage());
}
}
final List queries = dcRoot.getChildren("Query", OS_NS);
final List<Element> queries = dcRoot.getChildren("Query", OS_NS);
if (queries != null && queries.size() > 0) {
// Create the OSQuery list
final List osqList = new LinkedList();
final List<OSQuery> osqList = new LinkedList<OSQuery>();
for (final Iterator iter = queries.iterator(); iter.hasNext();) {
e = (Element) iter.next();
for (final Iterator<Element> iter = queries.iterator(); iter.hasNext();) {
e = iter.next();
osqList.add(parseQuery(e));
}
@ -124,7 +129,8 @@ public class OpenSearchModuleParser implements ModuleParser {
try {
// someones mistake should not cause the parser to fail, since these are only optional attributes
// someones mistake should not cause the parser to fail, since these
// are only optional attributes
att = e.getAttributeValue("totalResults");
if (att != null) {
@ -147,19 +153,22 @@ public class OpenSearchModuleParser implements ModuleParser {
final Link link = new Link();
String att = e.getAttributeValue("rel");// getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
String att = e.getAttributeValue("rel");// getAtomNamespace()); DONT
// KNOW WHY DOESN'T WORK
if (att != null) {
link.setRel(att);
}
att = e.getAttributeValue("type");// getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
att = e.getAttributeValue("type");// getAtomNamespace()); DONT KNOW WHY
// DOESN'T WORK
if (att != null) {
link.setType(att);
}
att = e.getAttributeValue("href");// getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
att = e.getAttributeValue("href");// getAtomNamespace()); DONT KNOW WHY
// DOESN'T WORK
if (att != null) {
@ -170,13 +179,15 @@ public class OpenSearchModuleParser implements ModuleParser {
}
}
att = e.getAttributeValue("hreflang");// getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
att = e.getAttributeValue("hreflang");// getAtomNamespace()); DONT KNOW
// WHY DOESN'T WORK
if (att != null) {
link.setHreflang(att);
}
att = e.getAttributeValue("length");// getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
att = e.getAttributeValue("length");// getAtomNamespace()); DONT KNOW
// WHY DOESN'T WORK
return link;
}
@ -188,7 +199,9 @@ public class OpenSearchModuleParser implements ModuleParser {
return true;
}
/** Use xml:base attributes at feed and entry level to resolve relative links */
/**
* Use xml:base attributes at feed and entry level to resolve relative links
*/
private static String resolveURI(final URL baseURI, final Parent parent, String url) {
url = url.equals(".") || url.equals("./") ? "" : url;
if (isRelativeURI(url) && parent != null && parent instanceof Element) {
@ -213,10 +226,10 @@ public class OpenSearchModuleParser implements ModuleParser {
/** Use feed links and/or xml:base attribute to determine baseURI of feed */
private static URL findBaseURI(final Element root) {
URL baseURI = null;
final List linksList = root.getChildren("link", OS_NS);
final List<Element> linksList = root.getChildren("link", OS_NS);
if (linksList != null) {
for (final Iterator links = linksList.iterator(); links.hasNext();) {
final Element link = (Element) links.next();
for (final Element element : linksList) {
final Element link = element;
if (!root.equals(link.getParent())) {
break;
}