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 org.jivesoftware.smack.provider.ExtensionElementProvider;
020import org.jivesoftware.smackx.httpfileupload.element.FileTooLargeError;
021import org.jivesoftware.smackx.httpfileupload.element.FileTooLargeError_V0_2;
022import org.xmlpull.v1.XmlPullParser;
023
024/**
025 * Provider for File Too Large error extension.
026 *
027 * @author Grigory Fedorov
028 * @see <a href="http://xmpp.org/extensions/xep-0363.html">XEP-0363: HTTP File Upload</a>
029 */
030public class FileTooLargeErrorProvider extends ExtensionElementProvider<FileTooLargeError> {
031
032    @Override
033    public FileTooLargeError parse(XmlPullParser parser, int initialDepth) throws Exception {
034        final String namespace = parser.getNamespace();
035        Long maxFileSize = null;
036
037        outerloop: while(true) {
038            int event = parser.next();
039
040            switch (event) {
041                case XmlPullParser.START_TAG:
042                    String name = parser.getName();
043                    switch(name) {
044                        case "max-file-size":
045                            maxFileSize = Long.valueOf(parser.nextText());
046                            break;
047                    }
048                    break;
049                case XmlPullParser.END_TAG:
050                    if (parser.getDepth() == initialDepth) {
051                        break outerloop;
052                    }
053                    break;
054            }
055        }
056
057        switch (namespace) {
058        case FileTooLargeError.NAMESPACE:
059            return new FileTooLargeError(maxFileSize);
060        case FileTooLargeError_V0_2.NAMESPACE:
061            return new FileTooLargeError_V0_2(maxFileSize);
062        default:
063            throw new AssertionError();
064        }
065    }
066}