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