KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmpp > packet > PacketExtension


1 /**
2  * $RCSfile: PacketExtension.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2005/05/11 19:56:11 $
5  *
6  * Copyright 2004 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.xmpp.packet;
22
23 import org.dom4j.DocumentFactory;
24 import org.dom4j.Element;
25 import org.dom4j.QName;
26
27 import java.util.Map JavaDoc;
28 import java.util.concurrent.ConcurrentHashMap JavaDoc;
29
30 /**
31  * A packet extension represents a child element of a Packet for a given qualified name. The
32  * PacketExtension acts as a wrapper on a child element the same way Packet does for a whole
33  * element. The wrapper provides an easy way to handle the packet extension.<p>
34  *
35  * Subclasses of this class should be registered in the static variable
36  * <tt>registeredExtensions</tt> when loaded. The registration process associates the new subclass
37  * with a given qualified name (ie. element name and namespace). This information will be used by
38  * {@link Packet#getExtension(String, String)} for locating the corresponding PacketExtension
39  * subclass to return for the requested qualified name.
40  *
41  * @author Gaston Dombiak
42  */

43 public abstract class PacketExtension {
44     protected static DocumentFactory docFactory = DocumentFactory.getInstance();
45     /**
46      * Subclasses of PacketExtension should register the element name and namespace that the
47      * subclass is using.
48      */

49     protected static Map JavaDoc<QName, Class JavaDoc> registeredExtensions = new ConcurrentHashMap JavaDoc<QName, Class JavaDoc>();
50
51     protected Element element;
52
53     /**
54      * Returns the extension class to use for the specified element name and namespace. For
55      * instance, the DataForm class should be used for the element "x" and
56      * namespace "jabber:x:data".
57      *
58      * @param name the child element name.
59      * @param namespace the child element namespace.
60      * @return the extension class to use for the specified element name and namespace.
61      */

62     public static Class JavaDoc getExtensionClass(String JavaDoc name, String JavaDoc namespace) {
63         return registeredExtensions.get(QName.get(name, namespace));
64     }
65
66     /**
67      * Constructs a new Packet extension using the specified name and namespace.
68      *
69      * @param name the child element name.
70      * @param namespace the child element namespace.
71      */

72     public PacketExtension(String JavaDoc name, String JavaDoc namespace) {
73         this.element = docFactory.createDocument().addElement(name, namespace);
74     }
75
76     /**
77      * Constructs a new PacketExtension.
78      *
79      * @param element the XML Element that contains the packet extension contents.
80      */

81     public PacketExtension(Element element) {
82         this.element = element;
83     }
84
85     /**
86      * Returns the DOM4J Element that backs the packet. The element is the definitive
87      * representation of the packet and can be manipulated directly to change
88      * packet contents.
89      *
90      * @return the DOM4J Element that represents the packet.
91      */

92     public Element getElement() {
93         return element;
94     }
95
96     /**
97      * Creates a deep copy of this packet extension.
98      *
99      * @return a deep copy of this packet extension.
100      */

101     public abstract PacketExtension createCopy();
102 }
103
Popular Tags