001/**
002 *
003 * Copyright 2003-2007 Jive Software, 2014 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.xhtmlim.provider;
018
019import org.jivesoftware.smack.packet.Message;
020import org.jivesoftware.smack.provider.ExtensionElementProvider;
021import org.jivesoftware.smack.util.PacketParserUtils;
022import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension;
023import org.xmlpull.v1.XmlPullParser;
024import org.xmlpull.v1.XmlPullParserException;
025
026import java.io.IOException;
027
028/**
029 * The XHTMLExtensionProvider parses XHTML packets.
030 *
031 * @author Florian Schmaus
032 */
033public class XHTMLExtensionProvider extends ExtensionElementProvider<XHTMLExtension> {
034
035    @Override
036    public XHTMLExtension parse(XmlPullParser parser, int initialDepth) throws IOException, XmlPullParserException {
037        XHTMLExtension xhtmlExtension = new XHTMLExtension();
038
039        while (true) {
040            int eventType = parser.getEventType();
041            String name = parser.getName();
042            if (eventType == XmlPullParser.START_TAG) {
043                if (name.equals(Message.BODY)) {
044                    xhtmlExtension.addBody(PacketParserUtils.parseElement(parser));
045                }
046            } else if (eventType == XmlPullParser.END_TAG) {
047                if (parser.getDepth() == initialDepth) {
048                    return xhtmlExtension;
049                }
050            }
051            parser.next();
052        }
053    }
054}