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.server.schema.bootstrap;
21  
22  
23  import java.io.Serializable;
24  import java.util.Comparator;
25  
26  import javax.naming.NamingException;
27  
28  import jdbm.helper.StringComparator;
29  
30  import org.apache.directory.server.schema.bootstrap.ProducerTypeEnum;
31  import org.apache.directory.server.schema.registries.Registries;
32  import org.apache.directory.shared.ldap.schema.DeepTrimToLowerNormalizer;
33  import org.apache.directory.shared.ldap.schema.NormalizingComparator;
34  import org.apache.directory.shared.ldap.util.StringTools;
35  
36  
37  
38  /**
39   * A producer of Comparator objects for the apachemeta schema.
40   * Modified by hand from generated code
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   * @version $Rev$
44   */
45  public class ApachemetaComparatorProducer extends AbstractBootstrapProducer
46  {
47      public ApachemetaComparatorProducer()
48      {
49          super( ProducerTypeEnum.COMPARATOR_PRODUCER );
50      }
51  
52      
53      public static class DeepTrimToLowerNormalizingComparator extends NormalizingComparator
54      {
55          public DeepTrimToLowerNormalizingComparator()
56          {
57              super( new DeepTrimToLowerNormalizer(), new StringComparator() );
58          }
59      }
60  
61      
62      // ------------------------------------------------------------------------
63      // BootstrapProducer Methods
64      // ------------------------------------------------------------------------
65  
66  
67      /**
68       * @see BootstrapProducer#produce(Registries, ProducerCallback)
69       */
70      public void produce( Registries registries, ProducerCallback cb )
71          throws NamingException
72      {
73          Comparator comparator = null;
74          
75          comparator = new NameOrNumericIdComparator();
76          cb.schemaObjectProduced( this, "1.3.6.1.4.1.18060.0.4.0.1.0", comparator );
77  
78          comparator = new ObjectClassTypeComparator();
79          cb.schemaObjectProduced( this, "1.3.6.1.4.1.18060.0.4.0.1.1", comparator );
80          
81          comparator = new StringComparator();
82          cb.schemaObjectProduced( this, "1.3.6.1.4.1.18060.0.4.0.1.2", comparator );
83          
84          comparator = new DeepTrimToLowerNormalizingComparator();
85          cb.schemaObjectProduced( this, "1.3.6.1.4.1.18060.0.4.0.1.3", comparator );
86          
87          comparator = new DeepTrimToLowerNormalizingComparator();
88          cb.schemaObjectProduced( this, "1.3.6.1.4.1.18060.0.4.0.1.4", comparator );
89      }
90  
91  
92      public static class ObjectClassTypeComparator implements Comparator<Object>, Serializable
93      {
94          private static final long serialVersionUID = 1L;
95  
96  
97          public int compare( Object o1, Object o2 )
98          {
99              String s1 = getString( o1 );
100             String s2 = getString( o2 );
101             
102             if ( s1 == null && s2 == null )
103             {
104                 return 0;
105             }
106             
107             if ( s1 == null )
108             {
109                 return -1;
110             }
111             
112             if ( s2 == null )
113             {
114                 return 1;
115             }
116             
117             return s1.compareTo( s2 );
118         }
119         
120         
121         String getString( Object obj )
122         {
123             String strValue;
124 
125             if ( obj == null )
126             {
127                 return null;
128             }
129             
130             if ( obj instanceof String )
131             {
132                 strValue = ( String ) obj;
133             }
134             else if ( obj instanceof byte[] )
135             {
136                 strValue = StringTools.utf8ToString( ( byte[] ) obj ); 
137             }
138             else
139             {
140                 strValue = obj.toString();
141             }
142 
143             return strValue;
144         }
145     }
146 }