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