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 Directory String according to RFC 4517.
31   * 
32   * From RFC 4517 :
33   * 
34   * <pre>
35   * DirectoryString = 1*UTF8
36   * </pre>
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  @SuppressWarnings("serial")
41  public final class DirectoryStringSyntaxChecker extends SyntaxChecker
42  {
43      /**
44       * A static instance of DirectoryStringSyntaxChecker
45       */
46      public static final DirectoryStringSyntaxChecker INSTANCE = 
47          new DirectoryStringSyntaxChecker( SchemaConstants.DIRECTORY_STRING_SYNTAX );
48      
49      /**
50       * A static Builder for this class
51       */
52      public static final class Builder extends SCBuilder<DirectoryStringSyntaxChecker>
53      {
54          /**
55           * The Builder constructor
56           */
57          private Builder()
58          {
59              super( SchemaConstants.DIRECTORY_STRING_SYNTAX );
60          }
61          
62          
63          /**
64           * Create a new instance of DirectoryStringSyntaxChecker
65           * @return A new instance of DirectoryStringSyntaxChecker
66           */
67          @Override
68          public DirectoryStringSyntaxChecker build()
69          {
70              return new DirectoryStringSyntaxChecker( oid );
71          }
72      }
73  
74      
75      /**
76       * Creates a new instance of DirectoryStringSyntaxChecker.
77       * 
78       * @param oid The OID to use for this SyntaxChecker
79       */
80      private DirectoryStringSyntaxChecker( 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 the value was an invalid UTF8 string, then it's length
127         // will be 0 as the StringTools.utf8ToString() call will
128         // return an empty string
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         // In any other case, we have to check that the
140         // string does not contains the '0xFFFD' character
141         for ( char c : strValue.toCharArray() )
142         {
143             if ( c == 0xFFFD )
144             {
145                 if ( LOG.isDebugEnabled() )
146                 {
147                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
148                 }
149                 
150                 return false;
151             }
152         }
153 
154         if ( LOG.isDebugEnabled() )
155         {
156             LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
157         }
158         
159         return true;
160     }
161 }