Search in sources :

Example 1 with XmlEnvironment

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

the class IQProvider method parse.

public final I parse(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws IOException, XmlPullParserException, SmackParsingException {
    // XPP3 calling convention assert: Parser should be at start tag
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    final XmlEnvironment xmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    I e = wrapExceptions(() -> parse(parser, initialDepth, xmlEnvironment));
    // XPP3 calling convention assert: Parser should be at end tag of the consumed/parsed element
    ParserUtils.forwardToEndTagOfDepth(parser, initialDepth);
    return e;
}
Also used : XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment)

Example 2 with XmlEnvironment

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

the class IqProvider method parse.

public final I parse(XmlPullParser parser, IqData iqData, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    final int initialDepth = parser.getDepth();
    final XmlEnvironment xmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    I i = wrapExceptions(() -> parse(parser, initialDepth, iqData, xmlEnvironment));
    return i;
}
Also used : XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment)

Example 3 with XmlEnvironment

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

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

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

the class PacketParserUtils method parseIQ.

/**
 * Parses an IQ packet.
 *
 * @param parser the XML parser, positioned at the start of an IQ packet.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return an IQ object.
 * @throws XmlPullParserException if an error in the XML parser occurred.
 * @throws XmppStringprepException if the provided string is invalid.
 * @throws IOException if an I/O error occurred.
 * @throws SmackParsingException if the Smack parser (provider) encountered invalid input.
 */
public static IQ parseIQ(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, XmppStringprepException, IOException, SmackParsingException {
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    XmlEnvironment iqXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    IQ iqPacket = null;
    StanzaError error = null;
    IqData iqData = parseIqData(parser);
    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":
                        error = PacketParserUtils.parseError(parser, iqXmlEnvironment);
                        break;
                    // this element name and namespace.
                    default:
                        IqProvider<IQ> provider = ProviderManager.getIQProvider(elementName, namespace);
                        if (provider != null) {
                            iqPacket = provider.parse(parser, iqData, outerXmlEnvironment);
                        } else // Note that if we reach this code, it is guaranteed that the result IQ contained a child element
                        // (RFC 6120 ยง 8.2.3 6) because otherwise we would have reached the END_ELEMENT first.
                        {
                            // No Provider found for the IQ stanza, parse it to an UnparsedIQ instance
                            // so that the content of the IQ can be examined later on
                            iqPacket = new UnparsedIQ(elementName, namespace, parseElement(parser));
                        }
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    // Decide what to do when an IQ packet was not understood
    if (iqPacket == null) {
        switch(iqData.getType()) {
            case error:
                // If an IQ packet wasn't created above, create an empty error IQ packet.
                iqPacket = new ErrorIQ(error);
                break;
            case result:
                iqPacket = new EmptyResultIQ();
                break;
            default:
                break;
        }
    }
    // Set basic values on the iq packet.
    iqPacket.setStanzaId(iqData.getStanzaId());
    iqPacket.setTo(iqData.getTo());
    iqPacket.setFrom(iqData.getFrom());
    iqPacket.setType(iqData.getType());
    iqPacket.setError(error);
    return iqPacket;
}
Also used : ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) UnparsedIQ(org.jivesoftware.smack.packet.UnparsedIQ) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) UnparsedIQ(org.jivesoftware.smack.packet.UnparsedIQ) IQ(org.jivesoftware.smack.packet.IQ) IqData(org.jivesoftware.smack.packet.IqData) EmptyResultIQ(org.jivesoftware.smack.packet.EmptyResultIQ) IqProvider(org.jivesoftware.smack.provider.IqProvider) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment) StanzaError(org.jivesoftware.smack.packet.StanzaError)

Aggregations

XmlEnvironment (org.jivesoftware.smack.packet.XmlEnvironment)12 XmlElement (org.jivesoftware.smack.packet.XmlElement)4 ArrayList (java.util.ArrayList)3 StanzaError (org.jivesoftware.smack.packet.StanzaError)3 IOException (java.io.IOException)2 SmackParsingException (org.jivesoftware.smack.parsing.SmackParsingException)2 Buffer (java.nio.Buffer)1 ByteBuffer (java.nio.ByteBuffer)1 SocketChannel (java.nio.channels.SocketChannel)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 SelectionKeyAttachment (org.jivesoftware.smack.SmackReactor.SelectionKeyAttachment)1 XmppInputOutputFilter (org.jivesoftware.smack.XmppInputOutputFilter)1 Failure (org.jivesoftware.smack.compress.packet.Failure)1 SmackDebugger (org.jivesoftware.smack.debugger.SmackDebugger)1 EmptyResultIQ (org.jivesoftware.smack.packet.EmptyResultIQ)1 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)1 IQ (org.jivesoftware.smack.packet.IQ)1 IqData (org.jivesoftware.smack.packet.IqData)1 MessageBuilder (org.jivesoftware.smack.packet.MessageBuilder)1