1 17 18 package scriptella.driver.ldap.ldif; 19 20 import javax.naming.NamingEnumeration ; 21 import javax.naming.NamingException ; 22 import javax.naming.directory.Attribute ; 23 import javax.naming.directory.Attributes ; 24 import javax.naming.directory.BasicAttribute ; 25 import javax.naming.directory.BasicAttributes ; 26 import javax.naming.directory.DirContext ; 27 import javax.naming.directory.ModificationItem ; 28 import javax.naming.ldap.Control ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 42 public class Entry 43 { 44 45 private int changeType=ADD; 46 47 48 private List <ModificationItem > modificationList = new ArrayList <ModificationItem >(); 49 50 private Map <String ,ModificationItem > modificationItems = new HashMap <String , ModificationItem >(); 51 52 53 private String dn; 54 55 56 private String newSuperior; 57 58 59 private String newRdn; 60 61 62 private boolean deleteOldRdn; 63 64 65 private BasicAttributes attributeList = new BasicAttributes (true); 66 67 68 public final static int ADD = 0; 69 70 public final static int MODIFY = 1; 71 72 public final static int MODDN = 2; 73 74 public final static int MODRDN = 3; 75 76 public final static int DELETE = 4; 77 78 79 private Control control; 80 81 82 88 public void setDn( String dn ) 89 { 90 this.dn = dn; 91 } 92 93 100 public void setChangeType( int changeType ) 101 { 102 this.changeType = changeType; 103 } 104 105 111 public void setChangeType( String changeType ) 112 { 113 if ( "add".equals( changeType ) ) 114 { 115 this.changeType = ADD; 116 } 117 else if ( "modify".equals( changeType ) ) 118 { 119 this.changeType = MODIFY; 120 } 121 else if ( "moddn".equals( changeType ) ) 122 { 123 this.changeType = MODDN; 124 } 125 else if ( "modrdn".equals( changeType ) ) 126 { 127 this.changeType = MODRDN; 128 } 129 else if ( "delete".equals( changeType ) ) 130 { 131 this.changeType = DELETE; 132 } 133 } 134 135 136 137 149 public void addModificationItem( int modOp, String id, Object value ) 150 { 151 if ( changeType == MODIFY ) 152 { 153 BasicAttribute attr = new BasicAttribute ( id, value ); 154 155 if ( modificationItems.containsKey( id ) ) 156 { 157 ModificationItem item = modificationItems.get( id ); 158 159 if ( item.getModificationOp() != modOp ) 160 { 161 164 throw new LdifParseException( "Bad modification" ); 165 } 166 167 Attribute attribute = item.getAttribute(); 168 169 attribute.add( value ); 170 } 171 else 172 { 173 ModificationItem item = new ModificationItem ( modOp, attr ); 174 modificationList.add( item ); 175 modificationItems.put( id, item ); 176 } 177 } 178 } 179 180 186 public void addAttribute( Attribute attr ) 187 { 188 attributeList.put( attr ); 189 } 190 191 201 public void addAttribute( String id, Object value ) 202 { 203 Attribute attr = get( id ); 204 205 if ( attr != null ) 206 { 207 attr.add( value ); 208 } 209 else 210 { 211 attributeList.put( id, value ); 212 } 213 } 214 215 225 public void putAttribute( String id, Object value ) 226 { 227 Attribute attribute = attributeList.get( id ); 228 229 if ( attribute != null ) 230 { 231 attribute.add( value ); 232 } 233 else 234 { 235 attributeList.put( id, value ); 236 } 237 } 238 239 245 public int getChangeType() 246 { 247 return changeType; 248 } 249 250 253 public List <ModificationItem > getModificationItems() 254 { 255 return modificationList; 256 } 257 258 261 public String getDn() 262 { 263 return dn; 264 } 265 266 269 public int size() 270 { 271 return modificationList.size(); 272 } 273 274 281 public Attribute get( String attributeId ) 282 { 283 if ( "dn".equalsIgnoreCase( attributeId ) ) 284 { 285 return new BasicAttribute ( "dn", dn ); 286 } 287 288 return attributeList.get( attributeId ); 289 } 290 291 296 public Attributes getAttributes() 297 { 298 if ( isEntry() ) 299 { 300 return attributeList; 301 } 302 else 303 { 304 return null; 305 } 306 } 307 308 311 public boolean isDeleteOldRdn() 312 { 313 return deleteOldRdn; 314 } 315 316 322 public void setDeleteOldRdn( boolean deleteOldRdn ) 323 { 324 this.deleteOldRdn = deleteOldRdn; 325 } 326 327 330 public String getNewRdn() 331 { 332 return newRdn; 333 } 334 335 341 public void setNewRdn( String newRdn ) 342 { 343 this.newRdn = newRdn; 344 } 345 346 349 public String getNewSuperior() 350 { 351 return newSuperior; 352 } 353 354 360 public void setNewSuperior( String newSuperior ) 361 { 362 this.newSuperior = newSuperior; 363 } 364 365 368 public boolean isChangeAdd() 369 { 370 return changeType == ADD; 371 } 372 373 376 public boolean isChangeDelete() 377 { 378 return changeType == DELETE; 379 } 380 381 384 public boolean isChangeModDn() 385 { 386 return changeType == MODDN; 387 } 388 389 392 public boolean isChangeModRdn() 393 { 394 return changeType == MODRDN; 395 } 396 397 400 public boolean isChangeModify() 401 { 402 return changeType == MODIFY; 403 } 404 405 public boolean isEntry() 406 { 407 return changeType == ADD; 408 } 409 410 413 public Control getControl() 414 { 415 return control; 416 } 417 418 424 public void setControl( Control control ) 425 { 426 this.control = control; 427 } 428 429 432 private String dumpAttributes() 433 { 434 StringBuffer sb = new StringBuffer (); 435 436 try 437 { 438 for ( NamingEnumeration attrs = attributeList.getAll(); attrs.hasMoreElements(); ) 439 { 440 Attribute attribute = (Attribute ) attrs.nextElement(); 441 442 sb.append( " ").append( attribute.getID() ).append( ":\n" ); 443 444 for ( NamingEnumeration values = attribute.getAll(); values.hasMoreElements(); ) 445 { 446 Object value = values.nextElement(); 447 448 if ( value instanceof String ) 449 { 450 sb.append( " " ).append( (String )value ).append('\n' ); 451 } 452 else 453 { 454 sb.append( " " ).append( Utils.dumpBytes( (byte[]) value ) ).append('\n' ); 455 } 456 } 457 } 458 } 459 catch ( NamingException ne ) 460 { 461 return ""; 462 } 463 464 return sb.toString(); 465 } 466 467 470 private String dumpModificationItems() 471 { 472 StringBuffer sb = new StringBuffer (); 473 474 for (ModificationItem modif : modificationList) { 475 sb.append(" Operation: "); 476 477 switch (modif.getModificationOp()) { 478 case DirContext.ADD_ATTRIBUTE : 479 sb.append("ADD\n"); 480 break; 481 482 case DirContext.REMOVE_ATTRIBUTE : 483 sb.append("REMOVE\n"); 484 break; 485 486 case DirContext.REPLACE_ATTRIBUTE : 487 sb.append("REPLACE \n"); 488 break; 489 } 490 491 Attribute attribute = modif.getAttribute(); 492 493 sb.append(" Attribute: ").append(attribute.getID()).append('\n'); 494 495 if (attribute.size() != 0) { 496 try { 497 for (NamingEnumeration values = attribute.getAll(); values.hasMoreElements();) { 498 Object value = values.nextElement(); 499 500 if (value instanceof String ) { 501 sb.append(" ").append((String ) value).append('\n'); 502 } else { 503 sb.append(" ").append(Utils.dumpBytes((byte[]) value)).append('\n'); 504 } 505 } 506 } 507 catch (NamingException ne) { 508 return ""; 509 } 510 } 511 } 512 513 return sb.toString(); 514 } 515 516 519 public String toString() 520 { 521 StringBuffer sb = new StringBuffer (); 522 sb.append( "Entry : " ).append( dn ).append( '\n' ); 523 524 if ( control != null ) 525 { 526 sb.append( " Control : " ).append( control ).append( '\n' ); 527 } 528 529 switch ( changeType ) 530 { 531 case ADD : 532 sb.append( " Change type is ADD\n" ); 533 sb.append( " Attributes : \n" ); 534 sb.append( dumpAttributes() ); 535 break; 536 537 case MODIFY : 538 sb.append( " Change type is MODIFY\n" ); 539 sb.append( " Modifications : \n" ); 540 sb.append( dumpModificationItems() ); 541 break; 542 543 case DELETE : 544 sb.append( " Change type is DELETE\n" ); 545 break; 546 547 case MODDN : 548 case MODRDN : 549 sb.append( " Change type is ").append( changeType == MODDN ? "MODDN\n" : "MODRDN\n" ); 550 sb.append( " Delete old RDN : " ).append( deleteOldRdn ? "true\n" : "false\n" ); 551 sb.append( " New RDN : " ).append( newRdn ).append( '\n' ); 552 553 if ( newSuperior !=null ) 554 { 555 sb.append( " New superior : " ).append( newSuperior ).append( '\n' ); 556 } 557 558 break; 559 } 560 561 return sb.toString(); 562 } 563 } 564 | Popular Tags |