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