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.schema.SyntaxChecker; 025import org.apache.directory.api.util.Strings; 026 027 028/** 029 * A SyntaxChecker implemented using Perl5 regular expressions to constrain 030 * values. 031 * 032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 033 */ 034@SuppressWarnings("serial") 035public final class RegexSyntaxChecker extends SyntaxChecker 036{ 037 /** the set of regular expressions */ 038 private String[] expressions; 039 040 /** 041 * A static Builder for this class 042 */ 043 public static final class Builder extends SCBuilder<RegexSyntaxChecker> 044 { 045 /** the set of regular expressions */ 046 private String[] expressions; 047 048 /** 049 * The Builder constructor 050 */ 051 private Builder() 052 { 053 super( null ); 054 } 055 056 057 /** 058 * Add a list of regexp to be applied by this SyntaxChecker 059 * 060 * @param expressions The regexp list to add 061 */ 062 public Builder setExpressions( String[] expressions ) 063 { 064 if ( ( expressions != null ) && ( expressions.length > 0 ) ) 065 { 066 this.expressions = new String[expressions.length]; 067 System.arraycopy( expressions, 0, this.expressions, 0, expressions.length ); 068 } 069 070 return this; 071 } 072 073 074 /** 075 * Create a new instance of RegexSyntaxChecker 076 * @return A new instance of RegexSyntaxChecker 077 */ 078 public RegexSyntaxChecker build() 079 { 080 return new RegexSyntaxChecker( oid, expressions ); 081 } 082 } 083 084 085 /** 086 * Creates a Syntax validator for a specific Syntax using Perl5 matching 087 * rules for validation. 088 * 089 * @param oid the oid of the Syntax values checked 090 * @param matchExprArray the array of matching expressions 091 */ 092 private RegexSyntaxChecker( String oid, String[] matchExprArray ) 093 { 094 super( oid ); 095 096 this.expressions = matchExprArray; 097 } 098 099 100 /** 101 * @return An instance of the Builder for this class 102 */ 103 public static Builder builder() 104 { 105 return new Builder(); 106 } 107 108 109 /** 110 * {@inheritDoc} 111 */ 112 @Override 113 public boolean isValidSyntax( Object value ) 114 { 115 String str; 116 117 if ( value instanceof String ) 118 { 119 str = ( String ) value; 120 121 for ( String regexp : expressions ) 122 { 123 if ( !str.matches( regexp ) ) 124 { 125 if ( LOG.isDebugEnabled() ) 126 { 127 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 128 } 129 130 return false; 131 } 132 } 133 } 134 135 if ( LOG.isDebugEnabled() ) 136 { 137 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) ); 138 } 139 140 return true; 141 } 142 143 144 /** 145 * Get the list of regexp stored into this SyntaxChecker 146 * 147 * @return AN array containing all the stored regexp 148 */ 149 public String[] getExpressions() 150 { 151 if ( expressions == null ) 152 { 153 return Strings.EMPTY_STRING_ARRAY; 154 } 155 156 String[] exprs = new String[expressions.length]; 157 System.arraycopy( expressions, 0, exprs, 0, expressions.length ); 158 159 return exprs; 160 } 161}