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.syntaxCheckers;
021    
022    
023    import org.apache.directory.shared.ldap.constants.SchemaConstants;
024    import org.apache.directory.shared.ldap.schema.SyntaxChecker;
025    import org.apache.directory.shared.ldap.util.StringTools;
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    
030    /**
031     * A SyntaxChecker which verifies that a value is a Boolean according to RFC 4517.
032     * 
033     * From RFC 4512 & RFC 4517 :
034     * 
035     * BitString    = SQUOTE *binary-digit SQUOTE "B"
036     * binary-digit = "0" / "1"
037     * SQUOTE  = %x27                           ; hyphen ("'")
038     * 
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev$
042     */
043    public class BitStringSyntaxChecker extends SyntaxChecker
044    {
045        /** A logger for this class */
046        private static final Logger LOG = LoggerFactory.getLogger( BitStringSyntaxChecker.class );
047    
048        /** The serialVersionUID */
049        private static final long serialVersionUID = 1L;
050    
051        /**
052         * 
053         * Creates a new instance of BitStringSyntaxChecker.
054         *
055         */
056        public BitStringSyntaxChecker()
057        {
058            super( SchemaConstants.BIT_STRING_SYNTAX );
059        }
060        
061    
062        /**
063         * A shared and static method used to check that the string is a BitString.
064         * A BitString is a string of bits, between quotes and followed by a 'B' :
065         * 
066         * '01010110'B for instance
067         * 
068         * @param strValue The string to check
069         * @return <code>true</code> if the string is a BitString
070         */
071        public static boolean isValid( String strValue )
072        {
073            if ( strValue.length() == 0 )
074            {
075                LOG.debug( "Syntax invalid for '{}'", strValue );
076                return false;
077            }
078            
079            int pos = 0;
080            
081            // Check that the String respect the syntax : ' ([01]+) ' B
082            if ( ! StringTools.isCharASCII( strValue, pos++, '\'' ) )
083            {
084                LOG.debug( "Syntax invalid for '{}'", strValue );
085                return false;
086            }
087    
088            // We must have at least one bit
089            if ( ! StringTools.isBit( strValue, pos++ ) )
090            {
091                LOG.debug( "Syntax invalid for '{}'", strValue );
092                return false;
093            }
094            
095            while ( StringTools.isBit( strValue, pos ) )
096            {
097                // Loop until we get a char which is not a 0 or a 1
098                pos++;
099            }
100    
101            // Now, we must have a simple quote 
102            if ( ! StringTools.isCharASCII( strValue, pos++, '\'' ) )
103            {
104                LOG.debug( "Syntax invalid for '{}'", strValue );
105                return false;
106            }
107    
108            // followed by a 'B'
109            if ( ! StringTools.isCharASCII( strValue, pos, 'B' ) )
110            {
111                LOG.debug( "Syntax invalid for '{}'", strValue );
112                return false;
113            }
114    
115            LOG.debug( "Syntax valid for '{}'", strValue );
116            return true;
117        }
118    
119        /**
120         * {@inheritDoc}
121         */
122        public boolean isValidSyntax( Object value )
123        {
124            String strValue = null;
125    
126            if ( value == null )
127            {
128                LOG.debug( "Syntax invalid for '{}'", strValue );
129                return false;
130            }
131            
132            if ( value instanceof String )
133            {
134                strValue = ( String ) value;
135            }
136            else if ( value instanceof byte[] )
137            {
138                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
139            }
140            else
141            {
142                strValue = value.toString();
143            }
144    
145            return isValid( strValue );
146        }
147    }