001/*
002 *
003 * Copyright 2019 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.message_fastening.provider;
018
019import java.io.IOException;
020
021import org.jivesoftware.smack.packet.XmlElement;
022import org.jivesoftware.smack.packet.XmlEnvironment;
023import org.jivesoftware.smack.parsing.SmackParsingException;
024import org.jivesoftware.smack.provider.ExtensionElementProvider;
025import org.jivesoftware.smack.util.PacketParserUtils;
026import org.jivesoftware.smack.util.ParserUtils;
027import org.jivesoftware.smack.xml.XmlPullParser;
028import org.jivesoftware.smack.xml.XmlPullParserException;
029
030import org.jivesoftware.smackx.message_fastening.MessageFasteningManager;
031import org.jivesoftware.smackx.message_fastening.element.ExternalElement;
032import org.jivesoftware.smackx.message_fastening.element.FasteningElement;
033
034import org.jxmpp.JxmppContext;
035
036public class FasteningElementProvider extends ExtensionElementProvider<FasteningElement> {
037
038    public static final FasteningElementProvider TEST_INSTANCE = new FasteningElementProvider();
039
040    @Override
041    public FasteningElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException, SmackParsingException {
042        FasteningElement.Builder builder = FasteningElement.builder();
043        builder.setOriginId(parser.getAttributeValue("", FasteningElement.ATTR_ID));
044        if (ParserUtils.getBooleanAttribute(parser, FasteningElement.ATTR_CLEAR, false)) {
045            builder.setClear();
046        }
047        if (ParserUtils.getBooleanAttribute(parser, FasteningElement.ATTR_SHELL, false)) {
048            builder.setShell();
049        }
050
051        outerloop: while (true) {
052            XmlPullParser.Event tag = parser.next();
053            switch (tag) {
054                case START_ELEMENT:
055                    String name = parser.getName();
056                    String namespace = parser.getNamespace();
057
058                    // Parse external payload
059                    if (MessageFasteningManager.NAMESPACE.equals(namespace) && ExternalElement.ELEMENT.equals(name)) {
060                        ExternalElement external = new ExternalElement(
061                                parser.getAttributeValue("", ExternalElement.ATTR_NAME),
062                                parser.getAttributeValue("", ExternalElement.ATTR_ELEMENT_NAMESPACE));
063                        builder.addExternalPayload(external);
064                        continue;
065                    }
066
067                    // Parse wrapped payload
068                    XmlElement wrappedPayload = PacketParserUtils.parseExtensionElement(name, namespace, parser, xmlEnvironment, jxmppContext);
069                    builder.addWrappedPayload(wrappedPayload);
070                    break;
071
072                case END_ELEMENT:
073                    if (parser.getDepth() == initialDepth) {
074                        break outerloop;
075                    }
076                    break;
077                default:
078                    break;
079            }
080        }
081        return builder.build();
082    }
083}