KickJava   Java API By Example, From Geeks To Geeks.

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


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.ldif;
22
23
24 import java.io.Serializable JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeRecord;
31 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
32 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
33 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifModSpec;
34 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord;
35
36
37 public class LdifFile implements Serializable JavaDoc
38 {
39
40     private static final long serialVersionUID = 846864138240517008L;
41
42     private List JavaDoc containerList;
43
44
45     public LdifFile()
46     {
47         super();
48         this.containerList = new ArrayList JavaDoc( 1 );
49     }
50
51
52     public boolean isContentType()
53     {
54         for ( Iterator JavaDoc it = this.containerList.iterator(); it.hasNext(); )
55         {
56             Object JavaDoc o = it.next();
57             if ( o instanceof LdifRecord )
58             {
59                 return o instanceof LdifContentRecord;
60             }
61         }
62         return false;
63     }
64
65
66     public boolean isChangeType()
67     {
68         for ( Iterator JavaDoc it = this.containerList.iterator(); it.hasNext(); )
69         {
70             Object JavaDoc o = it.next();
71             if ( o instanceof LdifRecord )
72             {
73                 return o instanceof LdifChangeRecord;
74             }
75         }
76         return false;
77     }
78
79
80     public void addContainer( LdifContainer container )
81     {
82         this.containerList.add( container );
83     }
84
85
86     /**
87      *
88      * @return all container, includes version, comments, records and
89      * unknown
90      */

91     public LdifContainer[] getContainers()
92     {
93         return ( LdifContainer[] ) this.containerList.toArray( new LdifContainer[this.containerList.size()] );
94     }
95
96
97     /**
98      *
99      * @return only records (even invalid), no version, comments, and
100      * unknown
101      */

102     public LdifRecord[] getRecords()
103     {
104         List JavaDoc l = new ArrayList JavaDoc();
105         for ( Iterator JavaDoc it = this.containerList.iterator(); it.hasNext(); )
106         {
107             Object JavaDoc o = it.next();
108             if ( o instanceof LdifRecord )
109             {
110                 l.add( o );
111             }
112         }
113         return ( LdifRecord[] ) l.toArray( new LdifRecord[l.size()] );
114     }
115
116
117     /**
118      *
119      * @return the last container or null
120      */

121     public LdifContainer getLastContainer()
122     {
123         if ( this.containerList.isEmpty() )
124         {
125             return null;
126         }
127         else
128         {
129             return ( LdifContainer ) this.containerList.get( this.containerList.size() - 1 );
130         }
131     }
132
133
134     public String JavaDoc toRawString()
135     {
136         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
137
138         LdifContainer[] containers = this.getContainers();
139         for ( int i = 0; i < containers.length; i++ )
140         {
141             sb.append( containers[i].toRawString() );
142         }
143
144         return sb.toString();
145     }
146
147
148     public String JavaDoc toFormattedString()
149     {
150         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
151
152         LdifContainer[] containers = this.getContainers();
153         for ( int i = 0; i < containers.length; i++ )
154         {
155             sb.append( containers[i].toFormattedString() );
156         }
157
158         return sb.toString();
159     }
160
161
162     public String JavaDoc toString()
163     {
164         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
165
166         LdifContainer[] containers = this.getContainers();
167         for ( int i = 0; i < containers.length; i++ )
168         {
169             sb.append( containers[i].toString() );
170         }
171
172         return sb.toString();
173     }
174
175
176     public static LdifContainer getContainer( LdifFile model, int offset )
177     {
178         if ( model == null || offset < 0 )
179             return null;
180
181         LdifContainer container = null;
182         LdifContainer[] containers = model.getContainers();
183         if ( containers.length > 0 )
184         {
185             for ( int i = 0; i < containers.length; i++ )
186             {
187                 if ( containers[i].getOffset() <= offset
188                     && offset < containers[i].getOffset() + containers[i].getLength() )
189                 {
190                     container = containers[i];
191                     break;
192                 }
193             }
194         }
195         return container;
196     }
197
198
199     public static LdifModSpec getInnerContainer( LdifContainer container, int offset )
200     {
201         if ( container == null || offset < container.getOffset()
202             || offset > container.getOffset() + container.getLength() )
203             return null;
204
205         LdifModSpec innerContainer = null;
206         LdifPart[] parts = container.getParts();
207         if ( parts.length > 0 )
208         {
209             int partIndex = -1;
210
211             for ( int i = 0; i < parts.length; i++ )
212             {
213                 int start = parts[i].getOffset();
214                 int end = parts[i].getOffset() + parts[i].getLength();
215                 if ( start <= offset && offset < end )
216                 {
217                     partIndex = i;
218                     break;
219                 }
220             }
221
222             if ( partIndex > -1 )
223             {
224                 if ( parts[partIndex] instanceof LdifModSpec )
225                 {
226                     innerContainer = ( LdifModSpec ) parts[partIndex];
227                 }
228             }
229         }
230         return innerContainer;
231     }
232
233
234     public static LdifContainer[] getContainers( LdifFile model, int offset, int length )
235     {
236         if ( model == null || offset < 0 )
237             return null;
238
239         ArrayList JavaDoc containerList = new ArrayList JavaDoc();
240
241         LdifContainer[] containers = model.getContainers();
242         if ( containers.length > 0 )
243         {
244             for ( int i = 0; i < containers.length; i++ )
245             {
246                 if ( offset < containers[i].getOffset() + containers[i].getLength()
247                     && offset + length > containers[i].getOffset() )
248                 {
249                     containerList.add( containers[i] );
250                 }
251             }
252         }
253
254         return ( LdifContainer[] ) containerList.toArray( new LdifContainer[containerList.size()] );
255     }
256
257
258     public static LdifPart[] getParts( LdifFile model, int offset, int length )
259     {
260         if ( model == null || offset < 0 )
261             return null;
262
263         LdifContainer[] containers = model.getContainers();
264         return getParts( containers, offset, length );
265
266     }
267
268
269     public static LdifPart[] getParts( LdifContainer[] containers, int offset, int length )
270     {
271         if ( containers == null || offset < 0 )
272             return null;
273
274         ArrayList JavaDoc partList = new ArrayList JavaDoc();
275
276         for ( int i = 0; i < containers.length; i++ )
277         {
278             if ( offset < containers[i].getOffset() + containers[i].getLength()
279                 && offset + length >= containers[i].getOffset() )
280             {
281                 LdifPart[] parts = containers[i].getParts();
282                 if ( parts.length > 0 )
283                 {
284                     for ( int p = 0; p < parts.length; p++ )
285                     {
286                         if ( offset < parts[p].getOffset() + parts[p].getLength()
287                             && offset + length >= parts[p].getOffset() )
288                         {
289                             LdifPart part = parts[p];
290
291                             if ( part instanceof LdifModSpec )
292                             {
293                                 LdifModSpec spec = ( LdifModSpec ) part;
294                                 partList.addAll( Arrays.asList( getParts( new LdifContainer[]
295                                     { spec }, offset, length ) ) );
296                             }
297                             else
298                             {
299                                 if ( part instanceof LdifInvalidPart && p > 0 )
300                                 {
301                                     part = parts[p - 1];
302                                 }
303
304                                 partList.add( part );
305                             }
306                         }
307                     }
308                 }
309             }
310         }
311
312         return ( LdifPart[] ) partList.toArray( new LdifPart[partList.size()] );
313     }
314
315
316     public static LdifPart getContainerContent( LdifContainer container, int offset )
317     {
318         if ( container == null || offset < container.getOffset()
319             || offset > container.getOffset() + container.getLength() )
320             return null;
321
322         LdifPart part = null;
323         LdifPart[] parts = container.getParts();
324         if ( parts.length > 0 )
325         {
326             int partIndex = -1;
327
328             for ( int i = 0; i < parts.length; i++ )
329             {
330                 int start = parts[i].getOffset();
331                 int end = parts[i].getOffset() + parts[i].getLength();
332                 if ( start <= offset && offset < end )
333                 {
334                     partIndex = i;
335                     break;
336                 }
337             }
338
339             if ( partIndex > -1 )
340             {
341                 part = parts[partIndex];
342                 if ( part instanceof LdifModSpec )
343                 {
344                     part = getContainerContent( ( LdifModSpec ) part, offset );
345                 }
346                 // if(part instanceof LdifInvalidPart && partIndex > 0) {
347
// partIndex--;
348
// part = parts[partIndex];
349
// }
350
}
351         }
352         return part;
353     }
354
355
356     // public static LdifPart getPart(LdifContainer container, int offset) {
357
// if(container == null || offset < 0)
358
// return null;
359
//
360
// LdifPart part = null;
361
// LdifPart[] parts = container.getParts();
362
// if(parts.length > 0) {
363
// int partIndex = -1;
364
//
365
// for (int i=0; i<parts.length; i++) {
366
// int start = parts[i].getOffset();
367
// int end = parts[i].getOffset()+parts[i].getLength();
368
// if(start <= offset && offset < end) {
369
// partIndex = i;
370
// break;
371
// }
372
// }
373
//
374
// if(partIndex > -1) {
375
// part = parts[partIndex];
376
//
377
// if(part instanceof LdifUnknownPart && partIndex > 0) {
378
// partIndex--;
379
// part = parts[partIndex];
380
// }
381
//
382
// if(part instanceof LdifContainer) {
383
// part = getPart((LdifContainer)part, offset);
384
// }
385
// }
386
// }
387
// return part;
388
// }
389

390     public void replace( LdifContainer[] oldContainers, LdifContainer[] newContainers )
391     {
392
393         // find index
394
int index = 0;
395         if ( oldContainers.length > 0 )
396         {
397             index = this.containerList.indexOf( oldContainers[0] );
398         }
399
400         // remove old containers
401
int removeLength = 0;
402         int removeOffset = 0;
403         if ( oldContainers.length > 0 )
404         {
405             removeOffset = oldContainers[0].getOffset();
406             for ( int i = 0; i < oldContainers.length; i++ )
407             {
408                 this.containerList.remove( index );
409                 removeLength += oldContainers[i].getLength();
410             }
411         }
412
413         // add new containers
414
int insertLength = 0;
415         for ( int i = 0; i < newContainers.length; i++ )
416         {
417             newContainers[i].adjustOffset( removeOffset );
418             insertLength += newContainers[i].getLength();
419             this.containerList.add( index + i, newContainers[i] );
420         }
421
422         // adjust offset of folling containers
423
int adjust = insertLength - removeLength;
424         for ( int i = index + newContainers.length; i < this.containerList.size(); i++ )
425         {
426             LdifContainer container = ( LdifContainer ) this.containerList.get( i );
427             container.adjustOffset( adjust );
428         }
429
430     }
431
432 }
433
Popular Tags