2003-05-08 17:53:40 +02:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Copyright 2003-2004 Jive Software.
|
2003-05-08 17:53:40 +02:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-05-08 17:53:40 +02:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-05-08 17:53:40 +02:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-05-08 17:53:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack.packet;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default implementation of the PacketExtension interface. Unless a PacketExtensionProvider
|
|
|
|
* is registered with {@link org.jivesoftware.smack.provider.ProviderManager ProviderManager},
|
|
|
|
* instances of this class will be returned when getting packet extensions.<p>
|
|
|
|
*
|
|
|
|
* This class provides a very simple representation of an XML sub-document. Each element
|
|
|
|
* is a key in a Map with its CDATA being the value. For example, given the following
|
|
|
|
* XML sub-document:
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* <foo xmlns="http://bar.com">
|
|
|
|
* <color>blue</color>
|
|
|
|
* <food>pizza</food>
|
|
|
|
* </foo></pre>
|
|
|
|
*
|
|
|
|
* In this case, getValue("color") would return "blue", and getValue("food") would
|
|
|
|
* return "pizza". This parsing mechanism mechanism is very simplistic and will not work
|
|
|
|
* as desired in all cases (for example, if some of the elements have attributes. In those
|
|
|
|
* cases, a custom PacketExtensionProvider should be used.
|
|
|
|
*
|
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
|
|
|
public class DefaultPacketExtension implements PacketExtension {
|
|
|
|
|
|
|
|
private String elementName;
|
|
|
|
private String namespace;
|
|
|
|
private Map map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new generic packet extension.
|
|
|
|
*
|
|
|
|
* @param elementName the name of the element of the XML sub-document.
|
|
|
|
* @param namespace the namespace of the element.
|
|
|
|
*/
|
|
|
|
public DefaultPacketExtension(String elementName, String namespace) {
|
|
|
|
this.elementName = elementName;
|
|
|
|
this.namespace = namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the XML element name of the extension sub-packet root element.
|
|
|
|
*
|
|
|
|
* @return the XML element name of the packet extension.
|
|
|
|
*/
|
|
|
|
public String getElementName() {
|
|
|
|
return elementName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the XML namespace of the extension sub-packet root element.
|
|
|
|
*
|
|
|
|
* @return the XML namespace of the packet extension.
|
|
|
|
*/
|
|
|
|
public String getNamespace() {
|
|
|
|
return namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toXML() {
|
2003-05-08 18:24:08 +02:00
|
|
|
StringBuffer buf = new StringBuffer();
|
|
|
|
buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\">");
|
|
|
|
for (Iterator i=getNames(); i.hasNext(); ) {
|
|
|
|
String name = (String)i.next();
|
|
|
|
String value = getValue(name);
|
|
|
|
buf.append("<").append(name).append(">");
|
|
|
|
buf.append(value);
|
|
|
|
buf.append("</").append(name).append(">");
|
|
|
|
}
|
|
|
|
buf.append("</").append(elementName).append(">");
|
|
|
|
return buf.toString();
|
2003-05-08 17:53:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an Iterator for the names that can be used to get
|
|
|
|
* values of the packet extension.
|
|
|
|
*
|
|
|
|
* @return an Iterator for the names.
|
|
|
|
*/
|
|
|
|
public synchronized Iterator getNames() {
|
|
|
|
if (map == null) {
|
2003-05-27 17:18:02 +02:00
|
|
|
return Collections.EMPTY_LIST.iterator();
|
2003-05-08 17:53:40 +02:00
|
|
|
}
|
|
|
|
return Collections.unmodifiableMap(new HashMap(map)).keySet().iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a packet extension value given a name.
|
|
|
|
*
|
|
|
|
* @param name the name.
|
|
|
|
* @return the value.
|
|
|
|
*/
|
|
|
|
public synchronized String getValue(String name) {
|
|
|
|
if (map == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (String)map.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a packet extension value using the given name.
|
|
|
|
*
|
|
|
|
* @param name the name.
|
|
|
|
* @param value the value.
|
|
|
|
*/
|
|
|
|
public synchronized void setValue(String name, String value) {
|
|
|
|
if (map == null) {
|
|
|
|
map = new HashMap();
|
|
|
|
}
|
|
|
|
map.put(name, value);
|
|
|
|
}
|
2003-05-27 17:18:02 +02:00
|
|
|
}
|