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