Search in sources :

Example 71 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class ItemProvider method parse.

@Override
public Item parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    String id = parser.getAttributeValue(null, "id");
    String node = parser.getAttributeValue(null, "node");
    String xmlns = parser.getNamespace();
    ItemNamespace itemNamespace = ItemNamespace.fromXmlns(xmlns);
    XmlPullParser.TagEvent event = parser.nextTag();
    switch(event) {
        case START_ELEMENT:
            String payloadElemName = parser.getName();
            String payloadNS = parser.getNamespace();
            final ExtensionElementProvider<ExtensionElement> extensionProvider = ProviderManager.getExtensionProvider(payloadElemName, payloadNS);
            if (extensionProvider == null) {
                // TODO: Should we use StandardExtensionElement in this case? And probably remove SimplePayload all together.
                CharSequence payloadText = PacketParserUtils.parseElement(parser, true);
                return new PayloadItem<>(itemNamespace, id, node, new SimplePayload(payloadText.toString()));
            } else {
                return new PayloadItem<>(itemNamespace, id, node, extensionProvider.parse(parser));
            }
        case END_ELEMENT:
            return new Item(itemNamespace, id, node);
        default:
            throw new AssertionError("unknown: " + event);
    }
}
Also used : PayloadItem(org.jivesoftware.smackx.pubsub.PayloadItem) PayloadItem(org.jivesoftware.smackx.pubsub.PayloadItem) Item(org.jivesoftware.smackx.pubsub.Item) ItemNamespace(org.jivesoftware.smackx.pubsub.Item.ItemNamespace) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) SimplePayload(org.jivesoftware.smackx.pubsub.SimplePayload)

Example 72 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class PrivacyProviderTest method testFull.

/**
 * Check the parser with an xml with all kind of stanzas.
 * To create the xml string based from an xml file, replace:\n with:	"\n  + "
 */
