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 org.apache.directory.api.ldap.model.constants.SchemaConstants;
24  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
25  import org.apache.directory.api.util.Strings;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  
30  /**
31   * A SyntaxChecker which verifies that a value is a DSAQualitySyntax according to 
32   * http://tools.ietf.org/id/draft-ietf-asid-ldapv3-attributes-03.txt, par 5.2.2.2 :
33   * 
34   * <DsaQualitySyntax> ::= <DSAKeyword> [ '#' <description> ]
35   *
36   * <DSAKeyword> ::= 'DEFUNCT' | 'EXPERIMENTAL' | 'BEST-EFFORT' |
37   *                  'PILOT-SERVICE' | 'FULL-SERVICE'
38   *
39   * <description> ::= encoded as a PrintableString
40   * 
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  @SuppressWarnings("serial")
45  public class DsaQualitySyntaxSyntaxChecker extends SyntaxChecker
46  {
47      /** A logger for this class */
48      private static final Logger LOG = LoggerFactory.getLogger( DsaQualitySyntaxSyntaxChecker.class );
49  
50  
51      /**
52       * Creates a new instance of DSAQualitySyntaxSyntaxChecker.
53       */
54      public DsaQualitySyntaxSyntaxChecker()
55      {
56          super( SchemaConstants.DSA_QUALITY_SYNTAX );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      public boolean isValidSyntax( Object value )
64      {
65          String strValue = null;
66  
67          if ( value == null )
68          {
69              LOG.debug( "Syntax invalid for 'null'" );
70              return false;
71          }
72  
73          if ( value instanceof String )
74          {
75              strValue = ( String ) value;
76          }
77          else if ( value instanceof byte[] )
78          {
79              strValue = Strings.utf8ToString( ( byte[] ) value );
80          }
81          else
82          {
83              strValue = value.toString();
84          }
85  
86          if ( strValue.length() < 7 )
87          {
88              LOG.debug( "Syntax invalid for '{}'", value );
89              return false;
90          }
91  
92          String remaining = null;
93  
94          switch ( strValue.charAt( 0 ) )
95          {
96              case 'B':
97                  if ( !strValue.startsWith( "BEST-EFFORT" ) )
98                  {
99                      LOG.debug( "Syntax invalid for '{}'", value );
100                     return false;
101                 }
102 
103                 remaining = strValue.substring( "BEST-EFFORT".length() );
104                 break;
105 
106             case 'D':
107                 if ( !strValue.startsWith( "DEFUNCT" ) )
108                 {
109                     LOG.debug( "Syntax invalid for '{}'", value );
110                     return false;
111                 }
112 
113                 remaining = strValue.substring( "DEFUNCT".length() );
114                 break;
115 
116             case 'E':
117                 if ( !strValue.startsWith( "EXPERIMENTAL" ) )
118                 {
119                     LOG.debug( "Syntax invalid for '{}'", value );
120                     return false;
121                 }
122 
123                 remaining = strValue.substring( "EXPERIMENTAL".length() );
124                 break;
125 
126             case 'F':
127                 if ( !strValue.startsWith( "FULL-SERVICE" ) )
128                 {
129                     LOG.debug( "Syntax invalid for '{}'", value );
130                     return false;
131                 }
132 
133                 remaining = strValue.substring( "FULL-SERVICE".length() );
134                 break;
135 
136             case 'P':
137                 if ( !strValue.startsWith( "PILOT-SERVICE" ) )
138                 {
139                     LOG.debug( "Syntax invalid for '{}'", value );
140                     return false;
141                 }
142 
143                 remaining = strValue.substring( "PILOT-SERVICE".length() );
144                 break;
145 
146             default:
147                 LOG.debug( "Syntax invalid for '{}'", value );
148                 return false;
149         }
150 
151         // Now, we might have a description separated from the keyword by a '#'
152         // but this is optional
153         if ( remaining.length() == 0 )
154         {
155             LOG.debug( "Syntax valid for '{}'", value );
156             return true;
157         }
158 
159         if ( remaining.charAt( 0 ) != '#' )
160         {
161             // We were expecting a '#'
162             LOG.debug( "Syntax invalid for '{}'", value );
163             return false;
164         }
165 
166         // Check that the description is a PrintableString
167         boolean result = Strings.isPrintableString( remaining.substring( 1 ) );
168 
169         if ( result )
170         {
171             LOG.debug( "Syntax valid for '{}'", value );
172         }
173         else
174         {
175             LOG.debug( "Syntax invalid for '{}'", value );
176         }
177 
178         return result;
179     }
180 }