Search in sources :

Example 11 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.

the class ItemValidationTest method parseComplexItem.

@Test
public void parseComplexItem() throws Exception {
    String itemContent = "<entry xmlns='http://www.w3.org/2005/Atom'>" + "<title>Soliloquy</title>" + "<summary>" + "To be, or not to be: that is the question:" + "Whether 'tis nobler in the mind to suffer" + "The slings and arrows of outrageous fortune," + "Or to take arms against a sea of troubles," + "And by opposing end them?" + "</summary>" + "<link rel='alternate' type='text/html' href='http://denmark.lit/2003/12/13/atom03'/>" + "<id>tag:denmark.lit,2003:entry-32397</id>" + "<published>2003-12-13T18:30:02Z</published>" + "<updated>2003-12-13T18:30:02Z</updated>" + "</entry>";
    XmlPullParser parser = PacketParserUtils.getParserFor("<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' >" + itemContent + "</item>" + "</items>" + "</event>" + "</message>");
    Stanza message = PacketParserUtils.parseMessage(parser);
    XmlElement eventExt = message.getExtension(PubSubNamespace.event.getXmlns());
    EventElement event = (EventElement) eventExt;
    NamedElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0);
    assertTrue(itemExt instanceof PayloadItem<?>);
    PayloadItem<?> item = (PayloadItem<?>) itemExt;
    assertEquals("testid1", item.getId());
    assertTrue(item.getPayload() instanceof SimplePayload);
    SimplePayload payload = (SimplePayload) item.getPayload();
    assertEquals("entry", payload.getElementName());
    assertEquals("http://www.w3.org/2005/Atom", payload.getNamespace());
    assertXmlSimilar(itemContent, payload.toXML().toString());
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlElement(org.jivesoftware.smack.packet.XmlElement) NamedElement(org.jivesoftware.smack.packet.NamedElement) Test(org.junit.jupiter.api.Test)

Example 12 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.

the class ItemValidationTest method parseBasicItem.

@Test
public void parseBasicItem() throws Exception {
    XmlPullParser parser = PacketParserUtils.getParserFor("<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' />" + "</items>" + "</event>" + "</message>");
    Stanza message = PacketParserUtils.parseMessage(parser);
    XmlElement eventExt = message.getExtension(PubSubNamespace.event.getXmlns());
    assertTrue(eventExt instanceof EventElement);
    EventElement event = (EventElement) eventExt;
    assertEquals(EventElementType.items, event.getEventType());
    assertEquals(1, event.getExtensions().size());
    assertTrue(event.getExtensions().get(0) instanceof ItemsExtension);
    assertEquals(1, ((ItemsExtension) event.getExtensions().get(0)).items.size());
    NamedElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0);
    assertTrue(itemExt instanceof Item);
    assertEquals("testid1", ((Item) itemExt).getId());
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlElement(org.jivesoftware.smack.packet.XmlElement) NamedElement(org.jivesoftware.smack.packet.NamedElement) Test(org.junit.jupiter.api.Test)

Example 13 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.

the class OpenPgpContentElement method addCommonXml.

protected void addCommonXml(XmlStringBuilder xml) {
    for (Jid toJid : to != null ? to : Collections.<Jid>emptySet()) {
        xml.halfOpenElement(ELEM_TO).attribute(ATTR_JID, toJid).closeEmptyElement();
    }
    ensureTimestampStringSet();
    xml.halfOpenElement(ELEM_TIME).attribute(ATTR_STAMP, timestampString).closeEmptyElement();
    xml.openElement(ELEM_PAYLOAD);
    for (XmlElement element : payload.values()) {
        xml.append(element.toXML(getNamespace()));
    }
    xml.closeElement(ELEM_PAYLOAD);
}
Also used : Jid(org.jxmpp.jid.Jid) XmlElement(org.jivesoftware.smack.packet.XmlElement)

Example 14 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.

the class ReferenceProvider method parse.

