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
public abstract class IqProvider<I extends IQ> extends AbstractProvider<I>
An abstract class for parsing customIQpackets. Each IqProvider must be registered with theProviderManagerfor 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.
The custom IQ provider may look like the follows<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>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 Constructor Description IqProvider()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Iparse(org.jivesoftware.smack.xml.XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment)Iparse(org.jivesoftware.smack.xml.XmlPullParser parser, IqData iqCommon)Iparse(org.jivesoftware.smack.xml.XmlPullParser parser, IqData iqData, XmlEnvironment outerXmlEnvironment)-
Methods inherited from class org.jivesoftware.smack.provider.AbstractProvider
getElementClass, wrapExceptions
-
-
-
-
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.XmlPullParserExceptionjava.io.IOExceptionSmackParsingException
-
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.XmlPullParserExceptionjava.io.IOExceptionSmackParsingException
-
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.XmlPullParserExceptionjava.io.IOExceptionSmackParsingExceptionjava.text.ParseException
-
-