View Javadoc
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   *    http://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           */
62          public Builder setExpressions( String[] expressions )
63          {
64              if ( ( expressions != null ) && ( expressions.length > 0 ) )
65              {
66                  this.expressions = new String[expressions.length];
67                  System.arraycopy( expressions, 0, this.expressions, 0, expressions.length );
68              }
69              
70              return this;
71          }
72          
73          
74          /**
75           * Create a new instance of RegexSyntaxChecker
76           * @return A new instance of RegexSyntaxChecker
77           */
78          public RegexSyntaxChecker build()
79          {
80              return new RegexSyntaxChecker( oid, expressions );
81          }
82      }
83  
84      
85      /**
86       * Creates a Syntax validator for a specific Syntax using Perl5 matching
87       * rules for validation.
88       * 
89       * @param oid the oid of the Syntax values checked
90       * @param matchExprArray the array of matching expressions
91       */
92      private RegexSyntaxChecker( String oid, String[] matchExprArray )
93      {
94          super( oid );
95  
96          this.expressions = matchExprArray;
97      }
98  
99      
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 }