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 TeletexTerminalIdentifier according to 
31   * RFC 4517 :
32   * <pre>
33   * teletex-id = ttx-term *(DOLLAR ttx-param)
34   * ttx-term   = PrintableString          ; terminal identifier
35   * ttx-param  = ttx-key COLON ttx-value  ; parameter
36   * ttx-key    = "graphic" | "control" | "misc" | "page" | "private"
37   * ttx-value  = *ttx-value-octet
38   *
39   * ttx-value-octet = %x00-23 | (%x5C "24") | %x25-5B | (%x5C "5C") | %x5D-FF
40   * </pre>
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  @SuppressWarnings("serial")
44  public final class TeletexTerminalIdentifierSyntaxChecker extends SyntaxChecker
45  {
46      /**
47       * A static instance of TeletexTerminalIdentifierSyntaxChecker
48       */
49      public static final TeletexTerminalIdentifierSyntaxChecker INSTANCE = 
50          new TeletexTerminalIdentifierSyntaxChecker( SchemaConstants.TELETEX_TERMINAL_IDENTIFIER_SYNTAX );
51      
52      /**
53       * A static Builder for this class
54       */
55      public static final class Builder extends SCBuilder<TeletexTerminalIdentifierSyntaxChecker>
56      {
57          /**
58           * The Builder constructor
59           */
60          private Builder()
61          {
62              super( SchemaConstants.TELETEX_TERMINAL_IDENTIFIER_SYNTAX );
63          }
64          
65          
66          /**
67           * Create a new instance of TeletexTerminalIdentifierSyntaxChecker
68           * @return A new instance of TeletexTerminalIdentifierSyntaxChecker
69           */
70          @Override
71          public TeletexTerminalIdentifierSyntaxChecker build()
72          {
73              return new TeletexTerminalIdentifierSyntaxChecker( oid );
74          }
75      }
76  
77      
78      /**
79       * Creates a new instance of TeletexTerminalIdentifier.
80       * 
81       * @param oid the child's OID
82       */
83      private TeletexTerminalIdentifierSyntaxChecker( String oid )
84      {
85          super( oid );
86      }
87  
88      
89      /**
90       * @return An instance of the Builder for this class
91       */
92      public static Builder builder()
93      {
94          return new Builder();
95      }
96  
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public boolean isValidSyntax( Object value )
103     {
104         String strValue;
105 
106         if ( value == null )
107         {
108             if ( LOG.isDebugEnabled() )
109             {
110                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
111             }
112             
113             return false;
114         }
115 
116         if ( value instanceof String )
117         {
118             strValue = ( String ) value;
119         }
120         else if ( value instanceof byte[] )
121         {
122             strValue = Strings.utf8ToString( ( byte[] ) value );
123         }
124         else
125         {
126             strValue = value.toString();
127         }
128 
129         if ( strValue.length() == 0 )
130         {
131             if ( LOG.isDebugEnabled() )
132             {
133                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
134             }
135             
136             return false;
137         }
138 
139         // Search for the first '$' separator
140         int dollar = strValue.indexOf( '$' );
141 
142         String terminalIdentifier = ( dollar == -1 ) ? strValue : strValue.substring( 0, dollar );
143 
144         if ( terminalIdentifier.length() == 0 )
145         {
146             // It should not be null
147             if ( LOG.isDebugEnabled() )
148             {
149                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
150             }
151             
152             return false;
153         }
154 
155         if ( !Strings.isPrintableString( terminalIdentifier ) )
156         {
157             // It's not a valid PrintableString 
158             if ( LOG.isDebugEnabled() )
159             {
160                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
161             }
162             
163             return false;
164         }
165 
166         if ( dollar == -1 )
167         {
168             // No ttx-param : let's get out
169             if ( LOG.isDebugEnabled() )
170             {
171                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
172             }
173             
174             return true;
175         }
176 
177         // Ok, now let's deal with optional ttx-params
178         String[] ttxParams = strValue.substring( dollar + 1 ).split( "\\$" );
179 
180         if ( ttxParams.length == 0 )
181         {
182             if ( LOG.isDebugEnabled() )
183             {
184                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
185             }
186                 
187             return false;
188         }
189 
190         for ( String ttxParam : ttxParams )
191         {
192             int colon = ttxParam.indexOf( ':' );
193 
194             if ( colon == -1 )
195             {
196                 // we must have a ':' separator
197                 if ( LOG.isDebugEnabled() )
198                 {
199                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
200                 }
201                 
202                 return false;
203             }
204 
205             String key = ttxParam.substring( 0, colon );
206 
207             if ( key.startsWith( "graphic" )
208                 || key.startsWith( "control" )
209                 || key.startsWith( "misc" )
210                 || key.startsWith( "page" )
211                 || key.startsWith( "private" ) )
212             {
213                 if ( colon + 1 == ttxParam.length() )
214                 {
215                     if ( LOG.isDebugEnabled() )
216                     {
217                         LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
218                     }
219                     
220                     return false;
221                 }
222 
223                 boolean hasEsc = false;
224 
225                 for ( byte b : Strings.getBytesUtf8( ttxParam ) )
226                 {
227                     switch ( b )
228                     {
229                         case 0x24:
230                             // '$' is not accepted
231                             if ( LOG.isDebugEnabled() )
232                             {
233                                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
234                             }
235                             
236                             return false;
237 
238                         case 0x5c:
239                             if ( hasEsc )
240                             {
241                                 // two following \ are not accepted
242                                 if ( LOG.isDebugEnabled() )
243                                 {
244                                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
245                                 }
246                                 
247                                 return false;
248                             }
249                             else
250                             {
251                                 hasEsc = true;
252                             }
253 
254                             continue;
255 
256                         case '2':
257                             continue;
258 
259                         case '4':
260                             // We have found a "\24"
261                             hasEsc = false;
262                             continue;
263 
264                         case '5':
265                             continue;
266 
267                         case 'c':
268                         case 'C':
269                             // We have found a "\5c" or a "\5C"
270                             hasEsc = false;
271                             continue;
272 
273                         default:
274                             if ( hasEsc )
275                             {
276                                 // A \ should be followed by "24" or "5c" or "5C"
277                                 return false;
278                             }
279 
280                             continue;
281                     }
282                 }
283             }
284             else
285             {
286                 if ( LOG.isDebugEnabled() )
287                 {
288                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
289                 }
290                 
291                 return false;
292             }
293         }
294 
295         if ( LOG.isDebugEnabled() )
296         {
297             LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
298         }
299         
300         return true;
301     }
302 }