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