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