001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.name;
021
022
023 import java.util.List;
024
025 import org.apache.directory.shared.ldap.exception.LdapInvalidDnException;
026
027
028 /**
029 * This class parses a DN. The DN MUST respect this BNF grammar (as of RFC2253,
030 * par. 3, and RFC1779, fig. 1) <br>
031 * <p> - <distinguishedName> ::= <name> | e <br> - <name> ::=
032 * <name-component> <name-components> <br> - <name-components>
033 * ::= <spaces> <separator> <spaces> <name-component>
034 * <name-components> | e <br> - <name-component> ::=
035 * <attributeType> <spaces> '=' <spaces>
036 * <attributeValue> <attributeTypeAndValues> <br> -
037 * <attributeTypeAndValues> ::= <spaces> '+' <spaces>
038 * <attributeType> <spaces> '=' <spaces>
039 * <attributeValue> <attributeTypeAndValues> | e <br> -
040 * <attributeType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9]
041 * <digits> <oids> | [0-9] <digits> <oids> <br> -
042 * <keychars> ::= [a-zA-Z] <keychars> | [0-9] <keychars> | '-'
043 * <keychars> | e <br> - <oidPrefix> ::= 'OID.' | 'oid.' | e <br> -
044 * <oids> ::= '.' [0-9] <digits> <oids> | e <br> -
045 * <attributeValue> ::= <pairs-or-strings> | '#' <hexstring>
046 * |'"' <quotechar-or-pairs> '"' <br> - <pairs-or-strings> ::= '\'
047 * <pairchar> <pairs-or-strings> | <stringchar>
048 * <pairs-or-strings> | e <br> - <quotechar-or-pairs> ::=
049 * <quotechar> <quotechar-or-pairs> | '\' <pairchar>
050 * <quotechar-or-pairs> | e <br> - <pairchar> ::= ',' | '=' | '+' |
051 * '<' | '>' | '#' | ';' | '\' | '"' | [0-9a-fA-F] [0-9a-fA-F] <br> -
052 * <hexstring> ::= [0-9a-fA-F] [0-9a-fA-F] <hexpairs> <br> -
053 * <hexpairs> ::= [0-9a-fA-F] [0-9a-fA-F] <hexpairs> | e <br> -
054 * <digits> ::= [0-9] <digits> | e <br> - <stringchar> ::=
055 * [0x00-0xFF] - [,=+<>#;\"\n\r] <br> - <quotechar> ::= [0x00-0xFF] -
056 * [\"] <br> - <separator> ::= ',' | ';' <br> - <spaces> ::= ' '
057 * <spaces> | e <br>
058 * </p>
059 *
060 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
061 * @version $Rev: 923667 $, $Date: 2010-03-16 12:22:36 +0200 (Tue, 16 Mar 2010) $
062 */
063 public enum DnParser
064 {
065 INSTANCE;
066
067 /**
068 * Get a reference to the NameParser. Needed to be compliant with the JNDI
069 * API
070 *
071 * @return An instance of the NameParser
072 */
073 public static DnParser getNameParser()
074 {
075 return INSTANCE;
076 }
077
078
079 /**
080 * Parse a DN.
081 *
082 * @param dn The DN to be parsed
083 * @param rdns The list that will contain the RDNs
084 * @throws LdapInvalidDnException If the DN is invalid
085 */
086 public static void parseInternal( String name, List<RDN> rdns ) throws LdapInvalidDnException
087 {
088 try
089 {
090 FastDnParser.INSTANCE.parseDn( name, rdns );
091 }
092 catch ( TooComplexException e )
093 {
094 rdns.clear();
095 new ComplexDnParser().parseDn( name, rdns );
096 }
097 }
098
099
100 /**
101 * Validate a DN
102 *
103 * @param dn The DN to be parsed
104 *
105 * @return <code>true</code> if the DN is valid
106 */
107 public static boolean validateInternal( String name )
108 {
109 DN dn = new DN();
110 try
111 {
112 parseInternal( name, dn.rdns );
113 return true;
114 }
115 catch ( LdapInvalidDnException e )
116 {
117 return false;
118 }
119 }
120
121
122 /**
123 * Parse a String and return a DN if the String is a valid DN
124 *
125 * @param dn The DN to parse
126 * @return A DN
127 * @throws LdapInvalidDnException If the String is not a valid DN
128 */
129 public DN parse( String dn ) throws LdapInvalidDnException
130 {
131 return new DN( dn );
132 }
133 }