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