| 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`). 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.

> #### Parsing 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`.

> >     // Parse the string name into a CompositeName
> >     Name cname = new CompositeName(
> >         "cn=homedir,cn=Jon Ruiz,ou=people/tutorial/report.txt");

> See the Composite Names section for examples of how to access and change the components of a `CompositeName`.

> #### Parsing Compound Names

> To parse a compound name, you use the `NameParser` interface. This interface contains a single method:

> > `Name parse(String name) throws InvalidNameException;`

> First, however, you must 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 Names 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. As a continuation of the example, you can use `ldapParser` to parse an LDAP string name into its structural form, as follows.

> >     // Parse the name using the LDAP parser
> >     Name compoundName = ldapParser.parse("cn=John Smith, ou=People, o=JNDITutorial");

> Similarly, you can use `fsParser` to parse a filename, as follows.

> >     // Parse the name using the 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, then you will get an `InvalidNameException`.

> See the Compound Names section for examples of how to access and change the components of a compound name.

> Although `parse()` returns a `Name`, `NameParser` is intended to be used only for compound names and 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?