001/** 002 * 003 * Copyright 2014 Andriy Tsykholyas 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.hoxt.provider; 018 019import java.io.IOException; 020 021import org.jivesoftware.smack.provider.ExtensionElementProvider; 022import org.jivesoftware.smackx.hoxt.packet.Base64BinaryChunk; 023import org.xmlpull.v1.XmlPullParser; 024import org.xmlpull.v1.XmlPullParserException; 025 026/** 027 * Stanza(/Packet) provider for base64 binary chunks. 028 * 029 * @author Andriy Tsykholyas 030 * @see <a href="http://xmpp.org/extensions/xep-0332.html">XEP-0332: HTTP over XMPP transport</a> 031 */ 032public class Base64BinaryChunkProvider extends ExtensionElementProvider<Base64BinaryChunk> { 033 034 @Override 035 public Base64BinaryChunk parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { 036 String streamId = parser.getAttributeValue("", Base64BinaryChunk.ATTRIBUTE_STREAM_ID); 037 String nrString = parser.getAttributeValue("", Base64BinaryChunk.ATTRIBUTE_NR); 038 String lastString = parser.getAttributeValue("", Base64BinaryChunk.ATTRIBUTE_LAST); 039 boolean last = false; 040 int nr = Integer.parseInt(nrString); 041 042 if (lastString != null) { 043 last = Boolean.parseBoolean(lastString); 044 } 045 046 String text = null; 047 boolean done = false; 048 049 while (!done) { 050 int eventType = parser.next(); 051 052 if (eventType == XmlPullParser.END_TAG) { 053 if (parser.getName().equals(Base64BinaryChunk.ELEMENT_CHUNK)) { 054 done = true; 055 } else { 056 throw new IllegalArgumentException("unexpected end tag of: " + parser.getName()); 057 } 058 } else if (eventType == XmlPullParser.TEXT) { 059 text = parser.getText(); 060 } else { 061 throw new IllegalArgumentException("unexpected eventType: " + eventType); 062 } 063 } 064 065 return new Base64BinaryChunk(text, streamId, nr, last); 066 } 067}