Package org.jivesoftware.smack.provider
Class IqProvider<I extends IQ>
java.lang.Object
org.jivesoftware.smack.provider.AbstractProvider<I>
org.jivesoftware.smack.provider.IqProvider<I>
- Type Parameters:
I- theIQthat is parsed by implementations.
- Direct Known Subclasses:
BindIQProvider,IntrospectionProvider.IQIntrospectionProvider,LegacyIQProvider
An abstract class for parsing custom
IQ packets. Each IqProvider must be registered with the ProviderManager for it to be used. Every implementation of this abstract class must have a public,
no-argument constructor.
Custom IQ Provider Example
Let us assume you want to write a provider for a new, unsupported IQ in Smack.
<iq type='set' from='juliet@capulet.example/balcony' to='romeo@montage.example'>
<myiq xmlns='example:iq:foo' token='secret'>
<user age='42'>John Doe</user>
<location>New York</location>
</myiq>
</iq>
The custom IQ provider may look like the follows
public class MyIQProvider extends IQProvider<MyIQ> {
{@literal @}Override
public MyIQ parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
// Define the data we are trying to collect with sane defaults
int age = -1;
String user = null;
String location = null;
// Start parsing loop
outerloop: while(true) {
XmlPullParser.Event eventType = parser.next();
switch(eventType) {
case START_ELEMENT:
String elementName = parser.getName();
switch (elementName) {
case "user":
age = ParserUtils.getIntegerAttribute(parser, "age");
user = parser.nextText();
break;
case "location"
location = parser.nextText();
break;
}
break;
case END_ELEMENT:
// Abort condition: if the are on a end tag (closing element) of the same depth
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
// Construct the IQ instance at the end of parsing, when all data has been collected
return new MyIQ(user, age, location);
}
}
-
Nested Class Summary
Nested classes/interfaces inherited from class org.jivesoftware.smack.provider.AbstractProvider
AbstractProvider.NumberFormatParseException, AbstractProvider.TextParseException, AbstractProvider.WrappableParser<E> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract Iparse(org.jivesoftware.smack.xml.XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) final Ifinal Iparse(org.jivesoftware.smack.xml.XmlPullParser parser, IqData iqData, XmlEnvironment outerXmlEnvironment) Methods inherited from class org.jivesoftware.smack.provider.AbstractProvider
getElementClass, toUrl, wrapExceptions
-
Constructor Details
-
IqProvider
public IqProvider()
-
-
Method Details
-
parse
public final I parse(org.jivesoftware.smack.xml.XmlPullParser parser, IqData iqCommon) throws org.jivesoftware.smack.xml.XmlPullParserException, IOException, SmackParsingException - Throws:
org.jivesoftware.smack.xml.XmlPullParserExceptionIOExceptionSmackParsingException
-
parse
public final I parse(org.jivesoftware.smack.xml.XmlPullParser parser, IqData iqData, XmlEnvironment outerXmlEnvironment) throws org.jivesoftware.smack.xml.XmlPullParserException, IOException, SmackParsingException - Throws:
org.jivesoftware.smack.xml.XmlPullParserExceptionIOExceptionSmackParsingException
-
parse
public abstract I parse(org.jivesoftware.smack.xml.XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws org.jivesoftware.smack.xml.XmlPullParserException, IOException, SmackParsingException, ParseException - Throws:
org.jivesoftware.smack.xml.XmlPullParserExceptionIOExceptionSmackParsingExceptionParseException
-