001/*
002 *
003 * Copyright 2018 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.reference.provider;
018
019import java.io.IOException;
020import java.net.URI;
021import java.net.URISyntaxException;
022
023import org.jivesoftware.smack.packet.XmlElement;
024import org.jivesoftware.smack.packet.XmlEnvironment;
025import org.jivesoftware.smack.parsing.SmackParsingException;
026import org.jivesoftware.smack.provider.ExtensionElementProvider;
027import org.jivesoftware.smack.provider.ProviderManager;
028import org.jivesoftware.smack.util.ParserUtils;
029import org.jivesoftware.smack.xml.XmlPullParser;
030import org.jivesoftware.smack.xml.XmlPullParserException;
031
032import org.jivesoftware.smackx.reference.element.ReferenceElement;
033
034import org.jxmpp.JxmppContext;
035
036public class ReferenceProvider extends ExtensionElementProvider<ReferenceElement> {
037
038    public static final ReferenceProvider TEST_PROVIDER = new ReferenceProvider();
039
040    @Override
041    public ReferenceElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException, SmackParsingException {
042        Integer begin = ParserUtils.getIntegerAttribute(parser, ReferenceElement.ATTR_BEGIN);
043        Integer end =   ParserUtils.getIntegerAttribute(parser, ReferenceElement.ATTR_END);
044        String typeString = parser.getAttributeValue(null, ReferenceElement.ATTR_TYPE);
045        ReferenceElement.Type type = ReferenceElement.Type.valueOf(typeString);
046        String anchor = parser.getAttributeValue(null, ReferenceElement.ATTR_ANCHOR);
047        String uriString = parser.getAttributeValue(null, ReferenceElement.ATTR_URI);
048        URI uri;
049        try {
050            uri = uriString != null ? new URI(uriString) : null;
051        }
052        catch (URISyntaxException e) {
053            // TODO: Should be SmackParseException and probably be factored into ParserUtils.
054            throw new IOException(e);
055        }
056        XmlElement child = null;
057        outerloop: while (true) {
058            XmlPullParser.Event eventType = parser.next();
059            if (eventType == XmlPullParser.Event.START_ELEMENT) {
060                String elementName = parser.getName();
061                String namespace = parser.getNamespace();
062                ExtensionElementProvider<?> provider = ProviderManager.getExtensionProvider(elementName, namespace);
063                if (provider != null) {
064                    child = provider.parse(parser);
065                }
066            }
067            else if (eventType == XmlPullParser.Event.END_ELEMENT) {
068                break outerloop;
069            }
070        }
071
072        return new ReferenceElement(begin, end, type, anchor, uri, child);
073    }
074}