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 characters, called meta characters, that have special meaning. For example, in the LDAP, if one of the following characters appear in the name, it must be preceded by the escape character (which is the backslash character): When you are specifying a name to one of the Context(in the API reference documentation) methods, in addition to paying 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 you have an LDAP name that contains a backslash character.

cn=backslash\a
Because the LDAP requires that the backslash character be escaped, you must precede the backslash with another backslash:
cn=backslash\\a
Because the backslash character is also a special character in the JNDI, if you supply this string name as a composite name, you must escape the backslashes, again, by preceding each with a backslash:
cn=backslash\\\\a
If you specify this as a literal in the Java programming language, the Java language requires that a backslash within a string literal be escaped with yet another backslash:
String cname = "cn=backslash\\\\\\\\a";

String Names are Composite Names

The important point to keep in mind is that the string names that you pass to the Context methods are composite names. To avoid any surprises should the name contain 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). There are two ways to do 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);
If you apply this technique to the LDAP sample name above, you eliminate the need to add escapes for the JNDI syntax manually because it is taken care of 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);
If you apply this technique to the LDAP sample name above, you eliminate the 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?