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.syntaxes;
021
022
023 import org.apache.directory.shared.ldap.constants.SchemaConstants;
024 import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
025 import org.apache.directory.shared.ldap.util.StringTools;
026
027
028 /**
029 * A SyntaxChecker which verifies that a value is a Numeric String according to RFC 4517.
030 *
031 * From RFC 4517 :
032 *
033 * NumericString = 1*(DIGIT / SPACE)
034 *
035 * From RFC 4512 :
036 * DIGIT = %x30 | LDIGIT ; "0"-"9"
037 * LDIGIT = %x31-39 ; "1"-"9"
038 * SPACE = %x20 ; space (" ")
039 *
040 *
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 * @version $Rev$
043 */
044 public class NumericStringSyntaxChecker extends AbstractSyntaxChecker
045 {
046 /**
047 *
048 * Creates a new instance of NumericStringSyntaxChecker.
049 *
050 */
051 public NumericStringSyntaxChecker()
052 {
053 super( SchemaConstants.NUMERIC_STRING_SYNTAX );
054 }
055
056
057 /**
058 *
059 * Creates a new instance of NumericStringSyntaxChecker.
060 *
061 * @param oid the oid to associate with this new SyntaxChecker
062 *
063 */
064 protected NumericStringSyntaxChecker( String oid )
065 {
066 super( oid );
067 }
068
069 /* (non-Javadoc)
070 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
071 */
072 public boolean isValidSyntax( Object value )
073 {
074 String strValue = null;
075
076 if ( value == null )
077 {
078 return false;
079 }
080
081 if ( value instanceof String )
082 {
083 strValue = ( String ) value;
084 }
085 else if ( value instanceof byte[] )
086 {
087 strValue = StringTools.utf8ToString( ( byte[] ) value );
088 }
089 else
090 {
091 strValue = value.toString();
092 }
093
094 // We should have at least one char
095 if ( strValue.length() == 0 )
096 {
097 return false;
098 }
099
100 // Check that each char is either a digit or a space
101 for ( int i = 0; i < strValue.length(); i++ )
102 {
103 switch ( strValue.charAt( i ) )
104 {
105 case '0':
106 case '1' :
107 case '2' :
108 case '3' :
109 case '4' :
110 case '5' :
111 case '6' :
112 case '7' :
113 case '8' :
114 case '9' :
115 case ' ' :
116 continue;
117
118 default :
119 return false;
120 }
121 }
122
123 return true;
124 }
125 }