diff options
Diffstat (limited to 'src/de/thedevstack/smackx/filetransferhttp/provider')
-rw-r--r-- | src/de/thedevstack/smackx/filetransferhttp/provider/FileListProvider.java | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/src/de/thedevstack/smackx/filetransferhttp/provider/FileListProvider.java b/src/de/thedevstack/smackx/filetransferhttp/provider/FileListProvider.java new file mode 100644 index 0000000..d6738b6 --- /dev/null +++ b/src/de/thedevstack/smackx/filetransferhttp/provider/FileListProvider.java @@ -0,0 +1,118 @@ +package de.thedevstack.smackx.filetransferhttp.provider; + +import java.io.IOException; + +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smack.util.ParserUtils; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +import de.thedevstack.smackx.filetransferhttp.element.FileList; +import de.thedevstack.smackx.filetransferhttp.element.RemoteFile; +import de.thedevstack.smackx.filetransferhttp.element.RemoteFileInfo; + +public class FileListProvider extends IQProvider<FileList> { + + @Override + public FileList parse(XmlPullParser parser, int initialDepth) throws Exception { + String namespace = parser.getNamespace(); + FileList fileList = new FileList(); + + int event = -1; + int currentDepth = -1; + do { + event = parser.next(); + currentDepth = parser.getDepth(); + + if (XmlPullParser.START_TAG == event) { + String name = parser.getName(); + switch (name) { + case "file": + RemoteFile remoteFile = this.parseRemoteFile(parser, currentDepth); + if (null != remoteFile) { + fileList.addRemoteFile(remoteFile); + } + break; + } + } + + } while (event != XmlPullParser.END_TAG && currentDepth != initialDepth); + + return fileList; + } + + protected RemoteFile parseRemoteFile(XmlPullParser parser, int initialDepth) throws IOException, XmlPullParserException { + long timestamp = ParserUtils.getLongAttribute(parser, "timestamp"); + String to = parser.getAttributeValue("", "to"); + String from = parser.getAttributeValue("", "from"); + String url = null; + RemoteFileInfo remoteFileInfo = null; + + int event = -1; + int currentDepth = -1; + do { + event = parser.next(); + currentDepth = parser.getDepth(); + + if (XmlPullParser.START_TAG == event) { + String name = parser.getName(); + switch (name) { + case "url": + url = parser.nextText(); + break; + case "file-info": + remoteFileInfo = parseRemoteFileInfo(parser, currentDepth); + break; + } + } + + } while (event != XmlPullParser.END_TAG && currentDepth != initialDepth); + + RemoteFile remoteFile = new RemoteFile(url, timestamp); + remoteFile.setFileInfo(remoteFileInfo); + remoteFile.setTo(to); + remoteFile.setFrom(from); + + return remoteFile; + } + + protected RemoteFileInfo parseRemoteFileInfo(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { + long size = -1; + String contentType = null; + String filename = null; + + int event = -1; + int currentDepth = -1; + do { + event = parser.next(); + currentDepth = parser.getDepth(); + + if (XmlPullParser.START_TAG == event) { + String name = parser.getName(); + switch (name) { + case "filename": + filename = parser.nextText(); + break; + case "size": + String sizeString = parser.nextText(); + if (null != sizeString) { + size = Long.valueOf(sizeString); + } + break; + case "content-type": + contentType = parser.nextText(); + break; + } + } + + } while (event != XmlPullParser.END_TAG && currentDepth != initialDepth); + + if (0 >= size || null == filename || filename.isEmpty()) { + return null; + } + + return new RemoteFileInfo(filename, size, contentType); + } + +} |