@Override
public ReferenceElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    Integer begin = ParserUtils.getIntegerAttribute(parser, ReferenceElement.ATTR_BEGIN);
    Integer end = ParserUtils.getIntegerAttribute(parser, ReferenceElement.ATTR_END);
    String typeString = parser.getAttributeValue(null, ReferenceElement.ATTR_TYPE);
    ReferenceElement.Type type = ReferenceElement.Type.valueOf(typeString);
    String anchor = parser.getAttributeValue(null, ReferenceElement.ATTR_ANCHOR);
    String uriString = parser.getAttributeValue(null, ReferenceElement.ATTR_URI);
    URI uri;
    try {
        uri = uriString != null ? new URI(uriString) : null;
    } catch (URISyntaxException e) {
        // TODO: Should be SmackParseException and probably be factored into ParserUtils.
        throw new IOException(e);
    }
    XmlElement child = null;
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            String elementName = parser.getName();
            String namespace = parser.getNamespace();
            ExtensionElementProvider<?> provider = ProviderManager.getExtensionProvider(elementName, namespace);
            if (provider != null) {
                child = provider.parse(parser);
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            break outerloop;
        }
    }
    return new ReferenceElement(begin, end, type, anchor, uri, child);
}
Also used : ExtensionElementProvider(org.jivesoftware.smack.provider.ExtensionElementProvider) ReferenceElement(org.jivesoftware.smackx.reference.element.ReferenceElement) XmlElement(org.jivesoftware.smack.packet.XmlElement) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI)

Example 15 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement in project Smack by igniterealtime.

the class FasteningElementProvider method parse.

@Override
public FasteningElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    FasteningElement.Builder builder = FasteningElement.builder();
    builder.setOriginId(parser.getAttributeValue("", FasteningElement.ATTR_ID));
    if (ParserUtils.getBooleanAttribute(parser, FasteningElement.ATTR_CLEAR, false)) {
        builder.setClear();
    }
    if (ParserUtils.getBooleanAttribute(parser, FasteningElement.ATTR_SHELL, false)) {
        builder.setShell();
    }
    outerloop: while (true) {
        XmlPullParser.Event tag = parser.next();
        switch(tag) {
            case START_ELEMENT:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                // Parse external payload
                if (MessageFasteningManager.NAMESPACE.equals(namespace) && ExternalElement.ELEMENT.equals(name)) {
                    ExternalElement external = new ExternalElement(parser.getAttributeValue("", ExternalElement.ATTR_NAME), parser.getAttributeValue("", ExternalElement.ATTR_ELEMENT_NAMESPACE));
                    builder.addExternalPayload(external);
                    continue;
                }
                // Parse wrapped payload
                XmlElement wrappedPayload = PacketParserUtils.parseExtensionElement(name, namespace, parser, xmlEnvironment);
                builder.addWrappedPayload(wrappedPayload);
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                break;
        }
    }
    return builder.build();
}
Also used : FasteningElement(org.jivesoftware.smackx.message_fastening.element.FasteningElement) XmlElement(org.jivesoftware.smack.packet.XmlElement) ExternalElement(org.jivesoftware.smackx.message_fastening.element.ExternalElement)

Aggregations

XmlElement (org.jivesoftware.smack.packet.XmlElement)28 XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)6 Test (org.junit.jupiter.api.Test)5 NamedElement (org.jivesoftware.smack.packet.NamedElement)4 Stanza (org.jivesoftware.smack.packet.Stanza)4 XmlEnvironment (org.jivesoftware.smack.packet.XmlEnvironment)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 QName (javax.xml.namespace.QName)2 Message (org.jivesoftware.smack.packet.Message)2 MessageBuilder (org.jivesoftware.smack.packet.MessageBuilder)2 JingleReason (org.jivesoftware.smackx.jingle.element.JingleReason)2 PubSub (org.jivesoftware.smackx.pubsub.packet.PubSub)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)1 Mechanisms (org.jivesoftware.smack.packet.Mechanisms)1