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.mam.provider;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.jivesoftware.smack.provider.IQProvider;
024import org.jivesoftware.smackx.mam.element.MamPrefsIQ;
025import org.jivesoftware.smackx.mam.element.MamPrefsIQ.DefaultBehavior;
026import org.jxmpp.jid.Jid;
027import org.jxmpp.jid.impl.JidCreate;
028import org.xmlpull.v1.XmlPullParser;
029import org.xmlpull.v1.XmlPullParserException;
030
031/**
032 * MAM Preferences IQ Provider class.
033 * 
034 * @see <a href="http://xmpp.org/extensions/xep-0313.html">XEP-0313: Message
035 *      Archive Management</a>
036 * @author Fernando Ramirez
037 * 
038 */
039public class MamPrefsIQProvider extends IQProvider<MamPrefsIQ> {
040
041    @Override
042    public MamPrefsIQ parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
043        String iqType = parser.getAttributeValue("", "type");
044        String defaultBehaviorString = parser.getAttributeValue("", "default");
045        DefaultBehavior defaultBehavior = null;
046        if (defaultBehaviorString != null) {
047            defaultBehavior = DefaultBehavior.valueOf(defaultBehaviorString);
048        }
049
050        if (iqType == null) {
051            iqType = "result";
052        }
053
054        List<Jid> alwaysJids = null;
055        List<Jid> neverJids = null;
056
057        outerloop: while (true) {
058            final int eventType = parser.next();
059            final String name = parser.getName();
060            switch (eventType) {
061            case XmlPullParser.START_TAG:
062                switch (name) {
063                case "always":
064                    alwaysJids = iterateJids(parser);
065                    break;
066                case "never":
067                    neverJids = iterateJids(parser);
068                    break;
069                }
070                break;
071            case XmlPullParser.END_TAG:
072                if (parser.getDepth() == initialDepth) {
073                    break outerloop;
074                }
075                break;
076            }
077        }
078
079        return new MamPrefsIQ(alwaysJids, neverJids, defaultBehavior);
080    }
081
082    private static List<Jid> iterateJids(XmlPullParser parser) throws XmlPullParserException, IOException {
083        List<Jid> jids = new ArrayList<>();
084
085        int initialDepth = parser.getDepth();
086
087        outerloop: while (true) {
088            final int eventType = parser.next();
089            final String name = parser.getName();
090            switch (eventType) {
091            case XmlPullParser.START_TAG:
092                switch (name) {
093                case "jid":
094                    parser.next();
095                    jids.add(JidCreate.from(parser.getText()));
096                    break;
097                }
098                break;
099            case  XmlPullParser.END_TAG:
100                if (parser.getDepth() == initialDepth) {
101                    break outerloop;
102                }
103                break;
104            }
105        }
106
107        return jids;
108    }
109
110}