001/*
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.workgroup.settings;
019
020import java.io.IOException;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.jivesoftware.smack.packet.IQ;
025import org.jivesoftware.smack.packet.IqData;
026import org.jivesoftware.smack.packet.XmlEnvironment;
027import org.jivesoftware.smack.provider.IqProvider;
028import org.jivesoftware.smack.util.StringUtils;
029import org.jivesoftware.smack.xml.XmlPullParser;
030import org.jivesoftware.smack.xml.XmlPullParserException;
031
032import org.jxmpp.JxmppContext;
033
034public class GenericSettings extends IQ {
035
036    private Map<String, String> map = new HashMap<>();
037
038    private String query;
039
040    public String getQuery() {
041        return query;
042    }
043
044    public void setQuery(String query) {
045        this.query = query;
046    }
047
048    public Map<String, String> getMap() {
049        return map;
050    }
051
052    public void setMap(Map<String, String> map) {
053        this.map = map;
054    }
055
056
057    /**
058     * Element name of the stanza extension.
059     */
060    public static final String ELEMENT_NAME = "generic-metadata";
061
062    /**
063     * Namespace of the stanza extension.
064     */
065    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
066
067    public GenericSettings() {
068        super(ELEMENT_NAME, NAMESPACE);
069    }
070
071    @Override
072    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
073        buf.append('>');
074        if (StringUtils.isNotEmpty(getQuery())) {
075            buf.append("<query>" + getQuery() + "</query>");
076        }
077        return buf;
078    }
079
080    /**
081     * Stanza extension provider for SoundSetting Packets.
082     */
083    public static class InternalProvider extends IqProvider<GenericSettings> {
084
085        @Override
086        public GenericSettings parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException {
087            GenericSettings setting = new GenericSettings();
088
089            boolean done = false;
090
091
092            while (!done) {
093                XmlPullParser.Event eventType = parser.next();
094                if (eventType == XmlPullParser.Event.START_ELEMENT && "entry".equals(parser.getName())) {
095                    eventType = parser.next();
096                    String name = parser.nextText();
097                    eventType = parser.next();
098                    String value = parser.nextText();
099                    setting.getMap().put(name, value);
100                }
101                else if (eventType == XmlPullParser.Event.END_ELEMENT && ELEMENT_NAME.equals(parser.getName())) {
102                    done = true;
103                }
104            }
105
106            return setting;
107        }
108    }
109
110
111}
112