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 org.jivesoftware.smackx.workgroup.util.ModelUtil;
021import org.jivesoftware.smack.packet.IQ;
022import org.jivesoftware.smack.provider.IQProvider;
023import org.xmlpull.v1.XmlPullParser;
024
025import java.util.HashMap;
026import java.util.Map;
027
028public class GenericSettings extends IQ {
029
030    private Map<String, String> map = new HashMap<String, String>();
031
032    private String query;
033
034    public String getQuery() {
035        return query;
036    }
037
038    public void setQuery(String query) {
039        this.query = query;
040    }
041
042    public Map<String, String> getMap() {
043        return map;
044    }
045
046    public void setMap(Map<String, String> map) {
047        this.map = map;
048    }
049
050
051    /**
052     * Element name of the packet extension.
053     */
054    public static final String ELEMENT_NAME = "generic-metadata";
055
056    /**
057     * Namespace of the packet extension.
058     */
059    public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
060
061    public String getChildElementXML() {
062        StringBuilder buf = new StringBuilder();
063
064        buf.append("<").append(ELEMENT_NAME).append(" xmlns=");
065        buf.append('"');
066        buf.append(NAMESPACE);
067        buf.append('"');
068        buf.append(">");
069        if (ModelUtil.hasLength(getQuery())) {
070            buf.append("<query>" + getQuery() + "</query>");
071        }
072        buf.append("</").append(ELEMENT_NAME).append("> ");
073        return buf.toString();
074    }
075
076
077    /**
078     * Packet extension provider for SoundSetting Packets.
079     */
080    public static class InternalProvider implements IQProvider {
081
082        public IQ parseIQ(XmlPullParser parser) throws Exception {
083            if (parser.getEventType() != XmlPullParser.START_TAG) {
084                throw new IllegalStateException("Parser not in proper position, or bad XML.");
085            }
086
087            GenericSettings setting = new GenericSettings();
088
089            boolean done = false;
090
091
092            while (!done) {
093                int eventType = parser.next();
094                if ((eventType == XmlPullParser.START_TAG) && ("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.END_TAG && ELEMENT_NAME.equals(parser.getName())) {
102                    done = true;
103                }
104            }
105
106            return setting;
107        }
108    }
109
110
111}
112