01: /**
02: * Copyright (C) 2004-2007 Jive Software. All rights reserved.
03: *
04: * This software is published under the terms of the GNU Public License (GPL),
05: * a copy of which is included in this distribution.
06: */package org.xmpp.packet;
07:
08: import org.dom4j.DocumentFactory;
09: import org.dom4j.Element;
10: import org.dom4j.QName;
11:
12: import java.util.Map;
13: import java.util.concurrent.ConcurrentHashMap;
14:
15: /**
16: * A packet extension represents a child element of a Packet for a given qualified name. The
17: * PacketExtension acts as a wrapper on a child element the same way Packet does for a whole
18: * element. The wrapper provides an easy way to handle the packet extension.<p>
19: *
20: * Subclasses of this class can be registered using the static variable
21: * <tt>registeredExtensions</tt>. The registration process associates the new subclass
22: * with a given qualified name (ie. element name and namespace). This information will be used by
23: * {@link Packet#getExtension(String, String)} for locating the corresponding PacketExtension
24: * subclass to return for the requested qualified name. Each PacketExtension must have a public
25: * constructor that takes an Element instance as an argument.
26: *
27: * @author Gaston Dombiak
28: */
29: public class PacketExtension {
30:
31: protected static DocumentFactory docFactory = DocumentFactory
32: .getInstance();
33: /**
34: * Subclasses of PacketExtension should register the element name and namespace that the
35: * subclass is using.
36: */
37: protected static Map<QName, Class> registeredExtensions = new ConcurrentHashMap<QName, Class>();
38:
39: protected Element element;
40:
41: /**
42: * Returns the extension class to use for the specified element name and namespace. For
43: * instance, the DataForm class should be used for the element "x" and
44: * namespace "jabber:x:data".
45: *
46: * @param name the child element name.
47: * @param namespace the child element namespace.
48: * @return the extension class to use for the specified element name and namespace.
49: */
50: public static Class getExtensionClass(String name, String namespace) {
51: return registeredExtensions.get(QName.get(name, namespace));
52: }
53:
54: /**
55: * Constructs a new Packet extension using the specified name and namespace.
56: *
57: * @param name the child element name.
58: * @param namespace the child element namespace.
59: */
60: public PacketExtension(String name, String namespace) {
61: this .element = docFactory.createDocument().addElement(name,
62: namespace);
63: }
64:
65: /**
66: * Constructs a new PacketExtension.
67: *
68: * @param element the XML Element that contains the packet extension contents.
69: */
70: public PacketExtension(Element element) {
71: this .element = element;
72: }
73:
74: /**
75: * Returns the DOM4J Element that backs the packet. The element is the definitive
76: * representation of the packet and can be manipulated directly to change
77: * packet contents.
78: *
79: * @return the DOM4J Element that represents the packet.
80: */
81: public Element getElement() {
82: return element;
83: }
84:
85: /**
86: * Creates a deep copy of this packet extension.
87: *
88: * @return a deep copy of this packet extension.
89: */
90: public PacketExtension createCopy() {
91: Element copy = element.createCopy();
92: docFactory.createDocument().add(copy);
93: return new PacketExtension(element);
94: }
95: }
|