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.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
26  import org.apache.directory.api.util.Chars;
27  import org.apache.directory.api.util.Strings;
28  
29  
30  /**
31   * A SyntaxChecker which verifies that a value is a Boolean according to RFC 4517.
32   * <br>
33   * From RFC 4512 &amp; RFC 4517 :
34   * <pre>
35   * BitString    = SQUOTE *binary-digit SQUOTE "B"
36   * binary-digit = "0" / "1"
37   * SQUOTE       = %x27                           ; hyphen ("'")
38   * </pre>
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  @SuppressWarnings("serial")
43  public final class BitStringSyntaxChecker extends SyntaxChecker
44  {
45      /**
46       * A static instance of BitStringSyntaxChecker
47       */
48      public static final BitStringSyntaxChecker INSTANCE = new BitStringSyntaxChecker( SchemaConstants.BIT_STRING_SYNTAX );
49  
50      /**
51       * A static Builder for this class
52       */
53      public static final class Builder extends SCBuilder<BitStringSyntaxChecker>
54      {
55          /**
56           * The Builder constructor
57           */
58          private Builder()
59          {
60              super( SchemaConstants.BIT_STRING_SYNTAX );
61          }
62          
63          
64          /**
65           * Create a new instance of BitStringSyntaxChecker
66           * @return A new instance of BitStringSyntaxChecker
67           */
68          @Override
69          public BitStringSyntaxChecker build()
70          {
71              return new BitStringSyntaxChecker( oid );
72          }
73      }
74  
75      
76      /**
77       * Creates a new instance of BitStringSyntaxChecker.
78       *
79       * @param oid The OID to use for this SyntaxChecker
80       */
81      private BitStringSyntaxChecker( String oid )
82      {
83          super( oid );
84      }
85  
86  
87      /**
88       * @return An instance of the Builder for this class
89       */
90      public static Builder builder()
91      {
92          return new Builder();
93      }
94  
95  
96      /**
97       * A shared and static method used to check that the string is a BitString.
98       * A BitString is a string of bits, between quotes and followed by a 'B' :
99       * 
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 }