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