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.schema.syntaxes;
021    
022    
023    import org.apache.directory.shared.ldap.constants.SchemaConstants;
024    import org.apache.directory.shared.ldap.name.LdapDN;
025    import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
026    import org.apache.directory.shared.ldap.util.StringTools;
027    
028    
029    /**
030     * A SyntaxChecker which verifies that a value is a valid Name and Optional UID.
031     * 
032     * This element is a composition of two parts : a DN and an optional UID :
033     * NameAndOptionalUID = distinguishedName [ SHARP BitString ]
034     * 
035     * Both part already have their syntax checkers, so we will just call them
036     * after having splitted the element in two ( if necessary)
037     * 
038     * We just check that the DN is valid, we don't need to verify each of the RDN 
039     * syntax.
040     * 
041     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042     * @version $Rev$
043     */
044    public class NameAndOptionalUIDSyntaxChecker extends AbstractSyntaxChecker
045    {
046        /**
047         * 
048         * Creates a new instance of NameAndOptionalUIDSyntaxChecker.
049         *
050         */
051        public NameAndOptionalUIDSyntaxChecker()
052        {
053            super( SchemaConstants.NAME_AND_OPTIONAL_UID_SYNTAX );
054        }
055        
056        
057        /**
058         * 
059         * Creates a new instance of NameAndOptionalUIDSyntaxChecker.
060         * 
061         * @param oid the oid to associate with this new SyntaxChecker
062         *
063         */
064        protected NameAndOptionalUIDSyntaxChecker( String oid )
065        {
066            super( oid );
067        }
068        
069        /* (non-Javadoc)
070         * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
071         */
072        public boolean isValidSyntax( Object value )
073        {
074            String strValue = null;
075    
076            if ( value == null )
077            {
078                return false;
079            }
080            
081            if ( value instanceof String )
082            {
083                strValue = ( String ) value;
084            }
085            else if ( value instanceof byte[] )
086            {
087                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
088            }
089            else
090            {
091                strValue = value.toString();
092            }
093    
094            if ( strValue.length() == 0 )
095            {
096                return false;
097            }
098            
099            // Let's see if we have an UID part
100            int sharpPos = strValue.lastIndexOf( '#' );
101            
102            if ( sharpPos != -1 )
103            {
104                // Now, check that we don't have another '#'
105                if ( strValue.indexOf( '#' ) != sharpPos )
106                {
107                    // Yes, we have one : this is not allowed, it should have been
108                    // escaped.
109                    return false;
110                }
111                
112                // This is an UID if the '#' is immediatly
113                // followed by a BitString, except if the '#' is
114                // on the last position
115                // We shoould not find a
116                if ( BitStringSyntaxChecker.isValid( strValue.substring( sharpPos + 1 ) ) && 
117                     ( sharpPos < strValue.length() ) )
118                {
119                    // Ok, we have a BitString, now check the DN,
120                    // except if the '#' is in first position
121                    if ( sharpPos > 0 )
122                    {
123                        return LdapDN.isValid( strValue.substring( 0, sharpPos ) );
124                    }
125                    else
126                    {
127                        // The DN must not be null ?
128                        return false;
129                    }
130                }
131                else
132                {
133                    // We have found a '#' but no UID part.
134                    return false;
135                }
136            }
137            else
138            {
139                // No UID, the strValue is a DN
140                // Check that the value is a valid DN
141                return LdapDN.isValid( strValue );
142            }
143        }
144    }