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
035public class MediaElementProvider extends FormFieldChildElementProvider<MediaElement> {
036
037    private static final Logger LOGGER = Logger.getLogger(MediaElementProvider.class.getName());
038
039    @Override
040    public QName getQName() {
041        return MediaElement.QNAME;
042    }
043
044    @Override
045    public MediaElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws IOException, XmlPullParserException, SmackUriSyntaxParsingException {
046        UInt16 height = ParserUtils.getUInt16Attribute(parser, "height");
047        UInt16 width = ParserUtils.getUInt16Attribute(parser, "width");
048
049        MediaElement.Builder mediaElementBuilder = MediaElement.builder();
050        if (height != null && width != null) {
051            mediaElementBuilder.setHeightAndWidth(height, width);
052        } else if (height != null || width != null) {
053            LOGGER.warning("Only one of height and width set while parsing media element");
054        }
055
056        outerloop: while (true) {
057            XmlPullParser.TagEvent event = parser.nextTag();
058            switch (event) {
059            case START_ELEMENT:
060                QName qname = parser.getQName();
061                if (qname.equals(MediaElement.Uri.QNAME)) {
062                    MediaElement.Uri uri = parseUri(parser);
063                    mediaElementBuilder.addUri(uri);
064                }
065                break;
066            case END_ELEMENT:
067                if (parser.getDepth() == initialDepth) {
068                    break outerloop;
069                }
070                break;
071            }
072        }
073
074        return mediaElementBuilder.build();
075    }
076
077    private static MediaElement.Uri parseUri(XmlPullParser parser)
078                    throws SmackUriSyntaxParsingException, XmlPullParserException, IOException {
079        String type = parser.getAttributeValue("type");
080        URI uri = ParserUtils.getUriFromNextText(parser);
081        return new MediaElement.Uri(uri, type);
082    }
083}