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 Printable String according to RFC 4517.
31   * <p>
32   * From RFC 4517 :
33   * <pre>
34   * PrintableString    = 1*PrintableCharacter
35   * PrintableCharacter = ALPHA | DIGIT | SQUOTE | LPAREN | RPAREN |
36   *                          PLUS | COMMA | HYPHEN | DOT | EQUALS |
37   *                          SLASH | COLON | QUESTION | SPACE
38   *                          
39   * SLASH   = %x2F                ; forward slash ("/")
40   * COLON   = %x3A                ; colon (":")
41   * QUESTION= %x3F                ; question mark ("?")
42   * </pre>
43   * From RFC 4512 :
44   * <pre>
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   * </pre>
58   *
59   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
60   */
61  @SuppressWarnings("serial")
62  public final class PrintableStringSyntaxChecker extends SyntaxChecker
63  {
64      /**
65       * A static instance of PrintableStringSyntaxChecker
66       */
67      public static final PrintableStringSyntaxChecker INSTANCE = 
68          new PrintableStringSyntaxChecker( SchemaConstants.PRINTABLE_STRING_SYNTAX );
69      
70      /**
71       * A static Builder for this class
72       */
73      public static final class Builder extends SCBuilder<PrintableStringSyntaxChecker>
74      {
75          /**
76           * The Builder constructor
77           */
78          private Builder()
79          {
80              super( SchemaConstants.PRINTABLE_STRING_SYNTAX );
81          }
82          
83          
84          /**
85           * Create a new instance of PrintableStringSyntaxChecker
86           * @return A new instance of PrintableStringSyntaxChecker
87           */
88          @Override
89          public PrintableStringSyntaxChecker build()
90          {
91              return new PrintableStringSyntaxChecker( oid );
92          }
93      }
94  
95      
96      /**
97       * Creates a new instance of PrintableStringSyntaxChecker.
98       * 
99       * @param oid The OID to use for this SyntaxChecker
100      */
101     private PrintableStringSyntaxChecker( String oid )
102     {
103         super( oid );
104     }
105 
106     
107     /**
108      * @return An instance of the Builder for this class
109      */
110     public static Builder builder()
111     {
112         return new Builder();
113     }
114 
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public boolean isValidSyntax( Object value )
121     {
122         String strValue;
123 
124         if ( value == null )
125         {
126             if ( LOG.isDebugEnabled() )
127             {
128                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
129             }
130             
131             return false;
132         }
133 
134         if ( value instanceof String )
135         {
136             strValue = ( String ) value;
137         }
138         else if ( value instanceof byte[] )
139         {
140             strValue = Strings.utf8ToString( ( byte[] ) value );
141         }
142         else
143         {
144             strValue = value.toString();
145         }
146 
147         if ( strValue.length() == 0 )
148         {
149             if ( LOG.isDebugEnabled() )
150             {
151                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
152             }
153             
154             return false;
155         }
156 
157         // We must have at least one char
158         if ( strValue.length() == 0 )
159         {
160             if ( LOG.isDebugEnabled() )
161             {
162                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
163             }
164             
165             return false;
166         }
167 
168         boolean result = Strings.isPrintableString( strValue );
169 
170         if ( LOG.isDebugEnabled() )
171         {
172             if ( result )
173             {
174                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
175             }
176             else
177             {
178                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
179             }
180         }
181 
182         return result;
183     }
184 }