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.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
28  import org.apache.directory.api.util.Strings;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * A SyntaxChecker which verifies that a name is valid for an ObjectClass
35   * or an AttributeType<br/><br/>
36   * 
37   * &lt;m-name&gt; = &lt;keystring&gt; <br/>
38   * &lt;keystring&gt; = &lt;leadkeychar&gt; *&lt;keychar&gt;<br/>
39   * &lt;leadkeychar&gt; = &lt;ALPHA&gt;<br/>
40   * &lt;keychar&gt; = &lt;ALPHA&gt; / &lt;DIGIT&gt; / &lt;HYPHEN&gt; / &lt;SEMI&gt;<br/>
41   * &lt;ALPHA&gt;   = %x41-5A / %x61-7A   ; "A"-"Z" / "a"-"z"<br/>
42   * &lt;DIGIT&gt;   = %x30 / &lt;LDIGIT       ; "0"-"9"<br/>
43   * &lt;LDIGIT&gt;  = %x31-39             ; "1"-"9"<br/>
44   * &lt;HYPHEN&gt;  = %x2D ; hyphen ("-")<br/>
45   * &lt;SEMI&gt;    = %x3B ; semicolon (";")<br/>
46   * 
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  @SuppressWarnings("serial")
50  public class ObjectNameSyntaxChecker extends SyntaxChecker
51  {
52      /** A logger for this class */
53      private static final Logger LOG = LoggerFactory.getLogger( ObjectNameSyntaxChecker.class );
54  
55      private static final String REGEXP = "^([a-zA-Z][a-zA-Z0-9-;]*)$";
56  
57      private static final Pattern PATTERN = Pattern.compile( REGEXP );
58  
59  
60      /**
61       * Creates a new instance of ObjectNameSyntaxChecker.
62       */
63      public ObjectNameSyntaxChecker()
64      {
65          super( SchemaConstants.OBJECT_NAME_SYNTAX );
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public boolean isValidSyntax( Object value )
73      {
74          String strValue = null;
75  
76          if ( value == null )
77          {
78              LOG.debug( "Syntax invalid for 'null'" );
79              return false;
80          }
81  
82          if ( value instanceof String )
83          {
84              strValue = ( String ) value;
85          }
86          else if ( value instanceof byte[] )
87          {
88              strValue = Strings.utf8ToString( ( byte[] ) value );
89          }
90          else
91          {
92              strValue = value.toString();
93          }
94  
95          if ( strValue.length() == 0 )
96          {
97              LOG.debug( "Syntax invalid for '{}'", value );
98              return false;
99          }
100 
101         // Search for the '$' separator
102         Matcher match = PATTERN.matcher( strValue );
103 
104         boolean result = match.matches();
105 
106         if ( result )
107         {
108             LOG.debug( "Syntax valid for '{}'", value );
109         }
110         else
111         {
112             LOG.debug( "Syntax invalid for '{}'", value );
113         }
114 
115         return result;
116     }
117 }