001/** 002 * 003 * Copyright 2017 Paul Schaub, 2021 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 033/** 034 * Smack ExtensionProvider that parses OMEMO device list element into OmemoDeviceListElement objects. 035 * 036 * @author Paul Schaub 037 */ 038public class OmemoDeviceListVAxolotlProvider extends ExtensionElementProvider<OmemoDeviceListElement_VAxolotl> { 039 040 @Override 041 public OmemoDeviceListElement_VAxolotl parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { 042 Set<Integer> deviceListIds = new HashSet<>(); 043 outerloop: while (true) { 044 XmlPullParser.Event tag = parser.next(); 045 switch (tag) { 046 case START_ELEMENT: 047 String name = parser.getName(); 048 if (name.equals(DEVICE)) { 049 for (int i = 0; i < parser.getAttributeCount(); i++) { 050 if (parser.getAttributeName(i).equals(ID)) { 051 Integer deviceId = Integer.parseInt(parser.getAttributeValue(i)); 052 deviceListIds.add(deviceId); 053 } 054 } 055 } 056 break; 057 case END_ELEMENT: 058 if (parser.getDepth() == initialDepth) { 059 break outerloop; 060 } 061 break; 062 default: 063 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. 064 break; 065 } 066 } 067 return new OmemoDeviceListElement_VAxolotl(deviceListIds); 068 } 069}