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.ldap.model.constants.SchemaConstants;
26  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
27  import org.apache.directory.api.ldap.model.schema.parsers.AttributeTypeDescriptionSchemaParser;
28  import org.apache.directory.api.util.Strings;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * A SyntaxChecker which verifies that a value follows the
35   * attribute type descripton syntax according to RFC 4512, par 4.2.2:
36   * 
37  *  <pre>
38   * AttributeTypeDescription = LPAREN WSP
39   *     numericoid                    ; object identifier
40   *     [ SP "NAME" SP qdescrs ]      ; short names (descriptors)
41   *     [ SP "DESC" SP qdstring ]     ; description
42   *     [ SP "OBSOLETE" ]             ; not active
43   *     [ SP "SUP" SP oid ]           ; supertype
44   *     [ SP "EQUALITY" SP oid ]      ; equality matching rule
45   *     [ SP "ORDERING" SP oid ]      ; ordering matching rule
46   *     [ SP "SUBSTR" SP oid ]        ; substrings matching rule
47   *     [ SP "SYNTAX" SP noidlen ]    ; value syntax
48   *     [ SP "SINGLE-VALUE" ]         ; single-value
49   *     [ SP "COLLECTIVE" ]           ; collective
50   *     [ SP "NO-USER-MODIFICATION" ] ; not user modifiable
51   *     [ SP "USAGE" SP usage ]       ; usage
52   *     extensions WSP RPAREN         ; extensions
53   * 
54   * usage = "userApplications"     /  ; user
55   *         "directoryOperation"   /  ; directory operational
56   *         "distributedOperation" /  ; DSA-shared operational
57   *         "dSAOperation"            ; DSA-specific operational     
58   * 
59   * extensions = *( SP xstring SP qdstrings )
60   * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
61   * 
62   * Each attribute type description must contain at least one of the SUP
63   * or SYNTAX fields. 
64   * 
65   * COLLECTIVE requires usage userApplications.
66   * 
67   * NO-USER-MODIFICATION requires an operational usage.
68   * 
69   * 
70   * </pre>
71   * 
72   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
73   */
74  @SuppressWarnings("serial")
75  public class AttributeTypeDescriptionSyntaxChecker extends SyntaxChecker
76  {
77      /** A logger for this class */
78      private static final Logger LOG = LoggerFactory.getLogger( AttributeTypeDescriptionSyntaxChecker.class );
79  
80      /** The schema parser used to parse the AttributeTypeDescription Syntax */
81      private AttributeTypeDescriptionSchemaParser schemaParser = new AttributeTypeDescriptionSchemaParser();
82  
83  
84      /**
85       * 
86       * Creates a new instance of AttributeTypeDescriptionSchemaParser.
87       *
88       */
89      public AttributeTypeDescriptionSyntaxChecker()
90      {
91          super( SchemaConstants.ATTRIBUTE_TYPE_DESCRIPTION_SYNTAX );
92      }
93  
94  
95      /**
96       * {@inheritDoc}
97       */
98      public boolean isValidSyntax( Object value )
99      {
100         String strValue = null;
101 
102         if ( value == null )
103         {
104             LOG.debug( "Syntax invalid for 'null'" );
105             return false;
106         }
107 
108         if ( value instanceof String )
109         {
110             strValue = ( String ) value;
111         }
112         else if ( value instanceof byte[] )
113         {
114             strValue = Strings.utf8ToString( ( byte[] ) value );
115         }
116         else
117         {
118             strValue = value.toString();
119         }
120 
121         try
122         {
123             schemaParser.parseAttributeTypeDescription( strValue );
124             LOG.debug( "Syntax valid for '{}'", value );
125             return true;
126         }
127         catch ( ParseException pe )
128         {
129             LOG.debug( "Syntax invalid for '{}'", value );
130             return false;
131         }
132     }
133 }