1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
21
22
23 import org.apache.directory.api.i18n.I18n;
24 import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
25 import org.apache.directory.api.util.Strings;
26
27
28 /**
29 * A SyntaxChecker implemented using Perl5 regular expressions to constrain
30 * values.
31 *
32 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33 */
34 @SuppressWarnings("serial")
35 public final class RegexSyntaxChecker extends SyntaxChecker
36 {
37 /** the set of regular expressions */
38 private String[] expressions;
39
40 /**
41 * A static Builder for this class
42 */
43 public static final class Builder extends SCBuilder<RegexSyntaxChecker>
44 {
45 /** the set of regular expressions */
46 private String[] expressions;
47
48 /**
49 * The Builder constructor
50 */
51 private Builder()
52 {
53 super( null );
54 }
55
56
57 /**
58 * Add a list of regexp to be applied by this SyntaxChecker
59 *
60 * @param expressions The regexp list to add
61 * @return the RegexSyntaxChecker Builder instance
62 */
63 public Builder setExpressions( String[] expressions )
64 {
65 if ( ( expressions != null ) && ( expressions.length > 0 ) )
66 {
67 this.expressions = new String[expressions.length];
68 System.arraycopy( expressions, 0, this.expressions, 0, expressions.length );
69 }
70
71 return this;
72 }
73
74
75 /**
76 * Create a new instance of RegexSyntaxChecker
77 * @return A new instance of RegexSyntaxChecker
78 */
79 public RegexSyntaxChecker build()
80 {
81 return new RegexSyntaxChecker( oid, expressions );
82 }
83 }
84
85
86 /**
87 * Creates a Syntax validator for a specific Syntax using Perl5 matching
88 * rules for validation.
89 *
90 * @param oid the oid of the Syntax values checked
91 * @param matchExprArray the array of matching expressions
92 */
93 private RegexSyntaxChecker( String oid, String[] matchExprArray )
94 {
95 super( oid );
96
97 this.expressions = matchExprArray;
98 }
99
100
101 /**
102 * @return An instance of the Builder for this class
103 */
104 public static Builder builder()
105 {
106 return new Builder();
107 }
108
109
110 /**
111 * {@inheritDoc}
112 */
113 @Override
114 public boolean isValidSyntax( Object value )
115 {
116 String str;
117
118 if ( value instanceof String )
119 {
120 str = ( String ) value;
121
122 for ( String regexp : expressions )
123 {
124 if ( !str.matches( regexp ) )
125 {
126 if ( LOG.isDebugEnabled() )
127 {
128 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
129 }
130
131 return false;
132 }
133 }
134 }
135
136 if ( LOG.isDebugEnabled() )
137 {
138 LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
139 }
140
141 return true;
142 }
143
144
145 /**
146 * Get the list of regexp stored into this SyntaxChecker
147 *
148 * @return AN array containing all the stored regexp
149 */
150 public String[] getExpressions()
151 {
152 if ( expressions == null )
153 {
154 return Strings.EMPTY_STRING_ARRAY;
155 }
156
157 String[] exprs = new String[expressions.length];
158 System.arraycopy( expressions, 0, exprs, 0, expressions.length );
159
160 return exprs;
161 }
162 }