Search in sources :

Example 1 with RemoteRosterEntry

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;
}
Also used : RosterExchange(org.jivesoftware.smackx.xroster.packet.RosterExchange) Jid(org.jxmpp.jid.Jid) ArrayList(java.util.ArrayList) RemoteRosterEntry(org.jivesoftware.smackx.xroster.RemoteRosterEntry)

Example 2 with RemoteRosterEntry

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);
}
Also used : ArrayList(java.util.ArrayList) RosterGroup(org.jivesoftware.smack.roster.RosterGroup) RemoteRosterEntry(org.jivesoftware.smackx.xroster.RemoteRosterEntry)

Example 3 with 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>
     * &lt;message id="MlIpV-4" to="gato1@gato.home" from="gato3@gato.home/Smack"&gt;
     *     &lt;subject&gt;Any subject you want&lt;/subject&gt;
     *     &lt;body&gt;This message contains roster items.&lt;/body&gt;
     *     &lt;x xmlns="jabber:x:roster"&gt;
     *         &lt;item jid="gato1@gato.home"/&gt;
     *         &lt;item jid="gato2@gato.home"/&gt;
     *     &lt;/x&gt;
     * &lt;/message&gt;
     * </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();
}
Also used : RemoteRosterEntry(org.jivesoftware.smackx.xroster.RemoteRosterEntry)

Aggregations

RemoteRosterEntry (org.jivesoftware.smackx.xroster.RemoteRosterEntry)3 ArrayList (java.util.ArrayList)2 RosterGroup (org.jivesoftware.smack.roster.RosterGroup)1 RosterExchange (org.jivesoftware.smackx.xroster.packet.RosterExchange)1 Jid (org.jxmpp.jid.Jid)1