2003-08-18 22:21:00 +02:00
|
|
|
/**
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-08-18 22:21:00 +02:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:37:00 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-08-18 22:21:00 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-08-18 22:21:00 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-08-02 20:46:55 +02:00
|
|
|
*/
|
2003-08-18 22:21:00 +02:00
|
|
|
|
2014-02-15 11:35:08 +01:00
|
|
|
package org.jivesoftware.smackx.xroster.provider;
|
2003-08-02 20:46:55 +02:00
|
|
|
|
2014-10-07 21:15:20 +02:00
|
|
|
import java.io.IOException;
|
2003-08-26 18:05:57 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2019-02-15 23:21:30 +01:00
|
|
|
import org.jivesoftware.smack.packet.XmlEnvironment;
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.provider.ExtensionElementProvider;
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jivesoftware.smack.util.ParserUtils;
|
2019-05-06 22:06:13 +02:00
|
|
|
import org.jivesoftware.smack.xml.XmlPullParser;
|
|
|
|
import org.jivesoftware.smack.xml.XmlPullParserException;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-02-15 11:35:08 +01:00
|
|
|
import org.jivesoftware.smackx.xroster.RemoteRosterEntry;
|
|
|
|
import org.jivesoftware.smackx.xroster.packet.RosterExchange;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.Jid;
|
2003-08-02 20:46:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* The RosterExchangeProvider parses RosterExchange packets.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
2015-02-26 18:41:17 +01:00
|
|
|
public class RosterExchangeProvider extends ExtensionElementProvider<RosterExchange> {
|
2003-08-02 20:46:55 +02:00
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Parses a RosterExchange stanza (extension sub-packet).
|
2003-08-02 20:46:55 +02:00
|
|
|
*
|
|
|
|
* @param parser the XML parser, positioned at the starting element of the extension.
|
|
|
|
* @return a PacketExtension.
|
2019-10-30 12:02:36 +01:00
|
|
|
* @throws IOException if an I/O error occurred.
|
|
|
|
* @throws XmlPullParserException if an error in the XML parser occurred.
|
2003-08-02 20:46:55 +02:00
|
|
|
*/
|
2014-10-07 21:15:20 +02:00
|
|
|
@Override
|
2019-02-15 23:21:30 +01:00
|
|
|
public RosterExchange parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
|
2014-10-07 21:15:20 +02:00
|
|
|
throws XmlPullParserException, IOException {
|
2003-08-02 20:46:55 +02:00
|
|
|
RosterExchange rosterExchange = new RosterExchange();
|
|
|
|
boolean done = false;
|
2017-12-13 23:10:11 +01:00
|
|
|
RemoteRosterEntry remoteRosterEntry;
|
2015-02-14 17:15:02 +01:00
|
|
|
Jid jid = null;
|
2019-05-07 21:24:00 +02:00
|
|
|
String name = "";
|
|
|
|
ArrayList<String> groupsName = new ArrayList<>();
|
2003-08-02 20:46:55 +02:00
|
|
|
while (!done) {
|
2019-05-06 22:06:13 +02:00
|
|
|
XmlPullParser.Event eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.Event.START_ELEMENT) {
|
2003-08-02 20:46:55 +02:00
|
|
|
if (parser.getName().equals("item")) {
|
2019-05-07 21:24:00 +02:00
|
|
|
// Reset this variable since they are optional for each item
|
|
|
|
groupsName = new ArrayList<>();
|
|
|
|
// Initialize the variables from the parsed XML
|
2015-02-14 17:15:02 +01:00
|
|
|
jid = ParserUtils.getJidAttribute(parser);
|
2003-08-26 18:05:57 +02:00
|
|
|
name = parser.getAttributeValue("", "name");
|
2003-08-02 20:46:55 +02:00
|
|
|
}
|
|
|
|
if (parser.getName().equals("group")) {
|
2019-05-07 21:24:00 +02:00
|
|
|
groupsName.add(parser.nextText());
|
2003-08-02 20:46:55 +02:00
|
|
|
}
|
2019-05-06 22:06:13 +02:00
|
|
|
} else if (eventType == XmlPullParser.Event.END_ELEMENT) {
|
2003-08-02 20:46:55 +02:00
|
|
|
if (parser.getName().equals("item")) {
|
2019-05-07 21:24:00 +02:00
|
|
|
// Create packet.
|
|
|
|
remoteRosterEntry = new RemoteRosterEntry(jid, name, groupsName.toArray(new String[groupsName.size()]));
|
2003-08-24 18:05:45 +02:00
|
|
|
rosterExchange.addRosterEntry(remoteRosterEntry);
|
2003-08-02 20:46:55 +02:00
|
|
|
}
|
|
|
|
if (parser.getName().equals("x")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rosterExchange;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|