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.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
26  import org.apache.directory.api.util.Strings;
27  
28  
29  /**
30   * A SyntaxChecker which verifies that a value is a IA5 String according to RFC 4517.
31   * <p>
32   * From RFC 4517 :
33   * <pre>
34   * IA5String          = *(%x00-7F)
35   * </pre>
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  @SuppressWarnings("serial")
39  public final class Ia5StringSyntaxChecker extends SyntaxChecker
40  {
41      /**
42       * A static instance of Ia5StringSyntaxChecker
43       */
44      public static final Ia5StringSyntaxChecker INSTANCE = new Ia5StringSyntaxChecker( SchemaConstants.IA5_STRING_SYNTAX );
45      
46      /**
47       * A static Builder for this class
48       */
49      public static final class Builder extends SCBuilder<Ia5StringSyntaxChecker>
50      {
51          /**
52           * The Builder constructor
53           */
54          private Builder()
55          {
56              super( SchemaConstants.IA5_STRING_SYNTAX );
57          }
58          
59          
60          /**
61           * Create a new instance of Ia5StringSyntaxChecker
62           * @return A new instance of Ia5StringSyntaxChecker
63           */
64          @Override
65          public Ia5StringSyntaxChecker build()
66          {
67              return new Ia5StringSyntaxChecker( oid );
68          }
69      }
70  
71      /**
72       * Creates a new instance of a child with a given OID.
73       * 
74       * @param oid The OID to use for this SyntaxChecker
75       */
76      private Ia5StringSyntaxChecker( String oid )
77      {
78          super( oid );
79      }
80  
81      
82      /**
83       * @return An instance of the Builder for this class
84       */
85      public static Builder builder()
86      {
87          return new Builder();
88      }
89  
90      
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public boolean isValidSyntax( Object value )
96      {
97          String strValue;
98  
99          if ( value == null )
100         {
101             if ( LOG.isDebugEnabled() )
102             {
103                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
104             }
105             
106             return true;
107         }
108 
109         if ( value instanceof String )
110         {
111             strValue = ( String ) value;
112         }
113         else if ( value instanceof byte[] )
114         {
115             strValue = Strings.utf8ToString( ( byte[] ) value );
116         }
117         else
118         {
119             strValue = value.toString();
120         }
121 
122         boolean result = Strings.isIA5String( strValue );
123 
124         if ( result )
125         {
126             if ( LOG.isDebugEnabled() )
127             {
128                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
129             }
130         }
131         else
132         {
133             if ( LOG.isDebugEnabled() )
134             {
135                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
136             }
137         }
138 
139         return result;
140     }
141 }