Previous | Next | Trail Map | Beyond the Basics | 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 ("\"): When you are specifying a name to one of the Context(in the API reference documentation) 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 first backslash character, which is an escape preceding a meta character, must be escaped again if you supply this string name as a JNDI composite name. Please note that the second backslash is a character in the original name and not an escape, thus does not need to be escaped again.
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(in the API reference documentation). Two ways are available for doing this. The first way is to use a CompositeName(in the API reference documentation). 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);


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