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 java.util.regex.Pattern;
24  
25  import org.apache.directory.api.i18n.I18n;
26  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
28  import org.apache.directory.api.util.Strings;
29  
30  
31  /**
32   * A SyntaxChecker which verifies that a value is a generalized time
33   * according to RFC 4517.
34   * <p>
35   * From RFC 4517 :
36   * <pre>
37   * GeneralizedTime = century year month day hour
38   *                          [ minute [ second / leap-second ] ]
39   *                          [ fraction ]
40   *                          g-time-zone
41   *
42   * century = 2(%x30-39)            ; "00" to "99"
43   * year    = 2(%x30-39)            ; "00" to "99"
44   * month   = ( %x30 %x31-39 )      ; "01" (January) to "09"
45   *           | ( %x31 %x30-32 )    ; "10" to "12"
46   * day     = ( %x30 %x31-39 )      ; "01" to "09"
47   *           | ( %x31-32 %x30-39 ) ; "10" to "29"
48   *           | ( %x33 %x30-31 )    ; "30" to "31"
49   * hour    = ( %x30-31 %x30-39 ) 
50   *           | ( %x32 %x30-33 )    ; "00" to "23"
51   * minute  = %x30-35 %x30-39       ; "00" to "59"
52   *
53   * second  = ( %x30-35 %x30-39 )   ; "00" to "59"
54   * leap-second = ( %x36 %x30 )     ; "60"
55   *
56   * fraction = ( DOT / COMMA ) 1*(%x30-39)
57   * g-time-zone = %x5A              ; "Z"
58   *               | g-differential
59   * g-differential = ( MINUS / PLUS ) hour [ minute ]
60   * MINUS   = %x2D  ; minus sign ("-")
61   * 
62   * From RFC 4512 :
63   * PLUS    = %x2B ; plus sign ("+")
64   * DOT     = %x2E ; period (".")
65   * COMMA   = %x2C ; comma (",")
66   * </pre>
67   *
68   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
69   */
70  @SuppressWarnings("serial")
71  public final class GeneralizedTimeSyntaxChecker extends SyntaxChecker
72  {
73      /** The GeneralizedDate pattern matching */
74      private static final String GENERALIZED_TIME_PATTERN =
75          // century + year : 0000 to 9999
76          "^\\d{4}"
77              // month : 01 to 12
78              + "(0[1-9]|1[0-2])"
79              // day : 01 to 31
80              + "(0[1-9]|[12]\\d|3[01])"
81              // hour : 00 to 23
82              + "([01]\\d|2[0-3])"
83              + "("
84              // optional minute : 00 to 59
85              + "([0-5]\\d)"
86              // optional second | leap second
87              + "([0-5]\\d|60)?"
88              + ")?"
89              // fraction
90              + "([.,]\\d+)?"
91              // time-zone
92              + "(Z|[+-]([01]\\d|2[0-3])([0-5]\\d)?)$";
93  
94      /** The date pattern. The regexp pattern is immutable, only one instance needed. */
95      private static final Pattern DATE_PATTERN = Pattern.compile( GENERALIZED_TIME_PATTERN );
96      
97      /**
98       * A static instance of GeneralizedTimeSyntaxChecker
99       */
100     public static final GeneralizedTimeSyntaxChecker INSTANCE = 
101         new GeneralizedTimeSyntaxChecker( SchemaConstants.GENERALIZED_TIME_SYNTAX );
102     
103     /**
104      * A static Builder for this class
105      */
106     public static final class Builder extends SCBuilder<GeneralizedTimeSyntaxChecker>
107     {
108         /**
109          * The Builder constructor
110          */
111         private Builder()
112         {
113             super( SchemaConstants.GENERALIZED_TIME_SYNTAX );
114         }
115         
116         
117         /**
118          * Create a new instance of GeneralizedTimeSyntaxChecker
119          * @return A new instance of GeneralizedTimeSyntaxChecker
120          */
121         @Override
122         public GeneralizedTimeSyntaxChecker build()
123         {
124             return new GeneralizedTimeSyntaxChecker( oid );
125         }
126     }
127 
128     
129     /**
130      * Creates a new instance of GeneralizedTimeSyntaxChecker.
131      * 
132      * @param oid The OID to use for this SyntaxChecker
133      */
134     private GeneralizedTimeSyntaxChecker( String oid )
135     {
136         super( oid );
137     }
138 
139     
140     /**
141      * @return An instance of the Builder for this class
142      */
143     public static Builder builder()
144     {
145         return new Builder();
146     }
147 
148 
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public boolean isValidSyntax( Object value )
154     {
155         String strValue;
156 
157         if ( value == null )
158         {
159             if ( LOG.isDebugEnabled() )
160             {
161                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
162             }
163             
164             return false;
165         }
166 
167         if ( value instanceof String )
168         {
169             strValue = ( String ) value;
170         }
171         else if ( value instanceof byte[] )
172         {
173             strValue = Strings.utf8ToString( ( byte[] ) value );
174         }
175         else
176         {
177             strValue = value.toString();
178         }
179 
180         // A generalized time must have a minimal length of 11 
181         if ( strValue.length() < 11 )
182         {
183             if ( LOG.isDebugEnabled() )
184             {
185                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
186             }
187             
188             return false;
189         }
190 
191         // Start the date parsing
192         boolean result = DATE_PATTERN.matcher( strValue ).find();
193 
194         if ( LOG.isDebugEnabled() )
195         {
196             if ( result )
197             {
198                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
199             }
200             else
201             {
202                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
203             }
204         }
205 
206         return result;
207     }
208 }