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