1 28 29 package com.caucho.xml; 30 31 import com.caucho.util.L10N; 32 33 import org.w3c.dom.DOMException ; 34 35 import java.io.Serializable ; 36 37 public class QName implements Comparable , Serializable { 38 protected static L10N L = new L10N(QName.class); 39 40 private String _prefix; private String _localName; private String _namespace; 44 private String _fullName; private String _canonicalName; 47 public QName(String qName) 48 { 49 this(qName, ""); 50 } 51 52 public QName(String qName, String namespace) 53 { 54 _fullName = qName.intern(); 55 56 if (namespace == null) { 57 _prefix = null; 58 _namespace = null; 59 _localName = null; 60 } 61 else if (namespace.equals("")) { 62 _prefix = null; 63 _namespace = ""; 64 _localName = _fullName; 65 } 66 else { 67 _namespace = namespace.intern(); 68 69 int p = qName.indexOf(':'); 70 if (p > 0) { 71 _prefix = qName.substring(0, p).intern(); 72 _localName = qName.substring(p + 1).intern(); 73 } 74 else { 75 _prefix = null; 76 _localName = _fullName; 77 } 78 } 79 80 if (_namespace != null) 81 _canonicalName = ("{" + _namespace + "}" + _localName).intern(); 82 else 83 _canonicalName = _fullName; 84 } 85 86 public QName(String prefix, String localName, String namespace) 87 { 88 init(prefix, localName, namespace); 89 } 90 91 public QName(String qName, String prefix, String localName, String namespace) 92 { 93 _fullName = qName.intern(); 94 95 if (prefix != null) 96 _prefix = prefix.intern(); 97 98 if (localName != null) 99 _localName = localName.intern(); 100 101 if (namespace != null) 102 _namespace = namespace.intern(); 103 104 if (_namespace != null) 105 _canonicalName = ("{" + _namespace + "}" + _localName).intern(); 106 else 107 _canonicalName = _fullName; 108 } 109 110 private void init(String prefix, String localName, String namespace) 111 { 112 if (localName == null || localName.equals("")) 113 throw new QDOMException(DOMException.INVALID_CHARACTER_ERR, L.l("`{0}' is an invalid XML name because the local name is empty. XML names must be `prefix:name' or simply `name'.", prefix + ":")); 114 115 if (prefix == null || prefix.equals("")) 116 _prefix = null; 117 else 118 _prefix = prefix.intern(); 119 120 _localName = localName.intern(); 121 122 if (_prefix != null && _prefix != "") 123 _fullName = (_prefix + ":" + localName).intern(); 124 else 125 _fullName = _localName; 126 127 if (namespace == null) { 128 _localName = null; 129 } 130 else if (namespace.equals("")) { 131 _namespace = ""; 132 _localName = _fullName; 133 } 134 else 135 _namespace = namespace.intern(); 136 137 if (_namespace != null) 138 _canonicalName = ("{" + _namespace + "}" + localName).intern(); 139 else 140 _canonicalName = _fullName; 141 } 142 143 public String getName() 144 { 145 return _fullName; 146 } 147 148 public String getPrefix() 149 { 150 return _prefix; 151 } 152 153 public String getLocalName() 154 { 155 return _localName; 156 } 157 158 public String getCanonicalName() 159 { 160 return _canonicalName; 161 } 162 163 public String getNamespace() 164 { 165 return _namespace; 166 } 167 168 public String getNamespaceURI() 169 { 170 return _namespace; 171 } 172 173 176 public int hashCode() 177 { 178 return _canonicalName.hashCode(); 179 185 } 186 187 190 public boolean equals(Object b) 191 { 192 if (this == b) 193 return true; 194 195 if (! (b instanceof QName)) 196 return false; 197 198 QName name = (QName) b; 199 200 return _canonicalName == name._canonicalName; 201 215 } 216 217 public int compareTo(Object b) 218 { 219 if (this == b) 220 return 0; 221 222 else if (! (b instanceof QName)) 223 return -1; 224 225 QName name = (QName) b; 226 227 return getCanonicalName().compareTo(name.getCanonicalName()); 228 240 } 241 242 public String toString() 243 { 244 if (_prefix != null) 245 return "QName[" + _prefix + ":" + _canonicalName + "]"; 246 else 247 return "QName[" + _canonicalName + "]"; 248 } 249 } 250 | Popular Tags |