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.time.provider;
018
019import java.io.IOException;
020import java.text.ParseException;
021
022import org.jivesoftware.smack.packet.IqData;
023import org.jivesoftware.smack.packet.XmlEnvironment;
024import org.jivesoftware.smack.provider.IqProvider;
025import org.jivesoftware.smack.xml.XmlPullParser;
026import org.jivesoftware.smack.xml.XmlPullParserException;
027
028import org.jivesoftware.smackx.time.packet.Time;
029import org.jivesoftware.smackx.time.packet.TimeBuilder;
030
031import org.jxmpp.JxmppContext;
032
033public class TimeProvider extends IqProvider<Time> {
034
035    @Override
036    public Time parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext)
037                    throws XmlPullParserException, IOException, ParseException {
038        String utc = null, tzo = null;
039        TimeBuilder timeBuilder = Time.builder(iqData);
040
041        outerloop: while (true) {
042            XmlPullParser.Event eventType = parser.next();
043            switch (eventType) {
044            case START_ELEMENT:
045                String name = parser.getName();
046                switch (name) {
047                case "utc":
048                    utc = parser.nextText();
049                    break;
050                case "tzo":
051                    tzo = parser.nextText();
052                    break;
053                }
054                break;
055            case END_ELEMENT:
056                if (parser.getDepth() == initialDepth) {
057                    break outerloop;
058                }
059                break;
060            default:
061                break;
062            }
063        }
064
065        if (utc != null) {
066            timeBuilder.setUtcAndTzo(utc, tzo);
067        }
068
069        return timeBuilder.build();
070    }
071
072}