001/*
002 *
003 * Copyright 2019 Florian Schmaus
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.mediaelement.provider;
018
019import java.io.IOException;
020import java.net.URI;
021import java.util.logging.Logger;
022
023import javax.xml.namespace.QName;
024
025import org.jivesoftware.smack.datatypes.UInt16;
026import org.jivesoftware.smack.packet.XmlEnvironment;
027import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException;
028import org.jivesoftware.smack.util.ParserUtils;
029import org.jivesoftware.smack.xml.XmlPullParser;
030import org.jivesoftware.smack.xml.XmlPullParserException;
031
032import org.jivesoftware.smackx.mediaelement.element.MediaElement;
033import org.jivesoftware.smackx.xdata.provider.FormFieldChildElementProvider;
034
035import org.jxmpp.JxmppContext;
036
037public class MediaElementProvider extends FormFieldChildElementProvider<MediaElement> {
038
039    private static final Logger LOGGER = Logger.getLogger(MediaElementProvider.class.getName());
040
041    @Override
042    public QName getQName() {
043        return MediaElement.QNAME;
044    }
045
046    @Override
047    public MediaElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws IOException, XmlPullParserException, SmackUriSyntaxParsingException {
048        UInt16 height = ParserUtils.getUInt16Attribute(parser, "height");
049        UInt16 width = ParserUtils.getUInt16Attribute(parser, "width");
050
051        MediaElement.Builder mediaElementBuilder = MediaElement.builder();
052        if (height != null && width != null) {
053            mediaElementBuilder.setHeightAndWidth(height, width);
054        } else if (height != null || width != null) {
055            LOGGER.warning("Only one of height and width set while parsing media element");
056        }
057
058        outerloop: while (true) {
059            XmlPullParser.TagEvent event = parser.nextTag();
060            switch (event) {
061            case START_ELEMENT:
062                QName qname = parser.getQName();
063                if (qname.equals(MediaElement.Uri.QNAME)) {
064                    MediaElement.Uri uri = parseUri(parser);
065                    mediaElementBuilder.addUri(uri);
066                }
067                break;
068            case END_ELEMENT:
069                if (parser.getDepth() == initialDepth) {
070                    break outerloop;
071                }
072                break;
073            }
074        }
075
076        return mediaElementBuilder.build();
077    }
078
079    private static MediaElement.Uri parseUri(XmlPullParser parser)
080                    throws SmackUriSyntaxParsingException, XmlPullParserException, IOException {
081        String type = parser.getAttributeValue("type");
082        URI uri = ParserUtils.getUriFromNextText(parser);
083        return new MediaElement.Uri(uri, type);
084    }
085}