1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-20 17:47:57 +01:00

Couple of fixes in providers.md

Mixed tabs and spaces fixed and made list of extension providers for
standard packets nested.
This commit is contained in:
Andrey Starodubtsev 2016-01-18 21:22:34 +03:00 committed by Florian Schmaus
parent 35ba917728
commit f8d04262c3

View file

@ -13,9 +13,11 @@ There are two types of providers:
* `IQProvider` -- parses IQ requests into Java objects. * `IQProvider` -- parses IQ requests into Java objects.
* `Extension Provider` -- parses XML sub-documents attached to packets into PacketExtension instances. By default, Smack only knows how to process a few standard packets and sub-packets that are in a few namespaces such as: * `Extension Provider` -- parses XML sub-documents attached to packets into PacketExtension instances. By default, Smack only knows how to process a few standard packets and sub-packets that are in a few namespaces such as:
* jabber:iq:auth * jabber:iq:auth
* jabber:iq:roster * jabber:iq:roster
* jabber:iq:register There are many more IQ types and extensions that are part of XMPP standards, and of course an endless number that can be added as custom extensions. To support this, an extensible parsing mechanism is provided via Smack and user build providers. * jabber:iq:register
There are many more IQ types and extensions that are part of XMPP standards, and of course an endless number that can be added as custom extensions. To support this, an extensible parsing mechanism is provided via Smack and user build providers.
Whenever a packet extension is found in a packet, parsing will be Whenever a packet extension is found in a packet, parsing will be
passed to the correct provider. Each provider must implement the passed to the correct provider. Each provider must implement the
@ -140,7 +142,7 @@ _Custom IQ_
<iq type='set' from='juliet@capulet.example/balcony' to='romeo@montage.example'> <iq type='set' from='juliet@capulet.example/balcony' to='romeo@montage.example'>
<myiq xmlns='example:iq:foo' token='secret'> <myiq xmlns='example:iq:foo' token='secret'>
<user age='42'>John Doe</user> <user age='42'>John Doe</user>
<location>New York</location> <location>New York</location>
</myiq> </myiq>
</iq> </iq>
``` ```
@ -154,36 +156,36 @@ public class MyIQProvider extends IQProvider<MyIQ> {
public MyIQ parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { public MyIQ parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
// Define the data we are trying to collect with sane defaults // Define the data we are trying to collect with sane defaults
int age = -1; int age = -1;
String user = null; String user = null;
String location = null; String location = null;
// Start parsing loop // Start parsing loop
outerloop: while(true) { outerloop: while(true) {
int eventType = parser.next(); int eventType = parser.next();
switch(eventType) { switch(eventType) {
case XmlPullParser.START_TAG: case XmlPullParser.START_TAG:
String elementName = parser.getName(); String elementName = parser.getName();
switch (elementName) { switch (elementName) {
case "user": case "user":
age = ParserUtils.getIntegerAttribute(parser, "age"); age = ParserUtils.getIntegerAttribute(parser, "age");
user = parser.nextText(); user = parser.nextText();
break; break;
case "location" case "location"
location = parser.nextText(); location = parser.nextText();
break; break;
} }
break; break;
case XmlPullParser.END_TAG: case XmlPullParser.END_TAG:
// Abort condition: if the are on a end tag (closing element) of the same depth // Abort condition: if the are on a end tag (closing element) of the same depth
if (parser.getDepth() == initialDepth) { if (parser.getDepth() == initialDepth) {
break outerloop; break outerloop;
} }
break; break;
} }
} }
// Construct the IQ instance at the end of parsing, when all data has been collected // Construct the IQ instance at the end of parsing, when all data has been collected
return new MyIQ(user, age, location); return new MyIQ(user, age, location);
} }
} }
``` ```