![]() ![]() ![]() ![]() |
What's in a Name? |
To parse a name means to use its string representation (java.lang.String) to obtain its structural representation ( Name). The JNDI provides a name parser for composite names and a generic interface for compound name parsers. Service providers provide the actual implementations of name parsers for compound names exported by their namespaces.
Composite Names
To parse a composite name, you pass its string representation to the CompositeName constructor. For example, the following code parses a string name into a structured name, CompositeName.
See the Composite Name Section for examples of how to access and change the components of a CompositeName.// Parse string name into CompositeName Name cname = new CompositeName( "cn=homedir,cn=Jon Ruiz,ou=people/tutorial/report.txt");Compound Names
To parse a compound name, you use the NameParserinterface. This interface contains a single method:
Name parse(String name) throws InvalidNameException;To parse a compound name, you must first obtain a NameParser from the service provider that supports the namespace. Here is an example that obtains name parsers for the LDAP namespace and file namespace.
See the Compound Name Section for more examples of how to get a NameParser instance.// Create the initial context Context ctx = new InitialContext(); // Get the parser for LDAP NameParser ldapParser = ctx.getNameParser("ldap://localhost:389/o=jnditutorial"); // Get the parser for filenames NameParser fsParser = ctx.getNameParser("file:/");Once you have an instance of a NameParser, you can use its parse() method to parse compound names. Continuing with the example, you can use ldapParser to parse an LDAP string name into its structural form:
Similarly, you can use fsParser to parse a file name:// Parse name using LDAP parser Name compoundName = ldapParser.parse("cn=John Smith, ou=People, o=JNDITutorial");// Parse name using LDAP parser Name compoundName = fsParser.parse("tmp/tutorial/beyond/names/parse.html");Note that each parser determines the syntax of names that it will accept. If you supply a valid filename that is not a legal LDAP name to an LDAP parser, you will get an InvalidNameException
.
See the Compound Name Section for examples of how to access and change the components of a compound name.
Although the parse() method returns a Name, NameParser is intended for use only for compound names, not composite names. The object returned by parse() might or might not be an instance of CompoundName
. The only requirement is that it implements the Name interface. The exact type of the object returned depends on the service provider implementation.
![]() ![]() ![]() ![]() |
What's in a Name? |