Substitute MXParser with a call to XmlPullParserFactory

This makes Smack more portable, as there are platforms that support the
XmlPullParser interface, but not MXParser (e.g. Android).

Also enable checkstyle check that MXParser is not used.
This commit is contained in:
Florian Schmaus 2014-02-20 13:38:12 +01:00
parent 4db0b101c2
commit 24b637876f
18 changed files with 40 additions and 35 deletions

View File

@ -23,7 +23,7 @@ import org.jivesoftware.smack.sasl.SASLMechanism.Challenge;
import org.jivesoftware.smack.sasl.SASLMechanism.Failure; import org.jivesoftware.smack.sasl.SASLMechanism.Failure;
import org.jivesoftware.smack.sasl.SASLMechanism.Success; import org.jivesoftware.smack.sasl.SASLMechanism.Success;
import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.util.PacketParserUtils;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.igniterealtime.jbosh.AbstractBody; import org.igniterealtime.jbosh.AbstractBody;
import org.igniterealtime.jbosh.BOSHClientResponseListener; import org.igniterealtime.jbosh.BOSHClientResponseListener;
@ -66,7 +66,7 @@ public class BOSHPacketReader implements BOSHClientResponseListener {
if (connection.authID == null) { if (connection.authID == null) {
connection.authID = body.getAttribute(BodyQName.create(BOSHConnection.BOSH_URI, "authid")); connection.authID = body.getAttribute(BodyQName.create(BOSHConnection.BOSH_URI, "authid"));
} }
final XmlPullParser parser = new MXParser(); final XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,
true); true);
parser.setInput(new StringReader(body.toXML())); parser.setInput(new StringReader(body.toXML()));

View File

@ -14,6 +14,10 @@
<module name="NewlineAtEndOfFile"> <module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf"/> <property name="lineSeparator" value="lf"/>
</module> </module>
<module name="RegexpSingleline">
<property name="format" value="MXParser"/>
<property name="message" value="Must not use MXParser, use XmlPullParserFactory instead"/>
</module>
<module name="TreeWalker"> <module name="TreeWalker">
<module name="UnusedImports"> <module name="UnusedImports">
<property name="processJavadoc" value="true"/> <property name="processJavadoc" value="true"/>

View File

