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 java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * A SyntaxChecker implemented using Perl5 regular expressions to constrain
33   * values.
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  @SuppressWarnings("serial")
38  public class RegexSyntaxChecker extends SyntaxChecker
39  {
40      /** A logger for this class */
41      private static final Logger LOG = LoggerFactory.getLogger( RegexSyntaxChecker.class );
42  
43      /** the set of regular expressions */
44      private List<String> expressions;
45  
46  
47      /**
48       * Creates a Syntax validator for a specific Syntax using Perl5 matching
49       * rules for validation.
50       * 
51       * @param oid
52       *            the oid of the Syntax values checked
53       * @param matchExprArray
54       *            the array of matching expressions
55       */
56      public RegexSyntaxChecker( String oid, String[] matchExprArray )
57      {
58          super( oid );
59  
60          if ( ( matchExprArray != null ) && ( matchExprArray.length != 0 ) )
61          {
62              expressions = new ArrayList<String>( matchExprArray.length );
63  
64              for ( String regexp : matchExprArray )
65              {
66                  expressions.add( regexp );
67              }
68          }
69          else
70          {
71              expressions = new ArrayList<String>();
72          }
73      }
74  
75  
76      /**
77       * 
78       * Creates a new instance of RegexSyntaxChecker.
79       * 
80       * @param oid the oid to associate with this new SyntaxChecker
81       *
82       */
83      public RegexSyntaxChecker( String oid )
84      {
85          super( oid );
86          expressions = new ArrayList<String>();
87      }
88  
89  
90      /**
91       * {@inheritDoc}
92       */
93      public boolean isValidSyntax( Object value )
94      {
95          String str = null;
96          boolean match = true;
97  
98          if ( value instanceof String )
99          {
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 }