001/*
002 *
003 * Copyright 2018 Paul Schaub.
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.mood.provider;
018
019import java.io.IOException;
020import java.util.logging.Level;
021import java.util.logging.Logger;
022
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.parsing.SmackParsingException;
025import org.jivesoftware.smack.provider.ExtensionElementProvider;
026import org.jivesoftware.smack.provider.ProviderManager;
027import org.jivesoftware.smack.xml.XmlPullParser;
028import org.jivesoftware.smack.xml.XmlPullParserException;
029
030import org.jivesoftware.smackx.mood.Mood;
031import org.jivesoftware.smackx.mood.element.MoodConcretisation;
032import org.jivesoftware.smackx.mood.element.MoodElement;
033
034import org.jxmpp.JxmppContext;
035
036public class MoodProvider extends ExtensionElementProvider<MoodElement> {
037
038    private static final Logger LOGGER = Logger.getLogger(MoodProvider.class.getName());
039    public static final MoodProvider INSTANCE = new MoodProvider();
040
041    @Override
042    public MoodElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext)
043                    throws XmlPullParserException, IOException, SmackParsingException {
044        String text = null;
045        Mood mood = null;
046        MoodConcretisation concretisation = null;
047
048        outerloop: while (true) {
049            XmlPullParser.Event tag = parser.next();
050
051            switch (tag) {
052                case START_ELEMENT:
053                    String name = parser.getName();
054                    String namespace = parser.getNamespace();
055                    if (MoodElement.ELEM_TEXT.equals(name)) {
056                        text = parser.nextText();
057                        continue outerloop;
058                    }
059
060                    if (!MoodElement.NAMESPACE.equals(namespace)) {
061                        LOGGER.log(Level.FINE, "Foreign namespace " + namespace + " detected. Try to find suitable MoodConcretisationProvider.");
062                        MoodConcretisationProvider<?> provider = (MoodConcretisationProvider<?>) ProviderManager.getExtensionProvider(name, namespace);
063                        if (provider != null) {
064                            concretisation = provider.parse(parser);
065                        } else {
066                            LOGGER.log(Level.FINE, "No provider for <" + name + " xmlns:'" + namespace + "'/> found. Ignore.");
067                        }
068                        continue outerloop;
069                    }
070
071                    try {
072                        mood = Mood.valueOf(name);
073                        continue outerloop;
074                    } catch (IllegalArgumentException e) {
075                        throw new XmlPullParserException("Unknown mood value: " + name + " encountered.");
076                    }
077
078                case END_ELEMENT:
079                    if (MoodElement.ELEMENT.equals(parser.getName())) {
080                        MoodElement.MoodSubjectElement subjectElement = (mood == null && concretisation == null) ?
081                                null : new MoodElement.MoodSubjectElement(mood, concretisation);
082                        return new MoodElement(subjectElement, text);
083                    }
084                    break;
085
086                default:
087                    // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
088                    break;
089            }
090        }
091    }
092}