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 Directory String according to RFC 4517.
030 *
031 * From RFC 4517 :
032 * DirectoryString = 1*UTF8
033 *
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 * @version $Rev: 437007 $
036 */
037 public class DirectoryStringSyntaxChecker extends AbstractSyntaxChecker
038 {
039 /**
040 *
041 * Creates a new instance of DirectoryStringSyntaxChecker.
042 *
043 */
044 public DirectoryStringSyntaxChecker()
045 {
046 super( SchemaConstants.DIRECTORY_STRING_SYNTAX );
047 }
048
049
050 /**
051 *
052 * Creates a new instance of DirectoryStringSyntaxChecker.
053 *
054 * @param oid the oid to associate with this new SyntaxChecker
055 *
056 */
057 protected DirectoryStringSyntaxChecker( String oid )
058 {
059 super( oid );
060 }
061
062 /* (non-Javadoc)
063 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
064 */
065 public boolean isValidSyntax( Object value )
066 {
067 String strValue = null;
068
069 if ( value == null )
070 {
071 return false;
072 }
073
074 if ( value instanceof String )
075 {
076 strValue = ( String ) value;
077 }
078 else if ( value instanceof byte[] )
079 {
080 strValue = StringTools.utf8ToString( ( byte[] ) value );
081 }
082 else
083 {
084 strValue = value.toString();
085 }
086
087 // If the value was an invalid UTF8 string, then it's length
088 // will be 0 as the StringTools.utf8ToString() call will
089 // return an empty string
090 if ( strValue.length() == 0 )
091 {
092 return false;
093 }
094
095 // In any other case, we have to check that the
096 // string does not contains the '0xFFFD' character
097 for ( char c:strValue.toCharArray() )
098 {
099 if ( c == 0xFFFD )
100 {
101 return false;
102 }
103 }
104
105 return true;
106 }
107 }