GenericSettings.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.settings;

  18. import java.io.IOException;
  19. import java.util.HashMap;
  20. import java.util.Map;

  21. import org.jivesoftware.smack.packet.IQ;
  22. import org.jivesoftware.smack.packet.IqData;
  23. import org.jivesoftware.smack.packet.XmlEnvironment;
  24. import org.jivesoftware.smack.provider.IqProvider;
  25. import org.jivesoftware.smack.util.StringUtils;
  26. import org.jivesoftware.smack.xml.XmlPullParser;
  27. import org.jivesoftware.smack.xml.XmlPullParserException;

  28. public class GenericSettings extends IQ {

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

  30.     private String query;

  31.     public String getQuery() {
  32.         return query;
  33.     }

  34.     public void setQuery(String query) {
  35.         this.query = query;
  36.     }

  37.     public Map<String, String> getMap() {
  38.         return map;
  39.     }

  40.     public void setMap(Map<String, String> map) {
  41.         this.map = map;
  42.     }


  43.     /**
  44.      * Element name of the stanza extension.
  45.      */
  46.     public static final String ELEMENT_NAME = "generic-metadata";

  47.     /**
  48.      * Namespace of the stanza extension.
  49.      */
  50.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  51.     public GenericSettings() {
  52.         super(ELEMENT_NAME, NAMESPACE);
  53.     }

  54.     @Override
  55.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  56.         buf.append('>');
  57.         if (StringUtils.isNotEmpty(getQuery())) {
  58.             buf.append("<query>" + getQuery() + "</query>");
  59.         }
  60.         return buf;
  61.     }

  62.     /**
  63.      * Stanza extension provider for SoundSetting Packets.
  64.      */
  65.     public static class InternalProvider extends IqProvider<GenericSettings> {

  66.         @Override
  67.         public GenericSettings parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  68.             GenericSettings setting = new GenericSettings();

  69.             boolean done = false;


  70.             while (!done) {
  71.                 XmlPullParser.Event eventType = parser.next();
  72.                 if (eventType == XmlPullParser.Event.START_ELEMENT && "entry".equals(parser.getName())) {
  73.                     eventType = parser.next();
  74.                     String name = parser.nextText();
  75.                     eventType = parser.next();
  76.                     String value = parser.nextText();
  77.                     setting.getMap().put(name, value);
  78.                 }
  79.                 else if (eventType == XmlPullParser.Event.END_ELEMENT && ELEMENT_NAME.equals(parser.getName())) {
  80.                     done = true;
  81.                 }
  82.             }

  83.             return setting;
  84.         }
  85.     }


  86. }