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 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      /**
52       * A static instance of LdapSyntaxDescriptionSyntaxChecker
53       */
54      public static final LdapSyntaxDescriptionSyntaxChecker INSTANCE = 
55          new LdapSyntaxDescriptionSyntaxChecker( SchemaConstants.LDAP_SYNTAX_DESCRIPTION_SYNTAX );
56      
57      /**
58       * A static Builder for this class
59       */
60      public static final class Builder extends SCBuilder<LdapSyntaxDescriptionSyntaxChecker>
61      {
62          /**
63           * The Builder constructor
64           */
65          private Builder()
66          {
67              super( SchemaConstants.LDAP_SYNTAX_DESCRIPTION_SYNTAX );
68          }
69          
70          
71          /**
72           * Create a new instance of LdapSyntaxDescriptionSyntaxChecker
73           * @return A new instance of LdapSyntaxDescriptionSyntaxChecker
74           */
75          @Override
76          public LdapSyntaxDescriptionSyntaxChecker build()
77          {
78              return new LdapSyntaxDescriptionSyntaxChecker( oid );
79          }
80      }
81  
82      
83      /**
84       * Creates a new instance of LdapSyntaxDescriptionSyntaxChecker.
85       *
86       * @param oid The OID to use for this SyntaxChecker
87       */
88      private LdapSyntaxDescriptionSyntaxChecker( String oid )
89      {
90          super( oid );
91      }
92  
93      
94      /**
95       * @return An instance of the Builder for this class
96       */
97      public static Builder builder()
98      {
99          return new Builder();
100     }
101 
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public boolean isValidSyntax( Object value )
108     {
109         String strValue;
110 
111         if ( value == null )
112         {
113             if ( LOG.isDebugEnabled() )
114             {
115                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
116             }
117             
118             return false;
119         }
120 
121         if ( value instanceof String )
122         {
123             strValue = ( String ) value;
124         }
125         else if ( value instanceof byte[] )
126         {
127             strValue = Strings.utf8ToString( ( byte[] ) value );
128         }
129         else
130         {
131             strValue = value.toString();
132         }
133 
134         try
135         {
136             schemaParser.parseLdapSyntaxDescription( strValue );
137 
138             if ( LOG.isDebugEnabled() )
139             {
140                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
141             }
142 
143             return true;
144         }
145         catch ( ParseException pe )
146         {
147             if ( LOG.isDebugEnabled() )
148             {
149                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
150             }
151             
152             return false;
153         }
154     }
155 }