Search in sources :

Example 6 with SpecfileSource

use of org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource 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 7 with SpecfileSource

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

the class SpecfileParser method parseComplexDefinition.

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 // $NON-NLS-1$
                    SpecfileParseException(// $NON-NLS-1$
                    Messages.getString("SpecfileParser.8"), 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 // $NON-NLS-1$
                            SpecfileParseException(// $NON-NLS-1$
                            Messages.getString("SpecfileParser.10"), 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 // $NON-NLS-1$
                            SpecfileParseException(// $NON-NLS-1$
                            Messages.getString("SpecfileParser.11"), lineNumber, 0, lineText.length(), IMarker.SEVERITY_ERROR));
                            return null;
                        }
                    } else {
                        number = 0;
                    }
                }
                // $NON-NLS-1$
                toReturn = new SpecfileSource(number, "");
                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 // $NON-NLS-1$
                    SpecfileParseException(// $NON-NLS-1$
                    Messages.getString("SpecfileParser.12"), lineNumber, 0, lineText.length(), IMarker.SEVERITY_ERROR));
                }
            }
        }
    }
    return toReturn;
}
Also used : SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource)

Example 8 with SpecfileSource

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

the class SpecfileElementHyperlinkDetectorTest method testDetectHyperlinks.

@Test
public void testDetectHyperlinks() throws PartInitException {
    String testText = "%define smth other\nSource0: test.zip\nPatch0: first.patch\n" + "%build\n %{SOURCE0}\n%patch0\n%{smth}\n";
    newFile(testText);
    SpecfileElementHyperlinkDetector elementDetector = new SpecfileElementHyperlinkDetector();
    elementDetector.setSpecfile(specfile);
    IEditorPart openEditor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), testFile, "org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor");
    editor = (SpecfileEditor) openEditor;
    editor.doRevertToSaved();
    // test source element
    IRegion region = new Region(74, 0);
    IHyperlink[] returned = elementDetector.detectHyperlinks(editor.getSpecfileSourceViewer(), region, false);
    SpecfileElementHyperlink element = (SpecfileElementHyperlink) returned[0];
    assertTrue(element.getSource() instanceof SpecfileSource);
    SpecfileSource source = (SpecfileSource) element.getSource();
    assertEquals(source.getSourceType(), SpecfileSource.SourceType.SOURCE);
    assertEquals(source.getFileName(), "test.zip");
    // test patch element
    region = new Region(83, 0);
    returned = elementDetector.detectHyperlinks(editor.getSpecfileSourceViewer(), region, false);
    element = (SpecfileElementHyperlink) returned[0];
    assertTrue(element.getSource() instanceof SpecfileSource);
    source = (SpecfileSource) element.getSource();
    assertEquals(source.getSourceType(), SpecfileSource.SourceType.PATCH);
    assertEquals(source.getFileName(), "first.patch");
    // test define
    region = new Region(89, 0);
    returned = elementDetector.detectHyperlinks(editor.getSpecfileSourceViewer(), region, false);
    element = (SpecfileElementHyperlink) returned[0];
    assertTrue(element.getSource() instanceof SpecfileDefine);
    SpecfileDefine define = (SpecfileDefine) element.getSource();
    assertEquals(define.getName(), "smth");
    assertEquals(define.getStringValue(), "other");
}
Also used : SpecfileElementHyperlink(org.eclipse.linuxtools.internal.rpm.ui.editor.hyperlink.SpecfileElementHyperlink) SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) SpecfileDefine(org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IEditorPart(org.eclipse.ui.IEditorPart) SpecfileElementHyperlinkDetector(org.eclipse.linuxtools.internal.rpm.ui.editor.hyperlink.SpecfileElementHyperlinkDetector) IRegion(org.eclipse.jface.text.IRegion) Test(org.junit.Test)

Example 9 with SpecfileSource

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

the class Specfile method getPatches.

public List<SpecfileSource> getPatches() {
    List<SpecfileSource> patchesList = new ArrayList<>(patches.values());
    Collections.sort(patchesList, new SourceComparator());
    return patchesList;
}
Also used : SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) ArrayList(java.util.ArrayList) SourceComparator(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SourceComparator)

Example 10 with SpecfileSource

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

the class Specfile method getSources.

public List<SpecfileSource> getSources() {
    List<SpecfileSource> sourcesList = new ArrayList<>(sources.values());
    Collections.sort(sourcesList, new SourceComparator());
    return sourcesList;
}
Also used : SpecfileSource(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource) ArrayList(java.util.ArrayList) SourceComparator(org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SourceComparator)

Aggregations

SpecfileSource (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource)18 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 HttpURLConnection (java.net.HttpURLConnection)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 URLConnection (java.net.URLConnection)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 IFile (org.eclipse.core.resources.IFile)2 Path (org.eclipse.core.runtime.Path)2 Job (org.eclipse.core.runtime.jobs.Job)2 IRegion (org.eclipse.jface.text.IRegion)2 SourceComparator (org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SourceComparator)2 DownloadJob (org.eclipse.linuxtools.rpm.core.utils.DownloadJob)2 SpecfileDefine (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine)2 SpecfileParser (org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser)2 StringTokenizer (com.ibm.icu.util.StringTokenizer)1