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.util.ArrayList;
020import java.util.List;
021
022import org.jivesoftware.smack.packet.IQ.Type;
023import org.jivesoftware.smack.provider.IQProvider;
024import org.jivesoftware.smack.util.ParserUtils;
025
026import org.jivesoftware.smackx.blocking.element.BlockListIQ;
027
028import org.jxmpp.jid.Jid;
029import org.xmlpull.v1.XmlPullParser;
030
031/**
032 * Block list IQ provider class.
033 *
034 * @author Fernando Ramirez
035 * @see <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
036 *      Command</a>
037 */
038public class BlockListIQProvider extends IQProvider<BlockListIQ> {
039
040    @Override
041    public BlockListIQ parse(XmlPullParser parser, int initialDepth) throws Exception {
042        List<Jid> jids = null;
043
044        outerloop: while (true) {
045            int eventType = parser.next();
046
047            switch (eventType) {
048
049            case XmlPullParser.START_TAG:
050                if (parser.getName().equals("item")) {
051                    if (jids == null) {
052                        jids = new ArrayList<>();
053                    }
054                    Jid jid = ParserUtils.getJidAttribute(parser);
055                    jids.add(jid);
056                }
057                break;
058
059            case XmlPullParser.END_TAG:
060                if (parser.getDepth() == initialDepth) {
061                    break outerloop;
062                }
063                break;
064
065            }
066        }
067
068        BlockListIQ blockListIQ = new BlockListIQ(jids);
069        blockListIQ.setType(Type.result);
070        return blockListIQ;
071    }
072
073}