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