use of org.jivesoftware.smackx.xroster.RemoteRosterEntry in project Smack by igniterealtime.
the class RosterExchangeProvider method parse.
/**
* Parses a RosterExchange stanza(/packet) (extension sub-packet).
*
* @param parser the XML parser, positioned at the starting element of the extension.
* @return a PacketExtension.
* @throws IOException
* @throws XmlPullParserException
*/
@Override
public RosterExchange parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
// CHECKSTYLE:OFF
RosterExchange rosterExchange = new RosterExchange();
boolean done = false;
RemoteRosterEntry remoteRosterEntry = null;
Jid jid = null;
String name = "";
ArrayList<String> groupsName = new ArrayList<String>();
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("item")) {
// Reset this variable since they are optional for each item
groupsName = new ArrayList<String>();
// Initialize the variables from the parsed XML
jid = ParserUtils.getJidAttribute(parser);
name = parser.getAttributeValue("", "name");
}
if (parser.getName().equals("group")) {
groupsName.add(parser.nextText());
}
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("item")) {
// Create packet.
remoteRosterEntry = new RemoteRosterEntry(jid, name, groupsName.toArray(new String[groupsName.size()]));
rosterExchange.addRosterEntry(remoteRosterEntry);
}
if (parser.getName().equals("x")) {
done = true;
}
}
}
// CHECKSTYLE:ON
return rosterExchange;
}
use of org.jivesoftware.smackx.xroster.RemoteRosterEntry in project Smack by igniterealtime.
the class RosterExchange method addRosterEntry.
/**
* Adds a roster entry to the packet.
*
* @param rosterEntry a roster entry to add.
*/
public void addRosterEntry(RosterEntry rosterEntry) {
// Obtain a String[] from the roster entry groups name
List<String> groupNamesList = new ArrayList<String>();
String[] groupNames;
for (RosterGroup group : rosterEntry.getGroups()) {
groupNamesList.add(group.getName());
}
groupNames = groupNamesList.toArray(new String[groupNamesList.size()]);
// Create a new Entry based on the rosterEntry and add it to the packet
RemoteRosterEntry remoteRosterEntry = new RemoteRosterEntry(rosterEntry.getJid(), rosterEntry.getName(), groupNames);
addRosterEntry(remoteRosterEntry);
}
use of org.jivesoftware.smackx.xroster.RemoteRosterEntry in project Smack by igniterealtime.
the class RosterExchange method toXML.
/**
* Returns the XML representation of a Roster Item Exchange according the specification.
*
* Usually the XML representation will be inside of a Message XML representation like
* in the following example:
* <pre>
* <message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack">
* <subject>Any subject you want</subject>
* <body>This message contains roster items.</body>
* <x xmlns="jabber:x:roster">
* <item jid="gato1@gato.home"/>
* <item jid="gato2@gato.home"/>
* </x>
* </message>
* </pre>
*
*/
@Override
public String toXML() {
StringBuilder buf = new StringBuilder();
buf.append('<').append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">");
// Loop through all roster entries and append them to the string buffer
for (Iterator<RemoteRosterEntry> i = getRosterEntries(); i.hasNext(); ) {
RemoteRosterEntry remoteRosterEntry = i.next();
buf.append(remoteRosterEntry.toXML());
}
buf.append("</").append(getElementName()).append('>');
return buf.toString();
}
Aggregations