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