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