001/** 002 * 003 * Copyright © 2014 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.SmackException; 024import org.jivesoftware.smack.provider.ExtensionElementProvider; 025 026import org.jivesoftware.smackx.delay.packet.DelayInformation; 027 028import org.xmlpull.v1.XmlPullParser; 029import org.xmlpull.v1.XmlPullParserException; 030 031public abstract class AbstractDelayInformationProvider extends ExtensionElementProvider<DelayInformation> { 032 033 @Override 034 public final DelayInformation parse(XmlPullParser parser, 035 int initialDepth) throws XmlPullParserException, 036 IOException, SmackException { 037 String stampString = (parser.getAttributeValue("", "stamp")); 038 String from = parser.getAttributeValue("", "from"); 039 String reason = null; 040 if (!parser.isEmptyElementTag()) { 041 int event = parser.next(); 042 switch (event) { 043 case XmlPullParser.TEXT: 044 reason = parser.getText(); 045 parser.next(); 046 break; 047 case XmlPullParser.END_TAG: 048 reason = ""; 049 break; 050 default: 051 throw new IllegalStateException("Unexpected event: " + event); 052 } 053 } else { 054 parser.next(); 055 } 056 Date stamp; 057 try { 058 stamp = parseDate(stampString); 059 } catch (ParseException e) { 060 throw new SmackException(e); 061 } 062 return new DelayInformation(stamp, from, reason); 063 } 064 065 protected abstract Date parseDate(String string) throws ParseException; 066}