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.shared.ldap.model.schema.syntaxCheckers; 021 022 023import org.apache.directory.shared.ldap.model.constants.SchemaConstants; 024import org.apache.directory.shared.ldap.model.schema.SyntaxChecker; 025import org.apache.directory.shared.util.Chars; 026import org.apache.directory.shared.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 * Creates a new instance of BitStringSyntaxChecker. 052 * 053 */ 054 public BitStringSyntaxChecker() 055 { 056 super( SchemaConstants.BIT_STRING_SYNTAX ); 057 } 058 059 060 /** 061 * A shared and static method used to check that the string is a BitString. 062 * A BitString is a string of bits, between quotes and followed by a 'B' : 063 * 064 * '01010110'B for instance 065 * 066 * @param strValue The string to check 067 * @return <code>true</code> if the string is a BitString 068 */ 069 public static boolean isValid( String strValue ) 070 { 071 if ( strValue.length() == 0 ) 072 { 073 LOG.debug( "Syntax invalid for '{}'", strValue ); 074 return false; 075 } 076 077 int pos = 0; 078 079 // Check that the String respect the syntax : ' ([01]+) ' B 080 if ( ! Strings.isCharASCII( strValue, pos++, '\'' ) ) 081 { 082 LOG.debug( "Syntax invalid for '{}'", strValue ); 083 return false; 084 } 085 086 // We must have at least one bit 087 if ( ! Chars.isBit(strValue, pos++) ) 088 { 089 LOG.debug( "Syntax invalid for '{}'", strValue ); 090 return false; 091 } 092 093 while ( Chars.isBit(strValue, pos) ) 094 { 095 // Loop until we get a char which is not a 0 or a 1 096 pos++; 097 } 098 099 // Now, we must have a simple quote 100 if ( ! Strings.isCharASCII( strValue, pos++, '\'' ) ) 101 { 102 LOG.debug( "Syntax invalid for '{}'", strValue ); 103 return false; 104 } 105 106 // followed by a 'B' 107 if ( ! Strings.isCharASCII( strValue, pos, 'B' ) ) 108 { 109 LOG.debug( "Syntax invalid for '{}'", strValue ); 110 return false; 111 } 112 113 LOG.debug( "Syntax valid for '{}'", strValue ); 114 return true; 115 } 116 117 /** 118 * {@inheritDoc} 119 */ 120 public boolean isValidSyntax( Object value ) 121 { 122 String strValue = null; 123 124 if ( value == null ) 125 { 126 LOG.debug( "Syntax invalid for 'null'" ); 127 return false; 128 } 129 130 if ( value instanceof String ) 131 { 132 strValue = ( String ) value; 133 } 134 else if ( value instanceof byte[] ) 135 { 136 strValue = Strings.utf8ToString((byte[]) value); 137 } 138 else 139 { 140 strValue = value.toString(); 141 } 142 143 return isValid( strValue ); 144 } 145}