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 */
020package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.constants.SchemaConstants;
025import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
026import org.apache.directory.api.util.Chars;
027import org.apache.directory.api.util.Strings;
028
029
030/**
031 * A SyntaxChecker which verifies that a value is a Boolean according to RFC 4517.
032 * <br>
033 * From RFC 4512 &amp; RFC 4517 :
034 * <pre>
035 * BitString    = SQUOTE *binary-digit SQUOTE "B"
036 * binary-digit = "0" / "1"
037 * SQUOTE       = %x27                           ; hyphen ("'")
038 * </pre>
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042@SuppressWarnings("serial")
043public final class BitStringSyntaxChecker extends SyntaxChecker
044{
045    /**
046     * A static instance of BitStringSyntaxChecker
047     */
048    public static final BitStringSyntaxChecker INSTANCE = new BitStringSyntaxChecker( SchemaConstants.BIT_STRING_SYNTAX );
049
050    /**
051     * A static Builder for this class
052     */
053    public static final class Builder extends SCBuilder<BitStringSyntaxChecker>
054    {
055        /**
056         * The Builder constructor
057         */
058        private Builder()
059        {
060            super( SchemaConstants.BIT_STRING_SYNTAX );
061        }
062        
063        
064        /**
065         * Create a new instance of BitStringSyntaxChecker
066         * @return A new instance of BitStringSyntaxChecker
067         */
068        @Override
069        public BitStringSyntaxChecker build()
070        {
071            return new BitStringSyntaxChecker( oid );
072        }
073    }
074
075    
076    /**
077     * Creates a new instance of BitStringSyntaxChecker.
078     *
079     * @param oid The OID to use for this SyntaxChecker
080     */
081    private BitStringSyntaxChecker( String oid )
082    {
083        super( oid );
084    }
085
086
087    /**
088     * @return An instance of the Builder for this class
089     */
090    public static Builder builder()
091    {
092        return new Builder();
093    }
094
095
096    /**
097     * A shared and static method used to check that the string is a BitString.
098     * A BitString is a string of bits, between quotes and followed by a 'B' :
099     * 
100     * '01010110'B for instance
101     * 
102     * @param strValue The string to check
103     * @return <code>true</code> if the string is a BitString
104     */
105    public static boolean isValid( String strValue )
106    {
107        if ( strValue.length() == 0 )
108        {
109            if ( LOG.isDebugEnabled() )
110            {
111                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) );
112            }
113            
114            return false;
115        }
116
117        int pos = 0;
118
119        // Check that the String respect the syntax : ' ([01]+) ' B
120        if ( !Strings.isCharASCII( strValue, pos++, '\'' ) )
121        {
122            if ( LOG.isDebugEnabled() )
123            {
124                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) );
125            }
126            
127            return false;
128        }
129
130        // We must have at least one bit
131        if ( !Chars.isBit( strValue, pos++ ) )
132        {
133            if ( LOG.isDebugEnabled() )
134            {
135                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) );
136            }
137            
138            return false;
139        }
140
141        while ( Chars.isBit( strValue, pos ) )
142        {
143            // Loop until we get a char which is not a 0 or a 1
144            pos++;
145        }
146
147        // Now, we must have a simple quote 
148        if ( !Strings.isCharASCII( strValue, pos++, '\'' ) )
149        {
150            if ( LOG.isDebugEnabled() )
151            {
152                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) );
153            }
154            
155            return false;
156        }
157
158        // followed by a 'B'
159        if ( !Strings.isCharASCII( strValue, pos, 'B' ) )
160        {
161            if ( LOG.isDebugEnabled() )
162            {
163                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) );
164            }
165            
166            return false;
167        }
168
169        if ( LOG.isDebugEnabled() )
170        {
171            LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, strValue ) );
172        }
173        
174        return true;
175    }
176
177
178    /**
179     * {@inheritDoc}
180     */
181    @Override
182    public boolean isValidSyntax( Object value )
183    {
184        String strValue;
185
186        if ( value == null )
187        {
188            if ( LOG.isDebugEnabled() )
189            {
190                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
191            }
192            
193            return false;
194        }
195
196        if ( value instanceof String )
197        {
198            strValue = ( String ) value;
199        }
200        else if ( value instanceof byte[] )
201        {
202            strValue = Strings.utf8ToString( ( byte[] ) value );
203        }
204        else
205        {
206            strValue = value.toString();
207        }
208
209        return isValid( strValue );
210    }
211}