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 */
020 package org.apache.directory.shared.ldap.schema.syntaxCheckers;
021
022 import org.apache.directory.shared.ldap.constants.SchemaConstants;
023 import org.apache.directory.shared.ldap.schema.SyntaxChecker;
024 import org.apache.directory.shared.ldap.util.StringTools;
025 import org.slf4j.Logger;
026 import org.slf4j.LoggerFactory;
027
028
029 /**
030 * An UUID syntax checker.
031 *
032 * UUID ::= OCTET STRING (SIZE(16)) -- constrained to an UUID [RFC4122]
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 * @version $Rev: 736240 $
036 */
037 public class UuidSyntaxChecker extends SyntaxChecker
038 {
039 /** A logger for this class */
040 private static final Logger LOG = LoggerFactory.getLogger( UuidSyntaxChecker.class );
041
042 /** The serialVersionUID */
043 private static final long serialVersionUID = 1L;
044
045 // Tells if the byte is alphanumeric
046 private static boolean isHex( byte b )
047 {
048 return ( b > 0 ) && StringTools.ALPHA_DIGIT[b];
049 }
050
051 /**
052 * Creates a new instance of UUIDSyntaxChecker.
053 */
054 public UuidSyntaxChecker()
055 {
056 super( SchemaConstants.UUID_SYNTAX );
057 }
058
059
060 /**
061 * {@inheritDoc}
062 */
063 public boolean isValidSyntax( Object value )
064 {
065 if ( value == null )
066 {
067 LOG.debug( "Syntax invalid for '{}'", value );
068 return false;
069 }
070
071 if ( ! ( value instanceof String ) )
072 {
073 LOG.debug( "Syntax invalid for '{}'", value );
074 return false;
075 }
076
077 byte[] b = ((String)value).getBytes();
078
079 if ( b.length < 36)
080 {
081 return false;
082 }
083
084 if (
085 isHex( b[0] ) && isHex( b[1] ) && isHex( b[2] ) && isHex( b[3] ) &
086 isHex( b[4] ) && isHex( b[5] ) && isHex( b[6] ) && isHex( b[7] ) &
087 b[8] == '-' &
088 isHex( b[9] ) && isHex( b[10] ) && isHex( b[11] ) && isHex( b[12] ) &
089 b[13] == '-' &
090 isHex( b[14] ) && isHex( b[15] ) && isHex( b[16] ) && isHex( b[17] ) &
091 b[18] == '-' &
092 isHex( b[19] ) && isHex( b[20] ) && isHex( b[21] ) && isHex( b[22] ) &
093 b[23] == '-' &
094 isHex( b[24] ) && isHex( b[25] ) && isHex( b[26] ) && isHex( b[27] ) &
095 isHex( b[28] ) && isHex( b[29] ) && isHex( b[30] ) && isHex( b[31] ) &
096 isHex( b[32] ) && isHex( b[33] ) && isHex( b[34] ) && isHex( b[35] ) )
097 {
098 // There is not that much more we can check.
099 LOG.debug( "Syntax valid for '{}'", value );
100 return true;
101 }
102
103 LOG.debug( "Syntax invalid for '{}'", value );
104 return false;
105 }
106 }