OmemoDeviceListVAxolotlProvider.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub, 2021 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.omemo.provider;

  18. import static org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement.DEVICE;
  19. import static org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement.ID;

  20. import java.io.IOException;
  21. import java.util.HashSet;
  22. import java.util.Set;

  23. import org.jivesoftware.smack.packet.XmlEnvironment;
  24. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  25. import org.jivesoftware.smack.xml.XmlPullParser;
  26. import org.jivesoftware.smack.xml.XmlPullParserException;

  27. import org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement_VAxolotl;

  28. /**
  29.  * Smack ExtensionProvider that parses OMEMO device list element into OmemoDeviceListElement objects.
  30.  *
  31.  * @author Paul Schaub
  32.  */
  33. public class OmemoDeviceListVAxolotlProvider extends ExtensionElementProvider<OmemoDeviceListElement_VAxolotl> {

  34.     @Override
  35.     public OmemoDeviceListElement_VAxolotl parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  36.         Set<Integer> deviceListIds = new HashSet<>();
  37.         outerloop: while (true) {
  38.             XmlPullParser.Event tag = parser.next();
  39.             switch (tag) {
  40.                 case START_ELEMENT:
  41.                     String name = parser.getName();
  42.                     if (name.equals(DEVICE)) {
  43.                         for (int i = 0; i < parser.getAttributeCount(); i++) {
  44.                             if (parser.getAttributeName(i).equals(ID)) {
  45.                                 Integer deviceId = Integer.parseInt(parser.getAttributeValue(i));
  46.                                 deviceListIds.add(deviceId);
  47.                             }
  48.                         }
  49.                     }
  50.                     break;
  51.                 case END_ELEMENT:
  52.                     if (parser.getDepth() == initialDepth) {
  53.                         break outerloop;
  54.                     }
  55.                     break;
  56.                 default:
  57.                     // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  58.                     break;
  59.             }
  60.         }
  61.         return new OmemoDeviceListElement_VAxolotl(deviceListIds);
  62.     }
  63. }