Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8a20c3a32efff57beeb91666bc98e7adfc6150a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*******************************************************************************
 * Copyright (c) 2007, 2013 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Red Hat - initial API and implementation
 *    Alphonse Van Assche
 *******************************************************************************/

package org.eclipse.linuxtools.rpm.ui.editor.parser;

import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.BUILD_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.CHANGELOG_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.CLEAN_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.DESCRIPTION_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.FILES_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.INSTALL_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.PACKAGE_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.POSTTRANS_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.POSTUN_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.POST_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.PREP_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.PRETRANS_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.PREUN_SECTION;
import static org.eclipse.linuxtools.internal.rpm.ui.editor.RpmSections.PRE_SECTION;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.linuxtools.internal.rpm.ui.editor.Activator;
import org.eclipse.linuxtools.internal.rpm.ui.editor.ISpecfileSpecialSymbols;
import org.eclipse.linuxtools.internal.rpm.ui.editor.RpmTags;
import org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileLog;
import org.eclipse.linuxtools.internal.rpm.ui.editor.parser.Messages;
import org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileParseException;
import org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfilePatchMacro;
import org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource;
import org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource.SourceType;
import org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileTag;
import org.eclipse.linuxtools.internal.rpm.ui.editor.preferences.PreferenceConstants;
import org.eclipse.linuxtools.rpm.ui.editor.markers.SpecfileErrorHandler;
import org.eclipse.linuxtools.rpm.ui.editor.markers.SpecfileTaskHandler;

public class SpecfileParser {

    private static final String DEFINE_SEPARATOR = ":"; //$NON-NLS-1$

    private static final String SPACE_REGEX = "\\s+"; //$NON-NLS-1$

    /**
     * These are SRPM-wide sections, and they also cannot have any flags like -n
     * or -f. Hence they are called simple. This is probably a misleading name
     * and it should be renamed to reflect that they are SRPM-wide sections.
     */
    public static final String[] simpleSections = { PREP_SECTION, BUILD_SECTION,
            INSTALL_SECTION, CLEAN_SECTION, CHANGELOG_SECTION };

    /**
     * These are sections that apply to a particular sub-package (i.e. binary
     * RPM), including the main package. These can also have flags like -f or -n
     * appended to them, hence they are called complex. This should probably be
     * renamed to reflect that they are in fact per-RPM sections.
     */
    private static String[] complexSections = { PRETRANS_SECTION, PRE_SECTION,
            PREUN_SECTION, POST_SECTION, POSTUN_SECTION, POSTTRANS_SECTION,
            FILES_SECTION, PACKAGE_SECTION, DESCRIPTION_SECTION };

    private static String[] simpleDefinitions = { RpmTags.EPOCH, RpmTags.NAME,
            RpmTags.VERSION, RpmTags.RELEASE, RpmTags.URL, RpmTags.BUILD_ARCH };

    private static String[] directValuesDefinitions = { RpmTags.LICENSE,
            RpmTags.BUILD_ROOT };
    // Note that the ordering here should match that in
    // SpecfileSource#SOURCETYPE
    private static String[] complexDefinitions = { "Source", "Patch" }; //$NON-NLS-1$ //$NON-NLS-2$

    private static String[] packageLevelDefinitions = { RpmTags.SUMMARY,
            RpmTags.GROUP, RpmTags.OBSOLETES, RpmTags.PROVIDES,
            RpmTags.REQUIRES, RpmTags.REQUIRES_PRE, RpmTags.REQUIRES_POST,
            RpmTags.REQUIRES_POSTUN };

    private SpecfileErrorHandler errorHandler;
    private SpecfileTaskHandler taskHandler;
    private IPreferenceStore store;
    private SpecfileSection lastSection;
    private SpecfilePackage activePackage;

    public SpecfileParser() {
        store = Activator.getDefault().getPreferenceStore();
    }

