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 java.text.ParseException;
024    
025    import org.apache.directory.shared.ldap.constants.SchemaConstants;
026    import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
027    import org.apache.directory.shared.ldap.schema.parsers.AttributeTypeDescriptionSchemaParser;
028    import org.apache.directory.shared.ldap.util.StringTools;
029    
030    
031    /**
032     * A SyntaxChecker which verifies that a value follows the
033     * attribute type descripton syntax according to RFC 4512, par 4.2.2:
034     * 
035    *  <pre>
036     * AttributeTypeDescription = LPAREN WSP
037     *     numericoid                    ; object identifier
038     *     [ SP "NAME" SP qdescrs ]      ; short names (descriptors)
039     *     [ SP "DESC" SP qdstring ]     ; description
040     *     [ SP "OBSOLETE" ]             ; not active
041     *     [ SP "SUP" SP oid ]           ; supertype
042     *     [ SP "EQUALITY" SP oid ]      ; equality matching rule
043     *     [ SP "ORDERING" SP oid ]      ; ordering matching rule
044     *     [ SP "SUBSTR" SP oid ]        ; substrings matching rule
045     *     [ SP "SYNTAX" SP noidlen ]    ; value syntax
046     *     [ SP "SINGLE-VALUE" ]         ; single-value
047     *     [ SP "COLLECTIVE" ]           ; collective
048     *     [ SP "NO-USER-MODIFICATION" ] ; not user modifiable
049     *     [ SP "USAGE" SP usage ]       ; usage
050     *     extensions WSP RPAREN         ; extensions
051     * 
052     * usage = "userApplications"     /  ; user
053     *         "directoryOperation"   /  ; directory operational
054     *         "distributedOperation" /  ; DSA-shared operational
055     *         "dSAOperation"            ; DSA-specific operational     
056     * 
057     * extensions = *( SP xstring SP qdstrings )
058     * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
059     * 
060     * Each attribute type description must contain at least one of the SUP
061     * or SYNTAX fields. 
062     * 
063     * COLLECTIVE requires usage userApplications.
064     * 
065     * NO-USER-MODIFICATION requires an operational usage.
066     * 
067     * 
068     * </pre>
069     * 
070     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
071     * @version $Rev$
072     */
073    public class AttributeTypeDescriptionSyntaxChecker extends AbstractSyntaxChecker
074    {
075        /** The schema parser used to parse the AttributeTypeDescription Syntax */
076        private AttributeTypeDescriptionSchemaParser schemaParser = new AttributeTypeDescriptionSchemaParser();
077    
078        /**
079         * 
080         * Creates a new instance of AttributeTypeDescriptionSchemaParser.
081         *
082         */
083        public AttributeTypeDescriptionSyntaxChecker()
084        {
085            super( SchemaConstants.ATTRIBUT_TYPE_DESCRIPTION_SYNTAX );
086        }
087        
088        /**
089         * 
090         * Creates a new instance of AttributeTypeDescriptionSyntaxChecker.
091         * 
092         * @param oid the oid to associate with this new SyntaxChecker
093         *
094         */
095        protected AttributeTypeDescriptionSyntaxChecker( String oid )
096        {
097            super( oid );
098        }
099    
100        /* (non-Javadoc)
101         * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
102         */
103        public boolean isValidSyntax( Object value )
104        {
105            String strValue = null;
106    
107            if ( value == null )
108            {
109                return false;
110            }
111    
112            if ( value instanceof String )
113            {
114                strValue = ( String ) value;
115            }
116            else if ( value instanceof byte[] )
117            {
118                strValue = StringTools.utf8ToString( ( byte[] ) value );
119            }
120            else
121            {
122                strValue = value.toString();
123            }
124    
125            try
126            {
127                schemaParser.parseAttributeTypeDescription( strValue );
128                return true;
129            }
130            catch ( ParseException pe )
131            {
132                return false;
133            }
134        }
135    }