View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
21  
22  
23  import org.apache.directory.api.asn1.util.Oid;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
26  import org.apache.directory.api.util.Chars;
27  import org.apache.directory.api.util.Strings;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  
32  /**
33   * A SyntaxChecker which verifies that a value is an oid according to RFC 4512.
34   * 
35   * From RFC 4512 :
36   * 
37   * oid = descr | numericoid
38   * descr = keystring
39   * keystring = leadkeychar *keychar
40   * leadkeychar = ALPHA
41   * keychar = ALPHA | DIGIT | HYPHEN
42   * number  = DIGIT | ( LDIGIT 1*DIGIT )
43   * ALPHA   = %x41-5A | %x61-7A              ; "A"-"Z" | "a"-"z"
44   * DIGIT   = %x30 | LDIGIT                  ; "0"-"9"
45   * LDIGIT  = %x31-39                        ; "1"-"9"
46   * HYPHEN  = %x2D                           ; hyphen ("-")
47   * numericoid = number 1*( DOT number )
48   * DOT     = %x2E                           ; period (".")
49   * 
50   *
51   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52   */
53  @SuppressWarnings("serial")
54  public class OidSyntaxChecker extends SyntaxChecker
55  {
56      /** A logger for this class */
57      private static final Logger LOG = LoggerFactory.getLogger( OidSyntaxChecker.class );
58  
59  
60      /**
61       * Creates a new instance of OidSyntaxChecker.
62       */
63      public OidSyntaxChecker()
64      {
65          super( SchemaConstants.OID_SYNTAX );
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public boolean isValidSyntax( Object value )
73      {
74          String strValue = null;
75  
76          if ( value == null )
77          {
78              LOG.debug( "Syntax invalid for 'null'" );
79              return false;
80          }
81  
82          if ( value instanceof String )
83          {
84              strValue = ( String ) value;
85          }
86          else if ( value instanceof byte[] )
87          {
88              strValue = Strings.utf8ToString( ( byte[] ) value );
89          }
90          else
91          {
92              strValue = value.toString();
93          }
94  
95          if ( strValue.length() == 0 )
96          {
97              LOG.debug( "Syntax invalid for '{}'", value );
98              return false;
99          }
100 
101         // if the first character is a digit it's an attempt at an OID and must be
102         // checked to make sure there are no other chars except '.' and digits.
103         if ( Chars.isDigit( strValue.charAt( 0 ) ) )
104         {
105             if ( !Oid.isOid( strValue ) )
106             {
107                 LOG.debug( "Syntax invalid for '{}'", value );
108                 return false;
109             }
110             else
111             {
112                 LOG.debug( "Syntax valid for '{}'", value );
113                 return true;
114             }
115         }
116 
117         // here we just need to make sure that we have the right characters in the 
118         // string and that it starts with a letter.
119         if ( Chars.isAlphaASCII( strValue, 0 ) )
120         {
121             for ( int index = 0; index < strValue.length(); index++ )
122             {
123                 char c = strValue.charAt( index );
124                 
125                 if ( !Chars.isAlphaDigitMinus( c ) )
126                 {
127                     LOG.debug( "Syntax invalid for '{}'", value );
128                     return false;
129                 }
130             }
131 
132             LOG.debug( "Syntax valid for '{}'", value );
133             return true;
134         }
135         else
136         {
137             LOG.debug( "Syntax invalid for '{}'", value );
138             return false;
139         }
140     }
141 }