001: /*
002: * Copyright (C) 2004 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 01. October 2004 by James Strachan
011: */
012: package com.thoughtworks.xstream.io.xml;
013:
014: import javax.xml.namespace.QName;
015:
016: import java.util.Collections;
017: import java.util.HashMap;
018: import java.util.Map;
019:
020: /**
021: * Represents a mapping of {@link QName} instances to Java class names
022: * allowing class aliases and namespace aware mappings of QNames to class names.
023: *
024: * @author James Strachan
025: * @version $Revision: 1345 $
026: */
027: public class QNameMap {
028:
029: // lets make the mapping a no-op unless we specify some mapping
030: private Map qnameToJava;
031: private Map javaToQName;
032: private String defaultPrefix = "";
033: private String defaultNamespace = "";
034:
035: /**
036: * Returns the Java class name that should be used for the given QName.
037: * If no explicit mapping has been made then the localPart of the QName is used
038: * which is the normal default in XStream.
039: */
040: public String getJavaClassName(QName qname) {
041: if (qnameToJava != null) {
042: String answer = (String) qnameToJava.get(qname);
043: if (answer != null) {
044: return answer;
045: }
046: }
047: return qname.getLocalPart();
048: }
049:
050: /**
051: * Returns the Java class name that should be used for the given QName.
052: * If no explicit mapping has been made then the localPart of the QName is used
053: * which is the normal default in XStream.
054: */
055: public QName getQName(String javaClassName) {
056: if (javaToQName != null) {
057: QName answer = (QName) javaToQName.get(javaClassName);
058: if (answer != null) {
059: return answer;
060: }
061: }
062: return new QName(defaultNamespace, javaClassName, defaultPrefix);
063: }
064:
065: /**
066: * Registers the mapping of the Java class name to the QName
067: */
068: public synchronized void registerMapping(QName qname,
069: String javaClassName) {
070: if (javaToQName == null) {
071: javaToQName = Collections.synchronizedMap(new HashMap());
072: }
073: if (qnameToJava == null) {
074: qnameToJava = Collections.synchronizedMap(new HashMap());
075: }
076: javaToQName.put(javaClassName, qname);
077: qnameToJava.put(qname, javaClassName);
078: }
079:
080: /**
081: * Registers the mapping of the type to the QName
082: */
083: public synchronized void registerMapping(QName qname, Class type) {
084: registerMapping(qname, type.getName());
085: }
086:
087: public String getDefaultNamespace() {
088: return defaultNamespace;
089: }
090:
091: public void setDefaultNamespace(String defaultNamespace) {
092: this .defaultNamespace = defaultNamespace;
093: }
094:
095: public String getDefaultPrefix() {
096: return defaultPrefix;
097: }
098:
099: public void setDefaultPrefix(String defaultPrefix) {
100: this.defaultPrefix = defaultPrefix;
101: }
102: }
|