| /* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| * |
| */ |
| package org.apache.directory.shared.ldap.name; |
| |
| |
| import java.util.List; |
| |
| import org.apache.directory.shared.ldap.exception.LdapInvalidDnException; |
| |
| |
| /** |
| * This class parses a DN. The DN MUST respect this BNF grammar (as of RFC2253, |
| * par. 3, and RFC1779, fig. 1) <br> |
| * <p> - <distinguishedName> ::= <name> | e <br> - <name> ::= |
| * <name-component> <name-components> <br> - <name-components> |
| * ::= <spaces> <separator> <spaces> <name-component> |
| * <name-components> | e <br> - <name-component> ::= |
| * <attributeType> <spaces> '=' <spaces> |
| * <attributeValue> <attributeTypeAndValues> <br> - |
| * <attributeTypeAndValues> ::= <spaces> '+' <spaces> |
| * <attributeType> <spaces> '=' <spaces> |
| * <attributeValue> <attributeTypeAndValues> | e <br> - |
| * <attributeType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9] |
| * <digits> <oids> | [0-9] <digits> <oids> <br> - |
| * <keychars> ::= [a-zA-Z] <keychars> | [0-9] <keychars> | '-' |
| * <keychars> | e <br> - <oidPrefix> ::= 'OID.' | 'oid.' | e <br> - |
| * <oids> ::= '.' [0-9] <digits> <oids> | e <br> - |
| * <attributeValue> ::= <pairs-or-strings> | '#' <hexstring> |
| * |'"' <quotechar-or-pairs> '"' <br> - <pairs-or-strings> ::= '\' |
| * <pairchar> <pairs-or-strings> | <stringchar> |
| * <pairs-or-strings> | e <br> - <quotechar-or-pairs> ::= |
| * <quotechar> <quotechar-or-pairs> | '\' <pairchar> |
| * <quotechar-or-pairs> | e <br> - <pairchar> ::= ',' | '=' | '+' | |
| * '<' | '>' | '#' | ';' | '\' | '"' | [0-9a-fA-F] [0-9a-fA-F] <br> - |
| * <hexstring> ::= [0-9a-fA-F] [0-9a-fA-F] <hexpairs> <br> - |
| * <hexpairs> ::= [0-9a-fA-F] [0-9a-fA-F] <hexpairs> | e <br> - |
| * <digits> ::= [0-9] <digits> | e <br> - <stringchar> ::= |
| * [0x00-0xFF] - [,=+<>#;\"\n\r] <br> - <quotechar> ::= [0x00-0xFF] - |
| * [\"] <br> - <separator> ::= ',' | ';' <br> - <spaces> ::= ' ' |
| * <spaces> | e <br> |
| * </p> |
| * |
| * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> |
| */ |
| public enum DnParser |
| { |
| INSTANCE; |
| |
| /** |
| * Get a reference to the NameParser. Needed to be compliant with the JNDI |
| * API |
| * |
| * @return An instance of the NameParser |
| */ |
| public static DnParser getNameParser() |
| { |
| return INSTANCE; |
| } |
| |
| |
| /** |
| * Parse a DN. |
| * |
| * @param name The DN to be parsed |
| * @param rdns The list that will contain the RDNs |
| * @throws LdapInvalidDnException If the DN is invalid |
| */ |
| public static void parseInternal( String name, List<RDN> rdns ) throws LdapInvalidDnException |
| { |
| try |
| { |
| FastDnParser.INSTANCE.parseDn( name, rdns ); |
| } |
| catch ( TooComplexException e ) |
| { |
| rdns.clear(); |
| new ComplexDnParser().parseDn( name, rdns ); |
| } |
| } |
| |
| |
| /** |
| * Validate a DN |
| * |
| * @param name The DN to be parsed |
| * |
| * @return <code>true</code> if the DN is valid |
| */ |
| public static boolean validateInternal( String name ) |
| { |
| DN dn = new DN(); |
| try |
| { |
| parseInternal( name, dn.rdns ); |
| return true; |
| } |
| catch ( LdapInvalidDnException e ) |
| { |
| return false; |
| } |
| } |
| |
| |
| /** |
| * Parse a String and return a DN if the String is a valid DN |
| * |
| * @param dn The DN to parse |
| * @return A DN |
| * @throws LdapInvalidDnException If the String is not a valid DN |
| */ |
| public DN parse( String dn ) throws LdapInvalidDnException |
| { |
| return new DN( dn ); |
| } |
| } |