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.Strings;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * A SyntaxChecker which verifies that a value is a numeric oid 
33   * according to RFC 4512.
34   * 
35   * From RFC 4512 :
36   * 
37   * numericoid = number 1*( DOT number )
38   * number  = DIGIT | ( LDIGIT 1*DIGIT )
39   * DIGIT   = %x30 | LDIGIT                  ; "0"-"9"
40   * LDIGIT  = %x31-39                        ; "1"-"9"
41   * DOT     = %x2E                           ; period (".")
42  
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  @SuppressWarnings("serial")
47  public class NumericOidSyntaxChecker extends SyntaxChecker
48  {
49      /** A logger for this class */
50      private static final Logger LOG = LoggerFactory.getLogger( NumericOidSyntaxChecker.class );
51  
52  
53      /**
54       * Creates a new instance of NumericOidSyntaxChecker.
55       */
56      public NumericOidSyntaxChecker()
57      {
58          super( SchemaConstants.NUMERIC_OID_SYNTAX );
59      }
60  
61  
62      /**
63       * {@inheritDoc}
64       */
65      public boolean isValidSyntax( Object value )
66      {
67          String strValue = null;
68  
69          if ( value == null )
70          {
71              LOG.debug( "Syntax invalid for 'null'" );
72              return false;
73          }
74  
75          if ( value instanceof String )
76          {
77              strValue = ( String ) value;
78          }
79          else if ( value instanceof byte[] )
80          {
81              strValue = Strings.utf8ToString( ( byte[] ) value );
82          }
83          else
84          {
85              strValue = value.toString();
86          }
87  
88          if ( strValue.length() == 0 )
89          {
90              LOG.debug( "Syntax invalid for '{}'", value );
91              return false;
92          }
93  
94          // Just check that the value is a valid OID
95          boolean result = ( Oid.isOid( strValue ) );
96  
97          if ( result )
98          {
99              LOG.debug( "Syntax valid for '{}'", value );
100         }
101         else
102         {
103             LOG.debug( "Syntax invalid for '{}'", value );
104         }
105 
106         return result;
107     }
108 }