Class IqProvider<I extends IQ>

java.lang.Object
org.jivesoftware.smack.provider.AbstractProvider<I>
org.jivesoftware.smack.provider.IqProvider<I>
Type Parameters:
I - the IQ that is parsed by implementations.
Direct Known Subclasses:
BindIQProvider, IntrospectionProvider.IQIntrospectionProvider, LegacyIQProvider

public abstract class IqProvider<I extends IQ> extends AbstractProvider<I>
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);
   }
 }