After several days of efforts, we can get the code for getting the objectclass definition and getting the attribute definition, so that we can dynamically obtain the objectclass and attribute in the Schema Based on the custom schema. This is especially useful for adding and modifying interfaces. If you modify the schema, you do not need to modify the code to adjust the code. You only need to arrange the number of retrieved attributes to allow others to enter values, you can also check whether must has been filled in.
/**
* Get the definition of the specified objectclass
* @ Param name
*/
Public void getobjectclassdefinition (string name ){
Try {
// Get the schema tree root
Dircontext schema = Dc. getschema ("");
// Get the schema object for "person"
Dircontext personschema = (dircontext) Schema. Lookup ("classdefinition/" + name );
Attributes a = personschema. getattributes ("");
Namingenumeration ane = A. getall ();
While (ANE. hasmore ()){
Attribute ATTR = (attribute) ANE. Next ();
String attrtype = ATTR. GETID ();
Namingenumeration values = ATTR. getall ();
While (values. hasmore ()){
Object oneval = values. nextelement ();
If (oneval instanceof string ){
System. Out. println (attrtype + ":" + (string) oneval );
} Else {
System. Out. println (attrtype + ":" + new string (byte []) oneval ));
}
}
}
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Get the objectclass definition of the specified DN
* @ Param DN
*/
Public void getdnobjectclassdefinition (string DN ){
Try {
// Get context containing class definitions for the "cn = Ted Geisel" entry
Dircontext tedclasses = Dc. getschemaclassdefinition (DN );
// Enumerate the class definitions
Namingenumeration enum1 = tedclasses. Search ("", null );
While (enum1.hasmore ()){
Object o = enum1.next ();
System. Out. println (searchresult) O). getname ());
Attributes A = (searchresult) O). getattributes ();
Namingenumeration ane = A. getall ();
While (ANE. hasmore ()){
Attribute ATTR = (attribute) ANE. Next ();
String attrtype = ATTR. GETID ();
Namingenumeration values = ATTR. getall ();
While (values. hasmore ()){
Object oneval = values. nextelement ();
If (oneval instanceof string ){
System. Out. println (attrtype + ":" + (string) oneval );
} Else {
System. Out. println (attrtype + ":" + new string (byte []) oneval ));
}
}
}
}
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Get the attribute definition of the specified name
* @ Param name
*/
Public void getattributedefinition (string name ){
Try {
// Get the schema tree root
Dircontext schema = Dc. getschema ("");
// Get the schema object for "person"
Dircontext personschema = (dircontext) Schema. Lookup ("attributedefinition/" + name );
Attributes a = personschema. getattributes ("");
Namingenumeration ane = A. getall ();
While (ANE. hasmore ()){
Attribute ATTR = (attribute) ANE. Next ();
String attrtype = ATTR. GETID ();
Namingenumeration values = ATTR. getall ();
While (values. hasmore ()){
Object oneval = values. nextelement ();
If (oneval instanceof string ){
System. Out. println (attrtype + ":" + (string) oneval );
} Else {
System. Out. println (attrtype + ":" + new string (byte []) oneval ));
}
}
}
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Get the attribute definition of the specified name in the specified DN
* @ Param DN
* @ Param name
*/
Public void getdnattributedefinition (string DN, string name ){
Try {
// Get an attribute of that type
Attributes attrs = Dc. getattributes (DN, new string [] {name });
Attribute cnattr = attrs. Get (name );
// Get its attribute type definition
Dircontext cnschema = cnattr. getattributedefinition ();
// Get cnschema's attributes
Attributes cnattrs = cnschema. getattributes ("");
Namingenumeration ane = cnattrs. getall ();
While (ANE. hasmore ()){
Attribute ATTR = (attribute) ANE. Next ();
String attrtype = ATTR. GETID ();
Namingenumeration values = ATTR. getall ();
While (values. hasmore ()){
Object oneval = values. nextelement ();
If (oneval instanceof string ){
System. Out. println (attrtype + ":" + (string) oneval );
} Else {
System. Out. println (attrtype + ":" + new string (byte []) oneval ));
}
}
}
} Catch (exception e ){
E. printstacktrace ();
}
}