@ -16,7 +16,7 @@ package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.provider.PrivacyProvider; import org.jivesoftware.smack.provider.PrivacyProvider;
import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smack.test.SmackTestCase;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -372,7 +372,7 @@ public class PrivacyProviderTest extends SmackTestCase {
} }
private XmlPullParser getParserFromXML(String xml) throws XmlPullParserException { private XmlPullParser getParserFromXML(String xml) throws XmlPullParserException {
MXParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new StringReader(xml)); parser.setInput(new StringReader(xml));
return parser; return parser;

View File

@ -32,7 +32,7 @@ import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.ConnectionUtils; import org.jivesoftware.smack.util.ConnectionUtils;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
/** /**
@ -411,7 +411,7 @@ public abstract class SmackTestCase extends TestCase {
InputStream systemStream = null; InputStream systemStream = null;
try { try {
systemStream = url.openStream(); systemStream = url.openStream();
XmlPullParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(systemStream, "UTF-8"); parser.setInput(systemStream, "UTF-8");
int eventType = parser.getEventType(); int eventType = parser.getEventType();

View File

@ -20,7 +20,7 @@ import java.io.StringReader;
import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smack.test.SmackTestCase;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -191,7 +191,7 @@ public class XMPPErrorTest extends SmackTestCase {
} }
private XmlPullParser getParserFromXML(String xml) throws XmlPullParserException { private XmlPullParser getParserFromXML(String xml) throws XmlPullParserException {
MXParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new StringReader(xml)); parser.setInput(new StringReader(xml));
return parser; return parser;

View File

@ -32,7 +32,7 @@ import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.RosterPacket.Item; import org.jivesoftware.smack.packet.RosterPacket.Item;
import org.jivesoftware.smack.util.Base32Encoder; import org.jivesoftware.smack.util.Base32Encoder;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -200,7 +200,7 @@ public class DefaultRosterStore implements RosterStore {
List<String> groupNames = new ArrayList<String>(); List<String> groupNames = new ArrayList<String>();
try { try {
XmlPullParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(new StringReader(s)); parser.setInput(new StringReader(s));
boolean done = false; boolean done = false;

View File

@ -26,7 +26,7 @@ import org.jivesoftware.smack.sasl.SASLMechanism.Failure;
import org.jivesoftware.smack.sasl.SASLMechanism.Success; import org.jivesoftware.smack.sasl.SASLMechanism.Success;
import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.util.PacketParserUtils;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -152,7 +152,7 @@ class PacketReader {
*/ */
private void resetParser() { private void resetParser() {
try { try {
parser = new MXParser(); parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(connection.reader); parser.setInput(connection.reader);
} }

View File

@ -31,7 +31,7 @@ import org.jivesoftware.smack.initializer.SmackInitializer;
import org.jivesoftware.smack.parsing.ExceptionThrowingCallback; import org.jivesoftware.smack.parsing.ExceptionThrowingCallback;
import org.jivesoftware.smack.parsing.ParsingExceptionCallback; import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smack.util.FileUtils;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -430,7 +430,7 @@ public final class SmackConfiguration {
} }
private static void readFile(InputStream cfgFileStream) throws Exception { private static void readFile(InputStream cfgFileStream) throws Exception {
XmlPullParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(cfgFileStream, "UTF-8"); parser.setInput(cfgFileStream, "UTF-8");
int eventType = parser.getEventType(); int eventType = parser.getEventType();

View File

@ -27,7 +27,7 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.PacketExtension;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
/** /**
@ -83,7 +83,7 @@ public class ProviderFileLoader implements ProviderLoader {
// Load processing providers. // Load processing providers.
try { try {
XmlPullParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(providerStream, "UTF-8"); parser.setInput(providerStream, "UTF-8");
int eventType = parser.getEventType(); int eventType = parser.getEventType();

View File

@ -35,7 +35,7 @@ import org.jivesoftware.smack.util.PacketParserUtils;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
/** /**
@ -315,7 +315,7 @@ public class RosterTest {
final String contactJID = "nurse@example.com"; final String contactJID = "nurse@example.com";
final Roster roster = connection.getRoster(); final Roster roster = connection.getRoster();
assertNotNull("Can't get the roster from the provided connection!", roster); assertNotNull("Can't get the roster from the provided connection!", roster);
final MXParser parser = new MXParser(); final XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append("<iq id=\"rostertest1\" type=\"set\" ") sb.append("<iq id=\"rostertest1\" type=\"set\" ")
@ -449,7 +449,7 @@ public class RosterTest {
final String contactJID = "nurse@example.com"; final String contactJID = "nurse@example.com";
final Roster roster = connection.getRoster(); final Roster roster = connection.getRoster();
assertNotNull("Can't get the roster from the provided connection!", roster); assertNotNull("Can't get the roster from the provided connection!", roster);
final MXParser parser = new MXParser(); final XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append("<iq id=\"rostertest2\" type=\"set\" ") sb.append("<iq id=\"rostertest2\" type=\"set\" ")

View File

@ -19,7 +19,7 @@ package org.jivesoftware.smack.test.util;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -40,8 +40,9 @@ final public class TestUtils {
} }
public static XmlPullParser getParser(String stanza, String startTag) { public static XmlPullParser getParser(String stanza, String startTag) {
XmlPullParser parser = new MXParser(); XmlPullParser parser;
try { try {
parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new StringReader(stanza)); parser.setInput(new StringReader(stanza));
boolean found = false; boolean found = false;

View File

@ -24,7 +24,7 @@ import java.util.List;
import org.jivesoftware.smack.SmackConfiguration; import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.initializer.SmackInitializer; import org.jivesoftware.smack.initializer.SmackInitializer;
import org.jivesoftware.smack.util.FileUtils; import org.jivesoftware.smack.util.FileUtils;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
public class ExtensionsStartupClasses implements SmackInitializer { public class ExtensionsStartupClasses implements SmackInitializer {
@ -41,7 +41,7 @@ public class ExtensionsStartupClasses implements SmackInitializer {
int eventType; int eventType;
try { try {
is = FileUtils.getStreamForUrl(EXTENSIONS_XML, null); is = FileUtils.getStreamForUrl(EXTENSIONS_XML, null);
parser = new MXParser(); parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(is, "UTF-8"); parser.setInput(is, "UTF-8");
eventType = parser.getEventType(); eventType = parser.getEventType();

View File

@ -34,7 +34,7 @@ import org.jivesoftware.smack.util.StringEncoder;
import org.jivesoftware.smackx.caps.EntityCapsManager; import org.jivesoftware.smackx.caps.EntityCapsManager;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo; import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.provider.DiscoverInfoProvider; import org.jivesoftware.smackx.disco.provider.DiscoverInfoProvider;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -159,7 +159,7 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache
Reader reader = new StringReader(fileContent); Reader reader = new StringReader(fileContent);
XmlPullParser parser; XmlPullParser parser;
try { try {
parser = new MXParser(); parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(reader); parser.setInput(reader);
} catch (XmlPullParserException xppe) { } catch (XmlPullParserException xppe) {

View File

@ -26,7 +26,7 @@ import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager.StanzaTyp
import org.jivesoftware.smackx.bytestreams.ibb.packet.Open; import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
import org.jivesoftware.smackx.bytestreams.ibb.provider.OpenIQProvider; import org.jivesoftware.smackx.bytestreams.ibb.provider.OpenIQProvider;
import org.junit.Test; import org.junit.Test;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -76,7 +76,7 @@ public class OpenIQProviderTest {
private XmlPullParser getParser(String control) throws XmlPullParserException, private XmlPullParser getParser(String control) throws XmlPullParserException,
IOException { IOException {
XmlPullParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(new StringReader(control)); parser.setInput(new StringReader(control));
while (true) { while (true) {
if (parser.next() == XmlPullParser.START_TAG if (parser.next() == XmlPullParser.START_TAG

View File

@ -33,7 +33,7 @@ import org.jivesoftware.smackx.delay.packet.DelayInformation;
import org.jivesoftware.smackx.delay.provider.DelayInfoProvider; import org.jivesoftware.smackx.delay.provider.DelayInfoProvider;
import org.jivesoftware.smackx.delay.provider.DelayInformationProvider; import org.jivesoftware.smackx.delay.provider.DelayInformationProvider;
import org.junit.Test; import org.junit.Test;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -237,7 +237,7 @@ public class DelayInformationTest {
private XmlPullParser getParser(String control, String startTag) private XmlPullParser getParser(String control, String startTag)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
XmlPullParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new StringReader(control)); parser.setInput(new StringReader(control));

View File

@ -32,7 +32,7 @@ import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -132,7 +132,7 @@ public class DeliveryReceiptTest {
private XmlPullParser getParser(String control, String startTag) private XmlPullParser getParser(String control, String startTag)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
XmlPullParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new StringReader(control)); parser.setInput(new StringReader(control));
while (true) { while (true) {

View File

@ -28,7 +28,7 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.jingle.JingleSession; import org.jivesoftware.smackx.jingle.JingleSession;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -138,7 +138,7 @@ public class STUNResolver extends TransportResolver {
int serverPort; int serverPort;
try { try {
XmlPullParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(stunConfigStream, "UTF-8"); parser.setInput(stunConfigStream, "UTF-8");

View File

@ -22,7 +22,7 @@ import java.io.StringReader;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider; import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.mxp1.MXParser; import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
/** /**
@ -179,7 +179,7 @@ public class Macros extends IQ {
public MacroGroup parseMacroGroups(String macros) throws Exception { public MacroGroup parseMacroGroups(String macros) throws Exception {
MacroGroup group = null; MacroGroup group = null;
XmlPullParser parser = new MXParser(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(new StringReader(macros)); parser.setInput(new StringReader(macros));
int eventType = parser.getEventType(); int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) { while (eventType != XmlPullParser.END_DOCUMENT) {