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.ldap.model.constants.SchemaConstants;
24  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
25  import org.apache.directory.api.util.Strings;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  
30  /**
31   * A SyntaxChecker which verifies that a value is a Telex Number according to 
32   * RFC 4517 :
33   * 
34   * telex-number  = actual-number DOLLAR country-code DOLLAR answerback
35   * actual-number = PrintableString
36   * country-code  = PrintableString
37   * answerback    = PrintableString
38   * 
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  @SuppressWarnings("serial")
42  public class TelexNumberSyntaxChecker extends SyntaxChecker
43  {
44      /** A logger for this class */
45      private static final Logger LOG = LoggerFactory.getLogger( TelexNumberSyntaxChecker.class );
46  
47  
48      /**
49       * Creates a new instance of TelexNumberSyntaxChecker.
50       */
51      public TelexNumberSyntaxChecker()
52      {
53          super( SchemaConstants.TELEX_NUMBER_SYNTAX );
54      }
55  
56  
57      /**
58       * {@inheritDoc}
59       */
60      public boolean isValidSyntax( Object value )
61      {
62          String strValue = null;
63  
64          if ( value == null )
65          {
66              LOG.debug( "Syntax invalid for 'null'" );
67              return false;
68          }
69  
70          if ( value instanceof String )
71          {
72              strValue = ( String ) value;
73          }
74          else if ( value instanceof byte[] )
75          {
76              strValue = Strings.utf8ToString( ( byte[] ) value );
77          }
78          else
79          {
80              strValue = value.toString();
81          }
82  
83          if ( strValue.length() == 0 )
84          {
85              LOG.debug( "Syntax invalid for '{}'", value );
86              return false;
87          }
88  
89          // Search for the first '$' separator
90          int dollar = strValue.indexOf( '$' );
91  
92          // We must have one, and not on first position
93          if ( dollar <= 0 )
94          {
95              // No '$' => error
96              LOG.debug( "Syntax invalid for '{}'", value );
97              return false;
98          }
99  
100         String actualNumber = strValue.substring( 0, dollar );
101 
102         // The actualNumber must not be empty
103         if ( actualNumber.length() == 0 )
104         {
105             LOG.debug( "Syntax invalid for '{}'", value );
106             return false;
107         }
108 
109         // The actual number should be a PrintableString 
110         if ( !Strings.isPrintableString( actualNumber ) )
111         {
112             LOG.debug( "Syntax invalid for '{}'", value );
113             return false;
114         }
115 
116         // Search for the second separator
117         int dollar2 = strValue.indexOf( '$', dollar + 1 );
118 
119         // We must have one
120         if ( dollar2 == -1 )
121         {
122             // No '$' => error
123             LOG.debug( "Syntax invalid for '{}'", value );
124             return false;
125         }
126 
127         String countryCode = strValue.substring( dollar + 1, dollar2 );
128 
129         // The countryCode must not be empty
130         if ( countryCode.length() == 0 )
131         {
132             LOG.debug( "Syntax invalid for '{}'", value );
133             return false;
134         }
135 
136         // The country Code should be a PrintableString 
137         if ( !Strings.isPrintableString( countryCode ) )
138         {
139             LOG.debug( "Syntax invalid for '{}'", value );
140             return false;
141         }
142 
143         // Now, check for the answerBack
144         if ( dollar2 + 1 == strValue.length() )
145         {
146             // The last string should not be null
147             LOG.debug( "Syntax invalid for '{}'", value );
148             return false;
149         }
150 
151         String answerBack = strValue.substring( dollar2 + 1 );
152 
153         // The answerBack should be a PrintableString 
154         if ( !Strings.isPrintableString( answerBack ) )
155         {
156             LOG.debug( "Syntax invalid for '{}'", value );
157             return false;
158         }
159 
160         // Check that the mailboxType is a PrintableString
161         boolean result = Strings.isPrintableString( answerBack );
162 
163         if ( result )
164         {
165             LOG.debug( "Syntax valid for '{}'", value );
166         }
167         else
168         {
169             LOG.debug( "Syntax invalid for '{}'", value );
170         }
171 
172         return result;
173     }
174 }