Search in sources :

Example 66 with XmlPullParser

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

the class ParseStreamManagementTest method testParseEnabledInvariant.

@Test
public void testParseEnabledInvariant() throws XmlPullParserException, IOException {
    String enabledString = new StreamManagement.Enabled("stream-id", false).toXML().toString();
    XmlPullParser parser = PacketParserUtils.getParserFor(enabledString);
    StreamManagement.Enabled enabled = ParseStreamManagement.enabled(parser);
    assertEquals(enabledString, enabled.toXML().toString());
}
Also used : StreamManagement(org.jivesoftware.smack.sm.packet.StreamManagement) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) Test(org.junit.jupiter.api.Test)

Example 67 with XmlPullParser

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

the class Xpp3ParserTest method factoryTest.

@Test
public void factoryTest() throws XmlPullParserException, IOException {
    Xpp3XmlPullParserFactory xpp3XmlPullParserFactory = new Xpp3XmlPullParserFactory();
    XmlPullParser parser = xpp3XmlPullParserFactory.newXmlPullParser(new StringReader("<element/>"));
    parser.next();
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) StringReader(java.io.StringReader) Test(org.junit.jupiter.api.Test)

Example 68 with XmlPullParser

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

the class AdHocCommandDataProvider method parse.

@Override
public AdHocCommandData parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    boolean done = false;
    AdHocCommandData adHocCommandData = new AdHocCommandData();
    DataFormProvider dataFormProvider = new DataFormProvider();
    XmlPullParser.Event eventType;
    String elementName;
    String namespace;
    adHocCommandData.setSessionID(parser.getAttributeValue("", "sessionid"));
    adHocCommandData.setNode(parser.getAttributeValue("", "node"));
    // Status
    String status = parser.getAttributeValue("", "status");
    if (AdHocCommand.Status.executing.toString().equalsIgnoreCase(status)) {
        adHocCommandData.setStatus(AdHocCommand.Status.executing);
    } else if (AdHocCommand.Status.completed.toString().equalsIgnoreCase(status)) {
        adHocCommandData.setStatus(AdHocCommand.Status.completed);
    } else if (AdHocCommand.Status.canceled.toString().equalsIgnoreCase(status)) {
        adHocCommandData.setStatus(AdHocCommand.Status.canceled);
    }
    // Action
    String action = parser.getAttributeValue("", "action");
    if (action != null) {
        Action realAction = AdHocCommand.Action.valueOf(action);
        if (realAction == null || realAction.equals(Action.unknown)) {
            adHocCommandData.setAction(Action.unknown);
        } else {
            adHocCommandData.setAction(realAction);
        }
    }
    while (!done) {
        eventType = parser.next();
        namespace = parser.getNamespace();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            elementName = parser.getName();
            if (parser.getName().equals("actions")) {
                String execute = parser.getAttributeValue("", "execute");
                if (execute != null) {
                    adHocCommandData.setExecuteAction(AdHocCommand.Action.valueOf(execute));
                }
            } else if (parser.getName().equals("next")) {
                adHocCommandData.addAction(AdHocCommand.Action.next);
            } else if (parser.getName().equals("complete")) {
                adHocCommandData.addAction(AdHocCommand.Action.complete);
            } else if (parser.getName().equals("prev")) {
                adHocCommandData.addAction(AdHocCommand.Action.prev);
            } else if (elementName.equals("x") && namespace.equals("jabber:x:data")) {
                adHocCommandData.setForm(dataFormProvider.parse(parser));
            } else if (parser.getName().equals("note")) {
                String typeString = parser.getAttributeValue("", "type");
                AdHocCommandNote.Type type;
                if (typeString != null) {
                    type = AdHocCommandNote.Type.valueOf(typeString);
                } else {
                    // Type is optional and 'info' if not present.
                    type = AdHocCommandNote.Type.info;
                }
                String value = parser.nextText();
                adHocCommandData.addNote(new AdHocCommandNote(type, value));
            } else if (parser.getName().equals("error")) {
                StanzaError error = PacketParserUtils.parseError(parser);
                adHocCommandData.setError(error);
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getName().equals("command")) {
                done = true;
            }
        }
    }
    return adHocCommandData;
}
Also used : Action(org.jivesoftware.smackx.commands.AdHocCommand.Action) AdHocCommandNote(org.jivesoftware.smackx.commands.AdHocCommandNote) DataFormProvider(org.jivesoftware.smackx.xdata.provider.DataFormProvider) AdHocCommandData(org.jivesoftware.smackx.commands.packet.AdHocCommandData) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) StanzaError(org.jivesoftware.smack.packet.StanzaError)

Example 69 with XmlPullParser

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

the class XHTMLExtensionProvider method parse.

@Override
public XHTMLExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws IOException, XmlPullParserException {
    XHTMLExtension xhtmlExtension = new XHTMLExtension();
    while (true) {
        XmlPullParser.Event eventType = parser.getEventType();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            String name = parser.getName();
            if (name.equals(Message.BODY)) {
                xhtmlExtension.addBody(PacketParserUtils.parseElement(parser));
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getDepth() == initialDepth) {
                return xhtmlExtension;
            }
        }
        parser.next();
    }
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XHTMLExtension(org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension)

Example 70 with XmlPullParser

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

the class RosterExchangeProvider method parse.

/**
 * Parses a RosterExchange stanza (extension sub-packet).
 *
 * @param parser the XML parser, positioned at the starting element of the extension.
 * @return a PacketExtension.
 * @throws IOException if an I/O error occurred.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 */
@Override
public RosterExchange parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
    RosterExchange rosterExchange = new RosterExchange();
    boolean done = false;
    RemoteRosterEntry remoteRosterEntry;
    Jid jid = null;
    String name = "";
    ArrayList<String> groupsName = new ArrayList<>();
    while (!done) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            if (parser.getName().equals("item")) {
                // Reset this variable since they are optional for each item
                groupsName = new ArrayList<>();
                // 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.Event.END_ELEMENT) {
            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;
            }
        }
    }
    return rosterExchange;
}
Also used : RosterExchange(org.jivesoftware.smackx.xroster.packet.RosterExchange) Jid(org.jxmpp.jid.Jid) ArrayList(java.util.ArrayList) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) RemoteRosterEntry(org.jivesoftware.smackx.xroster.RemoteRosterEntry)

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