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   *    https://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 java.text.ParseException;
24  
25  import org.apache.directory.api.i18n.I18n;
26  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
28  import org.apache.directory.api.ldap.model.schema.parsers.LdapSyntaxDescriptionSchemaParser;
29  import org.apache.directory.api.util.Strings;
30  
31  
32  /**
33   * A SyntaxChecker which verifies that a value follows the
34   * LDAP syntax description syntax according to RFC 4512, par 4.2.2:
35   * 
36   * <pre>
37   * SyntaxDescription = LPAREN WSP
38   *    numericoid                 ; object identifier
39   *    [ SP "DESC" SP qdstring ]  ; description
40   *    extensions WSP RPAREN      ; extensions
41   * </pre>
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  @SuppressWarnings("serial")
46  public final class LdapSyntaxDescriptionSyntaxChecker extends SyntaxChecker
47  {
48      /** The schema parser used to parse the LdapSyntax description Syntax */
49      private transient LdapSyntaxDescriptionSchemaParser schemaParser = new LdapSyntaxDescriptionSchemaParser();
50      /**
51       * A static instance of LdapSyntaxDescriptionSyntaxChecker
52       */
53      public static final LdapSyntaxDescriptionSyntaxChecker INSTANCE = 
54          new LdapSyntaxDescriptionSyntaxChecker( SchemaConstants.LDAP_SYNTAX_DESCRIPTION_SYNTAX );
55      
56      /**
57       * A static Builder for this class
58       */
59      public static final class Builder extends SCBuilder<LdapSyntaxDescriptionSyntaxChecker>
60      {
61          /**
62           * The Builder constructor
63           */
64          private Builder()
65          {
66              super( SchemaConstants.LDAP_SYNTAX_DESCRIPTION_SYNTAX );
67          }
68          
69          
70          /**
71           * Create a new instance of LdapSyntaxDescriptionSyntaxChecker
72           * @return A new instance of LdapSyntaxDescriptionSyntaxChecker
73           */
74          @Override
75          public LdapSyntaxDescriptionSyntaxChecker build()
76          {
77              return new LdapSyntaxDescriptionSyntaxChecker( oid );
78          }
79      }
80  
81      
82      /**
83       * Creates a new instance of LdapSyntaxDescriptionSyntaxChecker.
84       *
85       * @param oid The OID to use for this SyntaxChecker
86       */
87      private LdapSyntaxDescriptionSyntaxChecker( String oid )
88      {
89          super( oid );
90      }
91  
92      
93      /**
94       * @return An instance of the Builder for this class
95       */
96      public static Builder builder()
97      {
98          return new Builder();
99      }
100 
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public boolean isValidSyntax( Object value )
107     {
108         String strValue;
109 
110         if ( value == null )
111         {
112             if ( LOG.isDebugEnabled() )
113             {
114                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
115             }
116             
117             return false;
118         }
119 
120         if ( value instanceof String )
121         {
122             strValue = ( String ) value;
123         }
124         else if ( value instanceof byte[] )
125         {
126             strValue = Strings.utf8ToString( ( byte[] ) value );
127         }
128         else
129         {
130             strValue = value.toString();
131         }
132 
133         try
134         {
135             schemaParser.parse( strValue );
136 
137             if ( LOG.isDebugEnabled() )
138             {
139                 LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
140             }
141 
142             return true;
143         }
144         catch ( ParseException pe )
145         {
146             if ( LOG.isDebugEnabled() )
147             {
148                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
149             }
150             
151             return false;
152         }
153     }
154 }