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