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 Telex Number according to 
31   * RFC 4517 :
32   * <pre>
33   * telex-number  = actual-number DOLLAR country-code DOLLAR answerback
34   * actual-number = PrintableString
35   * country-code  = PrintableString
36   * answerback    = PrintableString
37   * </pre>
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  @SuppressWarnings("serial")
41  public final class TelexNumberSyntaxChecker extends SyntaxChecker
42  {
43      /**
44       * A static instance of TelexNumberSyntaxChecker
45       */
46      public static final TelexNumberSyntaxChecker INSTANCE = 
47          new TelexNumberSyntaxChecker( SchemaConstants.TELEX_NUMBER_SYNTAX );
48      
49      /**
50       * A static Builder for this class
51       */
52      public static final class Builder extends SCBuilder<TelexNumberSyntaxChecker>
53      {
54          /**
55           * The Builder constructor
56           */
57          private Builder()
58          {
59              super( SchemaConstants.TELEX_NUMBER_SYNTAX );
60          }
61          
62          
63          /**
64           * Create a new instance of TelexNumberSyntaxChecker
65           * @return A new instance of TelexNumberSyntaxChecker
66           */
67          @Override
68          public TelexNumberSyntaxChecker build()
69          {
70              return new TelexNumberSyntaxChecker( oid );
71          }
72      }
73  
74      
75      /**
76       * Creates a new instance of TelexNumberSyntaxChecker.
77       * 
78       * @param oid the child's OID
79       */
80      private TelexNumberSyntaxChecker( String oid )
81      {
82          super( oid );
83      }
84  
85      
86      /**
87       * @return An instance of the Builder for this class
88       */
89      public static Builder builder()
90      {
91          return new Builder();
92      }
93  
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public boolean isValidSyntax( Object value )
100     {
101         String strValue;
102 
103         if ( value == null )
104         {
105             if ( LOG.isDebugEnabled() )
106             {
107                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
108             }
109             
110             return false;
111         }
112 
113         if ( value instanceof String )
114         {
115             strValue = ( String ) value;
116         }
117         else if ( value instanceof byte[] )
118         {
119             strValue = Strings.utf8ToString( ( byte[] ) value );
120         }
121         else
122         {
123             strValue = value.toString();
124         }
125 
126         if ( strValue.length() == 0 )
127         {
128             if ( LOG.isDebugEnabled() )
129             {
130                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
131             }
132             
133             return false;
134         }
135 
136         // Search for the first '$' separator
137         int dollar = strValue.indexOf( '$' );
138 
139         // We must have one, and not on first position
140         if ( dollar <= 0 )
141         {
142             // No '$' => error
143             if ( LOG.isDebugEnabled() )
144             {
145                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
146             }
147             
148             return false;
149         }
150 
151         String actualNumber = strValue.substring( 0, dollar );
152 
153         // The actualNumber must not be empty
154         if ( actualNumber.length() == 0 )
155         {
156             if ( LOG.isDebugEnabled() )
157             {
158                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
159             }
160             
161             return false;
162         }
163 
164         // The actual number should be a PrintableString 
165         if ( !Strings.isPrintableString( actualNumber ) )
166         {
167             if ( LOG.isDebugEnabled() )
168             {
169                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
170             }
171             
172             return false;
173         }
174 
175         // Search for the second separator
176         int dollar2 = strValue.indexOf( '$', dollar + 1 );
177 
178         // We must have one
179         if ( dollar2 == -1 )
180         {
181             // No '$' => error
182             if ( LOG.isDebugEnabled() )
183             {
184                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
185             }
186             
187             return false;
188         }
189 
190         String countryCode = strValue.substring( dollar + 1, dollar2 );
191 
192         // The countryCode must not be empty
193         if ( countryCode.length() == 0 )
194         {
195             if ( LOG.isDebugEnabled() )
196             {
197                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
198             }
199             
200             return false;
201         }
202 
203         // The country Code should be a PrintableString 
204         if ( !Strings.isPrintableString( countryCode ) )
205         {
206             if ( LOG.isDebugEnabled() )
207             {
208                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
209             }
210             
211             return false;
212         }
213 
214         // Now, check for the answerBack
215         if ( dollar2 + 1 == strValue.length() )
216         {
217             // The last string should not be null
218             if ( LOG.isDebugEnabled() )
219             {
220                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
221             }
222             
223             return false;
224         }
225 
226         String answerBack = strValue.substring( dollar2 + 1 );
227 
228         // The answerBack should be a PrintableString 
229         if ( !Strings.isPrintableString( answerBack ) )
230         {
231             if ( LOG.isDebugEnabled() )
232             {
233                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
234             }
235             
236             return false;
237         }
238 
239         // Check that the mailboxType is a PrintableString
240         boolean result = Strings.isPrintableString( answerBack );
241 
242         if ( LOG.isDebugEnabled() )
243         {
244             if ( result )
245             {
246                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
247             }
248             else
249             {
250                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
251             }
252         }
253 
254         return result;
255     }
256 }