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;
}
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;
}
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");
}
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;
}
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;
}
Aggregations