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 034/** 035 * Provider for the Checksum element. 036 */ 037public class ChecksumProvider extends ExtensionElementProvider<Checksum> { 038 @Override 039 public Checksum parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException { 040 JingleContent.Creator creator = null; 041 String creatorString = parser.getAttributeValue(null, Checksum.ATTR_CREATOR); 042 if (creatorString != null) { 043 creator = JingleContent.Creator.valueOf(creatorString); 044 } 045 String name = parser.getAttributeValue(null, Checksum.ATTR_NAME); 046 047 048 JingleFileTransferChild.Builder cb = JingleFileTransferChild.getBuilder(); 049 HashElement hashElement = null; 050 Range range = null; 051 052 boolean go = true; 053 while (go) { 054 XmlPullParser.TagEvent tag = parser.nextTag(); 055 String n = parser.getText(); 056 057 switch (tag) { 058 case START_ELEMENT: 059 switch (n) { 060 case HashElement.ELEMENT: 061 hashElement = new HashElementProvider().parse(parser); 062 break; 063 064 case Range.ELEMENT: 065 String offset = parser.getAttributeValue(null, Range.ATTR_OFFSET); 066 String length = parser.getAttributeValue(null, Range.ATTR_LENGTH); 067 int o = offset == null ? 0 : Integer.parseInt(offset); 068 int l = length == null ? -1 : Integer.parseInt(length); 069 range = new Range(o, l); 070 } 071 break; 072 case END_ELEMENT: 073 switch (n) { 074 case Range.ELEMENT: 075 if (hashElement != null && range != null) { 076 range = new Range(range.getOffset(), range.getLength(), hashElement); 077 hashElement = null; 078 } 079 break; 080 081 case JingleFileTransferChild.ELEMENT: 082 if (hashElement != null) { 083 cb.setHash(hashElement); 084 } 085 if (range != null) { 086 cb.setRange(range); 087 } 088 go = false; 089 } 090 break; 091 } 092 } 093 return new Checksum(creator, name, cb.build()); 094 } 095}