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 javax.naming.InvalidNameException;
026 import javax.naming.Name;
027 import javax.naming.NameParser;
028
029
030 /**
031 * This class parses a DN. The DN MUST respect this BNF grammar (as of RFC2253,
032 * par. 3, and RFC1779, fig. 1) <br>
033 * <p> - <distinguishedName> ::= <name> | e <br> - <name> ::=
034 * <name-component> <name-components> <br> - <name-components>
035 * ::= <spaces> <separator> <spaces> <name-component>
036 * <name-components> | e <br> - <name-component> ::=
037 * <attributeType> <spaces> '=' <spaces>
038 * <attributeValue> <attributeTypeAndValues> <br> -
039 * <attributeTypeAndValues> ::= <spaces> '+' <spaces>
040 * <attributeType> <spaces> '=' <spaces>
041 * <attributeValue> <attributeTypeAndValues> | e <br> -
042 * <attributeType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9]
043 * <digits> <oids> | [0-9] <digits> <oids> <br> -
044 * <keychars> ::= [a-zA-Z] <keychars> | [0-9] <keychars> | '-'
045 * <keychars> | e <br> - <oidPrefix> ::= 'OID.' | 'oid.' | e <br> -
046 * <oids> ::= '.' [0-9] <digits> <oids> | e <br> -
047 * <attributeValue> ::= <pairs-or-strings> | '#' <hexstring>
048 * |'"' <quotechar-or-pairs> '"' <br> - <pairs-or-strings> ::= '\'
049 * <pairchar> <pairs-or-strings> | <stringchar>
050 * <pairs-or-strings> | e <br> - <quotechar-or-pairs> ::=
051 * <quotechar> <quotechar-or-pairs> | '\' <pairchar>
052 * <quotechar-or-pairs> | e <br> - <pairchar> ::= ',' | '=' | '+' |
053 * '<' | '>' | '#' | ';' | '\' | '"' | [0-9a-fA-F] [0-9a-fA-F] <br> -
054 * <hexstring> ::= [0-9a-fA-F] [0-9a-fA-F] <hexpairs> <br> -
055 * <hexpairs> ::= [0-9a-fA-F] [0-9a-fA-F] <hexpairs> | e <br> -
056 * <digits> ::= [0-9] <digits> | e <br> - <stringchar> ::=
057 * [0x00-0xFF] - [,=+<>#;\"\n\r] <br> - <quotechar> ::= [0x00-0xFF] -
058 * [\"] <br> - <separator> ::= ',' | ';' <br> - <spaces> ::= ' '
059 * <spaces> | e <br>
060 * </p>
061 *
062 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
063 * @version $Rev: 765066 $, $Date: 2009-04-15 08:41:39 +0200 (Mer, 15 avr 2009) $
064 */
065 public enum LdapDnParser implements NameParser
066 {
067 INSTANCE;
068
069 /**
070 * Get a reference to the NameParser. Needed to be compliant with the JNDI
071 * API
072 *
073 * @return An instance of the NameParser
074 */
075 public static NameParser getNameParser()
076 {
077 return INSTANCE;
078 }
079
080
081 /**
082 * Parse a DN.
083 *
084 * @param dn The DN to be parsed
085 * @param rdns The list that will contain the RDNs
086 * @throws InvalidNameException If the DN is invalid
087 */
088 public static void parseInternal( String name, List<Rdn> rdns ) throws InvalidNameException
089 {
090 try
091 {
092 FastLdapDnParser.INSTANCE.parseDn( name, rdns );
093 }
094 catch ( TooComplexException e )
095 {
096 rdns.clear();
097 new ComplexLdapDnParser().parseDn( name, rdns );
098 }
099 }
100
101
102 /**
103 * Validate a DN
104 *
105 * @param dn The DN to be parsed
106 *
107 * @return <code>true</code> if the DN is valid
108 */
109 public static boolean validateInternal( String name )
110 {
111 LdapDN dn = new LdapDN();
112 try
113 {
114 parseInternal( name, dn.rdns );
115 return true;
116 }
117 catch ( InvalidNameException e )
118 {
119 return false;
120 }
121 }
122
123
124 /**
125 * Parse a String and return a LdapDN if the String is a valid DN
126 *
127 * @param dn
128 * The DN to parse
129 * @return A LdapDN
130 * @throws InvalidNameException
131 * If the String is not a valid DN
132 */
133 public Name parse( String dn ) throws InvalidNameException
134 {
135 return new LdapDN( dn );
136 }
137 }