1 28 29 package com.caucho.naming; 30 31 import com.caucho.util.L10N; 32 33 import javax.naming.Context ; 34 import javax.naming.InvalidNameException ; 35 import javax.naming.Name ; 36 import javax.naming.NamingException ; 37 import java.util.ArrayList ; 38 import java.util.Collections ; 39 import java.util.Enumeration ; 40 41 44 public class QName implements Name { 45 private static L10N L = new L10N(QName.class); 46 47 protected Context _context; 49 50 private ArrayList <String > _items = new ArrayList <String >(); 52 53 58 public QName(Context context) 59 { 60 _context = context; 61 } 62 63 69 public QName(Context context, String first) 70 { 71 _context = context; 72 73 if (first != null) 74 _items.add(first); 75 } 76 77 78 85 public QName(Context context, String first, String rest) 86 { 87 _context = context; 88 89 if (first != null) 90 _items.add(first); 91 if (rest != null) 92 _items.add(rest); 93 } 94 95 98 public Object clone() 99 { 100 QName name = new QName(_context); 101 102 for (int i = 0; i < _items.size(); i++) 103 name._items.add(_items.get(i)); 104 105 return name; 106 } 107 108 public int size() 109 { 110 return _items.size(); 111 } 112 113 public boolean isEmpty() 114 { 115 return _items.size() == 0; 116 } 117 118 public Enumeration getAll() 119 { 120 return Collections.enumeration(_items); 121 } 122 123 public String get(int pos) 124 { 125 if (pos < _items.size()) 126 return (String ) _items.get(pos); 127 else 128 return null; 129 } 130 131 public Name getPrefix(int posn) 132 { 133 QName name = new QName(_context); 134 135 for (int i = 0; i < posn; i++) 136 name._items.add(_items.get(i)); 137 138 return name; 139 } 140 141 public Name getSuffix(int posn) 142 { 143 Context subcontext = _context; 144 145 for (int i = 0; i < posn; i++) { 146 String item = (String ) _items.get(i); 147 try { 148 Object obj = subcontext.lookup(item); 149 if (obj instanceof Context ) 150 subcontext = (Context ) obj; 151 else 152 break; 153 } catch (NamingException e) { 154 break; 155 } 156 } 157 158 QName name = new QName(subcontext); 159 for (int i = posn; i < _items.size(); i++) { 160 String item = (String ) _items.get(i); 161 162 name._items.add(_items.get(i)); 163 } 164 165 return name; 166 } 167 168 173 public boolean startsWith(Name name) 174 { 175 if (name == null) 176 return false; 177 178 if (size() < name.size()) 179 return false; 180 181 for (int i = 0; i < name.size(); i++) { 182 if (! get(i).equals(name.get(i))) 183 return false; 184 } 185 186 return true; 187 } 188 189 public boolean endsWith(Name name) 190 { 191 if (name == null) 192 return false; 193 194 int nameSize = name.size(); 195 if (size() < nameSize) 196 return false; 197 198 int offset = size() - nameSize; 199 for (int i = 0; i < nameSize; i++) 200 if (! get(i + offset).equals(name.get(i))) 201 return false; 202 203 return true; 204 } 205 206 213 public Name addAll(Name suffix) 214 throws InvalidNameException 215 { 216 for (int i = 0; i < suffix.size(); i++) 217 _items.add(suffix.get(i)); 218 219 return this; 220 } 221 222 223 230 public Name addAll(int posn, Name suffix) 231 throws InvalidNameException 232 { 233 for (int i = 0; i < suffix.size(); i++) 234 _items.add(posn, suffix.get(i)); 235 236 return this; 237 } 238 239 246 public Name add(String comp) 247 throws InvalidNameException 248 { 249 _items.add(comp); 250 251 return this; 252 } 253 254 259 public Name add(int posn, String comp) 260 throws InvalidNameException 261 { 262 _items.add(posn, comp); 263 264 return this; 265 } 266 267 public Object remove(int posn) 268 throws InvalidNameException 269 { 270 _items.remove(posn); 271 272 return this; 273 } 274 275 278 public int hashCode() 279 { 280 int hashCode = 337; 281 282 for (int i = size() - 1; i >= 0; i--) 283 hashCode = 65521 * hashCode + get(i).hashCode(); 284 285 return hashCode; 286 } 287 288 293 public boolean equals(Object obj) 294 { 295 if (! (obj instanceof Name )) 296 return false; 297 298 Name name = (Name ) obj; 299 300 if (size() != name.size()) 301 return false; 302 303 for (int i = size() - 1; i >= 0; i--) { 304 if (! get(i).equals(name.get(i))) 305 return false; 306 } 307 308 return true; 309 } 310 311 316 public int compareTo(Object rawB) 317 { 318 if (! (rawB instanceof Name )) 319 return -1; 320 321 Name b = (Name ) rawB; 322 323 for (int i = 0; i < size(); i++) { 324 if (i >= b.size()) 325 return 1; 326 327 String sa = (String ) get(i); 328 String sb = (String ) b.get(i); 329 330 int cmp = sa.compareTo(sb); 331 if (cmp != 0) 332 return cmp; 333 } 334 335 if (size() == b.size()) 336 return 0; 337 else 338 return -1; 339 } 340 341 344 public String toString() 345 { 346 String name = null; 347 348 for (int i = 0; i < size(); i++) { 349 String str = (String ) get(i); 350 351 if (name != null) { 352 try { 353 name = _context.composeName(str, name); 354 } catch (NamingException e) { 355 name = name + "/" + str; 356 } 357 } 358 else 359 name = str; 360 } 361 362 return name == null ? "" : name; 363 } 364 } 365 | Popular Tags |