View Javadoc
1 /* 2 * Copyright (C) 2002 Carsten Krebs (Team-Konzept GmbH & Co.KG) 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 package com.teamkonzept.dom4jb.dom; 19 20 import java.util.WeakHashMap; 21 22 import org.apache.xerces.util.XMLChar; 23 24 public class NodeName { 25 26 protected static final WeakHashMap names = new WeakHashMap(); 27 28 protected static final String ALL = "*".intern(); 29 30 public static final String XML_PREFIX = "xml"; 31 public static final String XML_NAMESPACE_URI = 32 "http://www.w3.org/XML/1998/namespace"; 33 public static final String NS_PREFIX = "xmlns"; 34 public static final String NS_NAMESPACE_URI = 35 "http://www.w3.org/2000/xmlns/"; 36 37 protected final String localName; 38 protected int hashCode; 39 40 /*** Creates new NodeName */ 41 protected NodeName(final String localName) { 42 this.localName = localName.intern(); 43 } 44 45 /*** Creates new NodeName */ 46 private NodeName(final NodeName name) { 47 this.localName = name.localName; 48 } 49 50 public String getLocalName() { 51 return null; 52 } 53 54 public String getQualifiedName() { 55 return localName; 56 } 57 58 public String getNamespaceURI() { 59 return null; 60 } 61 62 public String getPrefix() { 63 return null; 64 } 65 66 public NodeName setPrefix(final String prefix) { 67 return this; 68 } 69 70 public boolean equals(final Object obj) { 71 if (obj == null) { 72 return false; 73 } 74 if (obj == this) { 75 return true; 76 } 77 if (!(obj instanceof NodeName)) { 78 return false; 79 } 80 81 return this.compareTo((NodeName) obj) != 0; 82 } 83 84 public int compareTo(final NodeName other) { 85 if (this == other) { 86 return 0; 87 } 88 89 { // compare uri's 90 final String thisURI = this.getNamespaceURI(); 91 final String otherURI = other.getNamespaceURI(); 92 93 final boolean uriMatchAll = (thisURI == ALL || otherURI == ALL); 94 if (!uriMatchAll && thisURI != otherURI) { 95 if (thisURI == null) { 96 return -1; 97 } 98 if (otherURI == null) { 99 return +1; 100 } 101 return thisURI.compareTo(otherURI); 102 } 103 } 104 105 { // compare names 106 final String thisName = this.localName; 107 final String otherName = other.localName; 108 109 final boolean localNameMatchAll = 110 (thisName == ALL || otherName == ALL); 111 if (!localNameMatchAll && thisName != otherName) { 112 if (thisName == null) { 113 return -1; 114 } 115 if (otherName == null) { 116 return +1; 117 } 118 return thisName.compareTo(otherName); 119 } 120 } 121 122 return 0; 123 } 124 125 /*** 126 * Returns a hashcode for this string. The hashcode for a 127 * <code>NamedNode</code> object is computed as 128 * <blockquote><pre> 129 * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 130 * </pre></blockquote> 131 * using <code>int</code> arithmetic, where <code>s[i]</code> is the 132 * <i>i</i>th character of the string 133 * <code>[prefix:]namespaceURI:localNam</code>, 134 * <code>n</code> is the length of 135 * the string, and <code>^</code> indicates exponentiation. 136 * 137 * @return a hash code value for this object. 138 */ 139 public int hashCode() { 140 if (hashCode != 0) { 141 return hashCode; 142 } 143 int hashCode = 0; 144 145 final int localNameLength = localName.length(); 146 for (int i = 0; i < localNameLength; i++) { 147 hashCode = 31 * hashCode + localName.charAt(i); 148 } 149 hashCode = 31 * hashCode + ':'; 150 151 final String namespaceURI = getNamespaceURI(); 152 if (namespaceURI != null) { 153 final int uriLength = namespaceURI.length(); 154 for (int i = 0; i < uriLength; i++) { 155 hashCode = 31 * hashCode + namespaceURI.charAt(i); 156 } 157 } 158 159 this.hashCode = hashCode; 160 161 return hashCode; 162 } 163 164 public static NodeName getInstance(final String localName) { 165 if (localName == null 166 || (!localName.equals("*") && !XMLChar.isValidName(localName))) { 167 throw new DOMException( 168 DOMException.INVALID_CHARACTER_ERR, 169 "invalid localName!"); 170 } 171 final String key = getKey(null, null, localName); 172 173 synchronized (names) { 174 final Object name = names.get(key); 175 if (name == null) { 176 final NodeName newName = new NodeName(localName); 177 names.put(key, newName); 178 return newName; 179 } 180 return (NodeName) name; 181 } 182 } 183 184 protected static String getKey( 185 final String namespaceURI, 186 final String prefix, 187 final String localName) { 188 189 final StringBuffer buf = new StringBuffer(); 190 if (namespaceURI != null) { 191 buf.append(namespaceURI); 192 } 193 buf.append(':'); 194 if (prefix != null) { 195 buf.append(prefix); 196 } 197 buf.append(':'); 198 buf.append(localName); 199 return buf.toString(); 200 } 201 /*** 202 * @see java.lang.Object#toString() 203 */ 204 public String toString() { 205 return localName; 206 } 207 208 }

This page was automatically generated by Maven