CapsExtensionProvider.java

  1. /**
  2.  *
  3.  * Copyright © 2009 Jonas Ådahl, 2011-2014 Florian Schmaus
  4.  *
  5.  * 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. package org.jivesoftware.smackx.caps.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.SmackException;
  20. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  21. import org.jivesoftware.smackx.caps.EntityCapsManager;
  22. import org.jivesoftware.smackx.caps.packet.CapsExtension;
  23. import org.xmlpull.v1.XmlPullParser;
  24. import org.xmlpull.v1.XmlPullParserException;

  25. public class CapsExtensionProvider extends ExtensionElementProvider<CapsExtension> {

  26.     public CapsExtension parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
  27.             SmackException {
  28.         String hash = null;
  29.         String version = null;
  30.         String node = null;
  31.         if (parser.getEventType() == XmlPullParser.START_TAG
  32.                 && parser.getName().equalsIgnoreCase(EntityCapsManager.ELEMENT)) {
  33.             hash = parser.getAttributeValue(null, "hash");
  34.             version = parser.getAttributeValue(null, "ver");
  35.             node = parser.getAttributeValue(null, "node");
  36.         } else {
  37.             throw new SmackException("Malformed Caps element");
  38.         }

  39.         parser.next();

  40.         if (!(parser.getEventType() == XmlPullParser.END_TAG
  41.                 && parser.getName().equalsIgnoreCase(EntityCapsManager.ELEMENT))) {
  42.             throw new SmackException("Malformed nested Caps element");
  43.         }

  44.         if (hash != null && version != null && node != null) {
  45.             return new CapsExtension(node, version, hash);
  46.         } else {
  47.             throw new SmackException("Caps elment with missing attributes");
  48.         }
  49.     }

  50. }