1 /** 2 * $RCSfile$ 3 * $Revision$ 4 * $Date$ 5 * 6 * Copyright 2003-2007 Jive Software. 7 * 8 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package org.jivesoftware.smack.packet; 22 23 import java.util.*; 24 25 /** 26 * Default implementation of the PacketExtension interface. Unless a PacketExtensionProvider 27 * is registered with {@link org.jivesoftware.smack.provider.ProviderManager ProviderManager}, 28 * instances of this class will be returned when getting packet extensions.<p> 29 * 30 * This class provides a very simple representation of an XML sub-document. Each element 31 * is a key in a Map with its CDATA being the value. For example, given the following 32 * XML sub-document: 33 * 34 * <pre> 35 * <foo xmlns="http://bar.com"> 36 * <color>blue</color> 37 * <food>pizza</food> 38 * </foo></pre> 39 * 40 * In this case, getValue("color") would return "blue", and getValue("food") would 41 * return "pizza". This parsing mechanism mechanism is very simplistic and will not work 42 * as desired in all cases (for example, if some of the elements have attributes. In those 43 * cases, a custom PacketExtensionProvider should be used. 44 * 45 * @author Matt Tucker 46 */ 47 public class DefaultPacketExtension implements PacketExtension { 48 49 private String elementName; 50 private String namespace; 51 private Map<String,String> map; 52 53 /** 54 * Creates a new generic packet extension. 55 * 56 * @param elementName the name of the element of the XML sub-document. 57 * @param namespace the namespace of the element. 58 */ 59 public DefaultPacketExtension(String elementName, String namespace) { 60 this.elementName = elementName; 61 this.namespace = namespace; 62 } 63 64 /** 65 * Returns the XML element name of the extension sub-packet root element. 66 * 67 * @return the XML element name of the packet extension. 68 */ 69 public String getElementName() { 70 return elementName; 71 } 72 73 /** 74 * Returns the XML namespace of the extension sub-packet root element. 75 * 76 * @return the XML namespace of the packet extension. 77 */ 78 public String getNamespace() { 79 return namespace; 80 } 81 82 public String toXML() { 83 StringBuilder buf = new StringBuilder(); 84 buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\">"); 85 for (String name : getNames()) { 86 String value = getValue(name); 87 buf.append("<").append(name).append(">"); 88 buf.append(value); 89 buf.append("</").append(name).append(">"); 90 } 91 buf.append("</").append(elementName).append(">"); 92 return buf.toString(); 93 } 94 95 /** 96 * Returns an unmodifiable collection of the names that can be used to get 97 * values of the packet extension. 98 * 99 * @return the names. 100 */ 101 public synchronized Collection<String> getNames() { 102 if (map == null) { 103 return Collections.emptySet(); 104 } 105 return Collections.unmodifiableSet(new HashMap<String,String>(map).keySet()); 106 } 107 108 /** 109 * Returns a packet extension 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 map.get(name); 119 } 120 121 /** 122 * Sets a packet extension value using the given 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 }