ChatMetadata.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  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.workgroup.ext.history;

  18. import org.jivesoftware.smackx.workgroup.util.MetaDataUtils;
  19. import org.jivesoftware.smack.packet.IQ;
  20. import org.jivesoftware.smack.provider.IQProvider;
  21. import org.xmlpull.v1.XmlPullParser;
  22. import org.xmlpull.v1.XmlPullParserException;

  23. import java.io.IOException;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. public class ChatMetadata extends IQ {

  28.     /**
  29.      * Element name of the packet extension.
  30.      */
  31.     public static final String ELEMENT_NAME = "chat-metadata";

  32.     /**
  33.      * Namespace of the packet extension.
  34.      */
  35.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";


  36.     private String sessionID;

  37.     public ChatMetadata() {
  38.         super(ELEMENT_NAME, NAMESPACE);
  39.     }

  40.     public String getSessionID() {
  41.         return sessionID;
  42.     }

  43.     public void setSessionID(String sessionID) {
  44.         this.sessionID = sessionID;
  45.     }


  46.     private Map<String, List<String>> map = new HashMap<String, List<String>>();

  47.     public void setMetadata(Map<String, List<String>> metadata){
  48.         this.map = metadata;
  49.     }

  50.     public Map<String, List<String>> getMetadata(){
  51.         return map;
  52.     }

  53.     @Override
  54.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  55.         buf.rightAngleBracket();
  56.         buf.append("<sessionID>").append(getSessionID()).append("</sessionID>");

  57.         return buf;
  58.     }

  59.     /**
  60.      * An IQProvider for Metadata packets.
  61.      *
  62.      * @author Derek DeMoro
  63.      */
  64.     public static class Provider extends IQProvider<ChatMetadata> {

  65.         @Override
  66.         public ChatMetadata parse(XmlPullParser parser, int initialDepth)
  67.                         throws XmlPullParserException, IOException {
  68.             final ChatMetadata chatM = new ChatMetadata();

  69.             boolean done = false;
  70.             while (!done) {
  71.                 int eventType = parser.next();
  72.                 if (eventType == XmlPullParser.START_TAG) {
  73.                     if (parser.getName().equals("sessionID")) {
  74.                        chatM.setSessionID(parser.nextText());
  75.                     }
  76.                     else if (parser.getName().equals("metadata")) {
  77.                         Map<String, List<String>> map = MetaDataUtils.parseMetaData(parser);
  78.                         chatM.setMetadata(map);
  79.                     }
  80.                 }
  81.                 else if (eventType == XmlPullParser.END_TAG) {
  82.                     if (parser.getName().equals(ELEMENT_NAME)) {
  83.                         done = true;
  84.                     }
  85.                 }
  86.             }

  87.             return chatM;
  88.         }
  89.     }
  90. }