KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > ldap > ldif > Entry


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package scriptella.driver.ldap.ldif;
19
20 import javax.naming.NamingEnumeration JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22 import javax.naming.directory.Attribute JavaDoc;
23 import javax.naming.directory.Attributes JavaDoc;
24 import javax.naming.directory.BasicAttribute JavaDoc;
25 import javax.naming.directory.BasicAttributes JavaDoc;
26 import javax.naming.directory.DirContext JavaDoc;
27 import javax.naming.directory.ModificationItem JavaDoc;
28 import javax.naming.ldap.Control JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * A entry to be populated by an ldif parser.
36  *
37  * We will have different kind of entries : - added entries - deleted entries -
38  * modified entries - RDN modified entries - DN modified entries
39  *
40  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
41  */

42 public class Entry
43 {
44     /** the change type */
45     private int changeType=ADD;
46
47     /** the modification item list */
48     private List JavaDoc<ModificationItem JavaDoc> modificationList = new ArrayList JavaDoc<ModificationItem JavaDoc>();
49
50     private Map JavaDoc<String JavaDoc,ModificationItem JavaDoc> modificationItems = new HashMap JavaDoc<String JavaDoc, ModificationItem JavaDoc>();
51
52     /** the dn of the ldif entry */
53     private String JavaDoc dn;
54
55     /** The new superior */
56     private String JavaDoc newSuperior;
57
58     /** The new rdn */
59     private String JavaDoc newRdn;
60
61     /** The delete old rdn flag */
62     private boolean deleteOldRdn;
63
64     /** attributes of the entry */
65     private BasicAttributes JavaDoc attributeList = new BasicAttributes JavaDoc(true);
66
67     /** The possible change types */
68     public final static int ADD = 0;
69
70     public final static int MODIFY = 1;
71
72     public final static int MODDN = 2;
73
74     public final static int MODRDN = 3;
75
76     public final static int DELETE = 4;
77
78     /** The control */
79     private Control JavaDoc control;
80
81
82     /**
83      * Set the Distinguished Name
84      *
85      * @param dn
86      * The Distinguished Name
87      */

88     public void setDn( String JavaDoc dn )
89     {
90         this.dn = dn;
91     }
92
93     /**
94      * Set the modification type
95      *
96      * @param changeType
97      * The change type
98      *
99      */

100     public void setChangeType( int changeType )
101     {
102         this.changeType = changeType;
103     }
104
105     /**
106      * Set the change type
107      *
108      * @param changeType
109      * The change type
110      */

111     public void setChangeType( String JavaDoc changeType )
112     {
113         if ( "add".equals( changeType ) )
114         {
115             this.changeType = ADD;
116         }
117         else if ( "modify".equals( changeType ) )
118         {
119             this.changeType = MODIFY;
120         }
121         else if ( "moddn".equals( changeType ) )
122         {
123             this.changeType = MODDN;
124         }
125         else if ( "modrdn".equals( changeType ) )
126         {
127             this.changeType = MODRDN;
128         }
129         else if ( "delete".equals( changeType ) )
130         {
131             this.changeType = DELETE;
132         }
133     }
134
135
136
137     /**
138      * Add a modification item
139      *
140      * @param modOp
141      * The operation. One of : DirContext.ADD_ATTRIBUTE
142      * DirContext.REMOVE_ATTRIBUTE DirContext.REPLACE_ATTRIBUTE
143      *
144      * @param id
145      * The attribute's ID
146      * @param value
147      * The attribute's value
148      */

149     public void addModificationItem( int modOp, String JavaDoc id, Object JavaDoc value )
150     {
151         if ( changeType == MODIFY )
152         {
153             BasicAttribute JavaDoc attr = new BasicAttribute JavaDoc( id, value );
154
155             if ( modificationItems.containsKey( id ) )
156             {
157                 ModificationItem JavaDoc item = modificationItems.get( id );
158
159                 if ( item.getModificationOp() != modOp )
160                 {
161                     // This is an error : we can't have two different
162
// modifications of the same attribute for the same entry
163

164                     throw new LdifParseException( "Bad modification" );
165                 }
166
167                 Attribute JavaDoc attribute = item.getAttribute();
168
169                 attribute.add( value );
170             }
171             else
172             {
173                 ModificationItem JavaDoc item = new ModificationItem JavaDoc( modOp, attr );
174                 modificationList.add( item );
175                 modificationItems.put( id, item );
176             }
177         }
178     }
179
180     /**
181      * Add an attribute to the entry
182      *
183      * @param attr
184      * The attribute to be added
185      */

186     public void addAttribute( Attribute JavaDoc attr )
187     {
188         attributeList.put( attr );
189     }
190
191     /**
192      * Add an attribute to the entry
193      *
194      * @param id
195      * The attribute ID
196      *
197      * @param value
198      * The attribute value
199      *
200      */

201     public void addAttribute( String JavaDoc id, Object JavaDoc value )
202     {
203         Attribute JavaDoc attr = get( id );
204
205         if ( attr != null )
206         {
207             attr.add( value );
208         }
209         else
210         {
211             attributeList.put( id, value );
212         }
213     }
214
215     /**
216      * Add an attribute value to an existing attribute
217      *
218      * @param id
219      * The attribute ID
220      *
221      * @param value
222      * The attribute value
223      *
224      */

225     public void putAttribute( String JavaDoc id, Object JavaDoc value )
226     {
227         Attribute JavaDoc attribute = attributeList.get( id );
228
229         if ( attribute != null )
230         {
231             attribute.add( value );
232         }
233         else
234         {
235             attributeList.put( id, value );
236         }
237     }
238
239     /**
240      * Get the change type
241      *
242      * @return The change type. One of : ADD = 0; MODIFY = 1; MODDN = 2; MODRDN =
243      * 3; DELETE = 4;
244      */

245     public int getChangeType()
246     {
247         return changeType;
248     }
249
250     /**
251      * @return The list of modification items
252      */

253     public List JavaDoc<ModificationItem JavaDoc> getModificationItems()
254     {
255         return modificationList;
256     }
257
258     /**
259      * @return The entry Distinguished name
260      */

261     public String JavaDoc getDn()
262     {
263         return dn;
264     }
265
266     /**
267      * @return The number of entry modifications
268      */

269     public int size()
270     {
271         return modificationList.size();
272     }
273
274     /**
275      * Returns a attribute given it's id
276      *
277      * @param attributeId
278      * The attribute Id
279      * @return The attribute if it exists
280      */

281     public Attribute JavaDoc get( String JavaDoc attributeId )
282     {
283         if ( "dn".equalsIgnoreCase( attributeId ) )
284         {
285             return new BasicAttribute JavaDoc( "dn", dn );
286         }
287
288         return attributeList.get( attributeId );
289     }
290
291     /**
292      * Get the entry's attributes
293      *
294      * @return An Attributes
295      */

296     public Attributes JavaDoc getAttributes()
297     {
298         if ( isEntry() )
299         {
300             return attributeList;
301         }
302         else
303         {
304             return null;
305         }
306     }
307
308     /**
309      * @return True, if the old RDN should be deleted.
310      */

311     public boolean isDeleteOldRdn()
312     {
313         return deleteOldRdn;
314     }
315
316     /**
317      * Set the flage deleteOldRdn
318      *
319      * @param deleteOldRdn
320      * True if the old RDN should be deleted
321      */

322     public void setDeleteOldRdn( boolean deleteOldRdn )
323     {
324         this.deleteOldRdn = deleteOldRdn;
325     }
326
327     /**
328      * @return The new RDN
329      */

330     public String JavaDoc getNewRdn()
331     {
332         return newRdn;
333     }
334
335     /**
336      * Set the new RDN
337      *
338      * @param newRdn
339      * The new RDN
340      */

341     public void setNewRdn( String JavaDoc newRdn )
342     {
343         this.newRdn = newRdn;
344     }
345
346     /**
347      * @return The new superior
348      */

349     public String JavaDoc getNewSuperior()
350     {
351         return newSuperior;
352     }
353
354     /**
355      * Set the new superior
356      *
357      * @param newSuperior
358      * The new Superior
359      */

360     public void setNewSuperior( String JavaDoc newSuperior )
361     {
362         this.newSuperior = newSuperior;
363     }
364
365     /**
366      * @return True if the entry is an ADD entry
367      */

368     public boolean isChangeAdd()
369     {
370         return changeType == ADD;
371     }
372
373     /**
374      * @return True if the entry is a DELETE entry
375      */

376     public boolean isChangeDelete()
377     {
378         return changeType == DELETE;
379     }
380
381     /**
382      * @return True if the entry is a MODDN entry
383      */

384     public boolean isChangeModDn()
385     {
386         return changeType == MODDN;
387     }
388
389     /**
390      * @return True if the entry is a MODRDN entry
391      */

392     public boolean isChangeModRdn()
393     {
394         return changeType == MODRDN;
395     }
396
397     /**
398      * @return True if the entry is a MODIFY entry
399      */

400     public boolean isChangeModify()
401     {
402         return changeType == MODIFY;
403     }
404
405     public boolean isEntry()
406     {
407         return changeType == ADD;
408     }
409
410     /**
411      * @return The associated control, if any
412      */

413     public Control JavaDoc getControl()
414     {
415         return control;
416     }
417
418     /**
419      * Add a control to the entry
420      *
421      * @param control
422      * The control
423      */

424     public void setControl( Control JavaDoc control )
425     {
426         this.control = control;
427     }
428
429     /**
430      * Dumps the attributes
431      */

432     private String JavaDoc dumpAttributes()
433     {
434         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
435
436         try
437         {
438             for ( NamingEnumeration JavaDoc attrs = attributeList.getAll(); attrs.hasMoreElements(); )
439             {
440                 Attribute JavaDoc attribute = (Attribute JavaDoc) attrs.nextElement();
441
442                 sb.append( " ").append( attribute.getID() ).append( ":\n" );
443
444                 for ( NamingEnumeration JavaDoc values = attribute.getAll(); values.hasMoreElements(); )
445                 {
446                     Object JavaDoc value = values.nextElement();
447
448                     if ( value instanceof String JavaDoc )
449                     {
450                         sb.append( " " ).append( (String JavaDoc)value ).append('\n' );
451                     }
452                     else
453                     {
454                         sb.append( " " ).append( Utils.dumpBytes( (byte[]) value ) ).append('\n' );
455                     }
456                 }
457             }
458         }
459         catch ( NamingException JavaDoc ne )
460         {
461             return "";
462         }
463
464         return sb.toString();
465     }
466
467     /**
468      * Dumps the modifications
469      */

470     private String JavaDoc dumpModificationItems()
471     {
472         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
473
474         for (ModificationItem JavaDoc modif : modificationList) {
475             sb.append(" Operation: ");
476
477             switch (modif.getModificationOp()) {
478                 case DirContext.ADD_ATTRIBUTE :
479                     sb.append("ADD\n");
480                     break;
481
482                 case DirContext.REMOVE_ATTRIBUTE :
483                     sb.append("REMOVE\n");
484                     break;
485
486                 case DirContext.REPLACE_ATTRIBUTE :
487                     sb.append("REPLACE \n");
488                     break;
489             }
490
491             Attribute JavaDoc attribute = modif.getAttribute();
492
493             sb.append(" Attribute: ").append(attribute.getID()).append('\n');
494
495             if (attribute.size() != 0) {
496                 try {
497                     for (NamingEnumeration JavaDoc values = attribute.getAll(); values.hasMoreElements();) {
498                         Object JavaDoc value = values.nextElement();
499
500                         if (value instanceof String JavaDoc) {
501                             sb.append(" ").append((String JavaDoc) value).append('\n');
502                         } else {
503                             sb.append(" ").append(Utils.dumpBytes((byte[]) value)).append('\n');
504                         }
505                     }
506                 }
507                 catch (NamingException JavaDoc ne) {
508                     return "";
509                 }
510             }
511         }
512
513         return sb.toString();
514     }
515
516     /**
517      * Return a String representing the Entry
518      */

519     public String JavaDoc toString()
520     {
521         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
522         sb.append( "Entry : " ).append( dn ).append( '\n' );
523
524         if ( control != null )
525         {
526             sb.append( " Control : " ).append( control ).append( '\n' );
527         }
528
529         switch ( changeType )
530         {
531             case ADD :
532                 sb.append( " Change type is ADD\n" );
533                 sb.append( " Attributes : \n" );
534                 sb.append( dumpAttributes() );
535                 break;
536
537             case MODIFY :
538                 sb.append( " Change type is MODIFY\n" );
539                 sb.append( " Modifications : \n" );
540                 sb.append( dumpModificationItems() );
541                 break;
542
543             case DELETE :
544                 sb.append( " Change type is DELETE\n" );
545                 break;
546
547             case MODDN :
548             case MODRDN :
549                 sb.append( " Change type is ").append( changeType == MODDN ? "MODDN\n" : "MODRDN\n" );
550                 sb.append( " Delete old RDN : " ).append( deleteOldRdn ? "true\n" : "false\n" );
551                 sb.append( " New RDN : " ).append( newRdn ).append( '\n' );
552
553                 if ( newSuperior !=null )
554                 {
555                     sb.append( " New superior : " ).append( newSuperior ).append( '\n' );
556                 }
557
558                 break;
559         }
560
561         return sb.toString();
562     }
563 }
564
Popular Tags