001/* 002 * 003 * Copyright 2017 Paul Schaub 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.jingle_filetransfer.provider; 018 019import java.io.IOException; 020 021import org.jivesoftware.smack.packet.XmlEnvironment; 022import org.jivesoftware.smack.parsing.SmackParsingException; 023import org.jivesoftware.smack.provider.ExtensionElementProvider; 024import org.jivesoftware.smack.xml.XmlPullParser; 025import org.jivesoftware.smack.xml.XmlPullParserException; 026 027import org.jivesoftware.smackx.hashes.element.HashElement; 028import org.jivesoftware.smackx.hashes.provider.HashElementProvider; 029import org.jivesoftware.smackx.jingle.element.JingleContent; 030import org.jivesoftware.smackx.jingle_filetransfer.element.Checksum; 031import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild; 032import org.jivesoftware.smackx.jingle_filetransfer.element.Range; 033 034import org.jxmpp.JxmppContext; 035 036/** 037 * Provider for the Checksum element. 038 */ 039public class ChecksumProvider extends ExtensionElementProvider<Checksum> { 040 @Override 041 public Checksum parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException, SmackParsingException { 042 JingleContent.Creator creator = null; 043 String creatorString = parser.getAttributeValue(null, Checksum.ATTR_CREATOR); 044 if (creatorString != null) { 045 creator = JingleContent.Creator.valueOf(creatorString); 046 } 047 String name = parser.getAttributeValue(null, Checksum.ATTR_NAME); 048 049 050 JingleFileTransferChild.Builder cb = JingleFileTransferChild.getBuilder(); 051 HashElement hashElement = null; 052 Range range = null; 053 054 boolean go = true; 055 while (go) { 056 XmlPullParser.TagEvent tag = parser.nextTag(); 057 String n = parser.getText(); 058 059 switch (tag) { 060 case START_ELEMENT: 061 switch (n) { 062 case HashElement.ELEMENT: 063 hashElement = new HashElementProvider().parse(parser); 064 break; 065 066 case Range.ELEMENT: 067 String offset = parser.getAttributeValue(null, Range.ATTR_OFFSET); 068 String length = parser.getAttributeValue(null, Range.ATTR_LENGTH); 069 int o = offset == null ? 0 : Integer.parseInt(offset); 070 int l = length == null ? -1 : Integer.parseInt(length); 071 range = new Range(o, l); 072 } 073 break; 074 case END_ELEMENT: 075 switch (n) { 076 case Range.ELEMENT: 077 if (hashElement != null && range != null) { 078 range = new Range(range.getOffset(), range.getLength(), hashElement); 079 hashElement = null; 080 } 081 break; 082 083 case JingleFileTransferChild.ELEMENT: 084 if (hashElement != null) { 085 cb.setHash(hashElement); 086 } 087 if (range != null) { 088 cb.setRange(range); 089 } 090 go = false; 091 } 092 break; 093 } 094 } 095 return new Checksum(creator, name, cb.build()); 096 } 097}