View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
21  
22  
23  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
24  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
25  import org.apache.directory.api.util.Chars;
26  import org.apache.directory.api.util.Strings;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * A SyntaxChecker which verifies that a value is a Boolean according to RFC 4517.
33   * 
34   * From RFC 4512 & RFC 4517 :
35   * 
36   * BitString    = SQUOTE *binary-digit SQUOTE "B"
37   * binary-digit = "0" / "1"
38   * SQUOTE  = %x27                           ; hyphen ("'")
39   * 
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  @SuppressWarnings("serial")
44  public class BitStringSyntaxChecker extends SyntaxChecker
45  {
46      /** A logger for this class */
47      private static final Logger LOG = LoggerFactory.getLogger( BitStringSyntaxChecker.class );
48  
49  
50      /**
51       * 
52       * Creates a new instance of BitStringSyntaxChecker.
53       *
54       */
55      public BitStringSyntaxChecker()
56      {
57          super( SchemaConstants.BIT_STRING_SYNTAX );
58      }
59  
60  
61      /**
62       * A shared and static method used to check that the string is a BitString.
63       * A BitString is a string of bits, between quotes and followed by a 'B' :
64       * 
65       * '01010110'B for instance
66       * 
67       * @param strValue The string to check
68       * @return <code>true</code> if the string is a BitString
69       */
70      public static boolean isValid( String strValue )
71      {
72          if ( strValue.length() == 0 )
73          {
74              LOG.debug( "Syntax invalid for '{}'", strValue );
75              return false;
76          }
77  
78          int pos = 0;
79  
80          // Check that the String respect the syntax : ' ([01]+) ' B
81          if ( !Strings.isCharASCII( strValue, pos++, '\'' ) )
82          {
83              LOG.debug( "Syntax invalid for '{}'", strValue );
84              return false;
85          }
86  
87          // We must have at least one bit
88          if ( !Chars.isBit( strValue, pos++ ) )
89          {
90              LOG.debug( "Syntax invalid for '{}'", strValue );
91              return false;
92          }
93  
94          while ( Chars.isBit( strValue, pos ) )
95          {
96              // Loop until we get a char which is not a 0 or a 1
97              pos++;
98          }
99  
100         // Now, we must have a simple quote 
101         if ( !Strings.isCharASCII( strValue, pos++, '\'' ) )
102         {
103             LOG.debug( "Syntax invalid for '{}'", strValue );
104             return false;
105         }
106 
107         // followed by a 'B'
108         if ( !Strings.isCharASCII( strValue, pos, 'B' ) )
109         {
110             LOG.debug( "Syntax invalid for '{}'", strValue );
111             return false;
112         }
113 
114         LOG.debug( "Syntax valid for '{}'", strValue );
115         return true;
116     }
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 'null'" );
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 = Strings.utf8ToString( ( byte[] ) value );
139         }
140         else
141         {
142             strValue = value.toString();
143         }
144 
145         return isValid( strValue );
146     }
147 }