Project

General

Profile

Bug #342 » DelayInformationProvider.java

santhosh b, 02/04/2011 02:01 PM

 
1
/**
2
 * $RCSfile$
3
 * $Revision: 7071 $
4
 * $Date: 2007-02-12 01:59:05 +0100 (lun. 12 févr. 2007) $
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 org.jivesoftware.smack.packet.PacketExtension;
24
import org.jivesoftware.smack.provider.PacketExtensionProvider;
25
import org.jivesoftware.smackx.packet.DelayInformation;
26
import org.xmlpull.v1.XmlPullParser;
27

    
28
import java.text.ParseException;
29
import java.text.SimpleDateFormat;
30
import java.util.Date;
31
import java.util.TimeZone;
32

    
33
/**
34
 * The DelayInformationProvider parses DelayInformation packets.
35
 *
36
 * @author Gaston Dombiak
37
 */
38
public class DelayInformationProvider implements PacketExtensionProvider {
39

    
40
    /**
41
     * Creates a new DeliveryInformationProvider.
42
     * ProviderManager requires that every PacketExtensionProvider has a public, no-argument
43
     * constructor
44
     */
45
    public DelayInformationProvider() {
46
    }
47

    
48
    public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
49
        Date stamp = null;
50
        try {
51
            synchronized (DelayInformation.UTC_FORMAT) {
52
                stamp = DelayInformation.UTC_FORMAT.parse(parser.getAttributeValue("", "stamp"));
53
            }
54
        } catch (ParseException e) {
55
            // Try again but assuming that the date follows JEP-82 format
56
            // (Jabber Date and Time Profiles) 
57
            try {
58
                synchronized (DelayInformation.NEW_UTC_FORMAT) {
59
                    stamp = DelayInformation.NEW_UTC_FORMAT
60
                            .parse(parser.getAttributeValue("", "stamp"));
61
                }
62
            } catch (ParseException e1) {
63
                // Last attempt. Try parsing the date assuming that it does not include milliseconds
64
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
65
                formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
66
                stamp = formatter.parse(parser.getAttributeValue("", "stamp"));
67
            }
68
        }
69
        DelayInformation delayInformation = new DelayInformation(stamp);
70
        delayInformation.setFrom(parser.getAttributeValue("", "from"));
71
        delayInformation.setReason(parser.nextText());
72
        return delayInformation;
73
    }
74

    
75
}
(2-2/2)