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> - &lt;distinguishedName&gt; ::= &lt;name&gt; | e <br> - &lt;name&gt; ::=
032     * &lt;name-component&gt; &lt;name-components&gt; <br> - &lt;name-components&gt;
033     * ::= &lt;spaces&gt; &lt;separator&gt; &lt;spaces&gt; &lt;name-component&gt;
034     * &lt;name-components&gt; | e <br> - &lt;name-component&gt; ::=
035     * &lt;attributeType&gt; &lt;spaces&gt; '=' &lt;spaces&gt;
036     * &lt;attributeValue&gt; &lt;attributeTypeAndValues&gt; <br> -
037     * &lt;attributeTypeAndValues&gt; ::= &lt;spaces&gt; '+' &lt;spaces&gt;
038     * &lt;attributeType&gt; &lt;spaces&gt; '=' &lt;spaces&gt;
039     * &lt;attributeValue&gt; &lt;attributeTypeAndValues&gt; | e <br> -
040     * &lt;attributeType&gt; ::= [a-zA-Z] &lt;keychars&gt; | &lt;oidPrefix&gt; [0-9]
041     * &lt;digits&gt; &lt;oids&gt; | [0-9] &lt;digits&gt; &lt;oids&gt; <br> -
042     * &lt;keychars&gt; ::= [a-zA-Z] &lt;keychars&gt; | [0-9] &lt;keychars&gt; | '-'
043     * &lt;keychars&gt; | e <br> - &lt;oidPrefix&gt; ::= 'OID.' | 'oid.' | e <br> -
044     * &lt;oids&gt; ::= '.' [0-9] &lt;digits&gt; &lt;oids&gt; | e <br> -
045     * &lt;attributeValue&gt; ::= &lt;pairs-or-strings&gt; | '#' &lt;hexstring&gt;
046     * |'"' &lt;quotechar-or-pairs&gt; '"' <br> - &lt;pairs-or-strings&gt; ::= '\'
047     * &lt;pairchar&gt; &lt;pairs-or-strings&gt; | &lt;stringchar&gt;
048     * &lt;pairs-or-strings&gt; | e <br> - &lt;quotechar-or-pairs&gt; ::=
049     * &lt;quotechar&gt; &lt;quotechar-or-pairs&gt; | '\' &lt;pairchar&gt;
050     * &lt;quotechar-or-pairs&gt; | e <br> - &lt;pairchar&gt; ::= ',' | '=' | '+' |
051     * '&lt;' | '&gt;' | '#' | ';' | '\' | '"' | [0-9a-fA-F] [0-9a-fA-F] <br> -
052     * &lt;hexstring&gt; ::= [0-9a-fA-F] [0-9a-fA-F] &lt;hexpairs&gt; <br> -
053     * &lt;hexpairs&gt; ::= [0-9a-fA-F] [0-9a-fA-F] &lt;hexpairs&gt; | e <br> -
054     * &lt;digits&gt; ::= [0-9] &lt;digits&gt; | e <br> - &lt;stringchar&gt; ::=
055     * [0x00-0xFF] - [,=+&lt;&gt;#;\"\n\r] <br> - &lt;quotechar&gt; ::= [0x00-0xFF] -
056     * [\"] <br> - &lt;separator&gt; ::= ',' | ';' <br> - &lt;spaces&gt; ::= ' '
057     * &lt;spaces&gt; | 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 11:22:36 +0100 (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    }