use of java.io.LineNumberReader 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 java.io.LineNumberReader in project linuxtools by eclipse.
the class SpecfileNewWizardPage method getContent.
public String getContent() {
InputStream inputStream = runRpmdevNewSpec(selectedTemplate);
LineNumberReader reader = new LineNumberReader(new InputStreamReader(inputStream));
String line;
try {
// $NON-NLS-1$
content = "";
while ((line = reader.readLine()) != null) {
if (line.startsWith("Name:")) {
// $NON-NLS-1$
// $NON-NLS-1$ //$NON-NLS-2$
line = "Name:" + " " + nameText.getText();
}
if (line.startsWith("Version:")) {
// $NON-NLS-1$
// $NON-NLS-1$ //$NON-NLS-2$
line = "Version:" + " " + versionText.getText();
}
if (line.startsWith("Summary:")) {
// $NON-NLS-1$
// $NON-NLS-1$ //$NON-NLS-2$
line = "Summary:" + " " + summaryText.getText();
}
if (line.startsWith("Group:")) {
// $NON-NLS-1$
// $NON-NLS-1$ //$NON-NLS-2$
line = "Group:" + " " + groupCombo.getText();
}
if (line.startsWith("License:")) {
// $NON-NLS-1$
// $NON-NLS-1$ //$NON-NLS-2$
line = "License:" + " " + licenseText.getText();
}
if (line.startsWith("URL:")) {
// $NON-NLS-1$
// $NON-NLS-1$ //$NON-NLS-2$
line = "URL:" + " " + urlText.getText();
}
if (line.startsWith("Source0:")) {
// $NON-NLS-1$
// $NON-NLS-1$ //$NON-NLS-2$
line = "Source0:" + " " + source0Text.getText();
}
content += line + '\n';
}
} catch (IOException e1) {
SpecfileLog.logError(e1);
}
return content;
}
use of java.io.LineNumberReader in project linuxtools by eclipse.
the class RpmlintParser method getRealLineNbr.
/**
* Return the line number for given specContent and strToFind, it returns -1
* if the string to find is not found.
*
* @param specContent
* The content of the spec file.
*
* @param strToFind
* The string we are looking for.
* @return The line number.
*/
public static int getRealLineNbr(String specContent, String strToFind) {
int ret = -1;
if (strToFind.isEmpty()) {
return ret;
}
String line;
LineNumberReader reader = new LineNumberReader(new StringReader(specContent));
try {
while ((line = reader.readLine()) != null) {
if (// $NON-NLS-1$
line.replaceAll("\t| ", EMPTY_STRING).indexOf(strToFind.replaceAll("\t| ", EMPTY_STRING)) > -1) {
// $NON-NLS-1$
ret = reader.getLineNumber();
}
}
} catch (IOException e) {
// return -1 if an I/O Exception occure.
}
return ret;
}
use of java.io.LineNumberReader in project linuxtools by eclipse.
the class RpmlintParser method parseRpmlintOutput.
/**
* Parse a given rpmlint <code>InputStream</code>
*
* @param rpmlint
* <code>InputStream</code> to parse.
* @return a <code>RpmlintItem</code> ArrayList.
*/
private static List<RpmlintItem> parseRpmlintOutput(BufferedInputStream in) {
RpmlintItem item = new RpmlintItem();
ArrayList<RpmlintItem> rpmlintItems = new ArrayList<>();
LineNumberReader reader = new LineNumberReader(new InputStreamReader(in));
String line;
boolean isFirtItemLine = true;
String[] lineItems;
String description = EMPTY_STRING;
try {
while ((line = reader.readLine()) != null) {
if (isFirtItemLine) {
isFirtItemLine = false;
lineItems = line.split(COLON, 4);
item.setFileName(lineItems[0]);
int lineNbr;
// maybe we can find a better way to detect this line.
try {
Integer.parseInt(line.split(SPACE)[0]);
return rpmlintItems;
} catch (NumberFormatException e) {
// this line is not the summary
}
// removed.
try {
lineNbr = Integer.parseInt(lineItems[1]);
item.setSeverity(lineItems[2]);
lineItems = lineItems[3].trim().split(SPACE, 2);
} catch (NumberFormatException e) {
// No line number showed for this rpmlint warning.
lineItems = line.split(COLON, 3);
lineNbr = -1;
item.setSeverity(lineItems[1]);
lineItems = lineItems[2].trim().split(SPACE, 2);
}
item.setLineNbr(lineNbr);
item.setId(lineItems[0]);
if (lineItems.length > 1) {
// Maybe this error occur when rpmlint execute 'rpm -q
// --qf=
// --specfile file.spec' command
RpmlintItem tmpItem = parseRpmOutput(item, lineItems[1]);
if (tmpItem == null) {
item.setRefferedContent(lineItems[1]);
} else {
item = tmpItem;
}
} else {
item.setRefferedContent(EMPTY_STRING);
}
} else {
description += line + '\n';
}
if (line.equals(EMPTY_STRING)) {
if (item.getMessage() == null) {
item.setMessage(description.substring(0, description.length() - 2));
}
int useOfTabsAndSpaces = getMixedUseOfTabsAndSpaces(item.getRefferedContent());
if (useOfTabsAndSpaces != -1) {
item.setLineNbr(useOfTabsAndSpaces);
}
rpmlintItems.add(item);
item = new RpmlintItem();
// Reinitialize parser for the next item
isFirtItemLine = true;
description = EMPTY_STRING;
}
}
// Close the input stream
in.close();
} catch (IOException e) {
RpmlintLog.logError(e);
}
return rpmlintItems;
}
use of java.io.LineNumberReader in project linuxtools by eclipse.
the class STJunitUtils method compareIgnoreEOL.
/**
* Utility method to compare files
* @param dumpFile
* @param refFile
* @return
*/
public static boolean compareIgnoreEOL(String dumpFile, String refFile, boolean deleteDumpFileIfOk) {
String message = "Comparing ref file (" + refFile + ")and dump file (" + dumpFile + ")";
boolean equals = false;
try (LineNumberReader is1 = new LineNumberReader(new FileReader(dumpFile));
LineNumberReader is2 = new LineNumberReader(new FileReader(refFile))) {
do {
String line1 = is1.readLine();
String line2 = is2.readLine();
if (line1 == null) {
if (line2 == null) {
equals = true;
}
break;
} else if (line2 == null || !line1.equals(line2)) {
break;
}
} while (true);
if (!equals) {
assertEquals(message + ": not correspond ", true, false);
}
is1.close();
is2.close();
// delete dump only for successful tests
if (equals && deleteDumpFileIfOk) {
new File(dumpFile).delete();
}
} catch (FileNotFoundException fnfe) {
message += "... FAILED: One of these files may not exist";
assertNull(message, fnfe);
} catch (Exception e) {
message += ": exception raised ... FAILED";
assertNull(message, e);
}
return equals;
}
Aggregations