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 org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.entry.StringValue;
26  import org.apache.directory.api.ldap.model.entry.Value;
27  import org.apache.directory.api.ldap.model.exception.LdapException;
28  import org.apache.directory.api.ldap.model.name.Dn;
29  import org.apache.directory.api.ldap.model.schema.Normalizer;
30  import org.apache.directory.api.ldap.model.schema.SchemaManager;
31  import org.apache.directory.api.util.Strings;
32  
33  
34  /**
35   * A noirmalizer for UniqueMember
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  @SuppressWarnings("serial")
40  public class UniqueMemberNormalizer extends Normalizer
41  {
42      /** A reference to the schema manager used to normalize the Dn */
43      private SchemaManager schemaManager;
44  
45  
46      /**
47       * Creates a new UniqueMemberNormalizer instance
48       */
49      public UniqueMemberNormalizer()
50      {
51          super( SchemaConstants.UNIQUE_MEMBER_MATCH_MR_OID );
52      }
53  
54  
55      @Override
56      public Value<?> normalize( Value<?> value ) throws LdapException
57      {
58          String nameAndUid = value.getString();
59  
60          if ( nameAndUid.length() == 0 )
61          {
62              return null;
63          }
64  
65          // Let's see if we have an UID part
66          int sharpPos = nameAndUid.lastIndexOf( '#' );
67  
68          if ( sharpPos != -1 )
69          {
70              // Now, check that we don't have another '#'
71              if ( nameAndUid.indexOf( '#' ) != sharpPos )
72              {
73                  // Yes, we have one : this is not allowed, it should have been
74                  // escaped.
75                  return null;
76              }
77  
78              // This is an UID if the '#' is immediately
79              // followed by a BitString, except if the '#' is
80              // on the last position
81              String uid = nameAndUid.substring( sharpPos + 1 );
82  
83              if ( sharpPos > 0 )
84              {
85                  Dn dn = new Dn( schemaManager, nameAndUid.substring( 0, sharpPos ) );
86  
87                  return new StringValue( dn.getNormName() + '#' + uid );
88              }
89              else
90              {
91                  throw new IllegalStateException( I18n.err( I18n.ERR_04226, value.getClass() ) );
92              }
93          }
94          else
95          {
96              // No UID, the strValue is a Dn
97              // Return the normalized Dn
98              return new StringValue( new Dn( nameAndUid ).getNormName() );
99          }
100     }
101 
102 
103     @Override
104     public String normalize( String value ) throws LdapException
105     {
106         if ( Strings.isEmpty( value ) )
107         {
108             return null;
109         }
110 
111         // Let's see if we have an UID part
112         int sharpPos = value.lastIndexOf( '#' );
113 
114         if ( sharpPos != -1 )
115         {
116             // Now, check that we don't have another '#'
117             if ( value.indexOf( '#' ) != sharpPos )
118             {
119                 // Yes, we have one : this is not allowed, it should have been
120                 // escaped.
121                 return null;
122             }
123 
124             // This is an UID if the '#' is immediatly
125             // followed by a BitString, except if the '#' is
126             // on the last position
127             String uid = value.substring( sharpPos + 1 );
128 
129             if ( sharpPos > 0 )
130             {
131                 Dn dn = new Dn( schemaManager, value.substring( 0, sharpPos ) );
132 
133                 return dn.getNormName() + '#' + uid;
134             }
135             else
136             {
137                 throw new IllegalStateException( I18n.err( I18n.ERR_04226, value.getClass() ) );
138             }
139         }
140         else
141         {
142             // No UID, the strValue is a Dn
143             // Return the normalized Dn
144             return new Dn( schemaManager, value ).getNormName();
145         }
146     }
147 
148 
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public void setSchemaManager( SchemaManager schemaManager )
154     {
155         this.schemaManager = schemaManager;
156     }
157 }