blob: f42cf1bdfc815b330928f52ebdfabae36a3cf45a [file] [log] [blame]
/*
* 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 org.apache.directory.shared.ldap.exception.LdapInvalidDnException;
/**
* This class parse the name-component part or the following BNF grammar (as of
* RFC2253, par. 3, and RFC1779, fig. 1) : <br> - &lt;name-component&gt; ::=
* &lt;attributeType&gt; &lt;spaces&gt; '=' &lt;spaces&gt;
* &lt;attributeValue&gt; &lt;attributeTypeAndValues&gt; <br> -
* &lt;attributeTypeAndValues&gt; ::= &lt;spaces&gt; '+' &lt;spaces&gt;
* &lt;attributeType&gt; &lt;spaces&gt; '=' &lt;spaces&gt;
* &lt;attributeValue&gt; &lt;attributeTypeAndValues&gt; | e <br> -
* &lt;attributeType&gt; ::= [a-zA-Z] &lt;keychars&gt; | &lt;oidPrefix&gt; [0-9]
* &lt;digits&gt; &lt;oids&gt; | [0-9] &lt;digits&gt; &lt;oids&gt; <br> -
* &lt;keychars&gt; ::= [a-zA-Z] &lt;keychars&gt; | [0-9] &lt;keychars&gt; | '-'
* &lt;keychars&gt; | e <br> - &lt;oidPrefix&gt; ::= 'OID.' | 'oid.' | e <br> -
* &lt;oids&gt; ::= '.' [0-9] &lt;digits&gt; &lt;oids&gt; | e <br> -
* &lt;attributeValue&gt; ::= &lt;pairs-or-strings&gt; | '#' &lt;hexstring&gt;
* |'"' &lt;quotechar-or-pairs&gt; '"' <br> - &lt;pairs-or-strings&gt; ::= '\'
* &lt;pairchar&gt; &lt;pairs-or-strings&gt; | &lt;stringchar&gt;
* &lt;pairs-or-strings&gt; | e <br> - &lt;quotechar-or-pairs&gt; ::=
* &lt;quotechar&gt; &lt;quotechar-or-pairs&gt; | '\' &lt;pairchar&gt;
* &lt;quotechar-or-pairs&gt; | e <br> - &lt;pairchar&gt; ::= ',' | '=' | '+' |
* '&lt;' | '&gt;' | '#' | ';' | '\' | '"' | [0-9a-fA-F] [0-9a-fA-F] <br> -
* &lt;hexstring&gt; ::= [0-9a-fA-F] [0-9a-fA-F] &lt;hexpairs&gt; <br> -
* &lt;hexpairs&gt; ::= [0-9a-fA-F] [0-9a-fA-F] &lt;hexpairs&gt; | e <br> -
* &lt;digits&gt; ::= [0-9] &lt;digits&gt; | e <br> - &lt;stringchar&gt; ::=
* [0x00-0xFF] - [,=+&lt;&gt;#;\"\n\r] <br> - &lt;quotechar&gt; ::= [0x00-0xFF] -
* [\"] <br> - &lt;separator&gt; ::= ',' | ';' <br> - &lt;spaces&gt; ::= ' '
* &lt;spaces&gt; | e <br>
* <br>
* A RDN is a part of a DN. It can be composed of many types, as in the RDN
* following RDN :<br>
* ou=value + cn=other value<br>
* <br>
* In this case, we have to store an 'ou' and a 'cn' in the RDN.<br>
* <br>
* The types are case insensitive. <br>
* Spaces before and after types and values are not stored.<br>
* Spaces before and after '+' are not stored.<br>
* <br>
* Thus, we can consider that the following RDNs are equals :<br>
* <br>
* 'ou=test 1'<br> ' ou=test 1'<br>
* 'ou =test 1'<br>
* 'ou= test 1'<br>
* 'ou=test 1 '<br> ' ou = test 1 '<br>
* <br>
* So are the following :<br>
* <br>
* 'ou=test 1+cn=test 2'<br>
* 'ou = test 1 + cn = test 2'<br> ' ou =test 1+ cn =test 2 ' <br>
* 'cn = test 2 +ou = test 1'<br>
* <br>
* but the following are not equal :<br>
* 'ou=test 1' <br>
* 'ou=test 1'<br>
* because we have more than one spaces inside the value.<br>
* <br>
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
public class RdnParser
{
/**
* Parse a NameComponent : <br>
* <p>
* &lt;name-component&gt; ::= &lt;attributeType&gt; &lt;spaces&gt; '='
* &lt;spaces&gt; &lt;attributeValue&gt; &lt;nameComponents&gt;
* </p>
*
* @param dn The String to parse
* @param rdn The RDN to fill. Beware that if the RDN is not empty, the new
* AttributeTypeAndValue will be added.
* @throws LdapInvalidDnException If the NameComponent is invalid
*/
public static void parse( String dn, RDN rdn ) throws LdapInvalidDnException
{
try
{
FastDnParser.INSTANCE.parseRdn( dn, rdn );
}
catch ( TooComplexException e )
{
rdn.clear();
new ComplexDnParser().parseRdn( dn, rdn );
}
}
/**
* Validate a NameComponent : <br>
* <p>
* &lt;name-component&gt; ::= &lt;attributeType&gt; &lt;spaces&gt; '='
* &lt;spaces&gt; &lt;attributeValue&gt; &lt;nameComponents&gt;
* </p>
*
* @param dn The string to parse
* @return <code>true</code> if the RDN is valid
*/
public static boolean isValid( String dn )
{
RDN rdn = new RDN();
try
{
parse( dn, rdn );
return true;
}
catch ( LdapInvalidDnException e )
{
return false;
}
}
}