Search in sources :

Example 1 with SpecfilePatchMacro

use of org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfilePatchMacro in project linuxtools by eclipse.

the class SpecfileParser method parse.

public Specfile parse(IDocument specfileDocument) {
    // instantiated.
    if (errorHandler != null) {
        errorHandler.removeExistingMarkers();
    }
    if (taskHandler != null) {
        taskHandler.removeExistingMarkers();
    }
    LineNumberReader reader = new LineNumberReader(new StringReader(specfileDocument.get()));
    // $NON-NLS-1$
    String line = "";
    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);
                    }
                }
            }
            // 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;
}
Also used : SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) SpecfileTag(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileTag) StringReader(java.io.StringReader) SpecfilePatchMacro(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfilePatchMacro) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader)

Example 2 with SpecfilePatchMacro

use of org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfilePatchMacro in project linuxtools by eclipse.

the class SpecfileParser method parsePatch.

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 // $NON-NLS-1$
            SpecfileParseException(// $NON-NLS-1$
            Messages.getString("SpecfileParser.5"), lineNumber, 0, lineText.length(), IMarker.SEVERITY_ERROR));
            return null;
        }
    }
    return toReturn;
}
Also used : SpecfileParseException(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileParseException) SpecfilePatchMacro(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfilePatchMacro)

Aggregations

SpecfilePatchMacro (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfilePatchMacro)2 IOException (java.io.IOException)1 LineNumberReader (java.io.LineNumberReader)1 StringReader (java.io.StringReader)1 SpecfileParseException (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileParseException)1 SpecfileSource (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource)1 SpecfileTag (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileTag)1