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