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 Directory String according to RFC 4517.
031 * 
032 * From RFC 4517 :
033 * 
034 * <pre>
035 * DirectoryString = 1*UTF8
036 * </pre>
037 * 
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040@SuppressWarnings("serial")
041public final class DirectoryStringSyntaxChecker extends SyntaxChecker
042{
043    /**
044     * A static instance of DirectoryStringSyntaxChecker
045     */
046    public static final DirectoryStringSyntaxChecker INSTANCE = 
047        new DirectoryStringSyntaxChecker( SchemaConstants.DIRECTORY_STRING_SYNTAX );
048    
049    /**
050     * A static Builder for this class
051     */
052    public static final class Builder extends SCBuilder<DirectoryStringSyntaxChecker>
053    {
054        /**
055         * The Builder constructor
056         */
057        private Builder()
058        {
059            super( SchemaConstants.DIRECTORY_STRING_SYNTAX );
060        }
061        
062        
063        /**
064         * Create a new instance of DirectoryStringSyntaxChecker
065         * @return A new instance of DirectoryStringSyntaxChecker
066         */
067        @Override
068        public DirectoryStringSyntaxChecker build()
069        {
070            return new DirectoryStringSyntaxChecker( oid );
071        }
072    }
073
074    
075    /**
076     * Creates a new instance of DirectoryStringSyntaxChecker.
077     * 
078     * @param oid The OID to use for this SyntaxChecker
079     */
080    private DirectoryStringSyntaxChecker( String oid )
081    {
082        super( oid );
083    }
084
085    
086    /**
087     * @return An instance of the Builder for this class
088     */
089    public static Builder builder()
090    {
091        return new Builder();
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    public boolean isValidSyntax( Object value )
100    {
101        String strValue;
102
103        if ( value == null )
104        {
105            if ( LOG.isDebugEnabled() )
106            {
107                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
108            }
109            
110            return false;
111        }
112
113        if ( value instanceof String )
114        {
115            strValue = ( String ) value;
116        }
117        else if ( value instanceof byte[] )
118        {
119            strValue = Strings.utf8ToString( ( byte[] ) value );
120        }
121        else
122        {
123            strValue = value.toString();
124        }
125
126        // If the value was an invalid UTF8 string, then it's length
127        // will be 0 as the StringTools.utf8ToString() call will
128        // return an empty string
129        if ( strValue.length() == 0 )
130        {
131            if ( LOG.isDebugEnabled() )
132            {
133                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
134            }
135            
136            return false;
137        }
138
139        // In any other case, we have to check that the
140        // string does not contains the '0xFFFD' character
141        for ( char c : strValue.toCharArray() )
142        {
143            if ( c == 0xFFFD )
144            {
145                if ( LOG.isDebugEnabled() )
146                {
147                    LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
148                }
149                
150                return false;
151            }
152        }
153
154        if ( LOG.isDebugEnabled() )
155        {
156            LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
157        }
158        
159        return true;
160    }
161}