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 UTC time
032 * according to RFC 4517.
033 *
034 * From RFC 4517 :
035 * UTCTime = year month day hour minute [ second ] [ u-time-zone ]
036 * u-time-zone = %x5A ; "Z" | u-differential
037 * u-differential = ( MINUS | PLUS ) hour minute
038 *
039 * year = 2(%x30-39) ; "00" to "99"
040 * month = ( %x30 %x31-39 ) ; "01" (January) to "09"
041 * | ( %x31 %x30-32 ) ; "10" to "12"
042 * day = ( %x30 %x31-39 ) ; "01" to "09"
043 * | ( %x31-32 %x30-39 ) ; "10" to "29"
044 * | ( %x33 %x30-31 ) ; "30" to "31"
045 * hour = ( %x30-31 %x30-39 )
046 * | ( %x32 %x30-33 ) ; "00" to "23"
047 * minute = %x30-35 %x30-39 ; "00" to "59"
048 *
049 * second = ( %x30-35 %x30-39 ) ; "00" to "59"
050 *
051 * g-time-zone = %x5A ; "Z"
052 * | g-differential
053 * g-differential = ( MINUS / PLUS ) hour [ minute ]
054 * MINUS = %x2D ; minus sign ("-")
055 *
056 * From RFC 4512 :
057 * PLUS = %x2B ; plus sign ("+")
058 *
059 *
060 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
061 * @version $Rev$
062 */
063 public class UtcTimeSyntaxChecker extends AbstractSyntaxChecker
064 {
065 /** The GeneralizedDate pattern matching */
066 private static final String UTC_TIME_PATTERN =
067 "^\\d{2}" + // year : 00 to 99
068 "(0[1-9]|1[0-2])" + // month : 01 to 12
069 "(0[1-9]|[12]\\d|3[01])" + // day : 01 to 31
070 "([01]\\d|2[0-3])" + // hour: 00 to 23
071 "([0-5]\\d)" + // minute : 00 to 59
072 "(" +
073 "([0-5]\\d)?" + // optional second : 00 to 59
074 "(Z|([+-]([01]\\d|2[0-3])[0-5]\\d))?" + // optionnal time-zone
075 ")$";
076
077 // The regexp pattern matcher
078 private Pattern datePattern = Pattern.compile( UTC_TIME_PATTERN );
079
080 /**
081 *
082 * Creates a new instance of UtcTimeSyntaxChecker.
083 *
084 */
085 public UtcTimeSyntaxChecker()
086 {
087 super( SchemaConstants.UTC_TIME_SYNTAX );
088 }
089
090
091 /**
092 *
093 * Creates a new instance of UtcTimeSyntaxChecker.
094 *
095 * @param oid the oid to associate with this new SyntaxChecker
096 *
097 */
098 protected UtcTimeSyntaxChecker( String oid )
099 {
100 super( oid );
101 }
102
103 /* (non-Javadoc)
104 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
105 */
106 public boolean isValidSyntax( Object value )
107 {
108 String strValue = null;
109
110 if ( value == null )
111 {
112 return false;
113 }
114
115 if ( value instanceof String )
116 {
117 strValue = ( String ) value;
118 }
119 else if ( value instanceof byte[] )
120 {
121 strValue = StringTools.utf8ToString( ( byte[] ) value );
122 }
123 else
124 {
125 strValue = value.toString();
126 }
127
128 // A generalized time must have a minimal length of 11
129 if ( strValue.length() < 11 )
130 {
131 return false;
132 }
133
134 // Start the date parsing
135 return datePattern.matcher( strValue ).find();
136 }
137 }