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.Pattern;
24  
25  import org.apache.directory.api.i18n.I18n;
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  
30  
31  /**
32   * A SyntaxChecker which verifies that a name is valid for an ObjectClass
33   * or an AttributeType<br><br>
34   * <pre>
35   * &lt;m-name&gt; = &lt;keystring&gt; <br>
36   * &lt;keystring&gt; = &lt;leadkeychar&gt; *&lt;keychar&gt;<br>
37   * &lt;leadkeychar&gt; = &lt;ALPHA&gt;<br>
38   * &lt;keychar&gt; = &lt;ALPHA&gt; / &lt;DIGIT&gt; / &lt;HYPHEN&gt; / &lt;SEMI&gt;<br>
39   * &lt;ALPHA&gt;   = %x41-5A / %x61-7A   ; "A"-"Z" / "a"-"z"<br>
40   * &lt;DIGIT&gt;   = %x30 / &lt;LDIGIT       ; "0"-"9"<br>
41   * &lt;LDIGIT&gt;  = %x31-39             ; "1"-"9"<br>
42   * &lt;HYPHEN&gt;  = %x2D ; hyphen ("-")<br>
43   * &lt;SEMI&gt;    = %x3B ; semicolon (";")<br>
44   * </pre>
45   * 
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   */
48  @SuppressWarnings("serial")
49  public final class ObjectNameSyntaxChecker extends SyntaxChecker
50  {
51      private static final String REGEXP = "^([a-zA-Z][a-zA-Z0-9-;]*)$";
52  
53      private static final Pattern PATTERN = Pattern.compile( REGEXP );
54      
55      /**
56       * A static instance of ObjectNameSyntaxChecker
57       */
58      public static final ObjectNameSyntaxChecker INSTANCE = 
59          new ObjectNameSyntaxChecker( SchemaConstants.OBJECT_NAME_SYNTAX );
60      
61      /**
62       * A static Builder for this class
63       */
64      public static final class Builder extends SCBuilder<ObjectNameSyntaxChecker>
65      {
66          /**
67           * The Builder constructor
68           */
69          private Builder()
70          {
71              super( SchemaConstants.OBJECT_NAME_SYNTAX );
72          }
73          
74          
75          /**
76           * Create a new instance of ObjectNameSyntaxChecker
77           * @return A new instance of ObjectNameSyntaxChecker
78           */
79          @Override
80          public ObjectNameSyntaxChecker build()
81          {
82              return new ObjectNameSyntaxChecker( oid );
83          }
84      }
85  
86      
87      /**
88       * Creates a new instance of ObjectNameSyntaxChecker.
89       * 
90       * @param oid The OID to use for this SyntaxChecker
91       */
92      private ObjectNameSyntaxChecker( String oid )
93      {
94          super( oid );
95      }
96  
97      
98      /**
99       * @return An instance of the Builder for this class
100      */
101     public static Builder builder()
102     {
103         return new Builder();
104     }
105 
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public boolean isValidSyntax( Object value )
112     {
113         String strValue;
114 
115         if ( value == null )
116         {
117             if ( LOG.isDebugEnabled() )
118             {
119                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
120             }
121             
122             return false;
123         }
124 
125         if ( value instanceof String )
126         {
127             strValue = ( String ) value;
128         }
129         else if ( value instanceof byte[] )
130         {
131             strValue = Strings.utf8ToString( ( byte[] ) value );
132         }
133         else
134         {
135             strValue = value.toString();
136         }
137 
138         if ( strValue.length() == 0 )
139         {
140             if ( LOG.isDebugEnabled() )
141             {
142                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
143             }
144             
145             return false;
146         }
147 
148         // Search for the '$' separator
149         boolean result = PATTERN.matcher( strValue ).matches();
150 
151         if ( LOG.isDebugEnabled() )
152         {
153             if ( result )
154             {
155                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
156             }
157             else
158             {
159                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
160             }
161         }
162 
163         return result;
164     }
165 }