Search in sources :

Example 16 with XmlPullParser

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

the class SmackTestUtil method parse.

@SuppressWarnings("unchecked")
public static <E extends Element> E parse(Reader reader, AbstractProvider<E> abstractProvider, XmlPullParserKind parserKind) throws XmlPullParserException, IOException, SmackParsingException {
    XmlPullParser parser = getParserFor(reader, parserKind);
    final E element;
    if (abstractProvider instanceof Provider) {
        Provider<E> provider = (Provider<E>) abstractProvider;
        element = provider.parse(parser);
    } else if (abstractProvider instanceof IqProvider) {
        IqData iqData = PacketParserUtils.parseIqData(parser);
        parser.next();
        ParserUtils.forwardToStartElement(parser);
        IqProvider<?> iqProvider = (IqProvider<?>) abstractProvider;
        element = (E) iqProvider.parse(parser, iqData);
    } else {
        throw new AssertionError();
    }
    return element;
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) IqData(org.jivesoftware.smack.packet.IqData) IqProvider(org.jivesoftware.smack.provider.IqProvider) Provider(org.jivesoftware.smack.provider.Provider) AbstractProvider(org.jivesoftware.smack.provider.AbstractProvider) IqProvider(org.jivesoftware.smack.provider.IqProvider)

Example 17 with XmlPullParser

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

the class CarbonManagerProvider method parse.

@Override
public CarbonExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException, ParseException {
    Direction dir = Direction.valueOf(parser.getName());
    Forwarded<Message> fwd = null;
    boolean done = false;
    while (!done) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getName().equals("forwarded")) {
            fwd = ForwardedProvider.parseForwardedMessage(parser, xmlEnvironment);
        } else if (eventType == XmlPullParser.Event.END_ELEMENT && dir == Direction.valueOf(parser.getName()))
            done = true;
    }
    if (fwd == null) {
        throw new SmackParsingException("sent/received must contain exactly one <forwarded/> element");
    }
    return new CarbonExtension(dir, fwd);
}
Also used : Message(org.jivesoftware.smack.packet.Message) CarbonExtension(org.jivesoftware.smackx.carbons.packet.CarbonExtension) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) Direction(org.jivesoftware.smackx.carbons.packet.CarbonExtension.Direction) SmackParsingException(org.jivesoftware.smack.parsing.SmackParsingException)

Example 18 with XmlPullParser

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

the class AbstractHttpOverXmppProvider method parseData.

/**
 * Parses Data element if any.
 *
 * @param parser parser
 * @return Data or null if no data
 *
 * @throws XmlPullParserException if an error in the XML parser occurred.
 * @throws IOException if an I/O error occurred.
 */
protected AbstractHttpOverXmpp.Data parseData(XmlPullParser parser) throws XmlPullParserException, IOException {
    NamedElement child = null;
    boolean done = false;
    AbstractHttpOverXmpp.Data data = null;
    /* We are either at start of data or end of req/res */
    if (parser.getEventType() == XmlPullParser.Event.START_ELEMENT) {
        while (!done) {
            XmlPullParser.Event eventType = parser.next();
            if (eventType == XmlPullParser.Event.START_ELEMENT) {
                switch(parser.getName()) {
                    case ELEMENT_TEXT:
                        child = parseText(parser);
                        break;
                    case ELEMENT_BASE_64:
                        child = parseBase64(parser);
                        break;
                    case ELEMENT_CHUNKED_BASE_64:
                        child = parseChunkedBase64(parser);
                        break;
                    case ELEMENT_XML:
                        child = parseXml(parser);
                        break;
                    case ELEMENT_IBB:
                        child = parseIbb(parser);
                        break;
                    case ELEMENT_SIPUB:
                        // implemented yet
                        throw new UnsupportedOperationException("sipub is not supported yet");
                    case ELEMENT_JINGLE:
                        // implemented yet
                        throw new UnsupportedOperationException("jingle is not supported yet");
                    default:
                        // other elements are not allowed
                        throw new IllegalArgumentException("unsupported child tag: " + parser.getName());
                }
            } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
                if (parser.getName().equals(ELEMENT_DATA)) {
                    done = true;
                }
            }
        }
        data = new AbstractHttpOverXmpp.Data(child);
    }
    return data;
}
Also used : AbstractHttpOverXmpp(org.jivesoftware.smackx.hoxt.packet.AbstractHttpOverXmpp) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) NamedElement(org.jivesoftware.smack.packet.NamedElement)

Example 19 with XmlPullParser

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

the class JingleFileTransferProvider method parse.

@Override
public JingleFileTransfer parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException, ParseException {
    ArrayList<JingleContentDescriptionChildElement> payloads = new ArrayList<>();
    JingleFileTransferChild.Builder builder = JingleFileTransferChild.getBuilder();
    String elementName;
    while (true) {
        XmlPullParser.TagEvent tag = parser.nextTag();
        switch(tag) {
            case START_ELEMENT:
                elementName = parser.getName();
                switch(elementName) {
                    case JingleFileTransferChild.ELEM_DATE:
                        builder.setDate(XmppDateTime.parseXEP0082Date(parser.nextText()));
                        break;
                    case JingleFileTransferChild.ELEM_DESC:
                        builder.setDescription(parser.nextText());
                        break;
                    case JingleFileTransferChild.ELEM_MEDIA_TYPE:
                        builder.setMediaType(parser.nextText());
                        break;
                    case JingleFileTransferChild.ELEM_NAME:
                        builder.setName(parser.nextText());
                        break;
                    case JingleFileTransferChild.ELEM_SIZE:
                        builder.setSize(Integer.parseInt(parser.nextText()));
                        break;
                    case Range.ELEMENT:
                        Range range = parseRangeElement(parser);
                        builder.setRange(range);
                        break;
                    case HashElement.ELEMENT:
                        HashElement hashElement = HashElementProvider.INSTANCE.parse(parser);
                        builder.setHash(hashElement);
                        break;
                }
                break;
            case END_ELEMENT:
                elementName = parser.getName();
                switch(elementName) {
                    case JingleFileTransferChild.ELEMENT:
                        payloads.add(builder.build());
                        builder = JingleFileTransferChild.getBuilder();
                        break;
                    case JingleContentDescription.ELEMENT:
                        return new JingleFileTransfer(payloads);
                }
                break;
        }
    }
}
Also used : JingleFileTransferChild(org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild) JingleFileTransfer(org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer) HashElement(org.jivesoftware.smackx.hashes.element.HashElement) ArrayList(java.util.ArrayList) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) JingleContentDescriptionChildElement(org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement) Range(org.jivesoftware.smackx.jingle_filetransfer.element.Range)

Example 20 with XmlPullParser

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

the class ContentElementProvider method parsePayload.

private static void parsePayload(XmlPullParser parser, XmlEnvironment outerXmlEnvironment, ContentElement.Builder builder) throws IOException, XmlPullParserException, SmackParsingException {
    final int initialDepth = parser.getDepth();
    while (true) {
        XmlPullParser.Event tag = parser.next();
        if (tag == XmlPullParser.Event.START_ELEMENT) {
            String name = parser.getName();
            String namespace = parser.getNamespace();
            XmlElement element = PacketParserUtils.parseExtensionElement(name, namespace, parser, outerXmlEnvironment);
            builder.addPayloadItem(element);
        }
        if (tag == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
            return;
        }
    }
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlElement(org.jivesoftware.smack.packet.XmlElement)

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