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