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      public UniqueMemberNormalizer()
47      {
48          super( SchemaConstants.UNIQUE_MEMBER_MATCH_MR_OID );
49      }
50  
51  
52      public Value<?> normalize( Value<?> value ) throws LdapException
53      {
54          String nameAndUid = value.getString();
55  
56          if ( nameAndUid.length() == 0 )
57          {
58              return null;
59          }
60  
61          // Let's see if we have an UID part
62          int sharpPos = nameAndUid.lastIndexOf( '#' );
63  
64          if ( sharpPos != -1 )
65          {
66              // Now, check that we don't have another '#'
67              if ( nameAndUid.indexOf( '#' ) != sharpPos )
68              {
69                  // Yes, we have one : this is not allowed, it should have been
70                  // escaped.
71                  return null;
72              }
73  
74              // This is an UID if the '#' is immediately
75              // followed by a BitString, except if the '#' is
76              // on the last position
77              String uid = nameAndUid.substring( sharpPos + 1 );
78  
79              if ( sharpPos > 0 )
80              {
81                  Dn dn = new Dn( schemaManager, nameAndUid.substring( 0, sharpPos ) );
82  
83                  return new StringValue( dn.getNormName() + '#' + uid );
84              }
85              else
86              {
87                  throw new IllegalStateException( I18n.err( I18n.ERR_04226, value.getClass() ) );
88              }
89          }
90          else
91          {
92              // No UID, the strValue is a Dn
93              // Return the normalized Dn
94              return new StringValue( new Dn( nameAndUid ).getNormName() );
95          }
96      }
97  
98  
99      public String normalize( String value ) throws LdapException
100     {
101         if ( Strings.isEmpty( value ) )
102         {
103             return null;
104         }
105 
106         // Let's see if we have an UID part
107         int sharpPos = value.lastIndexOf( '#' );
108 
109         if ( sharpPos != -1 )
110         {
111             // Now, check that we don't have another '#'
112             if ( value.indexOf( '#' ) != sharpPos )
113             {
114                 // Yes, we have one : this is not allowed, it should have been
115                 // escaped.
116                 return null;
117             }
118 
119             // This is an UID if the '#' is immediatly
120             // followed by a BitString, except if the '#' is
121             // on the last position
122             String uid = value.substring( sharpPos + 1 );
123 
124             if ( sharpPos > 0 )
125             {
126                 Dn dn = new Dn( schemaManager, value.substring( 0, sharpPos ) );
127 
128                 return dn.getNormName() + '#' + uid;
129             }
130             else
131             {
132                 throw new IllegalStateException( I18n.err( I18n.ERR_04226, value.getClass() ) );
133             }
134         }
135         else
136         {
137             // No UID, the strValue is a Dn
138             // Return the normalized Dn
139             return new Dn( schemaManager, value ).getNormName();
140         }
141     }
142 
143 
144     /**
145      * {@inheritDoc}
146      */
147     public void setSchemaManager( SchemaManager schemaManager )
148     {
149         this.schemaManager = schemaManager;
150     }
151 }