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 java.util.ArrayList;
023
024import org.jivesoftware.smackx.hashes.element.HashElement;
025import org.jivesoftware.smackx.hashes.provider.HashElementProvider;
026import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement;
027import org.jivesoftware.smackx.jingle.provider.JingleContentDescriptionProvider;
028import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
029import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild;
030import org.jivesoftware.smackx.jingle_filetransfer.element.Range;
031
032import org.jxmpp.util.XmppDateTime;
033import org.xmlpull.v1.XmlPullParser;
034
035/**
036 * Provider for JingleContentDescriptionFileTransfer elements.
037 */
038public class JingleFileTransferProvider
039        extends JingleContentDescriptionProvider<JingleFileTransfer> {
040
041    @Override
042    public JingleFileTransfer parse(XmlPullParser parser, int initialDepth) throws Exception {
043        ArrayList<JingleContentDescriptionChildElement> payloads = new ArrayList<>();
044        boolean inRange = false;
045        JingleFileTransferChild.Builder builder = JingleFileTransferChild.getBuilder();
046        HashElement inRangeHash = null;
047
048        int offset = 0;
049        int length = -1;
050
051        while (true) {
052
053            int tag = parser.nextTag();
054            String elem = parser.getName();
055
056            if (tag == START_TAG) {
057                switch (elem) {
058                    case JingleFileTransferChild.ELEM_DATE:
059                        builder.setDate(XmppDateTime.parseXEP0082Date(parser.nextText()));
060                        break;
061
062                    case JingleFileTransferChild.ELEM_DESC:
063                        builder.setDescription(parser.nextText());
064                        break;
065
066                    case JingleFileTransferChild.ELEM_MEDIA_TYPE:
067                        builder.setMediaType(parser.nextText());
068                        break;
069
070                    case JingleFileTransferChild.ELEM_NAME:
071                        builder.setName(parser.nextText());
072                        break;
073
074                    case JingleFileTransferChild.ELEM_SIZE:
075                        builder.setSize(Integer.parseInt(parser.nextText()));
076                        break;
077
078                    case Range.ELEMENT:
079                        inRange = true;
080                        String offsetString = parser.getAttributeValue(null, Range.ATTR_OFFSET);
081                        String lengthString = parser.getAttributeValue(null, Range.ATTR_LENGTH);
082                        offset = (offsetString != null ? Integer.parseInt(offsetString) : 0);
083                        length = (lengthString != null ? Integer.parseInt(lengthString) : -1);
084
085                        if (parser.isEmptyElementTag()) {
086                            inRange = false;
087                            builder.setRange(new Range(offset, length));
088                        }
089                        break;
090
091                    case HashElement.ELEMENT:
092                        if (inRange) {
093                            inRangeHash = new HashElementProvider().parse(parser);
094                        } else {
095                            builder.setHash(new HashElementProvider().parse(parser));
096                        }
097                        break;
098                }
099
100            } else if (tag == END_TAG) {
101                switch (elem) {
102
103                    case Range.ELEMENT:
104                        inRange = false;
105                        builder.setRange(new Range(offset, length, inRangeHash));
106                        inRangeHash = null;
107                        break;
108
109                    case JingleFileTransferChild.ELEMENT:
110                        payloads.add(builder.build());
111                        builder = JingleFileTransferChild.getBuilder();
112                        break;
113
114                    case JingleFileTransfer.ELEMENT:
115                        return new JingleFileTransfer(payloads);
116                }
117            }
118        }
119    }
120}