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