001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.schema.syntaxes;
021
022
023 import java.util.regex.Pattern;
024
025 import org.apache.directory.shared.ldap.constants.SchemaConstants;
026 import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
027 import org.apache.directory.shared.ldap.util.StringTools;
028
029
030 /**
031 * A SyntaxChecker which verifies that a value is a generalized time
032 * according to RFC 4517.
033 *
034 * From RFC 4517 :
035 * GeneralizedTime = century year month day hour
036 * [ minute [ second / leap-second ] ]
037 * [ fraction ]
038 * g-time-zone
039 *
040 * century = 2(%x30-39) ; "00" to "99"
041 * year = 2(%x30-39) ; "00" to "99"
042 * month = ( %x30 %x31-39 ) ; "01" (January) to "09"
043 * | ( %x31 %x30-32 ) ; "10" to "12"
044 * day = ( %x30 %x31-39 ) ; "01" to "09"
045 * | ( %x31-32 %x30-39 ) ; "10" to "29"
046 * | ( %x33 %x30-31 ) ; "30" to "31"
047 * hour = ( %x30-31 %x30-39 )
048 * | ( %x32 %x30-33 ) ; "00" to "23"
049 * minute = %x30-35 %x30-39 ; "00" to "59"
050 *
051 * second = ( %x30-35 %x30-39 ) ; "00" to "59"
052 * leap-second = ( %x36 %x30 ) ; "60"
053 *
054 * fraction = ( DOT / COMMA ) 1*(%x30-39)
055 * g-time-zone = %x5A ; "Z"
056 * | g-differential
057 * g-differential = ( MINUS / PLUS ) hour [ minute ]
058 * MINUS = %x2D ; minus sign ("-")
059 *
060 * From RFC 4512 :
061 * PLUS = %x2B ; plus sign ("+")
062 * DOT = %x2E ; period (".")
063 * COMMA = %x2C ; comma (",")
064 *
065 *
066 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
067 * @version $Rev$
068 */
069 public class GeneralizedTimeSyntaxChecker extends AbstractSyntaxChecker
070 {
071 /** The GeneralizedDate pattern matching */
072 private static final String GENERALIZED_TIME_PATTERN =
073 "^\\d{4}" + // century + year : 0000 to 9999
074 "(0[1-9]|1[0-2])" + // month : 01 to 12
075 "(0[1-9]|[12]\\d|3[01])" + // day : 01 to 31
076 "([01]\\d|2[0-3])" + // hour : 00 to 23
077 "(" +
078 "([0-5]\\d)" + // optionnal minute : 00 to 59
079 "([0-5]\\d|60)?" + // optionnal second | leap second
080 ")?" +
081 "([.,]\\d+)?" + // fraction
082 "(Z|[+-]([01]\\d|2[0-3])([0-5]\\d)?)$"; // time-zone
083
084 // The regexp pattern matcher
085 private Pattern datePattern = Pattern.compile( GENERALIZED_TIME_PATTERN );
086
087 /**
088 *
089 * Creates a new instance of GeneralizedTimeSyntaxChecker.
090 *
091 */
092 public GeneralizedTimeSyntaxChecker()
093 {
094 super( SchemaConstants.GENERALIZED_TIME_SYNTAX );
095 }
096
097 /**
098 *
099 * Creates a new instance of GeneralizedTimeSyntaxChecker.
100 *
101 * @param oid the oid to associate with this new SyntaxChecker
102 *
103 */
104 protected GeneralizedTimeSyntaxChecker( String oid )
105 {
106 super( oid );
107 }
108
109 /* (non-Javadoc)
110 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
111 */
112 public boolean isValidSyntax( Object value )
113 {
114 String strValue = null;
115
116 if ( value == null )
117 {
118 return false;
119 }
120
121 if ( value instanceof String )
122 {
123 strValue = ( String ) value;
124 }
125 else if ( value instanceof byte[] )
126 {
127 strValue = StringTools.utf8ToString( ( byte[] ) value );
128 }
129 else
130 {
131 strValue = value.toString();
132 }
133
134 // A generalized time must have a minimal length of 11
135 if ( strValue.length() < 11 )
136 {
137 return false;
138 }
139
140 // Start the date parsing
141 return datePattern.matcher( strValue ).find();
142 }
143 }