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.ldap.model.constants.SchemaConstants;
024import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
025import org.apache.directory.api.util.Strings;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029
030/**
031 * A SyntaxChecker which verifies that a value is a Printable String according to RFC 4517.
032 * 
033 * From RFC 4517 :
034 * 
035 * PrintableString    = 1*PrintableCharacter
036 * PrintableCharacter = ALPHA | DIGIT | SQUOTE | LPAREN | RPAREN |
037 *                          PLUS | COMMA | HYPHEN | DOT | EQUALS |
038 *                          SLASH | COLON | QUESTION | SPACE
039 *                          
040 * SLASH   = %x2F                ; forward slash ("/")
041 * COLON   = %x3A                ; colon (":")
042 * QUESTION= %x3F                ; question mark ("?")
043 * 
044 * From RFC 4512 :
045 * ALPHA   = %x41-5A | %x61-7A   ; "A"-"Z" / "a"-"z"
046 * DIGIT   = %x30 | LDIGIT       ; "0"-"9"
047 * LDIGIT  = %x31-39             ; "1"-"9"
048 * SQUOTE  = %x27                ; single quote ("'")
049 * LPAREN  = %x28                ; left paren ("(")
050 * RPAREN  = %x29                ; right paren (")")
051 * PLUS    = %x2B                ; plus sign ("+")
052 * COMMA   = %x2C                ; comma (",")
053 * HYPHEN  = %x2D                ; hyphen ("-")
054 * DOT     = %x2E                ; period (".")
055 * EQUALS  = %x3D                ; equals sign ("=")
056 * SPACE   = %x20                ; space (" ")
057 *
058 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
059 */
060@SuppressWarnings("serial")
061public class PrintableStringSyntaxChecker extends SyntaxChecker
062{
063    /** A logger for this class */
064    private static final Logger LOG = LoggerFactory.getLogger( PrintableStringSyntaxChecker.class );
065
066
067    /**
068     * Creates a new instance of PrintableStringSyntaxChecker.
069     */
070    public PrintableStringSyntaxChecker()
071    {
072        super( SchemaConstants.PRINTABLE_STRING_SYNTAX );
073    }
074
075
076    /**
077     * {@inheritDoc}
078     */
079    public boolean isValidSyntax( Object value )
080    {
081        String strValue = null;
082
083        if ( value == null )
084        {
085            LOG.debug( "Syntax invalid for 'null'" );
086            return false;
087        }
088
089        if ( value instanceof String )
090        {
091            strValue = ( String ) value;
092        }
093        else if ( value instanceof byte[] )
094        {
095            strValue = Strings.utf8ToString( ( byte[] ) value );
096        }
097        else
098        {
099            strValue = value.toString();
100        }
101
102        if ( strValue.length() == 0 )
103        {
104            LOG.debug( "Syntax invalid for '{}'", value );
105            return false;
106        }
107
108        // We must have at least one char
109        if ( strValue.length() == 0 )
110        {
111            LOG.debug( "Syntax invalid for '{}'", value );
112            return false;
113        }
114
115        boolean result = Strings.isPrintableString( strValue );
116
117        if ( result )
118        {
119            LOG.debug( "Syntax valid for '{}'", value );
120        }
121        else
122        {
123            LOG.debug( "Syntax invalid for '{}'", value );
124        }
125
126        return result;
127    }
128}