| /* |
| * 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> - <name-component> ::= |
| * <attributeType> <spaces> '=' <spaces> |
| * <attributeValue> <attributeTypeAndValues> <br> - |
| * <attributeTypeAndValues> ::= <spaces> '+' <spaces> |
| * <attributeType> <spaces> '=' <spaces> |
| * <attributeValue> <attributeTypeAndValues> | e <br> - |
| * <attributeType> ::= [a-zA-Z] <keychars> | <oidPrefix> [0-9] |
| * <digits> <oids> | [0-9] <digits> <oids> <br> - |
| * <keychars> ::= [a-zA-Z] <keychars> | [0-9] <keychars> | '-' |
| * <keychars> | e <br> - <oidPrefix> ::= 'OID.' | 'oid.' | e <br> - |
| * <oids> ::= '.' [0-9] <digits> <oids> | e <br> - |
| * <attributeValue> ::= <pairs-or-strings> | '#' <hexstring> |
| * |'"' <quotechar-or-pairs> '"' <br> - <pairs-or-strings> ::= '\' |
| * <pairchar> <pairs-or-strings> | <stringchar> |
| * <pairs-or-strings> | e <br> - <quotechar-or-pairs> ::= |
| * <quotechar> <quotechar-or-pairs> | '\' <pairchar> |
| * <quotechar-or-pairs> | e <br> - <pairchar> ::= ',' | '=' | '+' | |
| * '<' | '>' | '#' | ';' | '\' | '"' | [0-9a-fA-F] [0-9a-fA-F] <br> - |
| * <hexstring> ::= [0-9a-fA-F] [0-9a-fA-F] <hexpairs> <br> - |
| * <hexpairs> ::= [0-9a-fA-F] [0-9a-fA-F] <hexpairs> | e <br> - |
| * <digits> ::= [0-9] <digits> | e <br> - <stringchar> ::= |
| * [0x00-0xFF] - [,=+<>#;\"\n\r] <br> - <quotechar> ::= [0x00-0xFF] - |
| * [\"] <br> - <separator> ::= ',' | ';' <br> - <spaces> ::= ' ' |
| * <spaces> | 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> |
| * <name-component> ::= <attributeType> <spaces> '=' |
| * <spaces> <attributeValue> <nameComponents> |
| * </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> |
| * <name-component> ::= <attributeType> <spaces> '=' |
| * <spaces> <attributeValue> <nameComponents> |
| * </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; |
| } |
| } |
| |
| } |