From 3733a6aaa50c95c3ad9ef42bddc2155b8e08b9d9 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 19 Oct 2014 20:58:42 +0200 Subject: [PATCH] Cache the parser availability of the roundtrip feature As otherwise there would be multiple log statements (which are of level fine but nevertheless). PacketParserUtils: XmlPullParser does not support XML_ROUNDTRIP org.xmlpull.v1.XmlPullParserException: unsupported feature: http://xmlpull.org/v1/doc/features.html#xml-roundtrip (position:START_DOCUMENT null@1:1) PacketParserUtils: at org.kxml2.io.KXmlParser.setFeature(KXmlParser.java:2091) PacketParserUtils: at org.jivesoftware.smack.util.PacketParserUtils.newXmppParser(PacketParserUtils.java:150) PacketParserUtils: at org.jivesoftware.smack.util.PacketParserUtils.newXmppParser(PacketParserUtils.java:172) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection.openStream(XMPPTCPConnection.java:963) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketWriter.access$2600(XMPPTCPConnection.java:1224) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketWriter$1.run(XMPPTCPConnection.java:1263) PacketParserUtils: XmlPullParser does not support XML_ROUNDTRIP org.xmlpull.v1.XmlPullParserException: unsupported feature: http://xmlpull.org/v1/doc/features.html#xml-roundtrip (position:START_DOCUMENT null@1:1) PacketParserUtils: at org.kxml2.io.KXmlParser.setFeature(KXmlParser.java:2091) PacketParserUtils: at org.jivesoftware.smack.util.PacketParserUtils.newXmppParser(PacketParserUtils.java:150) PacketParserUtils: at org.jivesoftware.smack.util.PacketParserUtils.newXmppParser(PacketParserUtils.java:172) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection.openStream(XMPPTCPConnection.java:963) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:970) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:989) PacketParserUtils: XmlPullParser does not support XML_ROUNDTRIP org.xmlpull.v1.XmlPullParserException: unsupported feature: http://xmlpull.org/v1/doc/features.html#xml-roundtrip (position:START_DOCUMENT null@1:1) PacketParserUtils: at org.kxml2.io.KXmlParser.setFeature(KXmlParser.java:2091) PacketParserUtils: at org.jivesoftware.smack.util.PacketParserUtils.newXmppParser(PacketParserUtils.java:150) PacketParserUtils: at org.jivesoftware.smack.util.PacketParserUtils.newXmppParser(PacketParserUtils.java:172) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection.openStream(XMPPTCPConnection.java:963) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:970) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:989) PacketParserUtils: XmlPullParser does not support XML_ROUNDTRIP org.xmlpull.v1.XmlPullParserException: unsupported feature: http://xmlpull.org/v1/doc/features.html#xml-roundtrip (position:START_DOCUMENT null@1:1) PacketParserUtils: at org.kxml2.io.KXmlParser.setFeature(KXmlParser.java:2091) PacketParserUtils: at org.jivesoftware.smack.util.PacketParserUtils.newXmppParser(PacketParserUtils.java:150) PacketParserUtils: at org.jivesoftware.smack.util.PacketParserUtils.newXmppParser(PacketParserUtils.java:172) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection.openStream(XMPPTCPConnection.java:963) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:970) PacketParserUtils: at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:989) --- .../smack/util/PacketParserUtils.java | 43 ++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java index 97da115d7..bb80cab08 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java @@ -61,6 +61,35 @@ public class PacketParserUtils { public static final String FEATURE_XML_ROUNDTRIP = "http://xmlpull.org/v1/doc/features.html#xml-roundtrip"; + private static final XmlPullParserFactory XML_PULL_PARSER_FACTORY; + + /** + * True if the XmlPullParser supports the XML_ROUNDTRIP feature. + */ + public static final boolean XML_PULL_PARSER_SUPPORTS_ROUNDTRIP; + + static { + XmlPullParser xmlPullParser; + boolean roundtrip = false; + try { + XML_PULL_PARSER_FACTORY = XmlPullParserFactory.newInstance(); + xmlPullParser = XML_PULL_PARSER_FACTORY.newPullParser(); + try { + xmlPullParser.setFeature(FEATURE_XML_ROUNDTRIP, true); + // We could successfully set the feature + roundtrip = true; + } catch (XmlPullParserException e) { + // Doesn't matter if FEATURE_XML_ROUNDTRIP isn't available + LOGGER.log(Level.FINEST, "XmlPullParser does not support XML_ROUNDTRIP", e); + } + } + catch (XmlPullParserException e) { + // Something really bad happened + throw new AssertionError(e); + } + XML_PULL_PARSER_SUPPORTS_ROUNDTRIP = roundtrip; + } + public static XmlPullParser getParserFor(String stanza) throws XmlPullParserException, IOException { return getParserFor(new StringReader(stanza)); } @@ -146,11 +175,15 @@ public class PacketParserUtils { public static XmlPullParser newXmppParser() throws XmlPullParserException { XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); - try { - parser.setFeature(FEATURE_XML_ROUNDTRIP, true); - } catch (XmlPullParserException e) { - // Doesn't matter if FEATURE_XML_ROUNDTRIP isn't available - LOGGER.log(Level.FINEST, "XmlPullParser does not support XML_ROUNDTRIP", e); + if (XML_PULL_PARSER_SUPPORTS_ROUNDTRIP) { + try { + parser.setFeature(FEATURE_XML_ROUNDTRIP, true); + } + catch (XmlPullParserException e) { + LOGGER.log(Level.SEVERE, + "XmlPullParser does not support XML_ROUNDTRIP, although it was first determined to be supported", + e); + } } return parser; }