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