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.normalizers;
021
022
023 import java.io.IOException;
024 import java.text.ParseException;
025
026 import org.apache.directory.shared.i18n.I18n;
027 import org.apache.directory.shared.ldap.constants.SchemaConstants;
028 import org.apache.directory.shared.ldap.entry.StringValue;
029 import org.apache.directory.shared.ldap.entry.Value;
030 import org.apache.directory.shared.ldap.exception.LdapException;
031 import org.apache.directory.shared.ldap.exception.LdapInvalidAttributeValueException;
032 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
033 import org.apache.directory.shared.ldap.schema.Normalizer;
034 import org.apache.directory.shared.ldap.schema.PrepareString;
035 import org.apache.directory.shared.ldap.util.GeneralizedTime;
036 import org.apache.directory.shared.ldap.util.GeneralizedTime.Format;
037 import org.apache.directory.shared.ldap.util.GeneralizedTime.FractionDelimiter;
038 import org.apache.directory.shared.ldap.util.GeneralizedTime.TimeZoneFormat;
039
040
041 /**
042 * Normalizer which normalize a time following those rules :
043 * </ul>
044 * <li>if minutes are ommited, then they are replaced by 00</li>
045 * <li>if seconds are ommited, then they are replaced by 00</li>
046 * <li>if fraction is 0 or omitted, it is replaced by 000</li>
047 * <li>the time is supposed to be expressed in Zulu (GMT), so
048 * increment is applied to hours/days/yeah, and a Z is added at the end</li>
049 * </ul>
050 *
051 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
052 * @version $Rev: 491034 $
053 */
054 public class GeneralizedTimeNormalizer extends Normalizer
055 {
056 /** The serial UID */
057 public static final long serialVersionUID = 1L;
058
059
060 /**
061 * Creates a new instance of GeneralizedTimeNormalizer.
062 */
063 public GeneralizedTimeNormalizer()
064 {
065 super( SchemaConstants.GENERALIZED_TIME_MATCH_MR_OID );
066 }
067
068
069 /**
070 * {@inheritDoc}
071 */
072 public Value<?> normalize( Value<?> value ) throws LdapException
073 {
074 try
075 {
076 String normalized = PrepareString.normalize( value.getString(), PrepareString.StringType.DIRECTORY_STRING );
077
078 return new StringValue( normalized );
079 }
080 catch ( IOException ioe )
081 {
082 throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err( I18n.ERR_04224, value ) );
083 }
084 }
085
086
087 /**
088 * {@inheritDoc}
089 */
090 public String normalize( String value ) throws LdapException
091 {
092 try
093 {
094 String prepared = PrepareString.normalize( value, PrepareString.StringType.DIRECTORY_STRING );
095
096 GeneralizedTime time = new GeneralizedTime( prepared );
097 String normalized = time.toGeneralizedTime( Format.YEAR_MONTH_DAY_HOUR_MIN_SEC_FRACTION,
098 FractionDelimiter.DOT, 3, TimeZoneFormat.Z );
099
100 return normalized;
101 }
102 catch ( IOException ioe )
103 {
104 throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err( I18n.ERR_04224, value ) );
105 }
106 catch ( ParseException pe )
107 {
108 throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err( I18n.ERR_04224, value ) );
109 }
110 }
111 }