    public Specfile parse(IDocument specfileDocument) {

        // remove all existing markers, if a SpecfileErrorHandler is
        // instantiated.
        if (errorHandler != null) {
            errorHandler.removeExistingMarkers();
        }
        if (taskHandler != null) {
            taskHandler.removeExistingMarkers();
        }
        LineNumberReader reader = new LineNumberReader(new StringReader(
                specfileDocument.get()));
        String line = ""; //$NON-NLS-1$
        int lineStartPosition = 0;
        Specfile specfile = new Specfile();
        specfile.setDocument(specfileDocument);
        try {
            while ((line = reader.readLine()) != null) {
                if (taskHandler != null) {
                    generateTaskMarker(reader.getLineNumber() - 1, line);
                }
                // IDocument.getLine(#) is 0-indexed whereas
                // reader.getLineNumber appears to be 1-indexed
                SpecfileElement element = parseLine(line, specfile, reader
                        .getLineNumber() - 1);
                if (element != null) {
                    element.setLineNumber(reader.getLineNumber() - 1);
                    element.setLineStartPosition(lineStartPosition);
                    element.setLineEndPosition(lineStartPosition
                            + line.length());
                    if (element.getClass() == SpecfileTag.class) {
                        SpecfileTag tag = (SpecfileTag) element;
                        specfile.addDefine(tag);
                    } else if ((element.getClass() == SpecfilePatchMacro.class)) {
                        SpecfilePatchMacro thisPatchMacro = (SpecfilePatchMacro) element;
                        if (thisPatchMacro != null) {
                            thisPatchMacro.setSpecfile(specfile);
                        }
                        SpecfileSource thisPatch = specfile
                                .getPatch(thisPatchMacro.getPatchNumber());
                        if (thisPatch != null) {
                            thisPatch.addLineUsed(reader.getLineNumber() - 1);
                            thisPatch.setSpecfile(specfile);
                        }
                    } else if ((element.getClass() == SpecfileDefine.class)) {
                        specfile.addDefine((SpecfileDefine) element);
                    } else if ((element.getClass() == SpecfileSource.class)) {
                        SpecfileSource source = (SpecfileSource) element;

                        source.setLineNumber(reader.getLineNumber() - 1);
                        if (source.getSourceType() == SpecfileSource.SourceType.SOURCE) {
                            specfile.addSource(source);
                        } else {
                            specfile.addPatch(source);
                        }
                    }
                }

                // sets the last SpecfileSection's end line to that of the end of
                // the end of the specfileDocument.
                // SpecfileParser#parseMacro will handle correcting the end line
                // if the last SpecfileSection was not truly the last 1
                // This is for the purpose of making DocumentRangeNode work
                if (lastSection != null) {
                    lastSection.setSectionEndLine(specfileDocument
                            .getNumberOfLines() - 1);
                }

                // The +1 is for the line delimiter. FIXME: will we end up off
                // by one on the last line?
                lineStartPosition += line.length() + 1;
            }
        } catch (IOException e) {
            // FIXME
            SpecfileLog.logError(e);
        }
        return specfile;
    }

