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