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 */
020package org.apache.directory.shared.ldap.model.schema.syntaxCheckers;
021
022
023import org.apache.directory.shared.ldap.model.constants.SchemaConstants;
024import org.apache.directory.shared.ldap.model.schema.SyntaxChecker;
025import org.apache.directory.shared.util.Strings;
026import org.slf4j.Logger;
027import 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 */
044@SuppressWarnings("serial")
045public class DSAQualitySyntaxSyntaxChecker extends SyntaxChecker
046{
047    /** A logger for this class */
048    private static final Logger LOG = LoggerFactory.getLogger( DSAQualitySyntaxSyntaxChecker.class );
049
050    /**
051     * Creates a new instance of DSAQualitySyntaxSyntaxChecker.
052     */
053    public DSAQualitySyntaxSyntaxChecker()
054    {
055        super( SchemaConstants.DSA_QUALITY_SYNTAX );
056    }
057    
058    
059    /**
060     * {@inheritDoc}
061     */
062    public boolean isValidSyntax( Object value )
063    {
064        String strValue = null;
065
066        if ( value == null )
067        {
068            LOG.debug( "Syntax invalid for 'null'" );
069            return false;
070        }
071        
072        if ( value instanceof String )
073        {
074            strValue = ( String ) value;
075        }
076        else if ( value instanceof byte[] )
077        {
078            strValue = Strings.utf8ToString((byte[]) value);
079        }
080        else
081        {
082            strValue = value.toString();
083        }
084
085        if ( strValue.length() < 7 )
086        {
087            LOG.debug( "Syntax invalid for '{}'", value );
088            return false;
089        }
090
091        String remaining = null;
092        
093        switch ( strValue.charAt( 0 ) )
094        {
095            case 'B' :
096                if ( !strValue.startsWith( "BEST-EFFORT" ) )
097                {
098                    LOG.debug( "Syntax invalid for '{}'", value );
099                    return false;
100                }
101                
102                remaining = strValue.substring( "BEST-EFFORT".length() );
103                break;
104                
105            case 'D' :
106                if ( !strValue.startsWith( "DEFUNCT" ) )
107                {
108                    LOG.debug( "Syntax invalid for '{}'", value );
109                    return false;
110                }
111                
112                remaining = strValue.substring( "DEFUNCT".length() );
113                break;
114                
115            case 'E' :
116                if ( !strValue.startsWith( "EXPERIMENTAL" ) )
117                {
118                    LOG.debug( "Syntax invalid for '{}'", value );
119                    return false;
120                }
121                
122                remaining = strValue.substring( "EXPERIMENTAL".length() );
123                break;
124                
125            case 'F' :
126                if ( !strValue.startsWith( "FULL-SERVICE" ) )
127                {
128                    LOG.debug( "Syntax invalid for '{}'", value );
129                    return false;
130                }
131                
132                remaining = strValue.substring( "FULL-SERVICE".length() );
133                break;
134                
135            case 'P' :
136                if ( !strValue.startsWith( "PILOT-SERVICE" ) )
137                {
138                    LOG.debug( "Syntax invalid for '{}'", value );
139                    return false;
140                }
141                
142                remaining = strValue.substring( "PILOT-SERVICE".length() );
143                break;
144                
145            default :
146                LOG.debug( "Syntax invalid for '{}'", value );
147                return false;
148        }
149        
150        // Now, we might have a description separated from the keyword by a '#'
151        // but this is optional
152        if ( remaining.length() == 0 )
153        {
154            LOG.debug( "Syntax valid for '{}'", value );
155            return true;
156        }
157        
158        if ( remaining.charAt( 0 ) != '#' )
159        {
160            // We were expecting a '#'
161            LOG.debug( "Syntax invalid for '{}'", value );
162            return false;
163        }
164        
165        // Check that the description is a PrintableString
166        boolean result = Strings.isPrintableString(remaining.substring(1));
167        
168        if ( result )
169        {
170            LOG.debug( "Syntax valid for '{}'", value );
171        }
172        else
173        {
174            LOG.debug( "Syntax invalid for '{}'", value );
175        }
176        
177        return result;
178    }
179}