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