KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > QNameMap


1 package com.thoughtworks.xstream.io.xml;
2
3 import javax.xml.namespace.QName JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 /**
9  * Represents a mapping of {@link QName} intsnaces to Java class names
10  * allowing class aliases and namespace aware mappings of QNames to class names.
11  *
12  * @author James Strachan
13  * @version $Revision: 370 $
14  */

15 public class QNameMap {
16
17     // lets make the mapping a no-op unless we specify some mapping
18
private Map JavaDoc qnameToJava;
19     private Map JavaDoc javaToQName;
20     private String JavaDoc defaultPrefix = "";
21     private String JavaDoc defaultNamespace = "";
22
23     /**
24      * Returns the Java class name that should be used for the given QName.
25      * If no explicit mapping has been made then the localPart of the QName is used
26      * which is the normal default in XStream.
27      */

28     public String JavaDoc getJavaClassName(QName JavaDoc qname) {
29         if (qnameToJava != null) {
30             String JavaDoc answer = (String JavaDoc) qnameToJava.get(qname);
31             if (answer != null) {
32                 return answer;
33             }
34         }
35         return qname.getLocalPart();
36     }
37
38     /**
39      * Returns the Java class name that should be used for the given QName.
40      * If no explicit mapping has been made then the localPart of the QName is used
41      * which is the normal default in XStream.
42      */

43     public QName JavaDoc getQName(String JavaDoc javaClassName) {
44         if (javaToQName != null) {
45             QName JavaDoc answer = (QName JavaDoc) javaToQName.get(javaClassName);
46             if (answer != null) {
47                 return answer;
48             }
49         }
50         return new QName JavaDoc(defaultNamespace, javaClassName, defaultPrefix);
51     }
52
53     /**
54      * Registers the mapping of the Java class name to the QName
55      */

56     public synchronized void registerMapping(QName JavaDoc qname, String JavaDoc javaClassName) {
57         if (javaToQName == null) {
58             javaToQName = Collections.synchronizedMap(new HashMap JavaDoc());
59         }
60         if (qnameToJava == null) {
61             qnameToJava = Collections.synchronizedMap(new HashMap JavaDoc());
62         }
63         javaToQName.put(javaClassName, qname);
64         qnameToJava.put(qname, javaClassName);
65     }
66
67     /**
68      * Registers the mapping of the type to the QName
69      */

70     public synchronized void registerMapping(QName JavaDoc qname, Class JavaDoc type) {
71         registerMapping(qname, type.getName());
72     }
73
74     public String JavaDoc getDefaultNamespace() {
75         return defaultNamespace;
76     }
77
78     public void setDefaultNamespace(String JavaDoc defaultNamespace) {
79         this.defaultNamespace = defaultNamespace;
80     }
81
82     public String JavaDoc getDefaultPrefix() {
83         return defaultPrefix;
84     }
85
86     public void setDefaultPrefix(String JavaDoc defaultPrefix) {
87         this.defaultPrefix = defaultPrefix;
88     }
89 }
90
Popular Tags