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