001/*
002 *
003 * Copyright © 2014-2021 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.delay.provider;
018
019import java.io.IOException;
020import java.text.ParseException;
021import java.util.Date;
022
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.provider.ExtensionElementProvider;
025import org.jivesoftware.smack.xml.XmlPullParser;
026import org.jivesoftware.smack.xml.XmlPullParserException;
027
028import org.jivesoftware.smackx.delay.packet.DelayInformation;
029
030import org.jxmpp.JxmppContext;
031
032public abstract class AbstractDelayInformationProvider extends ExtensionElementProvider<DelayInformation> {
033
034    @Override
035    public final DelayInformation parse(XmlPullParser parser,
036                    int initialDepth, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException,
037                    IOException, ParseException {
038        String stampString = parser.getAttributeValue("", "stamp");
039        String from = parser.getAttributeValue("", "from");
040        final String reason;
041        XmlPullParser.Event event = parser.next();
042        switch (event) {
043        case TEXT_CHARACTERS:
044            reason = parser.getText();
045            parser.next();
046            break;
047        case END_ELEMENT:
048            reason = null;
049            break;
050        default:
051            // TODO: Should be SmackParseException.
052            throw new IOException("Unexpected event: " + event);
053        }
054
055        Date stamp = parseDate(stampString);
056        return new DelayInformation(stamp, from, reason);
057    }
058
059    protected abstract Date parseDate(String string) throws ParseException;
060}