001/**
002 *
003 * Copyright 2016 Fernando Ramirez
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.blocking.provider;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.provider.IQProvider;
025import org.jivesoftware.smack.util.ParserUtils;
026import org.jivesoftware.smack.xml.XmlPullParser;
027import org.jivesoftware.smack.xml.XmlPullParserException;
028
029import org.jivesoftware.smackx.blocking.element.BlockContactsIQ;
030
031import org.jxmpp.jid.Jid;
032
033/**
034 * Block contact IQ provider class.
035 *
036 * @author Fernando Ramirez
037 * @see <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
038 *      Command</a>
039 */
040public class BlockContactsIQProvider extends IQProvider<BlockContactsIQ> {
041
042    @Override
043    public BlockContactsIQ parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
044        List<Jid> jids = new ArrayList<>();
045
046        outerloop: while (true) {
047            XmlPullParser.Event eventType = parser.next();
048            switch (eventType) {
049            case START_ELEMENT:
050                if (parser.getName().equals("item")) {
051                    Jid jid = ParserUtils.getJidAttribute(parser);
052                    jids.add(jid);
053                }
054                break;
055
056            case END_ELEMENT:
057                if (parser.getDepth() == initialDepth) {
058                    break outerloop;
059                }
060                break;
061
062            default:
063                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
064                break;
065            }
066        }
067
068        return new BlockContactsIQ(jids);
069    }
070
071}