1/**
2 * Copyright 2009 Jonas Ãdahl.
3 * Copyright 2011-2013 Florian Schmaus
4 *
5 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package org.jivesoftware.smackx.entitycaps.provider;
19
20import java.io.IOException;
21
22import org.jivesoftware.smack.XMPPException;
23import org.jivesoftware.smack.packet.PacketExtension;
24import org.jivesoftware.smack.provider.PacketExtensionProvider;
25import org.jivesoftware.smackx.entitycaps.EntityCapsManager;
26import org.jivesoftware.smackx.entitycaps.packet.CapsExtension;
27
28import org.xmlpull.v1.XmlPullParser;
29import org.xmlpull.v1.XmlPullParserException;
30
31public class CapsExtensionProvider implements PacketExtensionProvider {
32
33    public PacketExtension parseExtension(XmlPullParser parser) throws XmlPullParserException, IOException,
34            XMPPException {
35        String hash = null;
36        String version = null;
37        String node = null;
38        if (parser.getEventType() == XmlPullParser.START_TAG
39                && parser.getName().equalsIgnoreCase(EntityCapsManager.ELEMENT)) {
40            hash = parser.getAttributeValue(null, "hash");
41            version = parser.getAttributeValue(null, "ver");
42            node = parser.getAttributeValue(null, "node");
43        } else {
44            throw new XMPPException("Malformed Caps element");
45        }
46
47        parser.next();
48
49        if (!(parser.getEventType() == XmlPullParser.END_TAG
50                && parser.getName().equalsIgnoreCase(EntityCapsManager.ELEMENT))) {
51            throw new XMPPException("Malformed nested Caps element");
52        }
53
54        if (hash != null && version != null && node != null) {
55            return new CapsExtension(node, version, hash);
56        } else {
57            throw new XMPPException("Caps elment with missing attributes");
58        }
59    }
60}
61