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