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.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.csn.Csn;
26  import org.apache.directory.api.ldap.model.csn.InvalidCSNException;
27  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
28  
29  
30  /**
31   * An CSN syntax checker.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  @SuppressWarnings("serial")
36  public final class CsnSyntaxChecker extends SyntaxChecker
37  {
38      /**
39       * A static instance of CsnSyntaxChecker
40       */
41      public static final CsnSyntaxChecker INSTANCE = new CsnSyntaxChecker( SchemaConstants.CSN_SYNTAX );
42      
43      /**
44       * A static Builder for this class
45       */
46      public static final class Builder extends SCBuilder<CsnSyntaxChecker>
47      {
48          /**
49           * The Builder constructor
50           */
51          private Builder()
52          {
53              super( SchemaConstants.CSN_SYNTAX );
54          }
55          
56          
57          /**
58           * Create a new instance of CsnSyntaxChecker
59           * @return A new instance of CsnSyntaxChecker
60           */
61          @Override
62          public CsnSyntaxChecker build()
63          {
64              return new CsnSyntaxChecker( oid );
65          }
66      }
67  
68      
69      /**
70       * Creates a new instance of CsnSyntaxChecker.
71       * 
72       * @param oid The OID to use for this SyntaxChecker
73       */
74      private CsnSyntaxChecker( String oid )
75      {
76          super( oid );
77      }
78  
79      
80      /**
81       * @return An instance of the Builder for this class
82       */
83      public static Builder builder()
84      {
85          return new Builder();
86      }
87  
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public boolean isValidSyntax( Object value )
94      {
95          if ( value == null )
96          {
97              if ( LOG.isDebugEnabled() )
98              {
99                  LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
100             }
101             
102             return false;
103         }
104 
105         if ( !( value instanceof String ) )
106         {
107             if ( LOG.isDebugEnabled() )
108             {
109                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
110             }
111             
112             return false;
113         }
114 
115         String csnStr = ( String ) value;
116 
117         // It must be a valid CSN : try to create a new one.
118         try
119         {
120             boolean result = Csn.isValid( csnStr );
121 
122             if ( LOG.isDebugEnabled() )
123             {
124                 if ( result )
125                 {
126                     LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
127                 }
128                 else
129                 {
130                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
131                 }
132             }
133 
134             return result;
135         }
136         catch ( InvalidCSNException icsne )
137         {
138             if ( LOG.isDebugEnabled() )
139             {
140                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
141             }
142             
143             return false;
144         }
145     }
146 }