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 */
020package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
021
022
023import java.util.regex.Pattern;
024
025import org.apache.directory.api.ldap.model.constants.SchemaConstants;
026import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
027import org.apache.directory.api.util.Strings;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031
032/**
033 * A SyntaxChecker which verifies that a value is a UTC time
034 * according to RFC 4517.
035 * 
036 * From RFC 4517 :
037 * UTCTime         = year month day hour minute [ second ] [ u-time-zone ]
038 * u-time-zone     = %x5A          ; "Z" | u-differential
039 * u-differential  = ( MINUS | PLUS ) hour minute
040 *
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 *
053 * g-time-zone = %x5A              ; "Z"
054 *               | g-differential
055 * g-differential = ( MINUS / PLUS ) hour [ minute ]
056 * MINUS   = %x2D  ; minus sign ("-")
057 * 
058 * From RFC 4512 :
059 * PLUS    = %x2B ; plus sign ("+")
060 * 
061 *
062 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
063 */
064@SuppressWarnings("serial")
065public class UtcTimeSyntaxChecker extends SyntaxChecker
066{
067    /** A logger for this class */
068    private static final Logger LOG = LoggerFactory.getLogger( UtcTimeSyntaxChecker.class );
069
070    /** The GeneralizedDate pattern matching */
071    private static final String UTC_TIME_PATTERN =
072        // year : 00 to 99
073        "^\\d{2}"
074            // month : 01 to 12
075            + "(0[1-9]|1[0-2])"
076            // day : 01 to 31
077            + "(0[1-9]|[12]\\d|3[01])"
078            // hour: 00 to 23
079            + "([01]\\d|2[0-3])"
080            // minute : 00 to 59
081            + "([0-5]\\d)"
082            + "("
083            // optional second : 00 to 59
084            + "([0-5]\\d)?"
085            // optional time-zone
086            + "(Z|([+-]([01]\\d|2[0-3])[0-5]\\d))?"
087            + ")$";
088
089    // The regexp pattern, java.util.regex.Pattern is immutable so only one instance is needed.
090    private static final Pattern DATE_PATTERN = Pattern.compile( UTC_TIME_PATTERN );
091
092
093    /**
094     * 
095     * Creates a new instance of UtcTimeSyntaxChecker.
096     *
097     */
098    public UtcTimeSyntaxChecker()
099    {
100        super( SchemaConstants.UTC_TIME_SYNTAX );
101    }
102
103
104    /**
105     * {@inheritDoc}
106     */
107    public boolean isValidSyntax( Object value )
108    {
109        String strValue = null;
110
111        if ( value == null )
112        {
113            LOG.debug( "Syntax invalid for 'null'" );
114            return false;
115        }
116
117        if ( value instanceof String )
118        {
119            strValue = ( String ) value;
120        }
121        else if ( value instanceof byte[] )
122        {
123            strValue = Strings.utf8ToString( ( byte[] ) value );
124        }
125        else
126        {
127            strValue = value.toString();
128        }
129
130        // A generalized time must have a minimal length of 11 
131        if ( strValue.length() < 11 )
132        {
133            LOG.debug( "Syntax invalid for '{}'", value );
134            return false;
135        }
136
137        // Start the date parsing
138        boolean result = DATE_PATTERN.matcher( strValue ).find();
139
140        if ( result )
141        {
142            LOG.debug( "Syntax valid for '{}'", value );
143        }
144        else
145        {
146            LOG.debug( "Syntax invalid for '{}'", value );
147        }
148        return result;
149    }
150}