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.schema.AbstractSyntaxChecker;
025    import org.apache.directory.shared.ldap.util.StringTools;
026    
027    
028    /**
029     * A SyntaxChecker which verifies that a value is a Boolean according to RFC 4517.
030     * 
031     * From RFC 4512 & RFC 4517 :
032     * 
033     * BitString    = SQUOTE *binary-digit SQUOTE "B"
034     * binary-digit = "0" / "1"
035     * SQUOTE  = %x27                           ; hyphen ("'")
036     * 
037     *
038     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039     * @version $Rev$
040     */
041    public class BitStringSyntaxChecker extends AbstractSyntaxChecker
042    {
043        /**
044         * 
045         * Creates a new instance of BitStringSyntaxChecker.
046         *
047         */
048        public BitStringSyntaxChecker()
049        {
050            super( SchemaConstants.BIT_STRING_SYNTAX );
051        }
052        
053        /**
054         * 
055         * Creates a new instance of BitStringSyntaxChecker.
056         * 
057         * @param oid the oid to associate with this new SyntaxChecker
058         *
059         */
060        protected BitStringSyntaxChecker( String oid )
061        {
062            super( oid );
063        }
064        
065        /**
066         * A shared and static method used to check that the string is a BitString.
067         * A BitString is a string of bits, between quots and followed by a 'B' :
068         * 
069         * '01010110'B for instance
070         * 
071         * @param strValue The string to check
072         * @return <code>true</code> if teh string is a BitString
073         */
074        public static boolean isValid( String strValue )
075        {
076            if ( strValue.length() == 0 )
077            {
078                return false;
079            }
080            
081            int pos = 0;
082            
083            // Check that the String respect the syntax : ' ([01]+) ' B
084            if ( ! StringTools.isCharASCII( strValue, pos++, '\'' ) )
085            {
086                return false;
087            }
088    
089            // We must have at least one bit
090            if ( ! StringTools.isBit( strValue, pos++ ) )
091            {
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                return false;
105            }
106    
107            // followed by a 'B'
108            if ( ! StringTools.isCharASCII( strValue, pos, 'B' ) )
109            {
110                return false;
111            }
112    
113            return true;
114        }
115    
116        /* (non-Javadoc)
117         * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
118         */
119        public boolean isValidSyntax( Object value )
120        {
121            String strValue = null;
122    
123            if ( value == null )
124            {
125                return false;
126            }
127            
128            if ( value instanceof String )
129            {
130                strValue = ( String ) value;
131            }
132            else if ( value instanceof byte[] )
133            {
134                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
135            }
136            else
137            {
138                strValue = value.toString();
139            }
140    
141            return isValid( strValue );
142        }
143    }