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 * <foo xmlns="http://bar.com"> 036 * <color>blue</color> 037 * <food>pizza</food> 038 * </foo></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 final String elementName; 050 private final 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 stanza extension. 068 */ 069 @Override 070 public String getElementName() { 071 return elementName; 072 } 073 074 /** 075 * Returns the XML namespace of the private data sub-packet root element. 076 * 077 * @return the XML namespace of the stanza extension. 078 */ 079 @Override 080 public String getNamespace() { 081 return namespace; 082 } 083 084 @Override 085 public String toXML() { 086 StringBuilder buf = new StringBuilder(); 087 buf.append('<').append(elementName).append(" xmlns=\"").append(namespace).append("\">"); 088 for (String name : getNames()) { 089 String value = getValue(name); 090 buf.append('<').append(name).append('>'); 091 buf.append(value); 092 buf.append("</").append(name).append('>'); 093 } 094 buf.append("</").append(elementName).append('>'); 095 return buf.toString(); 096 } 097 098 /** 099 * Returns a Set of the names that can be used to get 100 * values of the private data. 101 * 102 * @return a Set of the names. 103 */ 104 public synchronized Set<String> getNames() { 105 if (map == null) { 106 return Collections.emptySet(); 107 } 108 return Collections.unmodifiableSet(map.keySet()); 109 } 110 111 /** 112 * Returns a value given a name. 113 * 114 * @param name the name. 115 * @return the value. 116 */ 117 public synchronized String getValue(String name) { 118 if (map == null) { 119 return null; 120 } 121 return map.get(name); 122 } 123 124 /** 125 * Sets a value given the name. 126 * 127 * @param name the name. 128 * @param value the value. 129 */ 130 public synchronized void setValue(String name, String value) { 131 if (map == null) { 132 map = new HashMap<>(); 133 } 134 map.put(name, value); 135 } 136}