Base64BinaryChunkProvider.java

  1. /**
  2.  *
  3.  * Copyright 2014 Andriy Tsykholyas
  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.hoxt.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  20. import org.jivesoftware.smackx.hoxt.packet.Base64BinaryChunk;
  21. import org.xmlpull.v1.XmlPullParser;
  22. import org.xmlpull.v1.XmlPullParserException;

  23. /**
  24.  * Packet provider for base64 binary chunks.
  25.  *
  26.  * @author Andriy Tsykholyas
  27.  * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a>
  28.  */
  29. public class Base64BinaryChunkProvider extends ExtensionElementProvider<Base64BinaryChunk> {

  30.     @Override
  31.     public Base64BinaryChunk parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
  32.         String streamId = parser.getAttributeValue("", Base64BinaryChunk.ATTRIBUTE_STREAM_ID);
  33.         String nrString = parser.getAttributeValue("", Base64BinaryChunk.ATTRIBUTE_NR);
  34.         String lastString = parser.getAttributeValue("", Base64BinaryChunk.ATTRIBUTE_LAST);
  35.         boolean last = false;
  36.         int nr = Integer.parseInt(nrString);

  37.         if (lastString != null) {
  38.             last = Boolean.parseBoolean(lastString);
  39.         }

  40.         String text = null;
  41.         boolean done = false;

  42.         while (!done) {
  43.             int eventType = parser.next();

  44.             if (eventType == XmlPullParser.END_TAG) {
  45.                 if (parser.getName().equals(Base64BinaryChunk.ELEMENT_CHUNK)) {
  46.                     done = true;
  47.                 } else {
  48.                     throw new IllegalArgumentException("unexpected end tag of: " + parser.getName());
  49.                 }
  50.             } else if (eventType == XmlPullParser.TEXT) {
  51.                 text = parser.getText();
  52.             } else {
  53.                 throw new IllegalArgumentException("unexpected eventType: " + eventType);
  54.             }
  55.         }

  56.         return new Base64BinaryChunk(text, streamId, nr, last);
  57.     }
  58. }