Search in sources :

Example 1 with XmlElement

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

the class AbstractXMPPConnection method parseFeatures.

protected final void parseFeatures(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
    streamFeatures.clear();
    final int initialDepth = parser.getDepth();
    while (true) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getDepth() == initialDepth + 1) {
            XmlElement streamFeature = null;
            String name = parser.getName();
            String namespace = parser.getNamespace();
            switch(name) {
                case StartTls.ELEMENT:
                    streamFeature = PacketParserUtils.parseStartTlsFeature(parser);
                    break;
                case Mechanisms.ELEMENT:
                    streamFeature = new Mechanisms(PacketParserUtils.parseMechanisms(parser));
                    break;
                case Bind.ELEMENT:
                    streamFeature = Bind.Feature.INSTANCE;
                    break;
                case Session.ELEMENT:
                    streamFeature = PacketParserUtils.parseSessionFeature(parser);
                    break;
                case Compress.Feature.ELEMENT:
                    streamFeature = PacketParserUtils.parseCompressionFeature(parser);
                    break;
                default:
                    ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getStreamFeatureProvider(name, namespace);
                    if (provider != null) {
                        streamFeature = provider.parse(parser, incomingStreamXmlEnvironment);
                    }
                    break;
            }
            if (streamFeature != null) {
                addStreamFeature(streamFeature);
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
            break;
        }
    }
}
Also used : Mechanisms(org.jivesoftware.smack.packet.Mechanisms) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) XmlElement(org.jivesoftware.smack.packet.XmlElement)

Example 2 with XmlElement

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

the class ExtensionElementFilter method accept.

@Override
public final boolean accept(Stanza stanza) {
    XmlElement extensionElement = stanza.getExtension(extensionElementQName);
    if (extensionElement == null) {
        return false;
    }
    if (!extensionElementClass.isInstance(extensionElement)) {
        return false;
    }
    E specificExtensionElement = extensionElementClass.cast(extensionElement);
    return accept(specificExtensionElement);
}
Also used : XmlElement(org.jivesoftware.smack.packet.XmlElement)

Example 3 with XmlElement

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

the class PacketParserUtils method parseMessage.

/**
 * Parses a message packet.
 *
 * @param parser the XML parser, positioned at the start of a message packet.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return a Message packet.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 * @throws IOException if an I/O error occurred.
 * @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
 */
public static Message parseMessage(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    ParserUtils.assertAtStartTag(parser);
    assert parser.getName().equals(Message.ELEMENT);
    XmlEnvironment messageXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    final int initialDepth = parser.getDepth();
    MessageBuilder message = parseCommonStanzaAttributes(id -> {
        return StanzaBuilder.buildMessage(id);
    }, parser, outerXmlEnvironment);
    String typeString = parser.getAttributeValue("", "type");
    if (typeString != null) {
        message.ofType(Message.Type.fromString(typeString));
    }
    // in arbitrary sub-elements.
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String elementName = parser.getName();
                String namespace = parser.getNamespace();
                switch(elementName) {
                    case "error":
                        message.setError(parseError(parser, messageXmlEnvironment));
                        break;
                    default:
                        XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, messageXmlEnvironment);
                        message.addExtension(extensionElement);
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            // fall out
            default:
        }
    }
    return message.build();
}
Also used : MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) XmlElement(org.jivesoftware.smack.packet.XmlElement) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment)

Example 4 with XmlElement

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

the class PacketParserUtils method parseError.

/**
 * Parses error sub-packets.
 *
 * @param parser the XML parser.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return an error sub-packet.
 * @throws IOException if an I/O error occurred.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 * @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
 */
public static StanzaError parseError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    final int initialDepth = parser.getDepth();
    Map<String, String> descriptiveTexts = null;
    XmlEnvironment stanzaErrorXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    List<XmlElement> extensions = new ArrayList<>();
    StanzaError.Builder builder = StanzaError.getBuilder();
    // Parse the error header
    builder.setType(StanzaError.Type.fromString(parser.getAttributeValue("", "type")));
    builder.setErrorGenerator(parser.getAttributeValue("", "by"));
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                switch(namespace) {
                    case StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE:
                        switch(name) {
                            case Stanza.TEXT:
                                descriptiveTexts = parseDescriptiveTexts(parser, descriptiveTexts);
                                break;
                            default:
                                builder.setCondition(StanzaError.Condition.fromString(name));
                                String conditionText = parser.nextText();
                                if (!conditionText.isEmpty()) {
                                    builder.setConditionText(conditionText);
                                }
                                break;
                        }
                        break;
                    default:
                        PacketParserUtils.addExtensionElement(extensions, parser, name, namespace, stanzaErrorXmlEnvironment);
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    builder.setExtensions(extensions).setDescriptiveTexts(descriptiveTexts);
    return builder.build();
}
Also used : ArrayList(java.util.ArrayList) XmlElement(org.jivesoftware.smack.packet.XmlElement) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment) StanzaError(org.jivesoftware.smack.packet.StanzaError)

Example 5 with XmlElement

use of org.jivesoftware.smack.packet.XmlElement 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

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