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.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 oid and a length
030 * constraint according to RFC 4512.
031 *
032 * From RFC 4512 :
033 *
034 * noidlen = numericoid [ LCURLY len RCURLY ]
035 * numericoid = number 1*( DOT number )
036 * len = number
037 * number = DIGIT | ( LDIGIT 1*DIGIT )
038 * DIGIT = %x30 | LDIGIT ; "0"-"9"
039 * LDIGIT = %x31-39 ; "1"-"9"
040 * DOT = %x2E ; period (".")
041 * LCURLY = %x7B ; left curly brace "{"
042 * RCURLY = %x7D ; right curly brace "}"
043 *
044 *
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 * @version $Rev$
047 */
048 public class OidLenSyntaxChecker extends AbstractSyntaxChecker
049 {
050 /** The Syntax OID */
051 private static final String SC_OID = "1.3.6.1.4.1.18060.0.4.0.0.5";
052
053 /**
054 *
055 * Creates a new instance of OidLenSyntaxChecker.
056 *
057 */
058 public OidLenSyntaxChecker()
059 {
060 super( SC_OID );
061 }
062
063 /**
064 *
065 * Creates a new instance of OidLenSyntaxChecker.
066 *
067 * @param oid the oid to associate with this new SyntaxChecker
068 *
069 */
070 protected OidLenSyntaxChecker( String oid )
071 {
072 super( oid );
073 }
074
075 /* (non-Javadoc)
076 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
077 */
078 public boolean isValidSyntax( Object value )
079 {
080 String strValue = null;
081
082 if ( value == null )
083 {
084 return false;
085 }
086
087 if ( value instanceof String )
088 {
089 strValue = ( String ) value;
090 }
091 else if ( value instanceof byte[] )
092 {
093 strValue = StringTools.utf8ToString( ( byte[] ) value );
094 }
095 else
096 {
097 strValue = value.toString();
098 }
099
100 if ( strValue.length() == 0 )
101 {
102 return false;
103 }
104
105 // We are looking at the first position of the len part
106 int pos = strValue.indexOf( '{' );
107
108 if ( pos < 0 )
109 {
110 // Not found ... but it may still be a valid OID
111 return OID.isOID( strValue );
112 }
113 else
114 {
115 // we should have a len value. First check that the OID is valid
116 String oid = strValue.substring( 0, pos );
117
118 if ( !OID.isOID( oid ) )
119 {
120 return false;
121 }
122
123 String len = strValue.substring( pos );
124
125 // We must have a lnumber and a '}' at the end
126 if ( len.charAt( len.length() -1 ) != '}' )
127 {
128 // No final '}'
129 return false;
130 }
131
132 for ( int i = 1; i < len.length() - 1; i++ )
133 {
134 switch ( len.charAt(i) )
135 {
136 case '0': case '1': case '2' : case '3': case '4':
137 case '5': case '6': case '7' : case '8': case '9':
138 break;
139
140 default:
141 return false;
142 }
143 }
144
145 if ( ( len.charAt( 1 ) == '0' ) && len.length() > 3 )
146 {
147 // A number can't start with a '0' unless it's the only
148 // number
149 return false;
150 }
151
152 return true;
153 }
154 }
155 }