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