Previous | Next | Trail Map | Beyond the Basics | What's in a Name?

Name Parsers

To parse a name means to use its string representation (java.lang.String) to obtain its structural representation ( Name(in the API reference documentation)). 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(in the API reference documentation) . For example, the following code parses a string name into a structured name, CompositeName.
// Parse string name into CompositeName
Name cname = new CompositeName(
    "cn=homedir,cn=Jon Ruiz,ou=people/tutorial/report.txt");
See the Composite Name Section for examples of how to access and change the components of a CompositeName.

Compound Names

To parse a compound name, you use the NameParser(in the API reference documentation) interface. This interface contains a single method:
Name parse(String name) throws InvalidNameException;(in the API reference documentation)

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.

// 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:/");
See the Compound Name Section for more examples of how to get a NameParser instance.

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:

// Parse name using LDAP parser
Name compoundName = ldapParser.parse("cn=John Smith, ou=People, o=JNDITutorial");
Similarly, you can use fsParser to parse a file name:
// 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(in the API reference documentation).

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(in the API reference documentation) . The only requirement is that it implements the Name interface. The exact type of the object returned depends on the service provider implementation.


Previous | Next | Trail Map | Beyond the Basics | What's in a Name?