KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > model > DN


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
21 package org.apache.directory.ldapstudio.browser.core.model;
22
23
24 import java.io.Serializable JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
29 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
30
31
32 /**
33  * A DN represents a LDAP distinguished name.
34  *
35  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
36  * @version $Rev$, $Date$
37  */

38 public class DN implements Serializable JavaDoc
39 {
40
41     /** The generated serialVersionUID. */
42     private static final long serialVersionUID = 2343676941769163982L;
43
44     /** The rdns */
45     private RDN[] rdns;
46
47
48     /**
49      * Creates an empty DN.
50      *
51      */

52     public DN()
53     {
54         this.rdns = new RDN[0];
55     }
56
57
58     /**
59      * Creates a new instance of DN containing only on RDN.
60      * The given RDN is cloned.
61      *
62      * @param rdn the rdn
63      */

64     public DN( RDN rdn )
65     {
66         if ( rdn == null )
67         {
68             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_rdn );
69         }
70
71         this.rdns = new RDN[1];
72         this.rdns[0] = new RDN( rdn );
73     }
74
75
76     /**
77      * Creates a new instance of DN. The given string is parsed.
78      *
79      * @param dn the dn
80      * @throws NameException if parsing fails.
81      */

82     public DN( String JavaDoc dn ) throws NameException
83     {
84         if ( dn == null )
85         {
86             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_dn );
87         }
88
89         // this.parseDn(dn.trim());
90
this.parseDn( dn );
91     }
92
93
94     /**
95      * Creates a clone of the given DN.
96      *
97      * @param dn the DN
98      */

99     public DN( DN dn )
100     {
101         if ( dn == null )
102         {
103             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_dn );
104         }
105
106         this.rdns = new RDN[dn.getRdns().length];
107         for ( int i = 0; i < dn.getRdns().length; i++ )
108         {
109             this.rdns[i] = new RDN( dn.getRdns()[i] );
110         }
111     }
112
113
114     /**
115      * Creates a new instance of DN using the given RDN and parent.
116      * The given RDN and parent are cloned.
117      *
118      * @param rdn the RDN
119      * @param parent the parent DN
120      */

121     public DN( RDN rdn, DN parent )
122     {
123         if ( rdn == null )
124         {
125             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_rdn );
126         }
127         if ( parent == null )
128         {
129             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_dn );
130         }
131
132         this.rdns = new RDN[parent.getRdns().length + 1];
133         this.rdns[0] = new RDN( rdn );
134         for ( int i = 0; i < parent.getRdns().length; i++ )
135         {
136             this.rdns[i + 1] = new RDN( parent.getRdns()[i] );
137         }
138     }
139
140
141     /**
142      * Creates a new instance of DN using the given local name and suffix.
143      * The given local name and suffix are cloned.
144      *
145      * @param localName the local name
146      * @param suffix the suffix
147      */

148     public DN( DN localName, DN suffix )
149     {
150         if ( localName == null )
151         {
152             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_dn );
153         }
154         if ( suffix == null )
155         {
156             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_dn );
157         }
158
159         this.rdns = new RDN[localName.getRdns().length + suffix.getRdns().length];
160         for ( int i = 0; i < localName.getRdns().length; i++ )
161         {
162             this.rdns[i] = new RDN( localName.getRdns()[i] );
163         }
164         for ( int i = 0; i < suffix.getRdns().length; i++ )
165         {
166             this.rdns[i + localName.getRdns().length] = new RDN( suffix.getRdns()[i] );
167         }
168     }
169
170
171     /**
172      * Creates a new instance of DN. The given strings are parsed.
173      *
174      * @param rdn the rdn
175      * @param parent the parent dn
176      * @throws NameException if parsing fails
177      */

178     public DN( String JavaDoc rdn, String JavaDoc parent ) throws NameException
179     {
180
181         if ( rdn == null )
182         {
183             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_rdn );
184         }
185         if ( parent == null )
186         {
187             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_dn );
188         }
189
190         // this.parseDn(parent.trim());
191
this.parseDn( parent );
192
193         RDN[] rdns = this.rdns;
194         this.rdns = new RDN[rdns.length + 1];
195         this.rdns[0] = new RDN( rdn );
196         System.arraycopy( rdns, 0, this.rdns, 1, rdns.length );
197     }
198
199
200     /**
201      * Gets the RDN of this DN.
202      *
203      * @return the RDN
204      */

