001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.schema.syntaxCheckers;
021
022
023 import org.apache.directory.shared.ldap.constants.SchemaConstants;
024 import org.apache.directory.shared.ldap.schema.SyntaxChecker;
025 import org.apache.directory.shared.ldap.util.StringTools;
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028
029
030 /**
031 * A SyntaxChecker which verifies that a value is a DSAQualitySyntax according to
032 * http://tools.ietf.org/id/draft-ietf-asid-ldapv3-attributes-03.txt, par 5.2.2.2 :
033 *
034 * <DsaQualitySyntax> ::= <DSAKeyword> [ '#' <description> ]
035 *
036 * <DSAKeyword> ::= 'DEFUNCT' | 'EXPERIMENTAL' | 'BEST-EFFORT' |
037 * 'PILOT-SERVICE' | 'FULL-SERVICE'
038 *
039 * <description> ::= encoded as a PrintableString
040 *
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 * @version $Rev$
044 */
045 public class DSAQualitySyntaxSyntaxChecker extends SyntaxChecker
046 {
047 /** A logger for this class */
048 private static final Logger LOG = LoggerFactory.getLogger( DSAQualitySyntaxSyntaxChecker.class );
049
050 /** The serialVersionUID */
051 private static final long serialVersionUID = 1L;
052
053 /**
054 * Creates a new instance of DSAQualitySyntaxSyntaxChecker.
055 */
056 public DSAQualitySyntaxSyntaxChecker()
057 {
058 super( SchemaConstants.DSA_QUALITY_SYNTAX );
059 }
060
061
062 /**
063 * {@inheritDoc}
064 */
065 public boolean isValidSyntax( Object value )
066 {
067 String strValue = null;
068
069 if ( value == null )
070 {
071 LOG.debug( "Syntax invalid for '{}'", value );
072 return false;
073 }
074
075 if ( value instanceof String )
076 {
077 strValue = ( String ) value;
078 }
079 else if ( value instanceof byte[] )
080 {
081 strValue = StringTools.utf8ToString( ( byte[] ) value );
082 }
083 else
084 {
085 strValue = value.toString();
086 }
087
088 if ( strValue.length() < 7 )
089 {
090 LOG.debug( "Syntax invalid for '{}'", value );
091 return false;
092 }
093
094 String remaining = null;
095
096 switch ( strValue.charAt( 0 ) )
097 {
098 case 'B' :
099 if ( !strValue.startsWith( "BEST-EFFORT" ) )
100 {
101 LOG.debug( "Syntax invalid for '{}'", value );
102 return false;
103 }
104
105 remaining = strValue.substring( "BEST-EFFORT".length() );
106 break;
107
108 case 'D' :
109 if ( !strValue.startsWith( "DEFUNCT" ) )
110 {
111 LOG.debug( "Syntax invalid for '{}'", value );
112 return false;
113 }
114
115 remaining = strValue.substring( "DEFUNCT".length() );
116 break;
117
118 case 'E' :
119 if ( !strValue.startsWith( "EXPERIMENTAL" ) )
120 {
121 LOG.debug( "Syntax invalid for '{}'", value );
122 return false;
123 }
124
125 remaining = strValue.substring( "EXPERIMENTAL".length() );
126 break;
127
128 case 'F' :
129 if ( !strValue.startsWith( "FULL-SERVICE" ) )
130 {
131 LOG.debug( "Syntax invalid for '{}'", value );
132 return false;
133 }
134
135 remaining = strValue.substring( "FULL-SERVICE".length() );
136 break;
137
138 case 'P' :
139 if ( !strValue.startsWith( "PILOT-SERVICE" ) )
140 {
141 LOG.debug( "Syntax invalid for '{}'", value );
142 return false;
143 }
144
145 remaining = strValue.substring( "PILOT-SERVICE".length() );
146 break;
147
148 default :
149 LOG.debug( "Syntax invalid for '{}'", value );
150 return false;
151 }
152
153 // Now, we might have a description separated from the keyword by a '#'
154 // but this is optional
155 if ( remaining.length() == 0 )
156 {
157 LOG.debug( "Syntax valid for '{}'", value );
158 return true;
159 }
160
161 if ( remaining.charAt( 0 ) != '#' )
162 {
163 // We were expecting a '#'
164 LOG.debug( "Syntax invalid for '{}'", value );
165 return false;
166 }
167
168 // Check that the description is a PrintableString
169 boolean result = StringTools.isPrintableString( remaining.substring( 1 ) );
170
171 if ( result )
172 {
173 LOG.debug( "Syntax valid for '{}'", value );
174 }
175 else
176 {
177 LOG.debug( "Syntax invalid for '{}'", value );
178 }
179
180 return result;
181 }
182 }