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.i18n.I18n;
25  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
26  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
27  import org.apache.directory.api.util.Strings;
28  
29  
30  /**
31   * A SyntaxChecker which verifies that a value is a numeric oid 
32   * according to RFC 4512.
33   * <p>
34   * From RFC 4512 :
35   * <pre>
36   * numericoid = number 1*( DOT number )
37   * number  = DIGIT | ( LDIGIT 1*DIGIT )
38   * DIGIT   = %x30 | LDIGIT                  ; "0"-"9"
39   * LDIGIT  = %x31-39                        ; "1"-"9"
40   * DOT     = %x2E                           ; period (".")
41   * </pre>
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  @SuppressWarnings("serial")
46  public final class NumericOidSyntaxChecker extends SyntaxChecker
47  {
48      /**
49       * A static instance of NumericOidSyntaxChecker
50       */
51      public static final NumericOidSyntaxChecker INSTANCE = 
52          new NumericOidSyntaxChecker( SchemaConstants.NUMERIC_OID_SYNTAX );
53      
54      /**
55       * A static Builder for this class
56       */
57      public static final class Builder extends SCBuilder<NumericOidSyntaxChecker>
58      {
59          /**
60           * The Builder constructor
61           */
62          private Builder()
63          {
64              super( SchemaConstants.NUMERIC_OID_SYNTAX );
65          }
66          
67          
68          /**
69           * Create a new instance of NumericOidSyntaxChecker
70           * @return A new instance of NumericOidSyntaxChecker
71           */
72          @Override
73          public NumericOidSyntaxChecker build()
74          {
75              return new NumericOidSyntaxChecker( oid );
76          }
77      }
78  
79      
80      /**
81       * Creates a new instance of NumericOidSyntaxChecker.
82       * 
83       * @param oid The OID to use for this SyntaxChecker
84       */
85      private NumericOidSyntaxChecker( String oid )
86      {
87          super( oid );
88      }
89  
90      
91      /**
92       * @return An instance of the Builder for this class
93       */
94      public static Builder builder()
95      {
96          return new Builder();
97      }
98  
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public boolean isValidSyntax( Object value )
105     {
106         String strValue;
107 
108         if ( value == null )
109         {
110             if ( LOG.isDebugEnabled() )
111             {
112                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
113             }
114             
115             return false;
116         }
117 
118         if ( value instanceof String )
119         {
120             strValue = ( String ) value;
121         }
122         else if ( value instanceof byte[] )
123         {
124             strValue = Strings.utf8ToString( ( byte[] ) value );
125         }
126         else
127         {
128             strValue = value.toString();
129         }
130 
131         if ( strValue.length() == 0 )
132         {
133             if ( LOG.isDebugEnabled() )
134             {
135                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
136             }
137             
138             return false;
139         }
140 
141         // Just check that the value is a valid OID
142         boolean result = Oid.isOid( strValue );
143 
144         if ( LOG.isDebugEnabled() )
145         {
146             if ( result )
147             {
148                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
149             }
150             else
151             {
152                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
153             }
154         }
155 
156         return result;
157     }
158 }