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 PostalAddress according to 
32   * RFC 4517 :
33   * 
34   * <postal-address> = <dstring> <dstring-list>
35   * <dstring-list> = "$" <dstring> <dstring-list> | e
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  @SuppressWarnings("serial")
40  public class PostalAddressSyntaxChecker extends SyntaxChecker
41  {
42      /** A logger for this class */
43      private static final Logger LOG = LoggerFactory.getLogger( PostalAddressSyntaxChecker.class );
44  
45  
46      /**
47       * Creates a new instance of PostalAddressSyntaxChecker.
48       */
49      public PostalAddressSyntaxChecker()
50      {
51          super( SchemaConstants.POSTAL_ADDRESS_SYNTAX );
52      }
53  
54  
55      /**
56       * {@inheritDoc}
57       */
58      public boolean isValidSyntax( Object value )
59      {
60          String strValue = null;
61  
62          if ( value == null )
63          {
64              LOG.debug( "Syntax invalid for 'null'" );
65              return false;
66          }
67  
68          if ( value instanceof String )
69          {
70              strValue = ( String ) value;
71          }
72          else if ( value instanceof byte[] )
73          {
74              strValue = Strings.utf8ToString( ( byte[] ) value );
75          }
76          else
77          {
78              strValue = value.toString();
79          }
80  
81          if ( strValue.length() == 0 )
82          {
83              LOG.debug( "Syntax invalid for '{}'", value );
84              return false;
85          }
86  
87          // Search for the '$' separator
88          int dollar = strValue.indexOf( '$' );
89  
90          if ( dollar == -1 )
91          {
92              // No '$' => only a dstring
93              LOG.debug( "Syntax valid for '{}'", value );
94              return true;
95          }
96  
97          int pos = 0;
98          do
99          {
100             // check that the element between each '$' is not empty
101             String address = strValue.substring( pos, dollar );
102 
103             if ( Strings.isEmpty( address ) )
104             {
105                 LOG.debug( "Syntax invalid for '{}'", value );
106                 return false;
107             }
108 
109             pos = dollar + 1;
110 
111             if ( pos == strValue.length() )
112             {
113                 // we should not have a '$' at the end
114                 LOG.debug( "Syntax invalid for '{}'", value );
115                 return false;
116             }
117 
118             dollar = strValue.indexOf( '$', pos );
119         }
120         while ( dollar > -1 );
121 
122         return true;
123     }
124 }