public void testFull() {
    // Make the XML to test
    String xml = "" + "  <iq type='result' id='getlist2' to='romeo@example.net/orchard'>	" + "  <query xmlns='jabber:iq:privacy'>	" + "    <active name='testFilter'/>	" + "    <default name='testSubscription'/>	" + "    <list name='testFilter'>	" + "      <item type='jid'	" + "            value='tybalt@example.com'	" + "            action='deny'	" + "            order='1'/>	" + "      <item action='allow' order='2'>	" + "        <message/>	" + "		<presence-in/>	" + "		<presence-out/>	" + "		<iq/>	" + "		</item>	" + "    </list>	" + "    <list name='testSubscription'>	" + "      <item type='subscription'	" + "            value='both'	" + "            action='allow'	" + "            order='10'/>	" + "      <item type='subscription'	" + "            value='to'	" + "            action='allow'	" + "            order='11'/>	" + "      <item type='subscription'	" + "            value='from'	" + "            action='allow'	" + "            order='12'/>	" + "      <item type='subscription'	" + "            value='none'	" + "            action='deny'	" + "            order='5'>	" + "        <message/>	" + "      </item>	" + "      <item action='deny' order='15'/>	" + "    </list>	" + "    <list name='testJID'>	" + "      <item type='jid'	" + "            value='juliet@example.com'	" + "            action='allow'	" + "            order='6'/>	" + "      <item type='jid'	" + "            value='benvolio@example.org/palm'	" + "            action='deny'	" + "            order='7'/>	" + "      <item type='jid'	" + "            action='allow'	" + "            order='42'/>	" + "      <item action='deny' order='666'/>	" + "    </list>	" + "    <list name='testGroup'>	" + "	      <item type='group'	" + "            value='Enemies'	" + "            action='deny'	" + "            order='4'>	" + "		   <message/>	" + "		</item>	" + "      <item action='deny' order='666'/>	" + "    </list>	" + "    <list name='testEmpty'/>	" + "  </query>	" + "  <error type='cancel'>	" + "    <item-not-found	" + "        xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>	" + "  </error>	" + "</iq>	";
    try {
        // Create the xml parser
        XmlPullParser parser = getParserFromXML(xml);
        // Create a packet from the xml
        Privacy packet = (Privacy) (new PrivacyProvider()).parse(parser);
        // check if it exist
        assertNotNull(packet);
        // assertEquals(xml, packet.getChildElementXML());
        // check the default and active names
        assertEquals("testFilter", packet.getActiveName());
        assertEquals("testSubscription", packet.getDefaultName());
        // check the list
        assertEquals(2, packet.getPrivacyList("testFilter").size());
        assertEquals(5, packet.getPrivacyList("testSubscription").size());
        assertEquals(4, packet.getPrivacyList("testJID").size());
        assertEquals(2, packet.getPrivacyList("testGroup").size());
        assertEquals(0, packet.getPrivacyList("testEmpty").size());
        // check each privacy item
        PrivacyItem item = packet.getItem("testGroup", 4);
        assertEquals("Enemies", item.getValue());
        assertEquals(PrivacyItem.Type.group, item.getType());
        assertEquals(false, item.isAllow());
        assertEquals(true, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(false, item.isFilterEverything());
        item = packet.getItem("testFilter", 1);
        assertEquals("tybalt@example.com", item.getValue());
        assertEquals(PrivacyItem.Type.jid, item.getType());
        assertEquals(false, item.isAllow());
        assertEquals(false, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(true, item.isFilterEverything());
        item = packet.getItem("testFilter", 2);
        assertEquals(null, item.getValue());
        assertEquals(null, item.getType());
        assertEquals(true, item.isAllow());
        assertEquals(true, item.isFilterMessage());
        assertEquals(true, item.isFilterIQ());
        assertEquals(true, item.isFilterPresence_in());
        assertEquals(true, item.isFilterPresence_out());
        assertEquals(false, item.isFilterEverything());
        // TEST THE testSubscription LIST
        item = packet.getItem("testSubscription", 10);
        assertEquals("both", item.getValue());
        assertEquals(PrivacyItem.Type.subscription, item.getType());
        assertEquals(true, item.isAllow());
        assertEquals(false, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(true, item.isFilterEverything());
        item = packet.getItem("testSubscription", 11);
        assertEquals("to", item.getValue());
        assertEquals(PrivacyItem.Type.subscription, item.getType());
        assertEquals(true, item.isAllow());
        assertEquals(false, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(true, item.isFilterEverything());
        item = packet.getItem("testSubscription", 12);
        assertEquals("from", item.getValue());
        assertEquals(PrivacyItem.Type.subscription, item.getType());
        assertEquals(true, item.isAllow());
        assertEquals(false, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(true, item.isFilterEverything());
        item = packet.getItem("testSubscription", 5);
        assertEquals("none", item.getValue());
        assertEquals(PrivacyItem.Type.subscription, item.getType());
        assertEquals(false, item.isAllow());
        assertEquals(true, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(false, item.isFilterEverything());
        item = packet.getItem("testSubscription", 15);
        assertEquals(null, item.getValue());
        assertEquals(null, item.getType());
        assertEquals(false, item.isAllow());
        assertEquals(false, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(true, item.isFilterEverything());
        // TEST THE testJID LIST
        item = packet.getItem("testJID", 6);
        assertEquals("juliet@example.com", item.getValue());
        assertEquals(PrivacyItem.Type.jid, item.getType());
        assertEquals(true, item.isAllow());
        assertEquals(false, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(true, item.isFilterEverything());
        item = packet.getItem("testJID", 7);
        assertEquals("benvolio@example.org/palm", item.getValue());
        assertEquals(PrivacyItem.Type.jid, item.getType());
        assertEquals(false, item.isAllow());
        assertEquals(false, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(true, item.isFilterEverything());
        item = packet.getItem("testJID", 42);
        assertEquals(null, item.getValue());
        assertEquals(PrivacyItem.Type.jid, item.getType());
        assertEquals(true, item.isAllow());
        assertEquals(false, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(true, item.isFilterEverything());
        item = packet.getItem("testJID", 666);
        assertEquals(null, item.getValue());
        assertEquals(null, item.getType());
        assertEquals(false, item.isAllow());
        assertEquals(false, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(true, item.isFilterEverything());
        // TEST THE testGroup LIST
        item = packet.getItem("testGroup", 4);
        assertEquals("Enemies", item.getValue());
        assertEquals(PrivacyItem.Type.group, item.getType());
        assertEquals(false, item.isAllow());
        assertEquals(true, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(false, item.isFilterEverything());
        item = packet.getItem("testGroup", 666);
        assertEquals(null, item.getValue());
        assertEquals(null, item.getType());
        assertEquals(false, item.isAllow());
        assertEquals(false, item.isFilterMessage());
        assertEquals(false, item.isFilterIQ());
        assertEquals(false, item.isFilterPresence_in());
        assertEquals(false, item.isFilterPresence_out());
        assertEquals(true, item.isFilterEverything());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : PrivacyProvider(org.jivesoftware.smack.provider.PrivacyProvider) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException)

Example 73 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class PrivacyProviderTest method testEmptyLists.

/**
 * Check the parser with an xml with empty lists. It includes the active,
 * default and special list.
 * To create the xml string based from an xml file, replace:\n with:	"\n  + "
 */
public void testEmptyLists() {
    // Make the XML to test
    String xml = "" + "  <iq type='result' id='getlist1' to='romeo@example.net/orchard'>	" + "  <query xmlns='jabber:iq:privacy'>	" + "    <active/>	" + "    <default name='public'/>	" + "    <list name='public'/>	" + "    <list name='private'/>	" + "    <list name='special'/>	" + "  </query>	" + " </iq>	";
    try {
        // Create the xml parser
        XmlPullParser parser = getParserFromXML(xml);
        // Create a packet from the xml
        Privacy packet = (Privacy) (new PrivacyProvider()).parse(parser);
        assertNotNull(packet);
        assertNotNull(packet.getChildElementXML());
        assertEquals("public", packet.getDefaultName());
        assertEquals(null, packet.getActiveName());
        assertEquals(0, packet.getPrivacyList("public").size());
        assertEquals(0, packet.getPrivacyList("private").size());
        assertEquals(0, packet.getPrivacyList("special").size());
        assertEquals(true, packet.isDeclineActiveList());
        assertEquals(false, packet.isDeclineDefaultList());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : PrivacyProvider(org.jivesoftware.smack.provider.PrivacyProvider) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException)

Example 74 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class ChecksumProvider method parse.

@Override
public Checksum parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    JingleContent.Creator creator = null;
    String creatorString = parser.getAttributeValue(null, Checksum.ATTR_CREATOR);
    if (creatorString != null) {
        creator = JingleContent.Creator.valueOf(creatorString);
    }
    String name = parser.getAttributeValue(null, Checksum.ATTR_NAME);
    JingleFileTransferChild.Builder cb = JingleFileTransferChild.getBuilder();
    HashElement hashElement = null;
    Range range = null;
    boolean go = true;
    while (go) {
        XmlPullParser.TagEvent tag = parser.nextTag();
        String n = parser.getText();
        switch(tag) {
            case START_ELEMENT:
                switch(n) {
                    case HashElement.ELEMENT:
                        hashElement = new HashElementProvider().parse(parser);
                        break;
                    case Range.ELEMENT:
                        String offset = parser.getAttributeValue(null, Range.ATTR_OFFSET);
                        String length = parser.getAttributeValue(null, Range.ATTR_LENGTH);
                        int o = offset == null ? 0 : Integer.parseInt(offset);
                        int l = length == null ? -1 : Integer.parseInt(length);
                        range = new Range(o, l);
                }
                break;
            case END_ELEMENT:
                switch(n) {
                    case Range.ELEMENT:
                        if (hashElement != null && range != null) {
                            range = new Range(range.getOffset(), range.getLength(), hashElement);
                            hashElement = null;
                        }
                        break;
                    case JingleFileTransferChild.ELEMENT:
                        if (hashElement != null) {
                            cb.setHash(hashElement);
                        }
                        if (range != null) {
                            cb.setRange(range);
                        }
                        go = false;
                }
                break;
        }
    }
    return new Checksum(creator, name, cb.build());
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) HashElementProvider(org.jivesoftware.smackx.hashes.provider.HashElementProvider) Range(org.jivesoftware.smackx.jingle_filetransfer.element.Range) JingleFileTransferChild(org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild) Checksum(org.jivesoftware.smackx.jingle_filetransfer.element.Checksum) HashElement(org.jivesoftware.smackx.hashes.element.HashElement) JingleContent(org.jivesoftware.smackx.jingle.element.JingleContent)

Example 75 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class IoTRegisterProvider method parse.

@Override
public IoTRegister parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
    boolean selfOwned = ParserUtils.getBooleanAttribute(parser, "selfOwned", false);
    NodeInfo nodeInfo = NodeInfoParser.parse(parser);
    List<Tag> tags = new ArrayList<>();
    while (parser.getDepth() != initialDepth) {
        XmlPullParser.Event event = parser.next();
        if (event != XmlPullParser.Event.START_ELEMENT) {
            continue;
        }
        final String element = parser.getName();
        Tag.Type type = null;
        switch(element) {
            case "str":
                type = Tag.Type.str;
                break;
            case "num":
                type = Tag.Type.num;
                break;
        }
        if (type == null) {
            continue;
        }
        String name = parser.getAttributeValue(null, "name");
        String value = parser.getAttributeValue(null, "value");
        tags.add(new Tag(name, type, value));
    }
    return new IoTRegister(tags, nodeInfo, selfOwned);
}
Also used : NodeInfo(org.jivesoftware.smackx.iot.element.NodeInfo) ArrayList(java.util.ArrayList) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) Tag(org.jivesoftware.smackx.iot.discovery.element.Tag) IoTRegister(org.jivesoftware.smackx.iot.discovery.element.IoTRegister)

Aggregations

XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)139 Test (org.junit.jupiter.api.Test)69 Message (org.jivesoftware.smack.packet.Message)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 Date (java.util.Date)12 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)11 Jid (org.jxmpp.jid.Jid)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 XmlPullParserException (org.jivesoftware.smack.xml.XmlPullParserException)10 EnumSource (org.junit.jupiter.params.provider.EnumSource)9 IQ (org.jivesoftware.smack.packet.IQ)8 Test (org.junit.Test)8 MarkupElement (org.jivesoftware.smackx.message_markup.element.MarkupElement)7 MarkupElementProvider (org.jivesoftware.smackx.message_markup.provider.MarkupElementProvider)7 XmlElement (org.jivesoftware.smack.packet.XmlElement)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 NamedElement (org.jivesoftware.smack.packet.NamedElement)4 HttpOverXmppResp (org.jivesoftware.smackx.hoxt.packet.HttpOverXmppResp)4