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, eCopyOfUuidSyntaxCheckerither 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.ldap.model.constants.SchemaConstants;
24  import org.apache.directory.api.ldap.model.csn.Csn;
25  import org.apache.directory.api.ldap.model.csn.InvalidCSNException;
26  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * An CSN syntax checker.
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  @SuppressWarnings("serial")
37  public class CsnSyntaxChecker extends SyntaxChecker
38  {
39      /** A logger for this class */
40      private static final Logger LOG = LoggerFactory.getLogger( CsnSyntaxChecker.class );
41  
42  
43      /**
44       * Creates a new instance of CsnSyntaxChecker.
45       */
46      public CsnSyntaxChecker()
47      {
48          super( SchemaConstants.CSN_SYNTAX );
49      }
50  
51  
52      /**
53       * {@inheritDoc}
54       */
55      public boolean isValidSyntax( Object value )
56      {
57          if ( value == null )
58          {
59              LOG.debug( "Syntax invalid for 'null'" );
60              return false;
61          }
62  
63          if ( !( value instanceof String ) )
64          {
65              LOG.debug( "Syntax invalid for '{}'", value );
66              return false;
67          }
68  
69          String csnStr = ( String ) value;
70  
71          // It must be a valid CSN : try to create a new one.
72          try
73          {
74              boolean result = Csn.isValid( csnStr );
75  
76              if ( result )
77              {
78                  LOG.debug( "Syntax valid for '{}'", value );
79              }
80              else
81              {
82                  LOG.debug( "Syntax invalid for '{}'", value );
83              }
84  
85              return result;
86          }
87          catch ( InvalidCSNException icsne )
88          {
89              LOG.debug( "Syntax invalid for '{}'", value );
90              return false;
91          }
92      }
93  }