KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > packet > DefaultPacketExtension


1 /**
2  * $RCSfile$
3  * $Revision: 2408 $
4  * $Date: 2004-11-02 20:53:30 -0300 (Tue, 02 Nov 2004) $
5  *
6  * Copyright 2003-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.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  * &lt;foo xmlns="http://bar.com"&gt;
36  * &lt;color&gt;blue&lt;/color&gt;
37  * &lt;food&gt;pizza&lt;/food&gt;
38  * &lt;/foo&gt;</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 JavaDoc elementName;
50     private String JavaDoc namespace;
51     private Map 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 JavaDoc elementName, String JavaDoc 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 JavaDoc 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 JavaDoc getNamespace() {
79         return namespace;
80     }
81
82     public String JavaDoc toXML() {
83         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
84         buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\">");
85         for (Iterator i=getNames(); i.hasNext(); ) {
86             String JavaDoc name = (String JavaDoc)i.next();
87             String JavaDoc value = getValue(name);
88             buf.append("<").append(name).append(">");
89             buf.append(value);
90             buf.append("</").append(name).append(">");
91         }
92         buf.append("</").append(elementName).append(">");
93         return buf.toString();
94     }
95
96     /**
97      * Returns an Iterator for the names that can be used to get
98      * values of the packet extension.
99      *
100      * @return an Iterator for the names.
101      */

102     public synchronized Iterator getNames() {
103         if (map == null) {
104             return Collections.EMPTY_LIST.iterator();
105         }
106         return Collections.unmodifiableMap(new HashMap(map)).keySet().iterator();
107     }
108
109     /**
110      * Returns a packet extension value given a name.
111      *
112      * @param name the name.
113      * @return the value.
114      */

115     public synchronized String JavaDoc getValue(String JavaDoc name) {
116         if (map == null) {
117             return null;
118         }
119         return (String JavaDoc)map.get(name);
120     }
121
122     /**
123      * Sets a packet extension value using the given name.
124      *
125      * @param name the name.
126      * @param value the value.
127      */

128     public synchronized void setValue(String JavaDoc name, String JavaDoc value) {
129         if (map == null) {
130             map = new HashMap();
131         }
132         map.put(name, value);
133     }
134 }
Popular Tags