FileTooLargeErrorProvider.java

  1. /**
  2.  *
  3.  * Copyright © 2017 Grigory Fedorov
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.httpfileupload.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.XmlEnvironment;
  20. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  21. import org.jivesoftware.smack.xml.XmlPullParser;
  22. import org.jivesoftware.smack.xml.XmlPullParserException;

  23. import org.jivesoftware.smackx.httpfileupload.element.FileTooLargeError;
  24. import org.jivesoftware.smackx.httpfileupload.element.FileTooLargeError_V0_2;

  25. /**
  26.  * Provider for File Too Large error extension.
  27.  *
  28.  * @author Grigory Fedorov
  29.  * @see <a href="http://xmpp.org/extensions/xep-0363.html">XEP-0363: HTTP File Upload</a>
  30.  */
  31. public class FileTooLargeErrorProvider extends ExtensionElementProvider<FileTooLargeError> {

  32.     @Override
  33.     public FileTooLargeError parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  34.         final String namespace = parser.getNamespace();
  35.         Long maxFileSize = null;

  36.         outerloop: while (true) {
  37.             XmlPullParser.Event event = parser.next();

  38.             switch (event) {
  39.                 case START_ELEMENT:
  40.                     String name = parser.getName();
  41.                     switch (name) {
  42.                         case "max-file-size":
  43.                             maxFileSize = Long.valueOf(parser.nextText());
  44.                             break;
  45.                     }
  46.                     break;
  47.                 case END_ELEMENT:
  48.                     if (parser.getDepth() == initialDepth) {
  49.                         break outerloop;
  50.                     }
  51.                     break;
  52.                 default:
  53.                     // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  54.                     break;
  55.             }
  56.         }

  57.         switch (namespace) {
  58.         case FileTooLargeError.NAMESPACE:
  59.             return new FileTooLargeError(maxFileSize);
  60.         case FileTooLargeError_V0_2.NAMESPACE:
  61.             return new FileTooLargeError_V0_2(maxFileSize);
  62.         default:
  63.             throw new AssertionError();
  64.         }
  65.     }
  66. }