205     public RDN getRdn()
206     {
207         if ( this.rdns.length > 0 )
208         {
209             return this.rdns[0];
210         }
211         else
212         {
213             return new RDN();
214         }
215     }
216
217
218     /**
219      * Gets the parent DN.
220      *
221      * @return the parent DN
222      */

223     public DN getParentDn()
224     {
225         if ( this.rdns.length < 1 )
226         {
227             return null;
228         }
229         else
230         {
231             RDN[] parentRdns = new RDN[this.rdns.length - 1];
232             for ( int i = 1; i < this.rdns.length; i++ )
233             {
234                 parentRdns[i - 1] = new RDN( this.rdns[i] );
235             }
236             DN parent = new DN();
237             parent.rdns = parentRdns;
238             return parent;
239         }
240     }
241
242
243     /**
244      * Extracts the local name by stripping the given
245      * suffix from this DN.
246      *
247      * @param suffix the suffix to strip
248      *
249      * @return the local name
250      */

251     public DN getLocalName( DN suffix )
252     {
253         if ( suffix != null && suffix.getRdns().length > 0 )
254         {
255             DN localName = new DN();
256             for ( int i = getRdns().length - suffix.getRdns().length - 1; i >= 0; i-- )
257             {
258                 localName = new DN( getRdns()[i], localName );
259             }
260             return localName;
261         }
262         else
263         {
264             return this;
265         }
266     }
267
268
269     /**
270      * Returns the string representation of this DN.
271      */

272     public String JavaDoc toString()
273     {
274         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
275
276         for ( int i = 0; i < this.rdns.length; i++ )
277         {
278             sb.append( this.rdns[i].toString() );
279             if ( i + 1 < rdns.length )
280             {
281                 sb.append( "," ); //$NON-NLS-1$
282
}
283         }
284
285         return sb.toString();
286     }
287
288
289     /**
290      * Returns the string representation of this DN, but
291      * lowercased and with the numerid OIDs instead of the types.
292      *
293      * @param schema the schema
294      * @return the lowercased and OID-fizied string representation of this DN
295      */

296     public String JavaDoc toOidString( Schema schema )
297     {
298         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
299
300         for ( int i = 0; i < this.rdns.length; i++ )
301         {
302             sb.append( this.rdns[i].toOidString( schema ) );
303             if ( i + 1 < rdns.length )
304             {
305                 sb.append( "," ); //$NON-NLS-1$
306
}
307         }
308
309         return sb.toString();
310     }
311
312
313     /**
314      * Parses the dn.
315      *
316      * @param dn the dn
317      * @throws NameException if parsing fails
318      */

319     private void parseDn( String JavaDoc dn ) throws NameException
320     {
321         List JavaDoc<RDN> rdnList = new ArrayList JavaDoc<RDN>( 3 );
322
323         boolean backslash = false;
324         int start = 0;
325         for ( int i = 0; i < dn.length(); i++ )
326         {
327             if ( dn.charAt( i ) == '\\' && !backslash )
328             {
329                 backslash = true;
330             }
331             else
332             {
333                 String JavaDoc rdn = null;
334                 if ( dn.charAt( i ) == ',' && !backslash )
335                 {
336                     rdn = dn.substring( start, i );
337                 }
338                 else if ( i == dn.length() - 1 )
339                 {
340                     rdn = dn.substring( start );
341                 }
342                 if ( rdn != null )
343                 {
344                     rdnList.add( new RDN( rdn ) );
345                     start = i + 1;
346
347                     // remove spaces between RDNs
348
for ( ; start < dn.length() && dn.charAt( start ) == ' '; i++ )
349                     {
350                         start++;
351                     }
352                 }
353                 backslash = false;
354             }
355         }
356
357         this.rdns = rdnList.toArray( new RDN[rdnList.size()] );
358     }
359
360
361     /**
362      * Gets the RDNs.
363      *
364      * @return the RDNs
365      */

366     public RDN[] getRdns()
367     {
368         return rdns;
369     }
370
371
372     /**
373      * Sets the RDNs.
374      *
375      * @param rdns the RDNs
376      */

377     public void setRdns( RDN[] rdns )
378     {
379         this.rdns = rdns;
380     }
381
382
383     /**
384      * {@inheritDoc}
385      */

386     public boolean equals( Object JavaDoc o ) throws ClassCastException JavaDoc
387     {
388         if ( o instanceof DN )
389         {
390             return this.toString().equals( ( ( DN ) o ).toString() );
391         }
392         return false;
393     }
394
395
396     /**
397      * {@inheritDoc}
398      */

399     public int hashCode()
400     {
401         return this.toString().hashCode();
402     }
403
404 }
405
Popular Tags