001/*
002 *
003 * Copyright 2017 Paul Schaub, 2021-2025 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.omemo.provider;
018
019import static org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement.DEVICE;
020import static org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement.ID;
021
022import java.io.IOException;
023import java.util.HashSet;
024import java.util.Set;
025
026import org.jivesoftware.smack.packet.XmlEnvironment;
027import org.jivesoftware.smack.provider.ExtensionElementProvider;
028import org.jivesoftware.smack.xml.XmlPullParser;
029import org.jivesoftware.smack.xml.XmlPullParserException;
030
031import org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement_VAxolotl;
032
033import org.jxmpp.JxmppContext;
034
035/**
036 * Smack ExtensionProvider that parses OMEMO device list element into OmemoDeviceListElement objects.
037 *
038 * @author Paul Schaub
039 */
040public class OmemoDeviceListVAxolotlProvider extends ExtensionElementProvider<OmemoDeviceListElement_VAxolotl> {
041
042    @Override
043    public OmemoDeviceListElement_VAxolotl parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
044        Set<Integer> deviceListIds = new HashSet<>();
045        outerloop: while (true) {
046            XmlPullParser.Event tag = parser.next();
047            switch (tag) {
048                case START_ELEMENT:
049                    String name = parser.getName();
050                    if (name.equals(DEVICE)) {
051                        for (int i = 0; i < parser.getAttributeCount(); i++) {
052                            if (parser.getAttributeName(i).equals(ID)) {
053                                Integer deviceId = Integer.parseInt(parser.getAttributeValue(i));
054                                deviceListIds.add(deviceId);
055                            }
056                        }
057                    }
058                    break;
059                case END_ELEMENT:
060                    if (parser.getDepth() == initialDepth) {
061                        break outerloop;
062                    }
063                    break;
064                default:
065                    // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
066                    break;
067            }
068        }
069        return new OmemoDeviceListElement_VAxolotl(deviceListIds);
070    }
071}