ChecksumProvider.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  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.jingle_filetransfer.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.XmlEnvironment;
  20. import org.jivesoftware.smack.parsing.SmackParsingException;
  21. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  22. import org.jivesoftware.smack.xml.XmlPullParser;
  23. import org.jivesoftware.smack.xml.XmlPullParserException;

  24. import org.jivesoftware.smackx.hashes.element.HashElement;
  25. import org.jivesoftware.smackx.hashes.provider.HashElementProvider;
  26. import org.jivesoftware.smackx.jingle.element.JingleContent;
  27. import org.jivesoftware.smackx.jingle_filetransfer.element.Checksum;
  28. import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild;
  29. import org.jivesoftware.smackx.jingle_filetransfer.element.Range;

  30. /**
  31.  * Provider for the Checksum element.
  32.  */
  33. public class ChecksumProvider extends ExtensionElementProvider<Checksum> {
  34.     @Override
  35.     public Checksum parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
  36.         JingleContent.Creator creator = null;
  37.         String creatorString = parser.getAttributeValue(null, Checksum.ATTR_CREATOR);
  38.         if (creatorString != null) {
  39.             creator = JingleContent.Creator.valueOf(creatorString);
  40.         }
  41.         String name = parser.getAttributeValue(null, Checksum.ATTR_NAME);


  42.         JingleFileTransferChild.Builder cb = JingleFileTransferChild.getBuilder();
  43.         HashElement hashElement = null;
  44.         Range range = null;

  45.         boolean go = true;
  46.         while (go) {
  47.             XmlPullParser.TagEvent tag = parser.nextTag();
  48.             String n = parser.getText();

  49.             switch (tag) {
  50.             case START_ELEMENT:
  51.                 switch (n) {
  52.                     case HashElement.ELEMENT:
  53.                         hashElement = new HashElementProvider().parse(parser);
  54.                         break;

  55.                     case Range.ELEMENT:
  56.                         String offset = parser.getAttributeValue(null, Range.ATTR_OFFSET);
  57.                         String length = parser.getAttributeValue(null, Range.ATTR_LENGTH);
  58.                         int o = offset == null ? 0 : Integer.parseInt(offset);
  59.                         int l = length == null ? -1 : Integer.parseInt(length);
  60.                         range = new Range(o, l);
  61.                 }
  62.                 break;
  63.             case END_ELEMENT:
  64.                 switch (n) {
  65.                     case Range.ELEMENT:
  66.                         if (hashElement != null && range != null) {
  67.                             range = new Range(range.getOffset(), range.getLength(), hashElement);
  68.                             hashElement = null;
  69.                         }
  70.                         break;

  71.                     case JingleFileTransferChild.ELEMENT:
  72.                         if (hashElement != null) {
  73.                             cb.setHash(hashElement);
  74.                         }
  75.                         if (range != null) {
  76.                             cb.setRange(range);
  77.                         }
  78.                         go = false;
  79.                 }
  80.                 break;
  81.             }
  82.         }
  83.         return new Checksum(creator, name, cb.build());
  84.     }
  85. }