1 20 21 package org.apache.directory.ldapstudio.browser.core.model.schema; 22 23 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.io.Reader ; 27 import java.io.Serializable ; 28 import java.io.StringReader ; 29 import java.io.Writer ; 30 import java.net.URL ; 31 import java.util.ArrayList ; 32 import java.util.Arrays ; 33 import java.util.HashMap ; 34 import java.util.HashSet ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Set ; 39 40 import org.apache.directory.ldapstudio.browser.core.internal.model.AttributeDescription; 41 import org.apache.directory.ldapstudio.browser.core.model.DN; 42 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 43 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEnumeration; 44 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer; 45 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord; 46 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine; 47 import org.apache.directory.ldapstudio.browser.core.model.ldif.parser.LdifParser; 48 import org.apache.directory.ldapstudio.browser.core.model.schema.parser.SchemaLexer; 49 import org.apache.directory.ldapstudio.browser.core.model.schema.parser.SchemaParser; 50 51 52 public class Schema implements Serializable 53 { 54 55 private static final long serialVersionUID = 2439355717760227167L; 56 57 public static final String SCHEMA_FILTER = "(objectClass=subschema)"; 58 59 public static final String SCHEMA_ATTRIBUTE_OBJECTCLASSES = "objectClasses"; 60 61 public static final String SCHEMA_ATTRIBUTE_ATTRIBUTETYPES = "attributeTypes"; 62 63 public static final String SCHEMA_ATTRIBUTE_LDAPSYNTAXES = "ldapSyntaxes"; 64 65 public static final String SCHEMA_ATTRIBUTE_MATCHINGRULES = "matchingRules"; 66 67 public static final String SCHEMA_ATTRIBUTE_MATCHINGRULEUSE = "matchingRuleUse"; 68 69 public static final Schema DEFAULT_SCHEMA; 70 static 71 { 72 Schema defaultSchema = null; 73 74 try 75 { 76 URL url = Schema.class.getClassLoader().getResource( 77 "default_schema.ldif" ); 78 InputStream is = url.openStream(); 79 Reader reader = new InputStreamReader ( is ); 80 81 defaultSchema = new Schema(); 82 defaultSchema.defaultSchema = true; 83 defaultSchema.loadFromLdif( reader ); 84 } 85 catch ( Exception e ) 86 { 87 e.printStackTrace(); 88 } 89 90 DEFAULT_SCHEMA = defaultSchema; 91 } 92 93 private boolean defaultSchema = false; 94 95 96 public boolean isDefault() 97 { 98 return this.defaultSchema; 99 } 100 101 private LdifContentRecord schemaRecord; 102 103 private DN dn; 104 105 private String [] objectClasses; 106 107 private String createTimestamp; 108 109 private String modifyTimestamp; 110 111 private Map ocdMapByName; 112 113 private Map atdMapByName; 114 115 private Map lsdMapByNumericOID; 116 117 private Map mrdMapByName; 118 119 private Map mrdMapByNumericOID; 120 121 private Map mrudMapByName; 122 123 private Map mrudMapByNumericOID; 124 125 126 public Schema() 127 { 128 this.schemaRecord = null; 129 this.dn = null; 130 this.objectClasses = new String [0]; 131 this.createTimestamp = null; 132 this.modifyTimestamp = null; 133 this.ocdMapByName = new HashMap (); 134 this.atdMapByName = new HashMap (); 135 this.lsdMapByNumericOID = new HashMap (); 136 this.mrdMapByName = new HashMap (); 137 this.mrdMapByNumericOID = new HashMap (); 138 this.mrudMapByName = new HashMap (); 139 this.mrudMapByNumericOID = new HashMap (); 140 } 141 142 143 149 public void loadFromLdif( Reader reader ) 150 { 151 try 152 { 153 LdifParser parser = new LdifParser(); 154 LdifEnumeration enumeration = parser.parse( reader ); 155 if ( enumeration.hasNext( null ) ) 156 { 157 LdifContainer container = enumeration.next( null ); 158 if ( container instanceof LdifContentRecord ) 159 { 160 LdifContentRecord schemaRecord = ( LdifContentRecord ) container; 161 this.parseSchemaRecord( schemaRecord ); 162 } 163 } 164 } 165 catch ( Exception e ) 166 { 167 System.out.println( "Schema#loadFromLdif: " + e.toString() ); 168 } 169 } 170 171 172 public void loadFromRecord( LdifContentRecord schemaRecord ) 173 { 174 try 175 { 176 this.parseSchemaRecord( schemaRecord ); 177 } 178 catch ( Exception e ) 179 { 180 System.out.println( "Schema#loadFromRecord: " + e.toString() ); 181 } 182 } 183 184 185 190 public void saveToLdif( Writer writer ) 191 { 192 try 193 { 194 writer.write( this.getSchemaRecord().toFormattedString() ); 195 } 196 catch ( Exception e ) 197 { 198 System.out.println( "Schema#saveToLdif: " + e.toString() ); 199 } 200 } 201 202 203 private void parseSchemaRecord( LdifContentRecord schemaRecord ) throws Exception 204 { 205 206 this.setSchemaRecord( schemaRecord ); 207 this.setDn( new DN( schemaRecord.getDnLine().getValueAsString() ) ); 208 209 LdifAttrValLine[] lines = schemaRecord.getAttrVals(); 210 for ( int i = 0; i < lines.length; i++ ) 211 { 212 LdifAttrValLine line = lines[i]; 213 String attributeName = line.getUnfoldedAttributeDescription(); 214 String value = line.getValueAsString(); 215 216 SchemaLexer lexer = new SchemaLexer( new StringReader ( value ) ); 217 SchemaParser parser = new SchemaParser( lexer ); 218 219 try 220 { 221 if ( attributeName.equalsIgnoreCase( Schema.SCHEMA_ATTRIBUTE_OBJECTCLASSES ) ) 222 { 223 ObjectClassDescription ocd = parser.objectClassDescription(); 224 ocd.setSchema( this ); 225 ocd.setLine( line ); 226 this.addObjectClassDescription( ocd ); 227 } 228 else if ( attributeName.equalsIgnoreCase( Schema.SCHEMA_ATTRIBUTE_ATTRIBUTETYPES ) ) 229 { 230 AttributeTypeDescription atd = parser.attributeTypeDescription(); 231 atd.setSchema( this ); 232 atd.setLine( line ); 233 this.addAttributeTypeDescription( atd ); 234 } 235 else if ( attributeName.equalsIgnoreCase( Schema.SCHEMA_ATTRIBUTE_LDAPSYNTAXES ) ) 236 { 237 LdapSyntaxDescription lsd = parser.syntaxDescription(); 238 lsd.setSchema( this ); 239 lsd.setLine( line ); 240 this.addLdapSyntaxDescription( lsd ); 241 } 242 else if ( attributeName.equalsIgnoreCase( Schema.SCHEMA_ATTRIBUTE_MATCHINGRULES ) ) 243 { 244 MatchingRuleDescription mrd = parser.matchingRuleDescription(); 245 mrd.setSchema( this ); 246 mrd.setLine( line ); 247 this.addMatchingRuleDescription( mrd ); 248 } 249 else if ( attributeName.equalsIgnoreCase( Schema.SCHEMA_ATTRIBUTE_MATCHINGRULEUSE ) ) 250 { 251 MatchingRuleUseDescription mrud = parser.matchingRuleUseDescription(); 252 mrud.setSchema( this ); 253 mrud.setLine( line ); 254 this.addMatchingRuleUseDescription( mrud ); 255 } 256 else if ( attributeName.equalsIgnoreCase( IAttribute.OPERATIONAL_ATTRIBUTE_CREATE_TIMESTAMP ) ) 257 { 258 this.setCreateTimestamp( value ); 259 } 260 else if ( attributeName.equalsIgnoreCase( IAttribute.OPERATIONAL_ATTRIBUTE_MODIFY_TIMESTAMP ) ) 261 { 262 this.setModifyTimestamp( value ); 263 } 264 } 265 catch ( Exception e ) 266 { 267 System.out.println( e.getMessage() + ": " + attributeName + " - " + value ); 268 e.printStackTrace(); 269 } 270 } 271 272 ObjectClassDescription extensibleObjectOcd = this 274 .getObjectClassDescription( ObjectClassDescription.EXTENSIBLEOBJECT_OBJECTCLASSNAME ); 275 AttributeTypeDescription[] userAtds = SchemaUtils.getUserAttributeDescriptions( this ); 276 String [] attributeTypeDescriptionNames = SchemaUtils.getAttributeTypeDescriptionNames( userAtds ); 277 extensibleObjectOcd.setMayAttributeTypeDescriptionNames( attributeTypeDescriptionNames ); 278 } 279 280 281 286 public LdifContentRecord getSchemaRecord() 287 { 288 return schemaRecord; 289 } 290 291 292 public void setSchemaRecord( LdifContentRecord schemaRecord ) 293 { 294 this.schemaRecord = schemaRecord; 295 } 296 297 298 302 public DN getDn() 303 { 304 return dn; 305 } 306 307 308 public void setDn( DN dn ) 309 { 310 this.dn = dn; 311 } 312 313 314 318 public String getCreateTimestamp() 319 { 320 return createTimestamp; 321 } 322 323 324 public void setCreateTimestamp( String createTimestamp ) 325 { 326 this.createTimestamp = createTimestamp; 327 } 328 329 330 334 public String getModifyTimestamp() 335 { 336 return modifyTimestamp; 337 } 338 339 340 public void setModifyTimestamp( String modifyTimestamp ) 341 { 342 this.modifyTimestamp = modifyTimestamp; 343 } 344 345 346 351 public String [] getObjectClasses() 352 { 353 return objectClasses; 354 } 355 356 357 public void setObjectClasses( String [] objectClasses ) 358 { 359 this.objectClasses = objectClasses; 360 } 361 362 363 367 Map getAtdMapByName() 368 { 369 return atdMapByName; 370 } 371 372 373 void setAtdMapByName( Map atdMapByName ) 374 { 375 this.atdMapByName = atdMapByName; 376 } 377 378 379 public void addAttributeTypeDescription( AttributeTypeDescription atd ) 380 { 381 if ( atd.getNames() != null && atd.getNames().length > 0 ) 382 { 383 for ( int i = 0; i < atd.getNames().length; i++ ) 384 { 385 this.atdMapByName.put( atd.getNames()[i].toLowerCase(), atd ); 386 } 387 } 388 if ( atd.getNumericOID() != null ) 389 { 390 this.atdMapByName.put( atd.getNumericOID().toLowerCase(), atd ); 391 } 392 } 393 394 395 399 public String [] getAttributeTypeDescriptionNames() 400 { 401 Set set = new HashSet (); 402 for ( Iterator it = this.atdMapByName.values().iterator(); it.hasNext(); ) 403 { 404 AttributeTypeDescription atd = ( AttributeTypeDescription ) it.next(); 405 for ( int i = 0; i < atd.getNames().length; i++ ) 406 { 407 set.add( atd.getNames()[i] ); 408 } 409 } 410 return ( String [] ) set.toArray( new String [set.size()] ); 411 } 412 413 414 public AttributeTypeDescription[] getAttributeTypeDescriptions() 415 { 416 Set set = new HashSet (); 417 for ( Iterator it = this.atdMapByName.values().iterator(); it.hasNext(); ) 418 { 419 AttributeTypeDescription atd = ( AttributeTypeDescription ) it.next(); 420 set.add( atd ); 421 } 422 return ( AttributeTypeDescription[] ) set.toArray( new AttributeTypeDescription[set.size()] ); 423 } 424 425 426 430 public Map getLsdMapByNumericOID() 431 { 432 return lsdMapByNumericOID; 433 } 434 435 436 public void setLsdMapByNumericOID( Map lsdMapByNumericOID ) 437 { 438 this.lsdMapByNumericOID = lsdMapByNumericOID; 439 } 440 441 442 public void addLdapSyntaxDescription( LdapSyntaxDescription lsd ) 443 { 444 if ( lsd.getNumericOID() != null ) 445 { 446 this.lsdMapByNumericOID.put( lsd.getNumericOID().toLowerCase(), lsd ); 447 } 448 } 449 450 451 455 public String [] getLdapSyntaxDescriptionOids() 456 { 457 Set set = new HashSet (); 458 for ( Iterator it = this.lsdMapByNumericOID.values().iterator(); it.hasNext(); ) 459 { 460 LdapSyntaxDescription lsd = ( LdapSyntaxDescription ) it.next(); 461 set.add( lsd.getNumericOID() ); 462 } 463 return ( String [] ) set.toArray( new String [set.size()] ); 464 } 465 466 467 public LdapSyntaxDescription[] getLdapSyntaxDescriptions() 468 { 469 Set set = new HashSet (); 470 for ( Iterator it = this.lsdMapByNumericOID.values().iterator(); it.hasNext(); ) 471 { 472 LdapSyntaxDescription lsd = ( LdapSyntaxDescription ) it.next(); 473 set.add( lsd ); 474 } 475 return ( LdapSyntaxDescription[] ) set.toArray( new LdapSyntaxDescription[set.size()] ); 476 } 477 478 479 483 public Map getMrdMapByName() 484 { 485 return mrdMapByName; 486 } 487 488 489 public void setMrdMapByName( Map mrdMapByName ) 490 { 491 this.mrdMapByName = mrdMapByName; 492 } 493 494 495 public void addMatchingRuleDescription( MatchingRuleDescription mrd ) 496 { 497 if ( mrd.getNames() != null && mrd.getNames().length > 0 ) 498 { 499 for ( int i = 0; i < mrd.getNames().length; i++ ) 500 { 501 this.mrdMapByName.put( mrd.getNames()[i].toLowerCase(), mrd ); 502 } 503 } 504 if ( mrd.getNumericOID() != null ) 505 { 506 this.mrdMapByNumericOID.put( mrd.getNumericOID().toLowerCase(), mrd ); 507 } 508 } 509 510 511 515 public Map getMrdMapByNumericOID() 516 { 517 return mrdMapByNumericOID; 518 } 519 520 521 public void setMrdMapByNumericOID( Map mrdMapByNumericOID ) 522 { 523 this.mrdMapByNumericOID = mrdMapByNumericOID; 524 } 525 526 527 531 public Map getMrudMapByName() 532 { 533 return mrudMapByName; 534 } 535 536 537 public void setMrudMapByName( Map mrudMapByName ) 538 { 539 this.mrudMapByName = mrudMapByName; 540 } 541 542 543 public void addMatchingRuleUseDescription( MatchingRuleUseDescription mrud ) 544 { 545 if ( mrud.getNames() != null && mrud.getNames().length > 0 ) 546 { 547 for ( int i = 0; i < mrud.getNames().length; i++ ) 548 { 549 this.mrudMapByName.put( mrud.getNames()[i].toLowerCase(), mrud ); 550 } 551 } 552 if ( mrud.getNumericOID() != null ) 553 { 554 this.mrudMapByNumericOID.put( mrud.getNumericOID().toLowerCase(), mrud ); 555 } 556 } 557 558 559 563 public Map getMrudMapByNumericOID() 564 { 565 return mrudMapByNumericOID; 566 } 567 568 569 public void setMrduMapByNumericOID( Map mrudMapByNumericOID ) 570 { 571 this.mrudMapByNumericOID = mrudMapByNumericOID; 572 } 573 574 575 579 Map getOcdMapByName() 580 { 581 return ocdMapByName; 582 } 583 584 585 void setOcdMapByName( Map ocdMapByName ) 586 { 587 this.ocdMapByName = ocdMapByName; 588 } 589 590 591 public void addObjectClassDescription( ObjectClassDescription ocd ) 592 { 593 if ( ocd.getNames() != null && ocd.getNames().length > 0 ) 594 { 595 for ( int i = 0; i < ocd.getNames().length; i++ ) 596 { 597 this.ocdMapByName.put( ocd.getNames()[i].toLowerCase(), ocd ); 598 } 599 } 600 if ( ocd.getNumericOID() != null ) 601 { 602 this.ocdMapByName.put( ocd.getNumericOID().toLowerCase(), ocd ); 603 } 604 } 605 606 607 611 public String [] getObjectClassDescriptionNames() 612 { 613 Set set = new HashSet (); 614 for ( Iterator it = this.ocdMapByName.values().iterator(); it.hasNext(); ) 615 { 616 ObjectClassDescription ocd = ( ObjectClassDescription ) it.next(); 617 for ( int i = 0; i < ocd.getNames().length; i++ ) 618 { 619 set.add( ocd.getNames()[i] ); 620 } 621 } 622 return ( String [] ) set.toArray( new String [set.size()] ); 623 } 624 625 626 public ObjectClassDescription[] getObjectClassDescriptions() 627 { 628 Set set = new HashSet (); 629 for ( Iterator it = this.ocdMapByName.values().iterator(); it.hasNext(); ) 630 { 631 ObjectClassDescription ocd = ( ObjectClassDescription ) it.next(); 632 set.add( ocd ); 633 } 634 return ( ObjectClassDescription[] ) set.toArray( new ObjectClassDescription[set.size()] ); 635 } 636 637 638 644 public boolean hasObjectClassDescription( String name ) 645 { 646 return this.ocdMapByName.containsKey( name.toLowerCase() ); 647 } 648 649 650 659 public ObjectClassDescription getObjectClassDescription( String name ) 660 { 661 if ( this.ocdMapByName.containsKey( name.toLowerCase() ) ) 662 { 663 return ( ObjectClassDescription ) this.ocdMapByName.get( name.toLowerCase() ); 664 } 665 else if ( !this.isDefault() ) 666 { 667 return DEFAULT_SCHEMA.getObjectClassDescription( name ); 668 } 669 else 670 { 671 ObjectClassDescription ocd = new ObjectClassDescription(); 673 ocd.setSchema( this ); 674 ocd.setNumericOID( name ); 675 ocd.setNames( new String [] 676 { name } ); 677 return ocd; 678 } 679 } 680 681 682 688 public boolean hasAttributeTypeDescription( String name ) 689 { 690 return this.atdMapByName.containsKey( name.toLowerCase() ); 691 } 692 693 694 703 public AttributeTypeDescription getAttributeTypeDescription( String description ) 704 { 705 706 AttributeDescription ad = new AttributeDescription( description ); 707 String attributeType = ad.getParsedAttributeType(); 708 709 if ( this.atdMapByName.containsKey( attributeType.toLowerCase() ) ) 710 { 711 return ( AttributeTypeDescription ) this.atdMapByName.get( attributeType.toLowerCase() ); 712 } 713 else if ( !this.isDefault() ) 714 { 715 return DEFAULT_SCHEMA.getAttributeTypeDescription( attributeType ); 716 } 717 else 718 { 719 AttributeTypeDescription atd = new AttributeTypeDescription(); 721 atd.setSchema( this ); 722 atd.setNumericOID( attributeType ); 723 atd.setNames( new String [] 724 { attributeType } ); 725 atd.setNoUserModification( true ); 726 atd.setUsage( "" ); 727 return atd; 728 } 729 } 730 731 732 737 public boolean hasLdapSyntaxDescription( String numericOID ) 738 { 739 return this.lsdMapByNumericOID.containsKey( numericOID.toLowerCase() ); 740 } 741 742 743 751 public LdapSyntaxDescription getLdapSyntaxDescription( String numericOID ) 752 { 753 if ( this.lsdMapByNumericOID.containsKey( numericOID.toLowerCase() ) ) 754 { 755 return ( LdapSyntaxDescription ) this.lsdMapByNumericOID.get( numericOID.toLowerCase() ); 756 } 757 else if ( !this.isDefault() ) 758 { 759 return DEFAULT_SCHEMA.getLdapSyntaxDescription( numericOID ); 760 } 761 else 762 { 763 LdapSyntaxDescription lsd = new LdapSyntaxDescription(); 765 lsd.setSchema( this ); 766 lsd.setNumericOID( numericOID ); 767 return lsd; 768 } 769 } 770 771 772 778 public boolean hasMatchingRuleDescription( String nameOrOID ) 779 { 780 return this.mrdMapByName.containsKey( nameOrOID.toLowerCase() ) 781 || this.mrdMapByNumericOID.containsKey( nameOrOID.toLowerCase() ); 782 } 783 784 785 794 public MatchingRuleDescription getMatchingRuleDescription( String nameOrOID ) 795 { 796 if ( this.mrdMapByName.containsKey( nameOrOID.toLowerCase() ) ) 797 { 798 return ( MatchingRuleDescription ) this.mrdMapByName.get( nameOrOID.toLowerCase() ); 799 } 800 else if ( this.mrdMapByNumericOID.containsKey( nameOrOID.toLowerCase() ) ) 801 { 802 return ( MatchingRuleDescription ) this.mrdMapByNumericOID.get( nameOrOID.toLowerCase() ); 803 } 804 else if ( !this.isDefault() ) 805 { 806 return DEFAULT_SCHEMA.getMatchingRuleDescription( nameOrOID ); 807 } 808 else 809 { 810 MatchingRuleDescription mrd = new MatchingRuleDescription(); 812 mrd.setSchema( this ); 813 mrd.setNumericOID( nameOrOID ); 814 return mrd; 815 } 816 } 817 818 819 825 public boolean hasMatchingRuleUseDescription( String nameOrOID ) 826 { 827 return this.mrudMapByName.containsKey( nameOrOID.toLowerCase() ) 828 || this.mrudMapByNumericOID.containsKey( nameOrOID.toLowerCase() ); 829 } 830 831 832 841 public MatchingRuleUseDescription getMatchingRuleUseDescription( String nameOrOID ) 842 { 843 if ( this.mrudMapByName.containsKey( nameOrOID.toLowerCase() ) ) 844 { 845 return ( MatchingRuleUseDescription ) this.mrudMapByName.get( nameOrOID.toLowerCase() ); 846 } 847 else if ( this.mrudMapByNumericOID.containsKey( nameOrOID.toLowerCase() ) ) 848 { 849 return ( MatchingRuleUseDescription ) this.mrudMapByNumericOID.get( nameOrOID.toLowerCase() ); 850 } 851 else if ( !this.isDefault() ) 852 { 853 return DEFAULT_SCHEMA.getMatchingRuleUseDescription( nameOrOID ); 854 } 855 else 856 { 857 MatchingRuleUseDescription mrud = new MatchingRuleUseDescription(); 859 mrud.setSchema( this ); 860 mrud.setNumericOID( nameOrOID ); 861 return mrud; 862 } 863 } 864 865 866 static String [] addValue( String [] array, String value ) 867 { 868 List list = new ArrayList ( Arrays.asList( array ) ); 869 list.add( value ); 870 return ( String [] ) list.toArray( new String [list.size()] ); 871 } 872 873 } 874 | Popular Tags |