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.model.schema.syntaxCheckers;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.constants.SchemaConstants;
025import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
026import org.apache.directory.api.util.Strings;
027
028
029/**
030 * A SyntaxChecker for the SyntaxChecker schema element
031 * 
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 */
034@SuppressWarnings("serial")
035public final class SyntaxCheckerSyntaxChecker extends SyntaxChecker
036{
037    /**
038     * A static instance of SyntaxCheckerSyntaxChecker
039     */
040    public static final SyntaxCheckerSyntaxChecker INSTANCE = 
041        new SyntaxCheckerSyntaxChecker( SchemaConstants.SYNTAX_CHECKER_SYNTAX );
042    
043    /**
044     * A static Builder for this class
045     */
046    public static final class Builder extends SCBuilder<SyntaxCheckerSyntaxChecker>
047    {
048        /**
049         * The Builder constructor
050         */
051        private Builder()
052        {
053            super( SchemaConstants.SYNTAX_CHECKER_SYNTAX );
054        }
055        
056        
057        /**
058         * Create a new instance of SyntaxCheckerSyntaxChecker
059         * @return A new instance of SyntaxCheckerSyntaxChecker
060         */
061        @Override
062        public SyntaxCheckerSyntaxChecker build()
063        {
064            return new SyntaxCheckerSyntaxChecker( oid );
065        }
066    }
067
068    /**
069     * Creates a new instance of SyntaxCheckerSyntaxChecker.
070     * 
071     * @param oid The OID to use for this SyntaxChecker
072     */
073    private SyntaxCheckerSyntaxChecker( String oid )
074    {
075        super( oid );
076    }
077
078    
079    /**
080     * @return An instance of the Builder for this class
081     */
082    public static Builder builder()
083    {
084        return new Builder();
085    }
086
087    
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public boolean isValidSyntax( Object value )
093    {
094        String strValue;
095
096        if ( value == null )
097        {
098            if ( LOG.isDebugEnabled() )
099            {
100                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
101            }
102            
103            return true;
104        }
105
106        if ( value instanceof String )
107        {
108            strValue = ( String ) value;
109        }
110        else if ( value instanceof byte[] )
111        {
112            strValue = Strings.utf8ToString( ( byte[] ) value );
113        }
114        else
115        {
116            strValue = value.toString();
117        }
118
119        boolean result = Strings.isIA5String( strValue );
120
121        if ( LOG.isDebugEnabled() )
122        {
123            if ( result )
124            {
125                LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
126            }
127            else
128            {
129                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
130            }
131        }
132
133        return result;
134    }
135}