001/*
002 *
003 * Copyright © 2017 Grigory Fedorov
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.httpfileupload.provider;
018
019import java.io.IOException;
020
021import org.jivesoftware.smack.packet.XmlEnvironment;
022import org.jivesoftware.smack.provider.ExtensionElementProvider;
023import org.jivesoftware.smack.xml.XmlPullParser;
024import org.jivesoftware.smack.xml.XmlPullParserException;
025
026import org.jivesoftware.smackx.httpfileupload.element.FileTooLargeError;
027import org.jivesoftware.smackx.httpfileupload.element.FileTooLargeError_V0_2;
028
029import org.jxmpp.JxmppContext;
030
031/**
032 * Provider for File Too Large error extension.
033 *
034 * @author Grigory Fedorov
035 * @see <a href="http://xmpp.org/extensions/xep-0363.html">XEP-0363: HTTP File Upload</a>
036 */
037public class FileTooLargeErrorProvider extends ExtensionElementProvider<FileTooLargeError> {
038
039    @Override
040    public FileTooLargeError parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
041        final String namespace = parser.getNamespace();
042        Long maxFileSize = null;
043
044        outerloop: while (true) {
045            XmlPullParser.Event event = parser.next();
046
047            switch (event) {
048                case START_ELEMENT:
049                    String name = parser.getName();
050                    switch (name) {
051                        case "max-file-size":
052                            maxFileSize = Long.valueOf(parser.nextText());
053                            break;
054                    }
055                    break;
056                case END_ELEMENT:
057                    if (parser.getDepth() == initialDepth) {
058                        break outerloop;
059                    }
060                    break;
061                default:
062                    // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
063                    break;
064            }
065        }
066
067        switch (namespace) {
068        case FileTooLargeError.NAMESPACE:
069            return new FileTooLargeError(maxFileSize);
070        case FileTooLargeError_V0_2.NAMESPACE:
071            return new FileTooLargeError_V0_2(maxFileSize);
072        default:
073            throw new AssertionError();
074        }
075    }
076}