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.ldap.model.constants.SchemaConstants; 024import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 025import org.apache.directory.api.util.Chars; 026import org.apache.directory.api.util.Strings; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030 031/** 032 * A SyntaxChecker which verifies that a value is a Boolean according to RFC 4517. 033 * 034 * From RFC 4512 & RFC 4517 : 035 * 036 * BitString = SQUOTE *binary-digit SQUOTE "B" 037 * binary-digit = "0" / "1" 038 * SQUOTE = %x27 ; hyphen ("'") 039 * 040 * 041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 042 */ 043@SuppressWarnings("serial") 044public class BitStringSyntaxChecker extends SyntaxChecker 045{ 046 /** A logger for this class */ 047 private static final Logger LOG = LoggerFactory.getLogger( BitStringSyntaxChecker.class ); 048 049 050 /** 051 * 052 * Creates a new instance of BitStringSyntaxChecker. 053 * 054 */ 055 public BitStringSyntaxChecker() 056 { 057 super( SchemaConstants.BIT_STRING_SYNTAX ); 058 } 059 060 061 /** 062 * A shared and static method used to check that the string is a BitString. 063 * A BitString is a string of bits, between quotes and followed by a 'B' : 064 * 065 * '01010110'B for instance 066 * 067 * @param strValue The string to check 068 * @return <code>true</code> if the string is a BitString 069 */ 070 public static boolean isValid( String strValue ) 071 { 072 if ( strValue.length() == 0 ) 073 { 074 LOG.debug( "Syntax invalid for '{}'", strValue ); 075 return false; 076 } 077 078 int pos = 0; 079 080 // Check that the String respect the syntax : ' ([01]+) ' B 081 if ( !Strings.isCharASCII( strValue, pos++, '\'' ) ) 082 { 083 LOG.debug( "Syntax invalid for '{}'", strValue ); 084 return false; 085 } 086 087 // We must have at least one bit 088 if ( !Chars.isBit( strValue, pos++ ) ) 089 { 090 LOG.debug( "Syntax invalid for '{}'", strValue ); 091 return false; 092 } 093 094 while ( Chars.isBit( strValue, pos ) ) 095 { 096 // Loop until we get a char which is not a 0 or a 1 097 pos++; 098 } 099 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}