    /**
     * Parse a File into a specfile
     *
     * @param file The File to be parsed
     * @return A Specfile object
     */
    public Specfile parse(IFile file) {
        SpecfileParser parser = new SpecfileParser();
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    file.getContents()));
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n"); //$NON-NLS-1$
            }
        } catch (IOException e) {
            SpecfileLog.logError(Messages.getString("SpecfileParseFile.1"), e); //$NON-NLS-1$
        } catch (CoreException e) {
            SpecfileLog.logError(Messages.getString("SpecfileParseFile.2"), e); //$NON-NLS-1$
        }
        return parser.parse(sb.toString());
    }

    private void generateTaskMarker(int lineNumber, String line) {
        String[] taskTags = store.getString(PreferenceConstants.P_TASK_TAGS)
                .split(";"); //$NON-NLS-1$
        int commentCharIndex = line
                .indexOf(ISpecfileSpecialSymbols.COMMENT_START);
        if (commentCharIndex > -1) {
            for (String item : taskTags) {
                int taskIndex = line.indexOf(item);
                if (taskIndex > commentCharIndex) {
                    taskHandler.handleTask(lineNumber, line, item);
                }
            }
        }
    }

    public Specfile parse(String specfileContent) {
        return parse(new Document(specfileContent));
    }

    public SpecfileElement parseLine(String lineText, Specfile specfile,
            int lineNumber) {

        if (lineText.startsWith("%")) {//$NON-NLS-1$
            return parseMacro(lineText, specfile, lineNumber);
        }

        for (String simpleDefinition : simpleDefinitions) {
            if (lineText.startsWith(simpleDefinition + DEFINE_SEPARATOR)) {
                return parseSimpleDefinition(lineText, specfile, lineNumber,
                        false);
            }
        }
        for (String directValuesDefinition : directValuesDefinitions) {
            if (lineText.startsWith(directValuesDefinition + DEFINE_SEPARATOR)) {
                return parseDirectDefinition(lineText, specfile, lineNumber);
            }
        }
        for (String directValuesDefinition : packageLevelDefinitions) {
            if (lineText.startsWith(directValuesDefinition + DEFINE_SEPARATOR)) {
                SpecfileElement definition = parseDirectDefinition(lineText,
                        specfile, lineNumber);
                if (directValuesDefinition.equals(RpmTags.REQUIRES)) {
                    if (activePackage != null) {
                        activePackage.addRequire((SpecfileTag) definition);
                    } else {
                        specfile.addRequire((SpecfileTag) definition);
                    }
                }
                return definition;
            }
        }

        if (lineText.startsWith(complexDefinitions[0])
                && lineText.contains(DEFINE_SEPARATOR)) {
            return parseComplexDefinition(lineText, lineNumber,
                    SourceType.SOURCE);
        } else if (lineText.startsWith(complexDefinitions[1])
                && lineText.contains(DEFINE_SEPARATOR)) {
            return parseComplexDefinition(lineText, lineNumber,
                    SourceType.PATCH);
        } else if (lineText.startsWith("BuildRequires")) { //$NON-NLS-1$
            return parseBuildRequire(lineText, lineNumber, specfile);
        }

        return null;
    }

    private SpecfileElement parseBuildRequire(String lineText, int lineNumber,
            Specfile specfile) {
        String value = lineText.substring(lineText.indexOf(':') + 1,
                lineText.length()).trim();
        SpecfileDefine buildRequire = new SpecfileDefine(
                "BuildRequires", value, specfile, null); //$NON-NLS-1$
        buildRequire.setLineNumber(lineNumber);
        specfile.addBuildRequire(buildRequire);
        return buildRequire;
    }

    private SpecfileSection parseSection(String lineText, Specfile specfile,
            int lineNumber) {
        List<String> tokens = Arrays.asList(lineText.split(SPACE_REGEX));
        SpecfileSection toReturn = null;
        boolean isSimpleSection = false;
        for (Iterator<String> iter = tokens.iterator(); iter.hasNext();) {
            String token = iter.next();

            // Sections
            // Simple Section Headers
            for (String simpleSection : simpleSections) {
                if (token.equals(simpleSection)) {
                    toReturn = new SpecfileSection(token.substring(1), specfile);
                    specfile.addSection(toReturn);
                    isSimpleSection = true;
                }

            }

            // Complex Section Headers
            for (String complexSection : complexSections) {
                if (token.equals(complexSection)) {
                    String name = token.substring(1);
                    if (!name.equals("package")) { //$NON-NLS-1$
                        toReturn = new SpecfileSection(name, specfile);
                        specfile.addComplexSection(toReturn);
                    }
                    while (iter.hasNext()) {
                        String nextToken = iter.next();
                        if (nextToken.equals("-n")) { //$NON-NLS-1$
                            if (!iter.hasNext()) {
                                errorHandler
                                        .handleError(new SpecfileParseException(
                                                Messages
                                                        .getString("SpecfileParser.1") //$NON-NLS-1$
                                                        + name
                                                        + Messages
                                                                .getString("SpecfileParser.2"), //$NON-NLS-1$
                                                lineNumber, 0, lineText
                                                        .length(),
                                                IMarker.SEVERITY_ERROR));
                                continue;
                            }

                            nextToken = iter.next();
                            if (nextToken.startsWith("-")) { //$NON-NLS-1$
                                errorHandler
                                        .handleError(new SpecfileParseException(
                                                Messages
                                                        .getString("SpecfileParser.3") //$NON-NLS-1$
                                                        + nextToken
                                                        + Messages
                                                                .getString("SpecfileParser.4"), //$NON-NLS-1$
                                                lineNumber, 0, lineText
                                                        .length(),
                                                IMarker.SEVERITY_ERROR));
                            }

                        } else if (nextToken.equals("-p")) { //$NON-NLS-1$
                            // FIXME: rest of line is the actual section
                            break;
                        } else if (nextToken.equals("-f")) { //$NON-NLS-1$
                            break;
                        }

                        // this is a package
                        if (toReturn == null) {
                            SpecfilePackage tmpPackage = specfile
                                    .getPackage(nextToken);

                            if (tmpPackage == null) {
                                tmpPackage = new SpecfilePackage(nextToken,
                                        specfile);
                                specfile.addPackage(tmpPackage);
                            }
                            activePackage = tmpPackage;
                            return tmpPackage;
                        }

                        // this is another section
                        SpecfilePackage enclosingPackage = specfile
                                .getPackage(nextToken);
                        if (enclosingPackage == null) {
                            enclosingPackage = new SpecfilePackage(nextToken,
                                    specfile);
                            specfile.addPackage(enclosingPackage);
                        }
                        toReturn.setPackage(enclosingPackage);
                        enclosingPackage.addSection(toReturn);
                    }
                }
            }
        }

        // if this package is part of the top level package, add it to
        // it
        if (toReturn != null && toReturn.getPackage() == null) {
            SpecfilePackage topPackage = specfile
                    .getPackage(specfile.getName());
            if (topPackage == null) {
                topPackage = new SpecfilePackage(specfile.getName(), specfile);
                specfile.addPackage(topPackage);
            }
            if (!isSimpleSection) {
                topPackage.addSection(toReturn);
            }
        }
        if (lastSection != null) {
            lastSection.setSectionEndLine(lineNumber);
        }
        return toReturn;
    }

    private SpecfileElement parseMacro(String lineText, Specfile specfile,
            int lineNumber) {
        // FIXME: handle other macros

        if (lineText.startsWith("%define") || lineText.startsWith("%global")) { //$NON-NLS-1$ //$NON-NLS-2$
            return parseDefine(lineText, specfile, lineNumber);
        } else if (lineText.startsWith("%patch")) { //$NON-NLS-1$
            return parsePatch(lineText, lineNumber);
        }

        String[] sections = new String[simpleSections.length
                + complexSections.length];
        System.arraycopy(simpleSections, 0, sections, 0, simpleSections.length);
        System.arraycopy(complexSections, 0, sections, simpleSections.length,
                complexSections.length);
        for (String section : sections) {
            if (lineText.startsWith(section)) {
                lastSection = parseSection(lineText, specfile, lineNumber);
                if (lastSection != null) {
                    lastSection.setSectionEndLine(lineNumber + 1);
                }
                return lastSection;
            }
        }
        // FIXME: add handling of lines containing %{SOURCENNN}
        return null;
    }

    private SpecfileElement parsePatch(String lineText, int lineNumber) {

        SpecfilePatchMacro toReturn = null;

        List<String> tokens = Arrays.asList(lineText.split(SPACE_REGEX));

        for (String token : tokens) {
            // %patchN+
            try {
                if (token.startsWith("%patch")) { //$NON-NLS-1$
                    int patchNumber = 0;
                    if (token.length() > 6) {
                        patchNumber = Integer.parseInt(token.substring(6));
                    }
                    toReturn = new SpecfilePatchMacro(patchNumber);
                }
            } catch (NumberFormatException e) {
                errorHandler.handleError(new SpecfileParseException(
                        Messages.getString("SpecfileParser.5"), //$NON-NLS-1$
                        lineNumber, 0, lineText.length(),
                        IMarker.SEVERITY_ERROR));
                return null;
            }
        }

        return toReturn;
    }

    private SpecfileDefine parseDefine(String lineText, Specfile specfile,
            int lineNumber) {
        List<String> tokens = Arrays.asList(lineText.split(SPACE_REGEX));
        SpecfileDefine toReturn = null;
        for (Iterator<String> iter = tokens.iterator(); iter.hasNext();) {
            // Eat the actual "%define" or "%global" token
            iter.next();
            while (iter.hasNext()) {
                String defineName = iter.next();
                // FIXME: is this true? investigate in rpmbuild source
                // Definitions must being with a letter
                if (!Character.isLetter(defineName.charAt(0))
                        && (defineName.charAt(0) != '_')) {
                    errorHandler.handleError(new SpecfileParseException(
                            Messages.getString("SpecfileParser.6"), //$NON-NLS-1$
                            lineNumber, 0, lineText.length(),
                            IMarker.SEVERITY_ERROR));
                    return null;
                } else {
                    if (iter.hasNext()) {
                        String defineStringValue = iter.next();
                        // Defines that are more than one token
                        if (iter.hasNext()) {
                            defineStringValue = lineText.substring(lineText
                                    .indexOf(defineStringValue));
                            // Eat up the rest of the tokens
                            while (iter.hasNext()) {
                                iter.next();
                            }
                        }
                        int defineIntValue = -1;
                        try {
                            defineIntValue = Integer
                                    .parseInt(defineStringValue);
                        } catch (NumberFormatException e) {
                            toReturn = new SpecfileDefine(defineName,
                                    defineStringValue, specfile, null);
                        }
                        if (toReturn == null) {
                            toReturn = new SpecfileDefine(defineName,
                                    defineIntValue, specfile, null);
                        }
                    } else {
                        errorHandler.handleError(new SpecfileParseException(defineName+
                                Messages.getString("SpecfileParser.14"), //$NON-NLS-1$
                                lineNumber, 0, lineText.length(),
                                IMarker.SEVERITY_ERROR));
                    }
                }
            }
        }
        return toReturn;
    }

    private SpecfileElement parseComplexDefinition(String lineText,
            int lineNumber, SourceType sourceType) {
        SpecfileSource toReturn = null;
        List<String> tokens = Arrays.asList(lineText.split(SPACE_REGEX));
        int number = -1;
        boolean firstToken = true;

        for (Iterator<String> iter = tokens.iterator(); iter.hasNext();) {
            String token = iter.next();
            if (token != null && token.length() > 0) {
                if (firstToken) {
                    if (token.endsWith(DEFINE_SEPARATOR)) {
                        token = token.substring(0, token.length() - 1);
                    } else {
                        // FIXME: come up with a better error message here
                        // FIXME: what about descriptions that begin a line with
                        // the word "Source" or "Patch"?
                        errorHandler.handleError(new SpecfileParseException(
                                Messages.getString("SpecfileParser.8"), //$NON-NLS-1$
                                lineNumber, 0, lineText.length(),
                                IMarker.SEVERITY_WARNING));
                        return null;
                    }
                    if (sourceType == SourceType.PATCH) {
                        if (token.length() > 5) {
                            number = Integer.parseInt(token.substring(5));
                            if (!("patch" + number).equalsIgnoreCase(token)) { //$NON-NLS-1$
                                errorHandler
                                        .handleError(new SpecfileParseException(
                                                Messages
                                                        .getString("SpecfileParser.10"), //$NON-NLS-1$
                                                lineNumber, 0, lineText
                                                        .length(),
                                                IMarker.SEVERITY_ERROR));
                                return null;
                            }
                        } else {
                            number = 0;
                        }
                    } else {
                        if (token.length() > 6) {
                            number = Integer.parseInt(token.substring(6));
                            if (!("source" + number).equalsIgnoreCase(token)) { //$NON-NLS-1$
                                errorHandler
                                        .handleError(new SpecfileParseException(
                                                Messages
                                                        .getString("SpecfileParser.11"), //$NON-NLS-1$
                                                lineNumber, 0, lineText
                                                        .length(),
                                                IMarker.SEVERITY_ERROR));
                                return null;
                            }
                        } else {
                            number = 0;
                        }
                    }
                    toReturn = new SpecfileSource(number, ""); //$NON-NLS-1$
                    toReturn.setSourceType(sourceType);
                    firstToken = false;
                } else {
                    // toReturn should never be null but check just in case
                    if (toReturn != null) {
                        toReturn.setFileName(token);
                    }
                    if (iter.hasNext()) {
                        errorHandler.handleError(new SpecfileParseException(
                                Messages.getString("SpecfileParser.12"), //$NON-NLS-1$
                                lineNumber, 0, lineText.length(),
                                IMarker.SEVERITY_ERROR));
                    }
                }
            }
        }

        return toReturn;
    }

    private SpecfileElement parseSimpleDefinition(String lineText,
            Specfile specfile, int lineNumber, boolean warnMultipleValues) {
        List<String> tokens = Arrays.asList(lineText.split(SPACE_REGEX));
        SpecfileTag toReturn = null;

        for (Iterator<String> iter = tokens.iterator(); iter.hasNext();) {
            String token = iter.next();

            if (token.length() <= 0) {
                break;
            }

            if (iter.hasNext()) {
                String possValue = iter.next();
                if (possValue.startsWith("%") && iter.hasNext()) { //$NON-NLS-1$
                    possValue += ' ' + iter.next();
                }
                toReturn = new SpecfileTag(token.substring(0,
                        token.length() - 1).toLowerCase(), possValue, specfile,
                        null);
                if (iter.hasNext() && !warnMultipleValues) {
                    errorHandler.handleError(new SpecfileParseException(
                            token.substring(0, token.length() - 1)
                                    + Messages.getString("SpecfileParser.13"), //$NON-NLS-1$
                            lineNumber, 0, lineText.length(),
                            IMarker.SEVERITY_ERROR));
                    return null;
                }
            } else {
                errorHandler.handleError(new SpecfileParseException(token
                        .substring(0, token.length() - 1)
                        + Messages.getString("SpecfileParser.14"), lineNumber, //$NON-NLS-1$
                        0, lineText.length(), IMarker.SEVERITY_ERROR));
                toReturn = null;
            }
        }
        if ((toReturn != null)) {
            String returnStringValue = toReturn.getStringValue();
            if (returnStringValue != null) {
                try {
                    int intValue = Integer.parseInt(returnStringValue);
                    toReturn.setValue(intValue);
                } catch (NumberFormatException e) {
                    if (toReturn.getName().equalsIgnoreCase(RpmTags.EPOCH)) {
                        errorHandler
                                .handleError(new SpecfileParseException(
                                        Messages.getString("SpecfileParser.16"), lineNumber, //$NON-NLS-1$
                                        0, lineText.length(),
                                        IMarker.SEVERITY_ERROR));
                        toReturn = null;
                    }
                }
            }
        }
        return toReturn;
    }

    private SpecfileElement parseDirectDefinition(String lineText,
            Specfile specfile, int lineNumber) {
        String[] parts = lineText.split(DEFINE_SEPARATOR, 2);
        SpecfileTag directDefinition;
        if (parts.length == 2) {
            directDefinition = new SpecfileTag(parts[0], parts[1].trim(),
                    specfile, activePackage);
            directDefinition.setLineNumber(lineNumber);
        } else {
            errorHandler.handleError(new SpecfileParseException(parts[0]
                    + Messages.getString("SpecfileParser.14"), lineNumber, //$NON-NLS-1$
                    0, lineText.length(), IMarker.SEVERITY_ERROR));
            directDefinition = null;
        }
        return directDefinition;
    }

    public void setErrorHandler(SpecfileErrorHandler specfileErrorHandler) {
        errorHandler = specfileErrorHandler;
    }

    public void setTaskHandler(SpecfileTaskHandler specfileTaskHandler) {
        taskHandler = specfileTaskHandler;
    }
}

Back to the top