KickJava   Java API By Example, From Geeks To Geeks.

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


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.parser;
22
23
24 import java.io.Reader JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.apache.directory.ldapstudio.browser.core.internal.model.ConnectionException;
32 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEOFPart;
33 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEnumeration;
34 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile;
35 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifInvalidPart;
36 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeAddRecord;
37 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeDeleteRecord;
38 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeModDnRecord;
39 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeModifyRecord;
40 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeRecord;
41 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifCommentContainer;
42 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
43 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
44 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifEOFContainer;
45 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifInvalidContainer;
46 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifModSpec;
47 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord;
48 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifSepContainer;
49 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifVersionContainer;
50 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine;
51 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifChangeTypeLine;
52 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifCommentLine;
53 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifControlLine;
54 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDeloldrdnLine;
55 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDnLine;
56 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifModSpecSepLine;
57 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifModSpecTypeLine;
58 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifNewrdnLine;
59 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifNewsuperiorLine;
60 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifSepLine;
61 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifVersionLine;
62
63
64 public class LdifParser
65 {
66
67     private LdifScanner scanner;
68
69
70     public LdifParser()
71     {
72         this.scanner = new LdifScanner();
73     }
74
75
76     public LdifFile parse( String JavaDoc ldif )
77     {
78
79         LdifFile model = new LdifFile();
80
81         try
82         {
83             LdifEnumeration enumeration = this.parse( new StringReader JavaDoc( ldif ) );
84             while ( enumeration.hasNext( null ) )
85             {
86                 LdifContainer container = enumeration.next( null );
87                 model.addContainer( container );
88             }
89         }
90         catch ( ConnectionException e )
91         {
92             // doesn't occur on the internal LdifEnumeration implementation
93
}
94
95         return model;
96     }
97
98
99     public LdifEnumeration parse( Reader JavaDoc ldifReader )
100     {
101
102         this.scanner.setLdif( ldifReader );
103
104         LdifEnumeration enumeration = new LdifEnumeration()
105         {
106
107             private List JavaDoc containerList = new ArrayList JavaDoc();
108
109             private boolean headerParsed = false;
110
111             private boolean bodyParsed = false;
112
113             private boolean footerParsed = false;
114
115
116             public boolean hasNext( org.apache.directory.ldapstudio.browser.core.jobs.ExtendedProgressMonitor monitor )
117             {
118                 if ( containerList.isEmpty() )
119                 {
120
121                     LdifFile model = new LdifFile();
122
123                     // parse header
124
if ( !headerParsed )
125                     {
126                         checkAndParseComment( model );
127                         checkAndParseVersion( model );
128                         checkAndParseComment( model );
129                         headerParsed = true;
130                     }
131
132                     // parse body (in a loop)
133
if ( headerParsed && !bodyParsed )
134                     {
135                         // parse comment lines
136
if ( !checkAndParseComment( model ) )
137                         {
138                             // parse record
139
if ( !checkAndParseRecord( model ) )
140                             {
141                                 // parse unknown
142
if ( !checkAndParseOther( model ) )
143                                 {
144                                     // end of body
145
bodyParsed = true;
146                                 }
147                             }
148                         }
149                     }
150
151                     // parse footer
152
if ( headerParsed && bodyParsed && !footerParsed )
153                     {
154                         checkAndParseComment( model );
155                         footerParsed = true;
156                     }
157
158                     LdifContainer[] containers = model.getContainers();
159                     this.containerList.addAll( Arrays.asList( containers ) );
160                     return !containerList.isEmpty() && !( containers[0] instanceof LdifEOFContainer );
161
162                 }
163                 else
164                 {
165                     return true;
166                 }
167             }
168
169
170             public LdifContainer next( org.apache.directory.ldapstudio.browser.core.jobs.ExtendedProgressMonitor monitor )
171             {
172                 if ( hasNext( monitor ) )
173                 {
174                     return ( LdifContainer ) this.containerList.remove( 0 );
175                 }
176                 else
177                 {
178                     return null;
179                 }
180             }
181         };
182
183         return enumeration;
184     }
185
186
187     // public LdifEnumeration parse(Reader ldifReader) {
188
//
189
// this.scanner.setLdif(ldifReader);
190
//
191
// LdifEnumeration enumeration = new LdifEnumeration(){
192
//
193
// private List containerList = new ArrayList();
194
//
195
// public boolean
196
// hasNext(org.apache.directory.ldapstudio.browser.core.jobs.ExtendedProgressMonitor
197
// monitor) {
198
// if(containerList.isEmpty()) {
199
// LdifFile model = parseFile();
200
// LdifContainer[] containers = model.getContainers();
201
// this.containerList.addAll(Arrays.asList(containers));
202
// return !containerList.isEmpty() && !(containers[0] instanceof
203
// LdifEOFContainer);
204
// }
205
// else {
206
// return true;
207
// }
208
// }
209
//
210
// public LdifContainer
211
// next(org.apache.directory.ldapstudio.browser.core.jobs.ExtendedProgressMonitor
212
// monitor) {
213
// if(hasNext(monitor)) {
214
// return (LdifContainer)this.containerList.remove(0);
215
// }
216
// else {
217
// return null;
218
// }
219
// }
220
// };
221
//
222
// return enumeration;
223
// }
224
//
225
// private LdifFile parseFile() {
226
//
227
// LdifFile model = new LdifFile();
228
//
229
// // start comment-version-comment
230
// checkAndParseComment(model);
231
// checkAndParseVersion(model);
232
// checkAndParseComment(model);
233
//
234
// parseRecords(model);
235
//
236
// checkAndParseComment(model);
237
//
238
// return model;
239
// }
240
//
241
// private void parseRecords(LdifFile model) {
242
// do {
243
// // parse comment lines
244
// if(!checkAndParseComment(model)) {
245
// // parse record
246
// if(!checkAndParseRecord(model)) {
247
// // parse unknown
248
// if(!checkAndParseOther(model)) {
249
// return;
250
// }
251
// }
252
// }
253
// }
254
// while(true);
255
//
256
// }
257

258     /**
259      * Checks for version line. If version line is present it is parsed and
260      * added to the given model.
261      *
262      * @param model
263      * the model
264      * @return true if version line was added to the model, false otherwise
265      */

266     private boolean checkAndParseRecord( LdifFile model )
267     {
268
269         // record starts with dn-spec
270
LdifToken dnSpecToken = this.scanner.matchDnSpec();
271         if ( dnSpecToken == null )
272         {
273             return false;
274         }
275
276         // get DN
277
LdifToken dnValueTypeToken = null;
278         LdifToken dnToken = null;
279         LdifToken dnSepToken = null;
280         dnValueTypeToken = this.scanner.matchValueType();
281         if ( dnValueTypeToken != null )
282         {
283             dnToken = this.scanner.matchValue();
284             if ( dnToken != null )
285             {
286                 dnSepToken = this.scanner.matchSep();
287             }
288         }
289         LdifDnLine dnLine = new LdifDnLine( dnSpecToken.getOffset(), getValueOrNull( dnSpecToken ),
290             getValueOrNull( dnValueTypeToken ), getValueOrNull( dnToken ), getValueOrNull( dnSepToken ) );
291         LdifToken dnErrorToken = null;
292         if ( dnSepToken == null )
293         {
294             dnErrorToken = this.scanner.matchCleanupLine();
295         }
296
297         // save comment lines after dns
298
LdifCommentLine[] commentLines = getCommentLines();
299
300         // check record type: to decide the record type we need the next token
301
// first check keywords 'control' and 'changetype'
302
LdifControlLine controlLine = getControlLine();
303         LdifChangeTypeLine changeTypeLine = getChangeTypeLine();
304         if ( controlLine != null || changeTypeLine != null )
305         {
306
307             LdifChangeRecord record = null;
308
309             // save all parts before changetype line
310
List JavaDoc partList = new ArrayList JavaDoc();
311             if ( dnErrorToken != null )
312             {
313                 partList.add( new LdifInvalidPart( dnErrorToken.getOffset(), dnErrorToken.getValue() ) );
314             }
315             for ( int i = 0; i < commentLines.length; i++ )
316             {
317                 partList.add( commentLines[i] );
318             }
319             if ( controlLine != null )
320             {
321                 partList.add( controlLine );
322                 if ( !controlLine.isValid() )
323                 {
324                     LdifToken errorToken = this.cleanupLine();
325                     if ( errorToken != null )
326                     {
327                         partList.add( new LdifInvalidPart( errorToken.getOffset(), errorToken.getValue() ) );
328                     }
329                 }
330             }
331
332             // save comments and controls before changetype line
333
while ( changeTypeLine == null && ( commentLines.length > 0 || controlLine != null ) )
334             {
335
336                 commentLines = getCommentLines();
337                 for ( int i = 0; i < commentLines.length; i++ )
338                 {
339                     partList.add( commentLines[i] );
340                 }
341
342                 controlLine = getControlLine();
343                 if ( controlLine != null )
344                 {
345                     partList.add( controlLine );
346                     if ( !controlLine.isValid() )
347                     {
348                         LdifToken errorToken = this.cleanupLine();
349                         if ( errorToken != null )
350                         {
351                             partList.add( new LdifInvalidPart( errorToken.getOffset(), errorToken.getValue() ) );
352                         }
353                     }
354                 }
355
356                 changeTypeLine = getChangeTypeLine();
357             }
358
359             if ( changeTypeLine != null )
360             {
361
362                 if ( changeTypeLine.isAdd() )
363                 {
364                     record = new LdifChangeAddRecord( dnLine );
365                     append( record, partList );
366                     record.setChangeType( changeTypeLine );
367                     if ( !changeTypeLine.isValid() )
368                     {
369                         this.cleanupLine( record );
370                     }
371                     parseAttrValRecord( record );
372                 }
373                 else if ( changeTypeLine.isDelete() )
374                 {
375                     record = new LdifChangeDeleteRecord( dnLine );
376                     append( record, partList );
377                     record.setChangeType( changeTypeLine );
378                     if ( !changeTypeLine.isValid() )
379                     {
380                         this.cleanupLine( record );
381                     }
382                     parseChangeDeleteRecord( record );
383                 }
384                 else if ( changeTypeLine.isModify() )
385                 {
386                     record = new LdifChangeModifyRecord( dnLine );
387                     append( record, partList );
388                     record.setChangeType( changeTypeLine );
389                     if ( !changeTypeLine.isValid() )
390                     {
391                         this.cleanupLine( record );
392                     }
393                     parseChangeModifyRecord( ( LdifChangeModifyRecord ) record );
394                 }
395                 else if ( changeTypeLine.isModDn() )
396                 {
397                     record = new LdifChangeModDnRecord( dnLine );
398                     append( record, partList );
399                     record.setChangeType( changeTypeLine );
400                     if ( !changeTypeLine.isValid() )
401                     {
402                         this.cleanupLine( record );
403                     }
404                     parseChangeModDnRecord( ( LdifChangeModDnRecord ) record );
405                 }
406                 else
407                 {
408                     record = new LdifChangeRecord( dnLine );
409                     append( record, partList );
410                     record.setChangeType( changeTypeLine );
411                     if ( !changeTypeLine.isValid() )
412                     {
413                         this.cleanupLine( record );
414                     }
415                 }
416             }
417             else
418             {
419                 record = new LdifChangeRecord( dnLine );
420                 append( record, partList );
421             }
422
423             model.addContainer( record );
424         }
425         else
426         {
427             // match attr-val-record
428
LdifContentRecord record = new LdifContentRecord( dnLine );
429             if ( dnErrorToken != null )
430             {
431                 record.addInvalid( new LdifInvalidPart( dnErrorToken.getOffset(), dnErrorToken.getValue() ) );
432             }
433             for ( int i = 0; i < commentLines.length; i++ )
434             {
435                 record.addComment( commentLines[i] );
436             }
437             parseAttrValRecord( record );
438             model.addContainer( record );
439         }
440
441         return true;
442     }
443
444
445     private void append( LdifChangeRecord record, List JavaDoc partList )
446     {
447         for ( Iterator JavaDoc it = partList.iterator(); it.hasNext(); )
448         {
449             Object JavaDoc o = it.next();
450             if ( o instanceof LdifCommentLine )
451                 record.addComment( ( LdifCommentLine ) o );
452             if ( o instanceof LdifControlLine )
453                 record.addControl( ( LdifControlLine ) o );
454             if ( o instanceof LdifInvalidPart )
455                 record.addInvalid( ( LdifInvalidPart ) o );
456         }
457     }
458
459
460     private void parseChangeDeleteRecord( LdifRecord record )
461     {
462         do
463         {
464             if ( checkAndParseEndOfRecord( record ) )
465             {
466                 return;
467             }
468
469             if ( !checkAndParseComment( record ) && !checkAndParseOther( record ) )
470             {
471                 return;
472             }
473         }
474         while ( true );
475     }
476
477
478     private void parseChangeModDnRecord( LdifChangeModDnRecord record )
479     {
480         // do {
481
if ( checkAndParseEndOfRecord( record ) )
482         {
483             return;
484         }
485
486         // comments
487
checkAndParseComment( record );
488
489         // read newrdn line
490
LdifToken newrdnSpecToken = this.scanner.matchNewrdnSpec();
491         if ( newrdnSpecToken != null )
492         {
493             LdifToken newrdnValueTypeToken = this.scanner.matchValueType();
494             LdifToken newrdnValueToken = this.scanner.matchValue();
495             LdifToken newrdnSepToken = null;
496             if ( newrdnValueTypeToken != null || newrdnValueToken != null )
497             {
498                 newrdnSepToken = this.scanner.matchSep();
499             }
500
501             LdifNewrdnLine newrdnLine = new LdifNewrdnLine( newrdnSpecToken.getOffset(),
502                 getValueOrNull( newrdnSpecToken ), getValueOrNull( newrdnValueTypeToken ),
503                 getValueOrNull( newrdnValueToken ), getValueOrNull( newrdnSepToken ) );
504             record.setNewrdn( newrdnLine );
505
506             if ( newrdnSepToken == null )
507             {
508                 this.cleanupLine( record );
509             }
510         }
511
512         if ( newrdnSpecToken == null )
513         {
514             if ( !checkAndParseComment( record ) && !checkAndParseOther( record ) )
515             {
516                 return;
517             }
518         }
519
520         // comments
521
checkAndParseComment( record );
522
523         // read deleteoldrdnline
524
LdifToken deleteoldrdnSpecToken = this.scanner.matchDeleteoldrdnSpec();
525         if ( deleteoldrdnSpecToken != null )
526         {
527             LdifToken deleteoldrdnValueTypeToken = this.scanner.matchValueType();
528             LdifToken deleteoldrdnValueToken = this.scanner.matchValue();
529             LdifToken deleteoldrdnSepToken = null;
530             if ( deleteoldrdnValueTypeToken != null || deleteoldrdnValueToken != null )
531             {
532                 deleteoldrdnSepToken = this.scanner.matchSep();
533             }
534
535             LdifDeloldrdnLine deloldrdnLine = new LdifDeloldrdnLine( deleteoldrdnSpecToken.getOffset(),
536                 getValueOrNull( deleteoldrdnSpecToken ), getValueOrNull( deleteoldrdnValueTypeToken ),
537                 getValueOrNull( deleteoldrdnValueToken ), getValueOrNull( deleteoldrdnSepToken ) );
538             record.setDeloldrdn( deloldrdnLine );
539
540             if ( deleteoldrdnSepToken == null )
541             {
542                 this.cleanupLine( record );
543             }
544         }
545
546         if ( deleteoldrdnSpecToken == null )
547         {
548             if ( !checkAndParseComment( record ) && !checkAndParseOther( record ) )
549             {
550                 return;
551             }
552         }
553
554         // comments
555
checkAndParseComment( record );
556
557         // read newsuperior line
558
LdifToken newsuperiorSpecToken = this.scanner.matchNewsuperiorSpec();
559         if ( newsuperiorSpecToken != null )
560         {
561             LdifToken newsuperiorValueTypeToken = this.scanner.matchValueType();
562             LdifToken newsuperiorValueToken = this.scanner.matchValue();
563             LdifToken newsuperiorSepToken = null;
564             if ( newsuperiorValueTypeToken != null || newsuperiorValueToken != null )
565             {
566                 newsuperiorSepToken = this.scanner.matchSep();
567             }
568
569             LdifNewsuperiorLine newsuperiorLine = new LdifNewsuperiorLine( newsuperiorSpecToken.getOffset(),
570                 getValueOrNull( newsuperiorSpecToken ), getValueOrNull( newsuperiorValueTypeToken ),
571                 getValueOrNull( newsuperiorValueToken ), getValueOrNull( newsuperiorSepToken ) );
572             record.setNewsuperior( newsuperiorLine );
573
574             if ( newsuperiorSepToken == null )
575             {
576                 this.cleanupLine( record );
577             }
578         }
579
580         // comments
581
checkAndParseComment( record );
582
583         // eor
584
checkAndParseEndOfRecord( record );
585
586         // comments
587
// checkAndParseComment(record);
588

589         // }
590
// while(true);
591
}
592
593
594     private void parseChangeModifyRecord( LdifChangeModifyRecord record )
595     {
596
597         do
598         {
599             if ( checkAndParseEndOfRecord( record ) )
600             {
601                 return;
602             }
603
604             // match mod type
605
LdifToken modSpecTypeSpecToken = this.scanner.matchModTypeSpec();
606             if ( modSpecTypeSpecToken != null )
607             {
608                 // read mod type line
609
LdifToken modSpecTypeValueTypeToken = null;
610                 LdifToken modSpecTypeAttributeDescriptionToken = null;
611                 LdifToken sepToken = null;
612                 modSpecTypeValueTypeToken = this.scanner.matchValueType();
613                 if ( modSpecTypeValueTypeToken != null )
614                 {
615                     modSpecTypeAttributeDescriptionToken = this.scanner.matchAttributeDescription();
616                     if ( modSpecTypeAttributeDescriptionToken != null )
617                     {
618                         sepToken = this.scanner.matchSep();
619                     }
620                 }
621                 LdifModSpecTypeLine modSpecTypeLine = new LdifModSpecTypeLine( modSpecTypeSpecToken.getOffset(),
622                     getValueOrNull( modSpecTypeSpecToken ), getValueOrNull( modSpecTypeValueTypeToken ),
623                     getValueOrNull( modSpecTypeAttributeDescriptionToken ), getValueOrNull( sepToken ) );
624                 LdifModSpec modSpec = new LdifModSpec( modSpecTypeLine );
625                 record.addModSpec( modSpec );
626
627                 // clean line
628
if ( sepToken == null )
629                 {
630                     this.cleanupLine( modSpec );
631                 }
632
633                 // comment
634
checkAndParseComment( record );
635
636                 // read attr-val lines
637
do
638                 {
639                     LdifAttrValLine line = this.getAttrValLine();
640                     if ( line != null )
641                     {
642                         modSpec.addAttrVal( line );
643
644                         // clean line
645
if ( "".equals( line.getRawNewLine() ) )
646                         {
647                             this.cleanupLine( record );
648                         }
649                     }
650                     else
651                     {
652                         if ( !checkAndParseComment( record ) )
653                         {
654                             break;
655                         }
656                     }
657                 }
658                 while ( true );
659
660                 // comments
661
checkAndParseComment( record );
662
663                 // read sep line
664
LdifToken modSpecSepToken = this.scanner.matchModSep();
665                 if ( modSpecSepToken != null )
666                 {
667                     LdifToken modSpecSepSepToken = this.scanner.matchSep();
668                     LdifModSpecSepLine modSpecSepLine = new LdifModSpecSepLine( modSpecSepToken.getOffset(),
669                         getValueOrNull( modSpecSepToken ), getValueOrNull( modSpecSepSepToken ) );
670                     modSpec.finish( modSpecSepLine );
671                 }
672             }
673
674             if ( modSpecTypeSpecToken == null )
675             {
676                 if ( !checkAndParseComment( record ) && !checkAndParseOther( record ) )
677                 {
678                     return;
679                 }
680             }
681         }
682         while ( true );
683     }
684
685
686     private void parseAttrValRecord( LdifRecord record )
687     {
688
689         do
690         {
691             if ( checkAndParseEndOfRecord( record ) )
692             {
693                 return;
694             }
695
696             // check attr-val line
697
LdifAttrValLine line = this.getAttrValLine();
698             if ( line != null )
699             {
700                 if ( record instanceof LdifContentRecord )
701                 {
702                     ( ( LdifContentRecord ) record ).addAttrVal( line );
703                 }
704                 else if ( record instanceof LdifChangeAddRecord )
705                 {
706                     ( ( LdifChangeAddRecord ) record ).addAttrVal( line );
707                 }
708
709                 // clean line
710
if ( "".equals( line.getRawNewLine() ) )
711                 {
712                     this.cleanupLine( record );
713                 }
714             }
715             else
716             {
717                 if ( !checkAndParseComment( record ) && !checkAndParseOther( record ) )
718                 {
719                     return;
720                 }
721             }
722
723             //
724
// // check comment line
725
// if(lineStartToken == null) {
726
// lineStartToken = this.scanner.matchComment();
727
// if(lineStartToken != null) {
728
// LdifToken sepToken = this.scanner.matchSep();
729
// record.addComment(new
730
// LdifCommentLine(lineStartToken.getOffset(),
731
// getValueOrNull(lineStartToken), getValueOrNull(sepToken)));
732
// }
733
// }
734
//
735
// // unknown line
736
// if(lineStartToken == null) {
737
// lineStartToken = this.scanner.matchOther();
738
// if(lineStartToken != null) {
739
// record.addOther(new
740
// LdifInvalidPart(lineStartToken.getOffset(),
741
// lineStartToken.getValue()));
742
// }
743
// }
744

745             // // end of file
746
// if(lineStartToken == null) {
747
// return;
748
// }
749
}
750         while ( true );
751     }
752
753
754     private boolean checkAndParseEndOfRecord( LdifRecord record )
755     {
756         // check end of record
757
LdifToken eorSepToken = this.scanner.matchSep();
758         if ( eorSepToken != null )
759         {
760             record.finish( new LdifSepLine( eorSepToken.getOffset(), getValueOrNull( eorSepToken ) ) );
761             return true;
762         }
763
764         // check end of file
765
LdifToken eofToken = this.scanner.matchEOF();
766         if ( eofToken != null )
767         {
768             record.finish( new LdifEOFPart( eofToken.getOffset() ) );
769             return true;
770         }
771         return false;
772     }
773
774
775     private boolean checkAndParseComment( LdifRecord record )
776     {
777         LdifToken commentToken = this.scanner.matchComment();
778         if ( commentToken != null )
779         {
780             LdifToken sepToken = this.scanner.matchSep();
781             record.addComment( new LdifCommentLine( commentToken.getOffset(), getValueOrNull( commentToken ),
782                 getValueOrNull( sepToken ) ) );
783             return true;
784         }
785         else
786         {
787             return false;
788         }
789     }
790
791
792     private boolean checkAndParseOther( LdifRecord record )
793     {
794         LdifToken otherToken = this.scanner.matchOther();
795         if ( otherToken != null )
796         {
797             record.addInvalid( new LdifInvalidPart( otherToken.getOffset(), otherToken.getValue() ) );
798             return true;
799         }
800         else
801         {
802             return false;
803         }
804     }
805
806
807     /**
808      * Checks for version line. If version line is present it is parsed and
809      * added to the given model.
810      *
811      * @param model
812      * the model
813      * @return true if version line was added to the model, false otherwise
814      */

815     private boolean checkAndParseVersion( LdifFile model )
816     {
817         LdifToken versionSpecToken = this.scanner.matchVersionSpec();
818         if ( versionSpecToken != null )
819         {
820
821             LdifToken versionTypeToken = null;
822             LdifToken versionToken = null;
823             LdifToken sepToken = null;
824             versionTypeToken = this.scanner.matchValueType();
825             if ( versionTypeToken != null )
826             {
827                 versionToken = this.scanner.matchNumber();
828                 if ( versionToken != null )
829                 {
830                     sepToken = this.scanner.matchSep();
831                 }
832             }
833
834             LdifVersionContainer container = new LdifVersionContainer( new LdifVersionLine( versionSpecToken
835                 .getOffset(), getValueOrNull( versionSpecToken ), getValueOrNull( versionTypeToken ),
836                 getValueOrNull( versionToken ), getValueOrNull( sepToken ) ) );
837             model.addContainer( container );
838
839             // clean line
840
if ( sepToken == null )
841             {
842                 this.cleanupLine( container );
843             }
844
845             return true;
846         }
847         else
848         {
849             return false;
850         }
851     }
852
853
854     /**
855      * Checks for comment lines or empty lines. If such lines are present
856      * they are parsed and added to the given model.
857      *
858      * @param model
859      * the model
860      * @return true if comment or empty lines were added to the model, false
861      * otherwise
862      */

863     private boolean checkAndParseComment( LdifFile model )
864     {
865         LdifToken sepToken = this.scanner.matchSep();
866         LdifToken commentToken = this.scanner.matchComment();
867
868         if ( sepToken != null || commentToken != null )
869         {
870
871             while ( sepToken != null || commentToken != null )
872             {
873
874                 if ( sepToken != null )
875                 {
876                     LdifSepLine sepLine = new LdifSepLine( sepToken.getOffset(), getValueOrNull( sepToken ) );
877                     LdifSepContainer sepContainer = new LdifSepContainer( sepLine );
878                     model.addContainer( sepContainer );
879                 }
880
881                 if ( commentToken != null )
882                 {
883                     LdifCommentContainer commentContainer = null;
884                     while ( commentToken != null )
885                     {
886                         LdifToken commentSepToken = this.scanner.matchSep();
887                         LdifCommentLine commentLine = new LdifCommentLine( commentToken.getOffset(),
888                             getValueOrNull( commentToken ), getValueOrNull( commentSepToken ) );
889
890                         if ( commentContainer == null )
891                         {
892                             commentContainer = new LdifCommentContainer( commentLine );
893                         }
894                         else
895                         {
896                             commentContainer.addComment( commentLine );
897                         }
898
899                         commentToken = this.scanner.matchComment();
900                     }
901                     model.addContainer( commentContainer );
902                 }
903
904                 sepToken = this.scanner.matchSep();
905                 commentToken = this.scanner.matchComment();
906             }
907
908             return true;
909         }
910         else
911         {
912             return false;
913         }
914     }
915
916
917     /**
918      * Checks for other line. If such line is present it is parsed and added
919      * to the given model.
920      *
921      * @param model
922      * the model
923      * @return always true except if EOF reached
924      */

925     private boolean checkAndParseOther( LdifFile model )
926     {
927         LdifToken token = this.scanner.matchOther();
928         if ( token != null )
929         {
930             LdifInvalidPart unknownLine = new LdifInvalidPart( token.getOffset(), getValueOrNull( token ) );
931             LdifInvalidContainer otherContainer = new LdifInvalidContainer( unknownLine );
932             model.addContainer( otherContainer );
933             return true;
934         }
935         else
936         {
937             return false;
938         }
939     }
940
941
942     private LdifControlLine getControlLine()
943     {
944
945         LdifToken controlSpecToken = this.scanner.matchControlSpec();
946         if ( controlSpecToken != null )
947         {
948             LdifToken controlTypeToken = null;
949             LdifToken oidToken = null;
950             LdifToken criticalityToken = null;
951             LdifToken valueTypeToken = null;
952             LdifToken valueToken = null;
953             LdifToken sepToken = null;
954             controlTypeToken = this.scanner.matchValueType();
955             if ( controlTypeToken != null )
956             {
957                 oidToken = this.scanner.matchOid();
958                 if ( oidToken != null )
959                 {
960                     criticalityToken = this.scanner.matchCriticality();
961                     valueTypeToken = this.scanner.matchValueType();
962                     if ( valueTypeToken != null )
963                     {
964                         valueToken = this.scanner.matchValue();
965                     }
966                     sepToken = this.scanner.matchSep();
967                 }
968             }
969
970             LdifControlLine controlLine = new LdifControlLine( controlSpecToken.getOffset(),
971                 getValueOrNull( controlSpecToken ), getValueOrNull( controlTypeToken ), getValueOrNull( oidToken ),
972                 getValueOrNull( criticalityToken ), getValueOrNull( valueTypeToken ), getValueOrNull( valueToken ),
973                 getValueOrNull( sepToken ) );
974
975             return controlLine;
976         }
977
978         return null;
979     }
980
981
982     private LdifChangeTypeLine getChangeTypeLine()
983     {
984
985         LdifToken changeTypeSpecToken = this.scanner.matchChangeTypeSpec();
986         if ( changeTypeSpecToken != null )
987         {
988             LdifToken changeTypeTypeToken = null;
989             LdifToken changeTypeToken = null;
990             LdifToken sepToken = null;
991             changeTypeTypeToken = this.scanner.matchValueType();
992             if ( changeTypeTypeToken != null )
993             {
994                 changeTypeToken = this.scanner.matchChangeType();
995                 if ( changeTypeToken != null )
996                 {
997                     sepToken = this.scanner.matchSep();
998                 }
999             }
1000
1001            LdifChangeTypeLine ctLine = new LdifChangeTypeLine( changeTypeSpecToken.getOffset(),
1002                getValueOrNull( changeTypeSpecToken ), getValueOrNull( changeTypeTypeToken ),
1003                getValueOrNull( changeTypeToken ), getValueOrNull( sepToken ) );
1004
1005            return ctLine;
1006        }
1007
1008        return null;
1009    }
1010
1011
1012    private LdifAttrValLine getAttrValLine()
1013    {
1014        LdifToken attrToken = this.scanner.matchAttributeDescription();
1015        if ( attrToken != null )
1016        {
1017            LdifToken valueTypeToken = null;
1018            LdifToken valueToken = null;
1019            LdifToken sepToken = null;
1020            valueTypeToken = this.scanner.matchValueType();
1021            if ( valueTypeToken != null )
1022            {
1023                valueToken = this.scanner.matchValue();
1024                if ( valueToken != null )
1025                {
1026                    sepToken = this.scanner.matchSep();
1027                }
1028            }
1029
1030            LdifAttrValLine line = new LdifAttrValLine( attrToken.getOffset(), getValueOrNull( attrToken ),
1031                getValueOrNull( valueTypeToken ), getValueOrNull( valueToken ), getValueOrNull( sepToken ) );
1032
1033            return line;
1034        }
1035
1036        return null;
1037    }
1038
1039
1040    private LdifCommentLine[] getCommentLines()
1041    {
1042        List JavaDoc list = new ArrayList JavaDoc( 1 );
1043        LdifToken commentToken = this.scanner.matchComment();
1044        while ( commentToken != null )
1045        {
1046            LdifToken sepToken = this.scanner.matchSep();
1047            list
1048                .add( new LdifCommentLine( commentToken.getOffset(), commentToken.getValue(), getValueOrNull( sepToken ) ) );
1049
1050            commentToken = this.scanner.matchComment();
1051        }
1052        return ( LdifCommentLine[] ) list.toArray( new LdifCommentLine[list.size()] );
1053    }
1054
1055
1056    private void cleanupLine( LdifContainer container )
1057    {
1058        LdifToken errorToken = this.scanner.matchCleanupLine();
1059        if ( errorToken != null )
1060        {
1061            container.addInvalid( new LdifInvalidPart( errorToken.getOffset(), errorToken.getValue() ) );
1062        }
1063    }
1064
1065
1066    private LdifToken cleanupLine()
1067    {
1068        LdifToken errorToken = this.scanner.matchCleanupLine();
1069        return errorToken;
1070    }
1071
1072
1073    private static String JavaDoc getValueOrNull( LdifToken token )
1074    {
1075        return token == null ? null : token.getValue();
1076    }
1077
1078}
1079
Popular Tags