Home | History | Annotate | Download | only in provider
      1 /**
      2  * $RCSfile$
      3  * $Revision$
      4  * $Date$
      5  *
      6  * Copyright 2003-2007 Jive Software.
      7  *
      8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      9  * you may not use this file except in compliance with the License.
     10  * You may obtain a copy of the License at
     11  *
     12  *     http://www.apache.org/licenses/LICENSE-2.0
     13  *
     14  * Unless required by applicable law or agreed to in writing, software
     15  * distributed under the License is distributed on an "AS IS" BASIS,
     16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17  * See the License for the specific language governing permissions and
     18  * limitations under the License.
     19  */
     20 
     21 package org.jivesoftware.smackx.provider;
     22 
     23 import java.text.ParseException;
     24 import java.util.Date;
     25 
     26 import org.jivesoftware.smack.packet.Packet;
     27 import org.jivesoftware.smack.packet.PacketExtension;
     28 import org.jivesoftware.smack.provider.PacketExtensionProvider;
     29 import org.jivesoftware.smack.util.StringUtils;
     30 import org.jivesoftware.smackx.packet.DelayInformation;
     31 import org.xmlpull.v1.XmlPullParser;
     32 
     33 /**
     34  * The DelayInformationProvider parses DelayInformation packets.
     35  *
     36  * @author Gaston Dombiak
     37  * @author Henning Staib
     38  */
     39 public class DelayInformationProvider implements PacketExtensionProvider {
     40 
     41     public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
     42         String stampString = (parser.getAttributeValue("", "stamp"));
     43         Date stamp = null;
     44 
     45         try {
     46             stamp = StringUtils.parseDate(stampString);
     47         }
     48         catch (ParseException parseExc) {
     49             /*
     50              * if date could not be parsed but XML is valid, don't shutdown
     51              * connection by throwing an exception instead set timestamp to epoch
     52              * so that it is obviously wrong.
     53              */
     54             if (stamp == null) {
     55                 stamp = new Date(0);
     56             }
     57         }
     58 
     59 
     60         DelayInformation delayInformation = new DelayInformation(stamp);
     61         delayInformation.setFrom(parser.getAttributeValue("", "from"));
     62         String reason = parser.nextText();
     63 
     64         /*
     65          * parser.nextText() returns empty string if there is no reason.
     66          * DelayInformation API specifies that null should be returned in that
     67          * case.
     68          */
     69         reason = "".equals(reason) ? null : reason;
     70         delayInformation.setReason(reason);
     71 
     72         return delayInformation;
     73     }
     74 }
     75