| What's in a Name?

## Handling Special Characters

> A naming convention, such as that for the LDAP or the file system, typically has meta characters. For example, in the LDAP, if one of the following characters appears in the name, then it must be preceded by the escape character, the backslash character ("\"):
> - A space or "#" character occurring at the beginning of the string
> - A space character occurring at the end of the string
> - One of the characters ",", "+", """, "\", "<", ">" or ";"
> When you are specifying a name to one of the `Context` methods, you not only must pay attention to the special characters and naming convention of the underlying naming system. You also need to be concerned about the JNDI composite name syntax, which also defines special characters. The combination of the two syntaxes might lead to many levels of escaping.

> For example, suppose that you have a `"cn"` attribute whose value contains a backslash character.

> >     backslash\a

> The LDAP requires that the backslash character in a name be escaped. Therefore when you use this attribute as an LDAP name, you must precede the backslash character in its value with another backslash character, as follows:

> >     cn=backslash\\a

> The backslash character is also a special character in the JNDI, so if you supply this string name as a composite name, then you must escape the backslashes, again by preceding each with a backslash character:

> >     cn=backslash\\\\a

> If you specify this as a literal in the Java programming language, then you need to follow the Java language requirements and escape a backslash within a string literal with yet another backslash:

> >     String cname = "cn=backslash\\\\\\\\a";

> #### String Names As Composite Names

> You need to keep in mind that the string names that you pass to the `Context` methods are composite names. To avoid any surprises if a name contains special characters that might conflict with the JNDI composite name syntax, you should use the `Context` methods that accept a `Name`. Two ways are available for doing this. The first way is to use a `CompositeName`. You create a `CompositeName` object and then append the naming system-specific name (such as an LDAP name) to it. Here is an example.

> >     String dn = ...; // An LDAP distinguished name
> >     Name composite = new CompositeName().add(dn);
> >     Object obj = ctx.lookup(composite);

> Applying this technique to the previous LDAP sample name, you would no longer need to add escapes for the JNDI syntax manually because that would be handled automatically by the `CompositeName` class.

> >     Name composite = new CompositeName().add("cn=backslash\\\\a");
> >     Object obj = ctx.lookup(composite);

> The second way is to use a compound name. You create a compound name by parsing the naming system-specific name (such as an LDAP name). Here is an example.

> >     String dn = ...; // An LDAP distinguished name
> >     NameParser ldapParser = ctx.getNameParser("");
> >     Name compound = ldapParser.parse(dn);
> >     Object obj = ctx.lookup(compound);

> Applying this technique to the previous LDAP sample name, you would no longer need to add escapes for the JNDI syntax because you are not using JNDI composite names.

> >     Name compound = ldapParser.parse("cn=backslash\\\\a");
> >     Object obj = ctx.lookup(compound);

| What's in a Name?