package org.jivesoftware.smack.provider;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public abstract class EmbeddedExtensionProvider<PE extends ExtensionElement> extends ExtensionElementProvider<PE> {
   public final PE parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException {
      String namespace = parser.getNamespace();
      String name = parser.getName();
      int attributeCount = parser.getAttributeCount();
      Map<String, String> attMap = new HashMap(attributeCount);

      for(int i = 0; i < attributeCount; ++i) {
         attMap.put(parser.getAttributeName(i), parser.getAttributeValue(i));
      }

      ArrayList extensions = new ArrayList();

      int event;
      do {
         event = parser.next();
         if (event == 2) {
            PacketParserUtils.addExtensionElement((Collection)extensions, parser);
         }
      } while(event != 3 || parser.getDepth() != initialDepth);

      return this.createReturnExtension(name, namespace, attMap, extensions);
   }

   protected abstract PE createReturnExtension(String var1, String var2, Map<String, String> var3, List<? extends ExtensionElement> var4);
}
