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.syntaxes;
021    
022    
023    import org.apache.directory.shared.ldap.constants.SchemaConstants;
024    import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
025    import org.apache.directory.shared.ldap.util.StringTools;
026    
027    
028    /**
029     * A SyntaxChecker which verifies that a value is a DSAQualitySyntax according to 
030     * http://tools.ietf.org/id/draft-ietf-asid-ldapv3-attributes-03.txt, par 5.2.2.2 :
031     * 
032     * <DsaQualitySyntax> ::= <DSAKeyword> [ '#' <description> ]
033     *
034     * <DSAKeyword> ::= 'DEFUNCT' | 'EXPERIMENTAL' | 'BEST-EFFORT' |
035     *                  'PILOT-SERVICE' | 'FULL-SERVICE'
036     *
037     * <description> ::= encoded as a PrintableString
038     * 
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev$
042     */
043    public class DSAQualitySyntaxSyntaxChecker extends AbstractSyntaxChecker
044    {
045        /**
046         * 
047         * Creates a new instance of DSAQualitySyntaxSyntaxChecker.
048         *
049         */
050        public DSAQualitySyntaxSyntaxChecker()
051        {
052            super( SchemaConstants.DSA_QUALITY_SYNTAX );
053        }
054        
055        /**
056         * 
057         * Creates a new instance of DSAQualitySyntaxSyntaxChecker.
058         * 
059         * @param oid the oid to associate with this new SyntaxChecker
060         *
061         */
062        protected DSAQualitySyntaxSyntaxChecker( String oid )
063        {
064            super( oid );
065        }
066        
067        
068        /* (non-Javadoc)
069         * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
070         */
071        public boolean isValidSyntax( Object value )
072        {
073            String strValue = null;
074    
075            if ( value == null )
076            {
077                return false;
078            }
079            
080            if ( value instanceof String )
081            {
082                strValue = ( String ) value;
083            }
084            else if ( value instanceof byte[] )
085            {
086                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
087            }
088            else
089            {
090                strValue = value.toString();
091            }
092    
093            if ( strValue.length() < 7 )
094            {
095                return false;
096            }
097    
098            String remaining = null;
099            
100            switch ( strValue.charAt( 0 ) )
101            {
102                case 'B' :
103                    if ( !strValue.startsWith( "BEST-EFFORT" ) )
104                    {
105                        return false;
106                    }
107                    
108                    remaining = strValue.substring( "BEST-EFFORT".length() );
109                    break;
110                    
111                case 'D' :
112                    if ( !strValue.startsWith( "DEFUNCT" ) )
113                    {
114                        return false;
115                    }
116                    
117                    remaining = strValue.substring( "DEFUNCT".length() );
118                    break;
119                    
120                case 'E' :
121                    if ( !strValue.startsWith( "EXPERIMENTAL" ) )
122                    {
123                        return false;
124                    }
125                    
126                    remaining = strValue.substring( "EXPERIMENTAL".length() );
127                    break;
128                    
129                case 'F' :
130                    if ( !strValue.startsWith( "FULL-SERVICE" ) )
131                    {
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                        return false;
142                    }
143                    
144                    remaining = strValue.substring( "PILOT-SERVICE".length() );
145                    break;
146                    
147                default :
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                return true;
156            }
157            
158            if ( remaining.charAt( 0 ) != '#' )
159            {
160                // We were expecting a '#'
161                return false;
162            }
163            
164            // Check that the description is a PrintableString
165            return StringTools.isPrintableString( remaining.substring( 1 ) );
166        }
167    }