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.asn1.primitives.OID;
024 import org.apache.directory.shared.ldap.constants.SchemaConstants;
025 import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
026 import org.apache.directory.shared.ldap.util.StringTools;
027
028
029 /**
030 * A SyntaxChecker which verifies that a value is an oid according to RFC 4512.
031 *
032 * From RFC 4512 :
033 *
034 * oid = descr | numericoid
035 * descr = keystring
036 * keystring = leadkeychar *keychar
037 * leadkeychar = ALPHA
038 * keychar = ALPHA | DIGIT | HYPHEN
039 * number = DIGIT | ( LDIGIT 1*DIGIT )
040 * ALPHA = %x41-5A | %x61-7A ; "A"-"Z" | "a"-"z"
041 * DIGIT = %x30 | LDIGIT ; "0"-"9"
042 * LDIGIT = %x31-39 ; "1"-"9"
043 * HYPHEN = %x2D ; hyphen ("-")
044 * numericoid = number 1*( DOT number )
045 * DOT = %x2E ; period (".")
046 *
047 *
048 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
049 * @version $Rev$
050 */
051 public class OidSyntaxChecker extends AbstractSyntaxChecker
052 {
053 /**
054 *
055 * Creates a new instance of OidSyntaxChecker.
056 *
057 */
058 public OidSyntaxChecker()
059 {
060 super( SchemaConstants.OID_SYNTAX );
061 }
062
063
064 /**
065 *
066 * Creates a new instance of OidSyntaxChecker.
067 *
068 * @param oid the oid to associate with this new SyntaxChecker
069 *
070 */
071 protected OidSyntaxChecker( String oid )
072 {
073 super( oid );
074 }
075
076 /* (non-Javadoc)
077 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
078 */
079 public boolean isValidSyntax( Object value )
080 {
081 String strValue = null;
082
083 if ( value == null )
084 {
085 return false;
086 }
087
088 if ( value instanceof String )
089 {
090 strValue = ( String ) value;
091 }
092 else if ( value instanceof byte[] )
093 {
094 strValue = StringTools.utf8ToString( ( byte[] ) value );
095 }
096 else
097 {
098 strValue = value.toString();
099 }
100
101 if ( strValue.length() == 0 )
102 {
103 return false;
104 }
105
106 // if the first character is a digit it's an attempt at an OID and must be
107 // checked to make sure there are no other chars except '.' and digits.
108 if ( StringTools.isDigit( strValue.charAt( 0 ) ) )
109 {
110 if ( ! OID.isOID( strValue ) )
111 {
112 return false;
113 }
114 else
115 {
116 return true;
117 }
118 }
119
120 // here we just need to make sure that we have the right characters in the
121 // string and that it starts with a letter.
122 if ( StringTools.isAlphaASCII( strValue, 0 ) )
123 {
124 for ( int index = 0; index < strValue.length(); index++ )
125 {
126 if ( ! StringTools.isAlphaDigitMinus( strValue, index ))
127 {
128 return false;
129 }
130 }
131
132 return true;
133 }
134 else
135 {
136 return false;
137 }
138 }
139 }