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