View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.model.schema.normalizers;
21  
22  
23  import java.io.IOException;
24  import java.text.ParseException;
25  
26  import org.apache.directory.api.i18n.I18n;
27  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
28  import org.apache.directory.api.ldap.model.entry.StringValue;
29  import org.apache.directory.api.ldap.model.entry.Value;
30  import org.apache.directory.api.ldap.model.exception.LdapException;
31  import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
32  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
33  import org.apache.directory.api.ldap.model.schema.Normalizer;
34  import org.apache.directory.api.ldap.model.schema.PrepareString;
35  import org.apache.directory.api.util.GeneralizedTime;
36  import org.apache.directory.api.util.GeneralizedTime.Format;
37  import org.apache.directory.api.util.GeneralizedTime.FractionDelimiter;
38  import org.apache.directory.api.util.GeneralizedTime.TimeZoneFormat;
39  
40  
41  /**
42   * Normalizer which normalize a time following those rules :
43   * <ul>
44   * <li>if minutes are ommited, then they are replaced by 00</li>
45   * <li>if seconds are ommited, then they are replaced by 00</li>
46   * <li>if fraction is 0 or omitted, it is replaced by 000</li>
47   * <li>the time is supposed to be expressed in Zulu (GMT), so 
48   * increment is applied to hours/days/yeah, and a Z is added at the end</li>
49   * </ul>
50   *
51   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52   */
53  @SuppressWarnings("serial")
54  public class GeneralizedTimeNormalizer extends Normalizer
55  {
56      /**
57       * Creates a new instance of GeneralizedTimeNormalizer.
58       */
59      public GeneralizedTimeNormalizer()
60      {
61          super( SchemaConstants.GENERALIZED_TIME_MATCH_MR_OID );
62      }
63  
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public Value<?> normalize( Value<?> value ) throws LdapException
70      {
71          try
72          {
73              String normalized = PrepareString.normalize( value.getString(), PrepareString.StringType.DIRECTORY_STRING );
74  
75              return new StringValue( normalized );
76          }
77          catch ( IOException ioe )
78          {
79              throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
80                  I18n.ERR_04224, value ), ioe );
81          }
82      }
83  
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public String normalize( String value ) throws LdapException
90      {
91          try
92          {
93              String prepared = PrepareString.normalize( value, PrepareString.StringType.DIRECTORY_STRING );
94  
95              GeneralizedTime time = new GeneralizedTime( prepared );
96  
97              return time.toGeneralizedTime( Format.YEAR_MONTH_DAY_HOUR_MIN_SEC_FRACTION,
98                  FractionDelimiter.DOT, 3, TimeZoneFormat.Z );
99          }
100         catch ( IOException ioe )
101         {
102             throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
103                 I18n.ERR_04224, value ), ioe );
104         }
105         catch ( ParseException pe )
106         {
107             throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
108                 I18n.ERR_04224, value ), pe );
109         }
110     }
111 }