KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkedHashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
31 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
32
33
34 /**
35  * A RDN represents a LDAP relative distinguished name.
36  *
37  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
38  * @version $Rev$, $Date$
39  */

40 public class RDN implements Serializable JavaDoc
41 {
42
43     /** The generated serialVersionUID. */
44     private static final long serialVersionUID = -4165959915339033047L;
45
46     /** The attribute type-value pairs */
47     private RDNPart[] parts;
48
49
50     /**
51      * Creates an empty RDN.
52      *
53      */

54     public RDN()
55     {
56         this.parts = new RDNPart[0];
57     }
58
59
60     /**
61      * Creates a new instance of RDN. The given string is parsed.
62      *
63      * @param rdn the rdn
64      * @throws NameException if parsing fails.
65      */

66     public RDN( String JavaDoc rdn ) throws NameException
67     {
68         if ( rdn == null )
69         {
70             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_rdn );
71         }
72
73         // this.parseMultiRdn(rdn.trim());
74
this.parseMultiRdn( rdn );
75     }
76
77
78     /**
79      * Creates a clone of the given RDN.
80      *
81      * @param rdn the RDN
82      */

83     public RDN( RDN rdn )
84     {
85         if ( rdn == null )
86         {
87             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_rdn );
88         }
89
90         this.parts = new RDNPart[rdn.getParts().length];
91         for ( int i = 0; i < this.parts.length; i++ )
92         {
93             this.parts[i] = new RDNPart( rdn.getParts()[i] );
94         }
95     }
96
97
98     /**
99      * Create a single-valued RDN with the given type and value.
100      *
101      * @param type the attribute type
102      * @param value the value
103      * @param isValueEncoded true if the value is already encoded according RFC4514, Section 2.4
104      * @throws NameException if the type or value are invalid
105      */

106     public RDN( String JavaDoc type, String JavaDoc value, boolean isValueEncoded ) throws NameException
107     {
108         if ( type == null )
109         {
110             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_attribute );
111         }
112         if ( value == null )
113         {
114             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_value );
115         }
116
117         this.parts = new RDNPart[1];
118         this.parts[0] = new RDNPart( type, value, isValueEncoded );
119     }
120
121
122     /**
123      * Creates a multi-values RDN with the given types and values.
124      *
125      * @param types the attribute types
126      * @param values the attribute values
127      * @param areValuesEncoded true if the values is already encoded according RFC4514, Section 2.4
128      * @throws NameException if the types or values are invalid
129      */

130     public RDN( String JavaDoc[] types, String JavaDoc[] values, boolean areValuesEncoded ) throws NameException
131     {
132
133         if ( types == null || types.length < 1 )
134         {
135             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_attribute );
136         }
137         if ( values == null || values.length < 1 )
138         {
139             throw new IllegalArgumentException JavaDoc( BrowserCoreMessages.model__empty_value );
140         }
141         if ( types.length != values.length )
142         {
143             throw new IllegalArgumentException JavaDoc( "Size of types and values is not equal" ); //$NON-NLS-1$
144
}
145
146         this.parts = new RDNPart[types.length];
147         for ( int i = 0; i < this.parts.length; i++ )
148         {
149             this.parts[i] = new RDNPart( types[i], values[i], areValuesEncoded );
150         }
151     }
152
153
154     /**
155      * Checks if the RDN is multi-valued.
156      *
157      * @return true, if the RDN is multi-valued
158      */

159     public boolean isMultivalued()
160     {
161         return this.parts.length > 1;
162     }
163
164
165     /**
166      * Gets the first attribute type.
167      *
168      * @return the first attribute type
169      */

170     public String JavaDoc getType()
171     {
172         return this.parts.length > 0 ? this.parts[0].getType() : ""; //$NON-NLS-1$
173
}
174
175
176     /**
177      * Gets the first attribute value.
178      *
179      * @return the first attribute value
180      */

181     public String JavaDoc getValue()
182     {
183         return this.parts.length > 0 ? this.parts[0].getValue() : ""; //$NON-NLS-1$
184
}
185
186
187     /**
188      * Gets the parts.
189      *
190      * @return the parts
191      */

192     public RDNPart[] getParts()
193     {
194         return this.parts;
195     }
196
197
198     /**
199      * Sets the parts.
200      *
201      * @param parts the parts
202      */

203     public void setParts( RDNPart[] parts )
204     {
205         this.parts = parts;
206     }
207
208
209     /**
210      * Gets the attribute types.
211      *
212      * @return the attribute types
213      */

