From 6920ff0fbe0a92899a0a1f7927fb2c24e02ceca4 Mon Sep 17 00:00:00 2001 From: mishako Date: Sun, 24 Apr 2016 20:39:43 +0200 Subject: [PATCH] Do not set unsupported XML parser features Fixes bug introduced in 2bc58a0. The call to `XMLReader::setFeature` is used to check if the feature is supported and must happen before `SAXBuilder::setFeature`. Otherwise XML parsing throws an exception. The change is not tested, because it is exceptionally cumbersome to test unsupported features. Possible ways to do it include using an existing XML library that is known to not support features that the code is trying to set, or implementing a mock XML parser that doesn't support any features at all. It's hard to include an XML library without interfering with other tests and it's hard to implement a mock XML parser. Fixes #294 --- .../com/rometools/rome/io/WireFeedInput.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/rome/src/main/java/com/rometools/rome/io/WireFeedInput.java b/rome/src/main/java/com/rometools/rome/io/WireFeedInput.java index e5f9b6f..c67680d 100644 --- a/rome/src/main/java/com/rometools/rome/io/WireFeedInput.java +++ b/rome/src/main/java/com/rometools/rome/io/WireFeedInput.java @@ -359,23 +359,29 @@ public class WireFeedInput { } } catch (final JDOMException e) { - throw new IllegalStateException("JDOM could not create a SAX parser"); + throw new IllegalStateException("JDOM could not create a SAX parser", e); } saxBuilder.setExpandEntities(false); - + return saxBuilder; - + } private void setFeature(SAXBuilder saxBuilder, XMLReader parser, String feature, boolean value) { - try { + if (isFeatureSupported(parser, feature, value)) { saxBuilder.setFeature(feature, value); + } + } + + private boolean isFeatureSupported(XMLReader parser, String feature, boolean value) { + try { parser.setFeature(feature, value); + return true; } catch (final SAXNotRecognizedException e) { - // ignore + return false; } catch (final SAXNotSupportedException e) { - // ignore + return false; } }