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.syntaxCheckers; 021 022 023 import org.apache.directory.shared.ldap.constants.SchemaConstants; 024 import org.apache.directory.shared.ldap.schema.SyntaxChecker; 025 import org.apache.directory.shared.ldap.util.StringTools; 026 import org.slf4j.Logger; 027 import 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 * @version $Rev$ 060 */ 061 public class PrintableStringSyntaxChecker extends SyntaxChecker 062 { 063 /** A logger for this class */ 064 private static final Logger LOG = LoggerFactory.getLogger( PrintableStringSyntaxChecker.class ); 065 066 /** The serialVersionUID */ 067 private static final long serialVersionUID = 1L; 068 069 /** 070 * Creates a new instance of PrintableStringSyntaxChecker. 071 */ 072 public PrintableStringSyntaxChecker() 073 { 074 super( SchemaConstants.PRINTABLE_STRING_SYNTAX ); 075 } 076 077 078 /** 079 * {@inheritDoc} 080 */ 081 public boolean isValidSyntax( Object value ) 082 { 083 String strValue = null; 084 085 if ( value == null ) 086 { 087 LOG.debug( "Syntax invalid for '{}'", value ); 088 return false; 089 } 090 091 if ( value instanceof String ) 092 { 093 strValue = ( String ) value; 094 } 095 else if ( value instanceof byte[] ) 096 { 097 strValue = StringTools.utf8ToString( ( byte[] ) value ); 098 } 099 else 100 { 101 strValue = value.toString(); 102 } 103 104 if ( strValue.length() == 0 ) 105 { 106 LOG.debug( "Syntax invalid for '{}'", value ); 107 return false; 108 } 109 110 // We must have at least one char 111 if ( strValue.length() == 0 ) 112 { 113 LOG.debug( "Syntax invalid for '{}'", value ); 114 return false; 115 } 116 117 boolean result = StringTools.isPrintableString( strValue ); 118 119 if ( result ) 120 { 121 LOG.debug( "Syntax valid for '{}'", value ); 122 } 123 else 124 { 125 LOG.debug( "Syntax invalid for '{}'", value ); 126 } 127 128 return result; 129 } 130 }