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
This commit is contained in:
mishako 2016-04-24 20:39:43 +02:00
parent 94861b0532
commit 6920ff0fbe

View file

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