Search in sources :

Example 6 with XmlEnvironment

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

the class DataFormProvider method parse.

@Override
public DataForm parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    DataForm.Type dataFormType = DataForm.Type.fromString(parser.getAttributeValue("", "type"));
    DataForm.Builder dataForm = DataForm.builder(dataFormType);
    String formType = null;
    DataForm.ReportedData reportedData = null;
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                XmlEnvironment elementXmlEnvironment = XmlEnvironment.from(parser, xmlEnvironment);
                switch(name) {
                    case "instructions":
                        dataForm.addInstruction(parser.nextText());
                        break;
                    case "title":
                        dataForm.setTitle(parser.nextText());
                        break;
                    case "field":
                        // Note that we parse this form field without any potential reportedData. We only use reportedData
                        // to lookup form field types of fields under <item/>.
                        FormField formField = parseField(parser, elementXmlEnvironment, formType);
                        TextSingleFormField hiddenFormTypeField = formField.asHiddenFormTypeFieldIfPossible();
                        if (hiddenFormTypeField != null) {
                            if (formType != null) {
                                throw new SmackParsingException("Multiple hidden form type fields");
                            }
                            formType = hiddenFormTypeField.getValue();
                        }
                        dataForm.addField(formField);
                        break;
                    case "item":
                        DataForm.Item item = parseItem(parser, elementXmlEnvironment, formType, reportedData);
                        dataForm.addItem(item);
                        break;
                    case "reported":
                        if (reportedData != null) {
                            throw new SmackParsingException("Data form with multiple <reported/> elements");
                        }
                        reportedData = parseReported(parser, elementXmlEnvironment, formType);
                        dataForm.setReportedData(reportedData);
                        break;
                    // See XEP-133 Example 32 for a corner case where the data form contains this extension.
                    case RosterPacket.ELEMENT:
                        if (namespace.equals(RosterPacket.NAMESPACE)) {
                            dataForm.addExtensionElement(RosterPacketProvider.INSTANCE.parse(parser));
                        }
                        break;
                    // See XEP-141 Data Forms Layout
                    case DataLayout.ELEMENT:
                        if (namespace.equals(DataLayout.NAMESPACE)) {
                            dataForm.addExtensionElement(DataLayoutProvider.parse(parser));
                        }
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return dataForm.build();
}
Also used : TextSingleFormField(org.jivesoftware.smackx.xdata.TextSingleFormField) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) ListMultiFormField(org.jivesoftware.smackx.xdata.ListMultiFormField) AbstractSingleStringValueFormField(org.jivesoftware.smackx.xdata.AbstractSingleStringValueFormField) BooleanFormField(org.jivesoftware.smackx.xdata.BooleanFormField) FormField(org.jivesoftware.smackx.xdata.FormField) ListSingleFormField(org.jivesoftware.smackx.xdata.ListSingleFormField) JidMultiFormField(org.jivesoftware.smackx.xdata.JidMultiFormField) TextSingleFormField(org.jivesoftware.smackx.xdata.TextSingleFormField) AbstractMultiFormField(org.jivesoftware.smackx.xdata.AbstractMultiFormField) JidSingleFormField(org.jivesoftware.smackx.xdata.JidSingleFormField) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment) SmackParsingException(org.jivesoftware.smack.parsing.SmackParsingException)

Example 7 with XmlEnvironment

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

the class PacketParserUtils method parseStreamError.

