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.api.ldap.codec.api;
021
022import org.apache.directory.api.ldap.model.schema.AttributeType;
023import org.apache.directory.api.ldap.model.schema.LdapSyntax;
024import org.apache.directory.api.ldap.model.schema.SchemaManager;
025import org.apache.directory.api.util.Strings;
026
027/**
028 * An implementation of the BinaryAttributeDetector interface. It's not
029 * schema aware, so it only uses the list of binary Attributes.
030 * 
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class SchemaBinaryAttributeDetector implements BinaryAttributeDetector
034{
035    /** The schemaManager to use */
036    private SchemaManager schemaManager;
037    
038    
039    protected SchemaBinaryAttributeDetector()
040    {
041    }
042    
043    
044    /**
045     * Create an instance of SchemaBinaryAttributeDetector.
046     * 
047     * @param schemaManager The SchemaManager to use
048     */
049    public SchemaBinaryAttributeDetector( SchemaManager schemaManager )
050    {
051        this.schemaManager = schemaManager;
052    }
053
054    /**
055     * @param schemaManager the schemaManager to set
056     */
057    public void setSchemaManager( SchemaManager schemaManager )
058    {
059        this.schemaManager = schemaManager;
060    }
061
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public boolean isBinary( String attributeId )
068    {
069        String attrId = Strings.toLowerCaseAscii( attributeId );
070
071        if ( attrId.endsWith( ";binary" ) )
072        {
073            return true;
074        }
075
076        if ( schemaManager != null )
077        {
078            AttributeType attributeType =  schemaManager.getAttributeType( attrId );
079            
080            if ( attributeType == null )
081            {
082                return false;
083            }
084            
085            LdapSyntax ldapSyntax = attributeType.getSyntax();
086            
087            return ( ldapSyntax != null ) && !ldapSyntax.isHumanReadable();
088        }
089
090        return false;
091    }
092}