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

@ -39,7 +39,7 @@ public class Opml extends WireFeed {
private Integer _windowLeft; private Integer _windowLeft;
private Integer _windowRight; private Integer _windowRight;
private Integer _windowTop; private Integer _windowTop;
private List _outlines; private List<Outline> _outlines;
private String _docs; private String _docs;
private String _ownerEmail; private String _ownerEmail;
private String _ownerId; 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; * @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; _outlines = outlines;
} }
@ -144,9 +144,9 @@ public class Opml extends WireFeed {
* *
* @return Root level Outline object that should appear in the &lt;body&gt; * @return Root level Outline object that should appear in the &lt;body&gt;
*/ */
public List getOutlines() { public List<Outline> getOutlines() {
if (_outlines == null) { if (_outlines == null) {
_outlines = new ArrayList(); _outlines = new ArrayList<Outline>();
} }
return _outlines; 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.EqualsBean;
import com.sun.syndication.feed.impl.ToStringBean; import com.sun.syndication.feed.impl.ToStringBean;
import com.sun.syndication.feed.module.Module;
/** /**
* This class represents an OPML outline element. * This class represents an OPML outline element.
@ -37,10 +38,10 @@ public class Outline implements Cloneable, Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private Date _created; private Date _created;
private List _attributes; private List<Attribute> _attributes;
private List _categories; private List<String> _categories;
private List _children; private List<Outline> _children;
private List _modules; private List<Module> _modules;
private String _text; private String _text;
private String _title; private String _title;
private String _type; private String _type;
@ -75,7 +76,7 @@ public class Outline implements Cloneable, Serializable {
super(); super();
setType("rss"); setType("rss");
setTitle(title); setTitle(title);
setAttributes(new ArrayList()); setAttributes(new ArrayList<Attribute>());
if (xmlUrl != null) { if (xmlUrl != null) {
getAttributes().add(new Attribute("xmlUrl", xmlUrl.toString())); 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. * @param attributes List of attributes on this outline.
*/ */
public void setAttributes(final List attributes) { public void setAttributes(final List<Attribute> attributes) {
_attributes = attributes; _attributes = attributes;
} }
@ -100,9 +101,9 @@ public class Outline implements Cloneable, Serializable {
* *
* @return List of attributes on this outline. * @return List of attributes on this outline.
*/ */
public List getAttributes() { public List<Attribute> getAttributes() {
if (_attributes == null) { if (_attributes == null) {
_attributes = new ArrayList(); _attributes = new ArrayList<Attribute>();
} }
return _attributes; 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. * @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; _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. * @return (OPML 2) A List of Strings indicating values in the category attribute.
*/ */
public List getCategories() { public List<String> getCategories() {
if (_categories == null) { if (_categories == null) {
_categories = new ArrayList(); _categories = new ArrayList<String>();
} }
return _categories; return _categories;
@ -155,7 +156,7 @@ public class Outline implements Cloneable, Serializable {
* *
* @param children A list of sub-outlines for this entry. * @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; _children = children;
} }
@ -164,9 +165,9 @@ public class Outline implements Cloneable, Serializable {
* *
* @return A list of sub-outlines for this entry. * @return A list of sub-outlines for this entry.
*/ */
public List getChildren() { public List<Outline> getChildren() {
if (_children == null) { if (_children == null) {
_children = new ArrayList(); _children = new ArrayList<Outline>();
} }
return _children; return _children;
@ -228,13 +229,13 @@ public class Outline implements Cloneable, Serializable {
return getAttributeValue("htmlUrl"); return getAttributeValue("htmlUrl");
} }
public void setModules(final List modules) { public void setModules(final List<Module> modules) {
_modules = modules; _modules = modules;
} }
public List getModules() { public List<Module> getModules() {
if (_modules == null) { if (_modules == null) {
_modules = new ArrayList(); _modules = new ArrayList<Module>();
} }
return _modules; return _modules;
@ -309,45 +310,40 @@ public class Outline implements Cloneable, Serializable {
* @param name name of the attribute. * @param name name of the attribute.
*/ */
public String getAttributeValue(final String name) { 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++) { 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)) { if (a.getName() != null && a.getName().equals(name)) {
return a.getValue(); return a.getValue();
} }
} }
return null; return null;
} }
@Override @Override
public Object clone() { public Object clone() {
final Outline o = new Outline(); final Outline o = new Outline();
o.setBreakpoint(isBreakpoint()); o.setBreakpoint(isBreakpoint());
o.setCategories(new ArrayList(getCategories())); o.setCategories(new ArrayList<String>(getCategories()));
o.setComment(isComment()); o.setComment(isComment());
o.setCreated(_created != null ? (Date) _created.clone() : null); o.setCreated(_created != null ? (Date) _created.clone() : null);
o.setModules(new ArrayList(getModules())); o.setModules(new ArrayList<Module>(getModules()));
o.setText(getText()); o.setText(getText());
o.setTitle(getTitle()); o.setTitle(getTitle());
o.setType(getType()); o.setType(getType());
final ArrayList children = new ArrayList(); final ArrayList<Outline> children = new ArrayList<Outline>();
for (int i = 0; i < getChildren().size(); i++) { 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); o.setChildren(children);
final ArrayList attributes = new ArrayList(); final ArrayList<Attribute> attributes = new ArrayList<Attribute>();
for (int i = 0; i < getAttributes().size(); i++) { 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); o.setAttributes(attributes);
return o; return o;

View file

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

View file

@ -146,10 +146,10 @@ public class OPML10Generator extends BaseWireFeedGenerator implements WireFeedGe
addNotNullAttribute(e, "isComment", "true"); 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++) { 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()); addNotNullAttribute(e, att.getName(), att.getValue());
} }
@ -159,13 +159,11 @@ public class OPML10Generator extends BaseWireFeedGenerator implements WireFeedGe
return e; return e;
} }
protected List generateOutlines(final List outlines) { protected List<Element> generateOutlines(final List<Outline> outlines) {
final ArrayList elements = new ArrayList(); final ArrayList<Element> elements = new ArrayList<Element>();
for (int i = 0; outlines != null && i < outlines.size(); i++) { 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; return elements;
} }

View file

@ -177,11 +177,11 @@ public class OPML10Parser extends BaseWireFeedParser implements WireFeedParser {
outline.setType(e.getAttributeValue("type")); outline.setType(e.getAttributeValue("type"));
outline.setTitle(e.getAttributeValue("title")); outline.setTitle(e.getAttributeValue("title"));
final List jAttributes = e.getAttributes(); final List<org.jdom2.Attribute> jAttributes = e.getAttributes();
final ArrayList attributes = new ArrayList(); final ArrayList<Attribute> attributes = new ArrayList<Attribute>();
for (int i = 0; i < jAttributes.size(); i++) { 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") if (!a.getName().equals("isBreakpoint") && !a.getName().equals("isComment") && !a.getName().equals("title") && !a.getName().equals("text")
&& !a.getName().equals("type")) { && !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.setModules(parseItemModules(e, locale));
outline.setChildren(parseOutlines(children, validate, locale)); outline.setChildren(parseOutlines(children, validate, locale));
return outline; return outline;
} }
protected List parseOutlines(final List elements, final boolean validate, final Locale locale) throws FeedException { protected List<Outline> parseOutlines(final List<Element> elements, final boolean validate, final Locale locale) throws FeedException {
final ArrayList results = new ArrayList(); final ArrayList<Outline> results = new ArrayList<Outline>();
for (int i = 0; i < elements.size(); i++) { 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; return results;
} }

View file

@ -54,7 +54,7 @@ public class OPML20Parser extends OPML10Parser {
if (e.getName().equals("opml") if (e.getName().equals("opml")
&& (e.getChild("head") != null && e.getChild("head").getChild("docs") != null || e.getAttributeValue("version") != null && (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; return true;
} }
@ -102,10 +102,10 @@ public class OPML20Parser extends OPML10Parser {
retValue.setCreated(DateParser.parseRFC822(e.getAttributeValue("created"), locale)); 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++) { 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")) { if (a.getName().equals("created")) {
retValue.getAttributes().remove(a); retValue.getAttributes().remove(a);

View file

@ -44,17 +44,22 @@ public class TestXmlReader extends TestCase {
} }
protected void _testRawNoBomValid(final String encoding) throws Exception { protected void _testRawNoBomValid(final String encoding) throws Exception {
InputStream is = getXmlStream("no-bom", "xml", encoding, encoding); 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"); assertEquals(xmlReader.getEncoding(), "UTF-8");
xmlReader.close();
is = getXmlStream("no-bom", "xml-prolog", encoding, encoding); is = getXmlStream("no-bom", "xml-prolog", encoding, encoding);
xmlReader = new XmlReader(is); final XmlReader xmlReader2 = new XmlReader(is);
assertEquals(xmlReader.getEncoding(), "UTF-8"); assertEquals(xmlReader.getEncoding(), "UTF-8");
xmlReader2.close();
is = getXmlStream("no-bom", "xml-prolog-encoding", encoding, encoding); is = getXmlStream("no-bom", "xml-prolog-encoding", encoding, encoding);
xmlReader = new XmlReader(is); final XmlReader xmlReader3 = new XmlReader(is);
assertEquals(xmlReader.getEncoding(), encoding); assertEquals(xmlReader.getEncoding(), encoding);
xmlReader3.close();
} }
protected void _testRawNoBomInvalid(final String encoding) throws Exception { protected void _testRawNoBomInvalid(final String encoding) throws Exception {
@ -62,6 +67,7 @@ public class TestXmlReader extends TestCase {
try { try {
final XmlReader xmlReader = new XmlReader(is, false); final XmlReader xmlReader = new XmlReader(is, false);
fail("It should have failed"); fail("It should have failed");
xmlReader.close();
} catch (final IOException ex) { } catch (final IOException ex) {
assertTrue(ex.getMessage().indexOf("Invalid encoding,") > -1); assertTrue(ex.getMessage().indexOf("Invalid encoding,") > -1);
} }
@ -81,6 +87,7 @@ public class TestXmlReader extends TestCase {
} else { } else {
assertEquals(xmlReader.getEncoding().substring(0, encoding.length()), encoding); assertEquals(xmlReader.getEncoding().substring(0, encoding.length()), encoding);
} }
xmlReader.close();
} }
protected void _testRawBomInvalid(final String bomEnc, final String streamEnc, final String prologEnc) throws Exception { protected void _testRawBomInvalid(final String bomEnc, final String streamEnc, final String prologEnc) throws Exception {
@ -88,6 +95,7 @@ public class TestXmlReader extends TestCase {
try { try {
final XmlReader xmlReader = new XmlReader(is, false); final XmlReader xmlReader = new XmlReader(is, false);
fail("It should have failed for BOM " + bomEnc + ", streamEnc " + streamEnc + " and prologEnc " + prologEnc); fail("It should have failed for BOM " + bomEnc + ", streamEnc " + streamEnc + " and prologEnc " + prologEnc);
xmlReader.close();
} catch (final IOException ex) { } catch (final IOException ex) {
assertTrue(ex.getMessage().indexOf("Invalid encoding,") > -1); assertTrue(ex.getMessage().indexOf("Invalid encoding,") > -1);
} }
@ -169,6 +177,7 @@ public class TestXmlReader extends TestCase {
} else { } else {
assertEquals(xmlReader.getEncoding().substring(0, streamEnc.length()), streamEnc); 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 { 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 { try {
final XmlReader xmlReader = new XmlReader(is, cT, false); 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); fail("It should have failed for HTTP Content-type " + cT + ", BOM " + bomEnc + ", streamEnc " + streamEnc + " and prologEnc " + prologEnc);
xmlReader.close();
} catch (final IOException ex) { } catch (final IOException ex) {
assertTrue(ex.getMessage().indexOf("Invalid encoding,") > -1); 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 InputStream is = getXmlStream(bomEnc, prologEnc == null ? "xml-prolog" : "xml-prolog-encoding", streamEnc, prologEnc);
final XmlReader xmlReader = new XmlReader(is, cT, true); final XmlReader xmlReader = new XmlReader(is, cT, true);
assertEquals(xmlReader.getEncoding(), shouldbe); assertEquals(xmlReader.getEncoding(), shouldbe);
xmlReader.close();
} }
// XML Stream generator // 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_16LE_BOM_BYTES = { 0xFF, 0XFE };
private static final int[] UTF_8_BOM_BYTES = { 0xEF, 0xBB, 0xBF }; 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 { static {
BOMs.put("no-bom", NO_BOM_BYTES); 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 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 { static {
XMLs.put("xml", XML); XMLs.put("xml", XML);
@ -227,11 +238,11 @@ public class TestXmlReader extends TestCase {
*/ */
protected InputStream getXmlStream(final String bomType, final String xmlType, final String streamEnc, final String prologEnc) throws IOException { protected InputStream getXmlStream(final String bomType, final String xmlType, final String streamEnc, final String prologEnc) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
int[] bom = (int[]) BOMs.get(bomType); int[] bom = BOMs.get(bomType);
if (bom == null) { if (bom == null) {
bom = new int[0]; bom = new int[0];
} }
final MessageFormat xml = (MessageFormat) XMLs.get(xmlType); final MessageFormat xml = XMLs.get(xmlType);
for (final int element : bom) { for (final int element : bom) {
baos.write(element); baos.write(element);
} }