Home | History | Annotate | Download | only in provider
      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 
     18 package org.jivesoftware.smackx.provider;
     19 
     20 import java.io.IOException;
     21 
     22 import org.jivesoftware.smack.XMPPException;
     23 import org.jivesoftware.smack.packet.PacketExtension;
     24 import org.jivesoftware.smack.provider.PacketExtensionProvider;
     25 import org.jivesoftware.smackx.entitycaps.packet.CapsExtension;
     26 
     27 import org.xmlpull.v1.XmlPullParser;
     28 import org.xmlpull.v1.XmlPullParserException;
     29 
     30 public class CapsExtensionProvider implements PacketExtensionProvider {
     31     private static final int MAX_DEPTH = 10;
     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         int depth = 0;
     39         while (true) {
     40             if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equalsIgnoreCase("c")) {
     41                 hash = parser.getAttributeValue(null, "hash");
     42                 version = parser.getAttributeValue(null, "ver");
     43                 node = parser.getAttributeValue(null, "node");
     44             }
     45 
     46             if (parser.getEventType() == XmlPullParser.END_TAG && parser.getName().equalsIgnoreCase("c")) {
     47                 break;
     48             } else {
     49                 parser.next();
     50             }
     51 
     52             if (depth < MAX_DEPTH) {
     53                 depth++;
     54             } else {
     55                 throw new XMPPException("Malformed caps element");
     56             }
     57         }
     58 
     59         if (hash != null && version != null && node != null) {
     60             return new CapsExtension(node, version, hash);
     61         } else {
     62             throw new XMPPException("Caps elment with missing attributes");
     63         }
     64     }
     65 }
     66