DefaultExtensionElement.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.smack.packet;

  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.Map;

  22. import org.jivesoftware.smack.util.XmlStringBuilder;

  23. /**
  24.  * Default implementation of the ExtensionElement interface. Unless a ExtensionElementProvider
  25.  * is registered with {@link org.jivesoftware.smack.provider.ProviderManager ProviderManager},
  26.  * instances of this class will be returned when getting packet extensions.<p>
  27.  *
  28.  * This class provides a very simple representation of an XML sub-document. Each element
  29.  * is a key in a Map with its CDATA being the value. For example, given the following
  30.  * XML sub-document:
  31.  *
  32.  * <pre>
  33.  * &lt;foo xmlns="http://bar.com"&gt;
  34.  *     &lt;color&gt;blue&lt;/color&gt;
  35.  *     &lt;food&gt;pizza&lt;/food&gt;
  36.  * &lt;/foo&gt;</pre>
  37.  *
  38.  * In this case, getValue("color") would return "blue", and getValue("food") would
  39.  * return "pizza". This parsing mechanism mechanism is very simplistic and will not work
  40.  * as desired in all cases (for example, if some of the elements have attributes. In those
  41.  * cases, a custom ExtensionElementProvider should be used.
  42.  *
  43.  * @author Matt Tucker
  44.  */
  45. public class DefaultExtensionElement implements ExtensionElement {

  46.     private String elementName;
  47.     private String namespace;
  48.     private Map<String,String> map;

  49.     /**
  50.      * Creates a new generic packet extension.
  51.      *
  52.      * @param elementName the name of the element of the XML sub-document.
  53.      * @param namespace the namespace of the element.
  54.      */
  55.     public DefaultExtensionElement(String elementName, String namespace) {
  56.         this.elementName = elementName;
  57.         this.namespace = namespace;
  58.     }

  59.      /**
  60.      * Returns the XML element name of the extension sub-packet root element.
  61.      *
  62.      * @return the XML element name of the packet extension.
  63.      */
  64.     public String getElementName() {
  65.         return elementName;
  66.     }

  67.     /**
  68.      * Returns the XML namespace of the extension sub-packet root element.
  69.      *
  70.      * @return the XML namespace of the packet extension.
  71.      */
  72.     public String getNamespace() {
  73.         return namespace;
  74.     }

  75.     @Override
  76.     public CharSequence toXML() {
  77.         XmlStringBuilder buf = new XmlStringBuilder();
  78.         buf.halfOpenElement(elementName).xmlnsAttribute(namespace).rightAngleBracket();
  79.         for (String name : getNames()) {
  80.             String value = getValue(name);
  81.             buf.element(name, value);
  82.         }
  83.         buf.closeElement(elementName);
  84.         return buf;
  85.     }

  86.     /**
  87.      * Returns an unmodifiable collection of the names that can be used to get
  88.      * values of the packet extension.
  89.      *
  90.      * @return the names.
  91.      */
  92.     public synchronized Collection<String> getNames() {
  93.         if (map == null) {
  94.             return Collections.emptySet();
  95.         }
  96.         return Collections.unmodifiableSet(new HashMap<String,String>(map).keySet());
  97.     }

  98.     /**
  99.      * Returns a packet extension value given a name.
  100.      *
  101.      * @param name the name.
  102.      * @return the value.
  103.      */
  104.     public synchronized String getValue(String name) {
  105.         if (map == null) {
  106.             return null;
  107.         }
  108.         return map.get(name);
  109.     }

  110.     /**
  111.      * Sets a packet extension value using the given name.
  112.      *
  113.      * @param name the name.
  114.      * @param value the value.
  115.      */
  116.     public synchronized void setValue(String name, String value) {
  117.         if (map == null) {
  118.             map = new HashMap<String,String>();
  119.         }
  120.         map.put(name, value);
  121.     }
  122. }