214     public String JavaDoc[] getTypes()
215     {
216         if ( !isMultivalued() )
217         {
218             return new String JavaDoc[]
219                 { getType() };
220         }
221         else
222         {
223             Set JavaDoc<String JavaDoc> typeSet = new LinkedHashSet JavaDoc<String JavaDoc>();
224             for ( int i = 0; i < this.parts.length; i++ )
225             {
226                 RDNPart entry = this.parts[i];
227                 typeSet.add( entry.getType() );
228             }
229             return typeSet.toArray( new String JavaDoc[typeSet.size()] );
230         }
231     }
232
233
234     /**
235      * Gets the values.
236      *
237      * @return the values
238      */

239     public String JavaDoc[] getValues()
240     {
241         if ( !isMultivalued() )
242         {
243             return new String JavaDoc[]
244                 { getValue() };
245         }
246         else
247         {
248             Set JavaDoc<String JavaDoc> valueSet = new LinkedHashSet JavaDoc<String JavaDoc>();
249             for ( int i = 0; i < this.parts.length; i++ )
250             {
251                 RDNPart entry = this.parts[i];
252                 valueSet.add( entry.getValue() );
253             }
254             return valueSet.toArray( new String JavaDoc[valueSet.size()] );
255         }
256     }
257
258
259     /**
260      * {@inheritDoc}
261      */

262     public int hashCode()
263     {
264         return this.toString().hashCode();
265     }
266
267
268     /**
269      * {@inheritDoc}
270      */

271     public boolean equals( Object JavaDoc o )
272     {
273         if ( o instanceof RDN )
274         {
275             return this.toString().equals( ( ( RDN ) o ).toString() );
276         }
277         return false;
278     }
279
280
281     /**
282      * Returns the string representation of this RDN,
283      * for example &lt;type&gt;=&lt;value&gt;
284      */

285     public String JavaDoc toString()
286     {
287         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
288
289         if ( isMultivalued() )
290         {
291             for ( int i = 0; i < this.parts.length; i++ )
292             {
293                 RDNPart part = this.parts[i];
294                 sb.append( part.toString() );
295
296                 if ( i + 1 < this.parts.length )
297                 {
298                     sb.append( "+" ); //$NON-NLS-1$
299
}
300             }
301         }
302         else if ( this.parts.length > 0 )
303         {
304             RDNPart part = this.parts[0];
305             sb.append( part.toString() );
306         }
307
308         return sb.toString();
309     }
310
311
312     /**
313      * Returns the string representation of this RDN, but
314      * lowercased and with the numerid OIDs instead of the types.
315      *
316      * @param schema the schema
317      * @return the lowercased and OID-fizied string representation of this RDN
318      */

319     public String JavaDoc toOidString( Schema schema )
320     {
321         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
322
323         if ( isMultivalued() )
324         {
325             for ( int i = 0; i < this.parts.length; i++ )
326             {
327                 RDNPart part = this.parts[i];
328                 sb.append( part.toOidString( schema ) );
329
330                 if ( i + 1 < this.parts.length )
331                 {
332                     sb.append( "+" ); //$NON-NLS-1$
333
}
334             }
335         }
336         else if ( this.parts.length > 0 )
337         {
338             RDNPart part = this.parts[0];
339             sb.append( part.toOidString( schema ) );
340         }
341
342         return sb.toString();
343     }
344
345
346     /**
347      * Parses the RDN.
348      *
349      * @param multirdn the rdn
350      * @throws NameException if parsing fails
351      */

352     private void parseMultiRdn( String JavaDoc multirdn ) throws NameException
353     {
354         List JavaDoc<RDNPart> partList = new ArrayList JavaDoc<RDNPart>( 1 );
355
356         boolean backslash = false;
357         int start = 0;
358         for ( int i = 0; i < multirdn.length(); i++ )
359         {
360             if ( multirdn.charAt( i ) == '\\' && !backslash )
361             {
362                 backslash = true;
363             }
364             else
365             {
366                 String JavaDoc rdn = null;
367                 if ( multirdn.charAt( i ) == '+' && !backslash )
368                 {
369                     rdn = multirdn.substring( start, i );
370                 }
371                 else if ( i == multirdn.length() - 1 )
372                 {
373                     rdn = multirdn.substring( start );
374                 }
375                 if ( rdn != null )
376                 {
377                     int index = rdn.indexOf( '=' );
378                     if ( index < 1 )
379                     {
380                         throw new NameException( BrowserCoreMessages.model__invalid_rdn );
381                     }
382                     String JavaDoc type = rdn.substring( 0, index );
383                     String JavaDoc value = rdn.substring( index + 1, rdn.length() );
384                     partList.add( new RDNPart( type, value, true ) );
385                     start = i + 1;
386                 }
387                 backslash = false;
388             }
389         }
390
391         if ( partList.isEmpty() )
392         {
393
394         }
395
396         this.parts = partList.toArray( new RDNPart[partList.size()] );
397     }
398
399 }
400
Popular Tags