DefaultPrivateData.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.smackx.iqprivate.packet;

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

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

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

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

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

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

  74.     public String toXML() {
  75.         StringBuilder buf = new StringBuilder();
  76.         buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\">");
  77.         for (String name : getNames()) {
  78.             String value = getValue(name);
  79.             buf.append("<").append(name).append(">");
  80.             buf.append(value);
  81.             buf.append("</").append(name).append(">");
  82.         }
  83.         buf.append("</").append(elementName).append(">");
  84.         return buf.toString();
  85.     }

  86.     /**
  87.      * Returns a Set of the names that can be used to get
  88.      * values of the private data.
  89.      *
  90.      * @return a Set of the names.
  91.      */
  92.     public synchronized Set<String> getNames() {
  93.         if (map == null) {
  94.             return Collections.<String>emptySet();
  95.         }
  96.         return Collections.unmodifiableSet(map.keySet());
  97.     }

  98.     /**
  99.      * Returns a 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 (String)map.get(name);
  109.     }

  110.     /**
  111.      * Sets a value given the 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. }