1 20 21 package org.apache.directory.ldapstudio.browser.core.model; 22 23 24 import java.io.Serializable ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 29 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema; 30 31 32 38 public class DN implements Serializable 39 { 40 41 42 private static final long serialVersionUID = 2343676941769163982L; 43 44 45 private RDN[] rdns; 46 47 48 52 public DN() 53 { 54 this.rdns = new RDN[0]; 55 } 56 57 58 64 public DN( RDN rdn ) 65 { 66 if ( rdn == null ) 67 { 68 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_rdn ); 69 } 70 71 this.rdns = new RDN[1]; 72 this.rdns[0] = new RDN( rdn ); 73 } 74 75 76 82 public DN( String dn ) throws NameException 83 { 84 if ( dn == null ) 85 { 86 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_dn ); 87 } 88 89 this.parseDn( dn ); 91 } 92 93 94 99 public DN( DN dn ) 100 { 101 if ( dn == null ) 102 { 103 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_dn ); 104 } 105 106 this.rdns = new RDN[dn.getRdns().length]; 107 for ( int i = 0; i < dn.getRdns().length; i++ ) 108 { 109 this.rdns[i] = new RDN( dn.getRdns()[i] ); 110 } 111 } 112 113 114 121 public DN( RDN rdn, DN parent ) 122 { 123 if ( rdn == null ) 124 { 125 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_rdn ); 126 } 127 if ( parent == null ) 128 { 129 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_dn ); 130 } 131 132 this.rdns = new RDN[parent.getRdns().length + 1]; 133 this.rdns[0] = new RDN( rdn ); 134 for ( int i = 0; i < parent.getRdns().length; i++ ) 135 { 136 this.rdns[i + 1] = new RDN( parent.getRdns()[i] ); 137 } 138 } 139 140 141 148 public DN( DN localName, DN suffix ) 149 { 150 if ( localName == null ) 151 { 152 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_dn ); 153 } 154 if ( suffix == null ) 155 { 156 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_dn ); 157 } 158 159 this.rdns = new RDN[localName.getRdns().length + suffix.getRdns().length]; 160 for ( int i = 0; i < localName.getRdns().length; i++ ) 161 { 162 this.rdns[i] = new RDN( localName.getRdns()[i] ); 163 } 164 for ( int i = 0; i < suffix.getRdns().length; i++ ) 165 { 166 this.rdns[i + localName.getRdns().length] = new RDN( suffix.getRdns()[i] ); 167 } 168 } 169 170 171 178 public DN( String rdn, String parent ) throws NameException 179 { 180 181 if ( rdn == null ) 182 { 183 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_rdn ); 184 } 185 if ( parent == null ) 186 { 187 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_dn ); 188 } 189 190 this.parseDn( parent ); 192 193 RDN[] rdns = this.rdns; 194 this.rdns = new RDN[rdns.length + 1]; 195 this.rdns[0] = new RDN( rdn ); 196 System.arraycopy( rdns, 0, this.rdns, 1, rdns.length ); 197 } 198 199 200 205 public RDN getRdn() 206 { 207 if ( this.rdns.length > 0 ) 208 { 209 return this.rdns[0]; 210 } 211 else 212 { 213 return new RDN(); 214 } 215 } 216 217 218 223 public DN getParentDn() 224 { 225 if ( this.rdns.length < 1 ) 226 { 227 return null; 228 } 229 else 230 { 231 RDN[] parentRdns = new RDN[this.rdns.length - 1]; 232 for ( int i = 1; i < this.rdns.length; i++ ) 233 { 234 parentRdns[i - 1] = new RDN( this.rdns[i] ); 235 } 236 DN parent = new DN(); 237 parent.rdns = parentRdns; 238 return parent; 239 } 240 } 241 242 243 251 public DN getLocalName( DN suffix ) 252 { 253 if ( suffix != null && suffix.getRdns().length > 0 ) 254 { 255 DN localName = new DN(); 256 for ( int i = getRdns().length - suffix.getRdns().length - 1; i >= 0; i-- ) 257 { 258 localName = new DN( getRdns()[i], localName ); 259 } 260 return localName; 261 } 262 else 263 { 264 return this; 265 } 266 } 267 268 269 272 public String toString() 273 { 274 StringBuffer sb = new StringBuffer (); 275 276 for ( int i = 0; i < this.rdns.length; i++ ) 277 { 278 sb.append( this.rdns[i].toString() ); 279 if ( i + 1 < rdns.length ) 280 { 281 sb.append( "," ); } 283 } 284 285 return sb.toString(); 286 } 287 288 289 296 public String toOidString( Schema schema ) 297 { 298 StringBuffer sb = new StringBuffer (); 299 300 for ( int i = 0; i < this.rdns.length; i++ ) 301 { 302 sb.append( this.rdns[i].toOidString( schema ) ); 303 if ( i + 1 < rdns.length ) 304 { 305 sb.append( "," ); } 307 } 308 309 return sb.toString(); 310 } 311 312 313 319 private void parseDn( String dn ) throws NameException 320 { 321 List <RDN> rdnList = new ArrayList <RDN>( 3 ); 322 323 boolean backslash = false; 324 int start = 0; 325 for ( int i = 0; i < dn.length(); i++ ) 326 { 327 if ( dn.charAt( i ) == '\\' && !backslash ) 328 { 329 backslash = true; 330 } 331 else 332 { 333 String rdn = null; 334 if ( dn.charAt( i ) == ',' && !backslash ) 335 { 336 rdn = dn.substring( start, i ); 337 } 338 else if ( i == dn.length() - 1 ) 339 { 340 rdn = dn.substring( start ); 341 } 342 if ( rdn != null ) 343 { 344 rdnList.add( new RDN( rdn ) ); 345 start = i + 1; 346 347 for ( ; start < dn.length() && dn.charAt( start ) == ' '; i++ ) 349 { 350 start++; 351 } 352 } 353 backslash = false; 354 } 355 } 356 357 this.rdns = rdnList.toArray( new RDN[rdnList.size()] ); 358 } 359 360 361 366 public RDN[] getRdns() 367 { 368 return rdns; 369 } 370 371 372 377 public void setRdns( RDN[] rdns ) 378 { 379 this.rdns = rdns; 380 } 381 382 383 386 public boolean equals( Object o ) throws ClassCastException 387 { 388 if ( o instanceof DN ) 389 { 390 return this.toString().equals( ( ( DN ) o ).toString() ); 391 } 392 return false; 393 } 394 395 396 399 public int hashCode() 400 { 401 return this.toString().hashCode(); 402 } 403 404 } 405 | Popular Tags |