Class IqProvider<I extends IQ>

  • 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);
       }
     }
     
    • Constructor Detail

      • IqProvider

        public IqProvider()
    • Method Detail

      • parse

        public final I parse​(org.jivesoftware.smack.xml.XmlPullParser parser,
                             IqData iqCommon)
                      throws org.jivesoftware.smack.xml.XmlPullParserException,
                             java.io.IOException,
                             SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • parse

        public final I parse​(org.jivesoftware.smack.xml.XmlPullParser parser,
                             IqData iqData,
                             XmlEnvironment outerXmlEnvironment)
                      throws org.jivesoftware.smack.xml.XmlPullParserException,
                             java.io.IOException,
                             SmackParsingException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
      • parse

        public abstract I parse​(org.jivesoftware.smack.xml.XmlPullParser parser,
                                int initialDepth,
                                IqData iqData,
                                XmlEnvironment xmlEnvironment)
                         throws org.jivesoftware.smack.xml.XmlPullParserException,
                                java.io.IOException,
                                SmackParsingException,
                                java.text.ParseException
        Throws:
        org.jivesoftware.smack.xml.XmlPullParserException
        java.io.IOException
        SmackParsingException
        java.text.ParseException