/**
 * Parses stream error packets.
 *
 * @param parser the XML parser.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return an stream error 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 StreamError parseStreamError(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    final int initialDepth = parser.getDepth();
    List<XmlElement> extensions = new ArrayList<>();
    Map<String, String> descriptiveTexts = null;
    StreamError.Condition condition = null;
    String conditionText = null;
    XmlEnvironment streamErrorXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                switch(namespace) {
                    case StreamError.NAMESPACE:
                        switch(name) {
                            case "text":
                                descriptiveTexts = parseDescriptiveTexts(parser, descriptiveTexts);
                                break;
                            default:
                                // If it's not a text element, that is qualified by the StreamError.NAMESPACE,
                                // then it has to be the stream error code
                                condition = StreamError.Condition.fromString(name);
                                conditionText = parser.nextText();
                                if (conditionText.isEmpty()) {
                                    conditionText = null;
                                }
                                break;
                        }
                        break;
                    default:
                        PacketParserUtils.addExtensionElement(extensions, parser, name, namespace, streamErrorXmlEnvironment);
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return new StreamError(condition, conditionText, descriptiveTexts, extensions);
}
Also used : StreamError(org.jivesoftware.smack.packet.StreamError) ArrayList(java.util.ArrayList) XmlElement(org.jivesoftware.smack.packet.XmlElement) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment)

Example 8 with XmlEnvironment

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

the class PacketParserUtils method parsePresence.

/**
 * Parses a presence packet.
 *
 * @param parser the XML parser, positioned at the start of a presence packet.
 * @param outerXmlEnvironment the outer XML environment (optional).
 * @return a Presence 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 Presence parsePresence(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    XmlEnvironment presenceXmlEnvironment = XmlEnvironment.from(parser, outerXmlEnvironment);
    PresenceBuilder presence = parseCommonStanzaAttributes(stanzaId -> StanzaBuilder.buildPresence(stanzaId), parser, outerXmlEnvironment);
    Presence.Type type = Presence.Type.available;
    String typeString = parser.getAttributeValue("", "type");
    if (typeString != null && !typeString.equals("")) {
        type = Presence.Type.fromString(typeString);
    }
    presence.ofType(type);
    // Parse 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 "status":
                        presence.setStatus(parser.nextText());
                        break;
                    case "priority":
                        Byte priority = ParserUtils.getByteAttributeFromNextText(parser);
                        presence.setPriority(priority);
                        break;
                    case "show":
                        String modeText = parser.nextText();
                        if (StringUtils.isNotEmpty(modeText)) {
                            presence.setMode(Presence.Mode.fromString(modeText));
                        } else {
                            // Some implementations send presence stanzas with a
                            // '<show />' element, which is a invalid XMPP presence
                            // stanza according to RFC 6121 4.7.2.1
                            LOGGER.warning("Empty or null mode text in presence show element form " + presence + "' which is invalid according to RFC6121 4.7.2.1");
                        }
                        break;
                    case "error":
                        presence.setError(parseError(parser, presenceXmlEnvironment));
                        break;
                    default:
                        // failing completely here. See SMACK-390 for more information.
                        try {
                            XmlElement extensionElement = parseExtensionElement(elementName, namespace, parser, presenceXmlEnvironment);
                            presence.addExtension(extensionElement);
                        } catch (Exception e) {
                            LOGGER.log(Level.WARNING, "Failed to parse extension element in Presence stanza: " + presence, e);
                        }
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return presence.build();
}
Also used : PresenceBuilder(org.jivesoftware.smack.packet.PresenceBuilder) Presence(org.jivesoftware.smack.packet.Presence) XmlElement(org.jivesoftware.smack.packet.XmlElement) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) SmackParsingException(org.jivesoftware.smack.parsing.SmackParsingException) IOException(java.io.IOException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment)

Example 9 with XmlEnvironment

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

the class Provider method parse.

public final E 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);
    E 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 10 with XmlEnvironment

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

the class FailureProvider method parse.

@Override
public Failure parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    Failure.CompressFailureError compressFailureError = null;
    StanzaError stanzaError = null;
    XmlEnvironment failureXmlEnvironment = XmlEnvironment.from(parser, xmlEnvironment);
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                switch(namespace) {
                    case Failure.NAMESPACE:
                        compressFailureError = Failure.CompressFailureError.valueOf(name.replace("-", "_"));
                        if (compressFailureError == null) {
                            LOGGER.warning("Unknown element in " + Failure.NAMESPACE + ": " + name);
                        }
                        break;
                    case StreamOpen.CLIENT_NAMESPACE:
                    case StreamOpen.SERVER_NAMESPACE:
                        switch(name) {
                            case StanzaError.ERROR:
                                stanzaError = PacketParserUtils.parseError(parser, failureXmlEnvironment);
                                break;
                            default:
                                LOGGER.warning("Unknown element in " + namespace + ": " + name);
                                break;
                        }
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            // fall out
            default:
        }
    }
    return new Failure(compressFailureError, stanzaError);
}
Also used : Failure(org.jivesoftware.smack.compress.packet.Failure) StanzaError(org.jivesoftware.smack.packet.StanzaError) XmlEnvironment(org.jivesoftware.smack.packet.XmlEnvironment)

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