aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/pubsub/PubSubPacketParser.java
blob: c8df0d5ea8ce018f711a377e5ba80c04b201745b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package de.thedevstack.conversationsplus.xmpp.pubsub;

import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;

/**
 * Parses the IQ Packets for handling pubsub
 * as defined in XEP-0060.
 * @see <a href="http://xmpp.org/extensions/xep-0060.html">http://xmpp.org/extensions/xep-0060.html</a>
 */
public final class PubSubPacketParser {
    /**
     * Finds the pubsub element within an IQ packet.
     * @param packet the retrieved IQ packet
     * @return the {@value PubSubPacket#ELEMENT_NAME} Element or <code>null</code> if the IqPacket is null or the IQ packet does not contain an pubsub element.
     */
    public static Element findPubSubPacket(IqPacket packet){
        if (null == packet) {
            return null;
        }
        return packet.findChild(PubSubPacket.ELEMENT_NAME, PubSubPacket.NAMESPACE);
    }

    /**
     * Finds the "items" element within an pubSubPacket element.
     * @param pubSubPacket the pubSubPacket element
     * @return the  items Element or <code>null</code> if the pubSubPacket is null or the pubSubPacket does not contain an items element.
     */
    public static Element findItemsFromPubSubElement(Element pubSubPacket) {
        if (null == pubSubPacket) {
            return null;
        }
        return pubSubPacket.findChild("items");
    }

    /**
     * Finds the "items" element within an pubSubPacket element.
     * @param packet the IqPacket element
     * @return the  items Element or <code>null</code> if the IqPacket is null or the IQ packet does not contain an pubsub element with an items element.
     */
    public static Element findItems(IqPacket packet) {
        return PubSubPacketParser.findItemsFromPubSubElement(PubSubPacketParser.findPubSubPacket(packet));
    }

    /**
     * Utility class - avoid instantiation
     */
    private PubSubPacketParser() {
        // Avoid instantiation
    }
}