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.AttributeTypeDescriptionSchemaParser;
29  import org.apache.directory.api.util.Strings;
30  
31  
32  /**
33   * A SyntaxChecker which verifies that a value follows the
34   * attribute type descripton syntax according to RFC 4512, par 4.2.2:
35   * 
36  *  <pre>
37   * AttributeTypeDescription = 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 "SUP" SP oid ]           ; supertype
43   *     [ SP "EQUALITY" SP oid ]      ; equality matching rule
44   *     [ SP "ORDERING" SP oid ]      ; ordering matching rule
45   *     [ SP "SUBSTR" SP oid ]        ; substrings matching rule
46   *     [ SP "SYNTAX" SP noidlen ]    ; value syntax
47   *     [ SP "SINGLE-VALUE" ]         ; single-value
48   *     [ SP "COLLECTIVE" ]           ; collective
49   *     [ SP "NO-USER-MODIFICATION" ] ; not user modifiable
50   *     [ SP "USAGE" SP usage ]       ; usage
51   *     extensions WSP RPAREN         ; extensions
52   * 
53   * usage = "userApplications"     /  ; user
54   *         "directoryOperation"   /  ; directory operational
55   *         "distributedOperation" /  ; DSA-shared operational
56   *         "dSAOperation"            ; DSA-specific operational     
57   * 
58   * extensions = *( SP xstring SP qdstrings )
59   * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
60   * 
61   * Each attribute type description must contain at least one of the SUP
62   * or SYNTAX fields. 
63   * 
64   * COLLECTIVE requires usage userApplications.
65   * 
66   * NO-USER-MODIFICATION requires an operational usage.
67   * </pre>
68   * 
69   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
70   */
71  @SuppressWarnings("serial")
72  public final class AttributeTypeDescriptionSyntaxChecker extends SyntaxChecker
73  {
74      /** The schema parser used to parse the AttributeTypeDescription Syntax */
75      private transient AttributeTypeDescriptionSchemaParser schemaParser = new AttributeTypeDescriptionSchemaParser();
76      
77      /**
78       * A static instance of AttributeTypeDescriptionSyntaxChecker
79       */
80      public static final AttributeTypeDescriptionSyntaxChecker INSTANCE = new AttributeTypeDescriptionSyntaxChecker( 
81          SchemaConstants.ATTRIBUTE_TYPE_DESCRIPTION_SYNTAX );
82  
83      /**
84       * A static Builder for this class
85       */
86      public static final class Builder extends SCBuilder<AttributeTypeDescriptionSyntaxChecker>
87      {
88          /**
89           * The Builder constructor
90           */
91          private Builder()
92          {
93              super( SchemaConstants.ATTRIBUTE_TYPE_DESCRIPTION_SYNTAX );
94          }
95          
96          
97          /**
98           * Create a new instance of AttributeTypeDescriptionSyntaxChecker
99           * @return A new instance of AttributeTypeDescriptionSyntaxChecker
100          */
101         @Override
102         public AttributeTypeDescriptionSyntaxChecker build()
103         {
104             return new AttributeTypeDescriptionSyntaxChecker( oid );
105         }
106     }
107 
108 
109     /**
110      * Creates a new instance of AttributeTypeDescriptionSchemaParser.
111      * 
112      * @param oid The OID to use for this SyntaxChecker
113      *
114      */
115     private AttributeTypeDescriptionSyntaxChecker( String oid )
116     {
117         super( oid );
118     }
119 
120 
121     /**
122      * @return An instance of the Builder for this class
123      */
124     public static Builder builder()
125     {
126         return new Builder();
127     }
128 
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public boolean isValidSyntax( Object value )
135     {
136         String strValue;
137 
138         if ( value == null )
139         {
140             if ( LOG.isDebugEnabled() )
141             {
142                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
143             }
144             
145             return false;
146         }
147 
148         if ( value instanceof String )
149         {
150             strValue = ( String ) value;
151         }
152         else if ( value instanceof byte[] )
153         {
154             strValue = Strings.utf8ToString( ( byte[] ) value );
155         }
156         else
157         {
158             strValue = value.toString();
159         }
160 
161         try
162         {
163             schemaParser.parseAttributeTypeDescription( strValue );
164 
165             if ( LOG.isDebugEnabled() )
166             {
167                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
168             }
169 
170             return true;
171         }
172         catch ( ParseException pe )
173         {
174             if ( LOG.isDebugEnabled() )
175             {
176                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
177             }
178             
179             return false;
180         }
181     }
182 }