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.aci; 021 022 023import java.text.ParseException; 024 025import org.apache.directory.api.i18n.I18n; 026import org.apache.directory.api.ldap.model.constants.SchemaConstants; 027import org.apache.directory.api.ldap.model.schema.SchemaManager; 028import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 029import org.apache.directory.api.util.Strings; 030 031 032/** 033 * A SyntaxChecker which verifies that a value is a valid ACIItem. 034 * 035 * 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 */ 038@SuppressWarnings("serial") 039public final class ACIItemSyntaxChecker extends SyntaxChecker 040{ 041 /** An instance of ACI Item Checker */ 042 private transient ACIItemChecker aciItemChecker; 043 044 /** 045 * A static instance of ACIItemSyntaxChecker 046 */ 047 public static final ACIItemSyntaxChecker INSTANCE = 048 new ACIItemSyntaxChecker( SchemaConstants.ACI_ITEM_SYNTAX, null ); 049 050 /** 051 * A static Builder for this class 052 */ 053 public static final class Builder extends SCBuilder<ACIItemSyntaxChecker> 054 { 055 /** The schemaManager to use */ 056 private SchemaManager schemaManager; 057 058 /** 059 * The Builder constructor 060 */ 061 private Builder() 062 { 063 super( SchemaConstants.ACI_ITEM_SYNTAX ); 064 } 065 066 067 public Builder setSchemaManager( SchemaManager schemaManager ) 068 { 069 this.schemaManager = schemaManager; 070 071 return this; 072 } 073 074 /** 075 * Create a new instance of ACIItemSyntaxChecker 076 * @return A new instance of ACIItemSyntaxChecker 077 */ 078 @Override 079 public ACIItemSyntaxChecker build() 080 { 081 return new ACIItemSyntaxChecker( oid, schemaManager ); 082 } 083 } 084 085 086 /** 087 * Creates a new instance of ACIItemSyntaxChecker 088 * 089 * @param oid The OID to use for this SyntaxChecker 090 * @param schemaManager The SchemaManager instance 091 */ 092 private ACIItemSyntaxChecker( String oid, SchemaManager schemaManager ) 093 { 094 super( oid ); 095 aciItemChecker = new ACIItemChecker( schemaManager ); 096 } 097 098 099 /** 100 * @return An instance of the Builder for this class 101 */ 102 public static Builder builder() 103 { 104 return new Builder(); 105 } 106 107 108 /** 109 * {@inheritDoc} 110 */ 111 @Override 112 public boolean isValidSyntax( Object value ) 113 { 114 String strValue; 115 116 if ( value == null ) 117 { 118 if ( LOG.isDebugEnabled() ) 119 { 120 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) ); 121 } 122 123 return false; 124 } 125 126 if ( value instanceof String ) 127 { 128 strValue = ( String ) value; 129 } 130 else if ( value instanceof byte[] ) 131 { 132 strValue = Strings.utf8ToString( ( byte[] ) value ); 133 } 134 else 135 { 136 strValue = value.toString(); 137 } 138 139 if ( strValue.length() == 0 ) 140 { 141 if ( LOG.isDebugEnabled() ) 142 { 143 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 144 } 145 146 return false; 147 } 148 149 try 150 { 151 synchronized ( aciItemChecker ) 152 { 153 aciItemChecker.parse( strValue ); 154 } 155 156 if ( LOG.isDebugEnabled() ) 157 { 158 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) ); 159 } 160 161 return true; 162 } 163 catch ( ParseException pe ) 164 { 165 if ( LOG.isDebugEnabled() ) 166 { 167 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 168 } 169 170 return false; 171 } 172 } 173}