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    import java.text.ParseException;
023    
024    import org.apache.directory.shared.ldap.aci.ACIItemChecker;
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.util.StringTools;
028    import org.slf4j.Logger;
029    import org.slf4j.LoggerFactory;
030    
031    
032    /**
033     * A SyntaxChecker which verifies that a value is a valid ACIItem.
034     * 
035     *  
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev: 488616 $
038     */
039    public class ACIItemSyntaxChecker extends SyntaxChecker
040    {
041        /** A logger for this class */
042        private static final Logger LOG = LoggerFactory.getLogger( ACIItemSyntaxChecker.class );
043    
044        /** The serialVersionUID */
045        private static final long serialVersionUID = 1L;
046    
047        /** An instance of ACI Item Checker */
048        private static final ACIItemChecker ACI_ITEM_CHECKER = new ACIItemChecker();
049    
050        /**
051         * Creates a new instance of ACIItemSyntaxChecker
052         */
053        public ACIItemSyntaxChecker()
054        {
055            super( SchemaConstants.ACI_ITEM_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 '{}'", value );
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 = StringTools.utf8ToString( ( byte[] ) value ); 
079            }
080            else
081            {
082                strValue = value.toString();
083            }
084    
085            if ( strValue.length() == 0 )
086            {
087                LOG.debug( "Syntax invalid for '{}'", value );
088                return false;
089            }
090    
091            try
092            {
093                synchronized( ACI_ITEM_CHECKER )
094                {
095                    ACI_ITEM_CHECKER.parse( strValue );
096                }
097                
098                LOG.debug( "Syntax valid for '{}'", value );
099                return true;
100            }
101            catch ( ParseException pe )
102            {
103                LOG.debug( "Syntax invalid for '{}'", value );
104                return false;
105            }
106        }
107    }