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.iqprivate.packet;
019
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.Map;
023import java.util.Set;
024
025/**
026 * Default implementation of the PrivateData interface. Unless a PrivateDataProvider
027 * is registered with the PrivateDataManager class, instances of this class will be
028 * returned when getting private data.<p>
029 *
030 * This class provides a very simple representation of an XML sub-document. Each element
031 * is a key in a Map with its CDATA being the value. For example, given the following
032 * XML sub-document:
033 *
034 * <pre>
035 * &lt;foo xmlns="http://bar.com"&gt;
036 *     &lt;color&gt;blue&lt;/color&gt;
037 *     &lt;food&gt;pizza&lt;/food&gt;
038 * &lt;/foo&gt;</pre>
039 *
040 * In this case, getValue("color") would return "blue", and getValue("food") would
041 * return "pizza". This parsing mechanism mechanism is very simplistic and will not work
042 * as desired in all cases (for example, if some of the elements have attributes. In those
043 * cases, a custom {@link org.jivesoftware.smackx.iqprivate.provider.PrivateDataProvider} should be used.
044 *
045 * @author Matt Tucker
046 */
047public class DefaultPrivateData implements PrivateData {
048
049    private String elementName;
050    private String namespace;
051    private Map<String, String> map;
052
053    /**
054     * Creates a new generic private data object.
055     *
056     * @param elementName the name of the element of the XML sub-document.
057     * @param namespace the namespace of the element.
058     */
059    public DefaultPrivateData(String elementName, String namespace) {
060        this.elementName = elementName;
061        this.namespace = namespace;
062    }
063
064     /**
065     * Returns the XML element name of the private data sub-packet root element.
066     *
067     * @return the XML element name of the packet extension.
068     */
069    public String getElementName() {
070        return elementName;
071    }
072
073    /**
074     * Returns the XML namespace of the private data sub-packet root element.
075     *
076     * @return the XML namespace of the packet extension.
077     */
078    public String getNamespace() {
079        return namespace;
080    }
081
082    public String toXML() {
083        StringBuilder buf = new StringBuilder();
084        buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\">");
085        for (String name : getNames()) {
086            String value = getValue(name);
087            buf.append("<").append(name).append(">");
088            buf.append(value);
089            buf.append("</").append(name).append(">");
090        }
091        buf.append("</").append(elementName).append(">");
092        return buf.toString();
093    }
094
095    /**
096     * Returns a Set of the names that can be used to get
097     * values of the private data.
098     *
099     * @return a Set of the names.
100     */
101    public synchronized Set<String> getNames() {
102        if (map == null) {
103            return Collections.<String>emptySet();
104        }
105        return Collections.unmodifiableSet(map.keySet());
106    }
107
108    /**
109     * Returns a value given a name.
110     *
111     * @param name the name.
112     * @return the value.
113     */
114    public synchronized String getValue(String name) {
115        if (map == null) {
116            return null;
117        }
118        return (String)map.get(name);
119    }
120
121    /**
122     * Sets a value given the name.
123     *
124     * @param name the name.
125     * @param value the value.
126     */
127    public synchronized void setValue(String name, String value) {
128        if (map == null) {
129            map = new HashMap<String,String>();
130        }
131        map.put(name, value);
132    }
133}