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.name.Dn; 026import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 027import org.apache.directory.api.util.Strings; 028 029 030/** 031 * A SyntaxChecker which verifies that a value is a valid {@link Dn}. We just check 032 * that the {@link Dn} is valid, we don't need to verify each of the {@link Rdn} syntax. 033 * 034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 035 */ 036@SuppressWarnings("serial") 037public final class DnSyntaxChecker extends SyntaxChecker 038{ 039 /** 040 * A static instance of DnSyntaxChecker 041 */ 042 public static final DnSyntaxChecker INSTANCE = new DnSyntaxChecker( SchemaConstants.DN_SYNTAX ); 043 044 /** 045 * A static Builder for this class 046 */ 047 public static final class Builder extends SCBuilder<DnSyntaxChecker> 048 { 049 /** 050 * The Builder constructor 051 */ 052 private Builder() 053 { 054 super( SchemaConstants.DN_SYNTAX ); 055 } 056 057 058 /** 059 * Create a new instance of DnSyntaxChecker 060 * @return A new instance of DnSyntaxChecker 061 */ 062 @Override 063 public DnSyntaxChecker build() 064 { 065 return new DnSyntaxChecker( oid ); 066 } 067 } 068 069 070 /** 071 * Creates a new instance of DNSyntaxChecker. 072 * 073 * @param oid The OID to use for this SyntaxChecker 074 */ 075 private DnSyntaxChecker( String oid ) 076 { 077 super( oid ); 078 } 079 080 081 /** 082 * @return An instance of the Builder for this class 083 */ 084 public static Builder builder() 085 { 086 return new Builder(); 087 } 088 089 090 /** 091 * {@inheritDoc} 092 */ 093 @Override 094 public boolean isValidSyntax( Object value ) 095 { 096 String strValue; 097 098 if ( value == null ) 099 { 100 if ( LOG.isDebugEnabled() ) 101 { 102 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) ); 103 } 104 105 return false; 106 } 107 108 if ( value instanceof String ) 109 { 110 strValue = ( String ) value; 111 } 112 else if ( value instanceof byte[] ) 113 { 114 strValue = Strings.utf8ToString( ( byte[] ) value ); 115 } 116 else 117 { 118 strValue = value.toString(); 119 } 120 121 if ( strValue.length() == 0 ) 122 { 123 // TODO: this should be a false, but for 124 // some reason, the principal is empty in 125 // some cases. 126 if ( LOG.isDebugEnabled() ) 127 { 128 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, strValue ) ); 129 } 130 131 return true; 132 } 133 134 // Check that the value is a valid Dn 135 boolean result = Dn.isValid( strValue ); 136 137 if ( LOG.isDebugEnabled() ) 138 { 139 if ( result ) 140 { 141 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, strValue ) ); 142 } 143 else 144 { 145 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) ); 146 } 147 } 148 149 return result; 150 } 151}