001/**
002 *
003 * Copyright 2018-2019 Florian Schmaus
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.smack.compress.provider;
018
019import java.io.IOException;
020import java.util.logging.Logger;
021
022import org.jivesoftware.smack.compress.packet.Failure;
023import org.jivesoftware.smack.packet.StanzaError;
024import org.jivesoftware.smack.packet.StreamOpen;
025import org.jivesoftware.smack.packet.XmlEnvironment;
026import org.jivesoftware.smack.parsing.SmackParsingException;
027import org.jivesoftware.smack.provider.NonzaProvider;
028import org.jivesoftware.smack.util.PacketParserUtils;
029import org.jivesoftware.smack.xml.XmlPullParser;
030import org.jivesoftware.smack.xml.XmlPullParserException;
031
032public final class FailureProvider extends NonzaProvider<Failure> {
033
034    private static final Logger LOGGER = Logger.getLogger(FailureProvider.class.getName());
035
036    public static final FailureProvider INSTANCE = new FailureProvider();
037
038    private FailureProvider() {
039    }
040
041    @Override
042    public Failure parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
043        Failure.CompressFailureError compressFailureError = null;
044        StanzaError stanzaError = null;
045        XmlEnvironment failureXmlEnvironment = XmlEnvironment.from(parser, xmlEnvironment);
046
047        outerloop: while (true) {
048            XmlPullParser.Event eventType = parser.next();
049            switch (eventType) {
050            case START_ELEMENT:
051                String name = parser.getName();
052                String namespace = parser.getNamespace();
053                switch (namespace) {
054                case Failure.NAMESPACE:
055                    compressFailureError = Failure.CompressFailureError.valueOf(name.replace("-", "_"));
056                    if (compressFailureError == null) {
057                        LOGGER.warning("Unknown element in " + Failure.NAMESPACE + ": " + name);
058                    }
059                    break;
060                case StreamOpen.CLIENT_NAMESPACE:
061                case StreamOpen.SERVER_NAMESPACE:
062                    switch (name) {
063                        case StanzaError.ERROR:
064                            stanzaError = PacketParserUtils.parseError(parser, failureXmlEnvironment);
065                            break;
066                        default:
067                            LOGGER.warning("Unknown element in " + namespace + ": " + name);
068                            break;
069                    }
070                    break;
071                }
072                break;
073            case END_ELEMENT:
074                if (parser.getDepth() == initialDepth) {
075                    break outerloop;
076                }
077                break;
078            default: // fall out
079            }
080        }
081
082        return new Failure(compressFailureError, stanzaError);
083    }
084
085}