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 Printable String according to RFC 4517.
32   * 
33   * From RFC 4517 :
34   * 
35   * PrintableString    = 1*PrintableCharacter
36   * PrintableCharacter = ALPHA | DIGIT | SQUOTE | LPAREN | RPAREN |
37   *                          PLUS | COMMA | HYPHEN | DOT | EQUALS |
38   *                          SLASH | COLON | QUESTION | SPACE
39   *                          
40   * SLASH   = %x2F                ; forward slash ("/")
41   * COLON   = %x3A                ; colon (":")
42   * QUESTION= %x3F                ; question mark ("?")
43   * 
44   * From RFC 4512 :
45   * ALPHA   = %x41-5A | %x61-7A   ; "A"-"Z" / "a"-"z"
46   * DIGIT   = %x30 | LDIGIT       ; "0"-"9"
47   * LDIGIT  = %x31-39             ; "1"-"9"
48   * SQUOTE  = %x27                ; single quote ("'")
49   * LPAREN  = %x28                ; left paren ("(")
50   * RPAREN  = %x29                ; right paren (")")
51   * PLUS    = %x2B                ; plus sign ("+")
52   * COMMA   = %x2C                ; comma (",")
53   * HYPHEN  = %x2D                ; hyphen ("-")
54   * DOT     = %x2E                ; period (".")
55   * EQUALS  = %x3D                ; equals sign ("=")
56   * SPACE   = %x20                ; space (" ")
57   *
58   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
59   */
60  @SuppressWarnings("serial")
61  public class PrintableStringSyntaxChecker extends SyntaxChecker
62  {
63      /** A logger for this class */
64      private static final Logger LOG = LoggerFactory.getLogger( PrintableStringSyntaxChecker.class );
65  
66  
67      /**
68       * Creates a new instance of PrintableStringSyntaxChecker.
69       */
70      public PrintableStringSyntaxChecker()
71      {
72          super( SchemaConstants.PRINTABLE_STRING_SYNTAX );
73      }
74  
75  
76      /**
77       * {@inheritDoc}
78       */
79      public boolean isValidSyntax( Object value )
80      {
81          String strValue = null;
82  
83          if ( value == null )
84          {
85              LOG.debug( "Syntax invalid for 'null'" );
86              return false;
87          }
88  
89          if ( value instanceof String )
90          {
91              strValue = ( String ) value;
92          }
93          else if ( value instanceof byte[] )
94          {
95              strValue = Strings.utf8ToString( ( byte[] ) value );
96          }
97          else
98          {
99              strValue = value.toString();
100         }
101 
102         if ( strValue.length() == 0 )
103         {
104             LOG.debug( "Syntax invalid for '{}'", value );
105             return false;
106         }
107 
108         // We must have at least one char
109         if ( strValue.length() == 0 )
110         {
111             LOG.debug( "Syntax invalid for '{}'", value );
112             return false;
113         }
114 
115         boolean result = Strings.isPrintableString( strValue );
116 
117         if ( result )
118         {
119             LOG.debug( "Syntax valid for '{}'", value );
120         }
121         else
122         {
123             LOG.debug( "Syntax invalid for '{}'", value );
124         }
125 
126         return result;
127     }
128 }