use of com.axway.ats.common.filesystem.FileMatchInfo in project ats-framework by Axway.
the class FileSystemOperations method findNewTextInFile.
/**
* Searches some texts in file and it remembers where the search stopped.
* The following search starts from the point where the last search stopped<br/>
*
* This method is appropriate for growing text files(for example some kind of a log file)
*
* @param filePath the file to work with
* @param searchTexts the texts to search for. At least one should match.
* @param isRegex if the searchText is regular expression, otherwise the text must
* simply be contained by the file line
* @return {@link FileMatchInfo} object
*/
@PublicAtsApi
public FileMatchInfo findNewTextInFile(@Validate(name = "filePath", type = ValidationType.STRING_NOT_EMPTY) String filePath, @Validate(name = "searchTexts", type = ValidationType.NOT_NULL) String[] searchTexts, boolean isRegex) {
// validate input parameters
new Validator().validateMethodParameters(new Object[] { filePath, searchTexts, isRegex });
// execute action
IFileSystemOperations operations = getOperationsImplementationFor(atsAgent);
long searchFromPosition = 0l;
int lastReadLineNumber = 0;
FileMatchInfo fileInfo = this.savedFileMatchDetails.get(filePath);
if (fileInfo != null) {
searchFromPosition = fileInfo.lastReadByte;
lastReadLineNumber = fileInfo.lastReadLineNumber;
}
fileInfo = operations.findTextInFileAfterGivenPosition(filePath, searchTexts, isRegex, searchFromPosition, lastReadLineNumber);
this.savedFileMatchDetails.put(filePath, fileInfo);
return fileInfo;
}
use of com.axway.ats.common.filesystem.FileMatchInfo in project ats-framework by Axway.
the class LocalFileSystemOperations method findTextInFileAfterGivenPosition.
@Override
public FileMatchInfo findTextInFileAfterGivenPosition(String fileName, String[] searchTexts, boolean isRegex, long searchFromPosition, int currentLineNumber) {
File targetFile = new File(fileName);
if (!targetFile.exists()) {
throw new FileDoesNotExistException(fileName);
}
if (searchFromPosition < 0l) {
searchFromPosition = 0l;
} else if (searchFromPosition > targetFile.length()) {
throw new FileSystemOperationException("The file '" + fileName + "' has size(" + targetFile.length() + " bytes) lower than the starting position for search (" + searchFromPosition + " byte). It is possible that file size had been cut since previous inspection but this is not supported.");
}
if (currentLineNumber <= 0) {
currentLineNumber = 1;
}
int matches = 0;
RandomAccessFile raf = null;
try {
List<Pattern> searchPatterns = null;
if (isRegex) {
searchPatterns = new ArrayList<Pattern>();
for (String searchText : searchTexts) {
searchPatterns.add(Pattern.compile(searchText, Pattern.DOTALL));
}
}
raf = new RandomAccessFile(targetFile, "r");
raf.seek(searchFromPosition);
List<String> matchedLines = new ArrayList<String>();
List<String> matchedPatterns = new ArrayList<String>();
List<Integer> matchedLineNumbers = new ArrayList<Integer>();
String line = null;
long lastLineByteOffset = searchFromPosition;
while ((line = IoUtils.readLineWithEOL(raf)) != null) {
for (int i = 0; i < searchTexts.length; i++) {
if ((isRegex && searchPatterns.get(i).matcher(line).matches()) || (!isRegex && line.contains(searchTexts[i]))) {
matches++;
matchedLines.add(line.trim());
matchedPatterns.add(searchTexts[i]);
matchedLineNumbers.add(currentLineNumber);
lastLineByteOffset = raf.getFilePointer();
break;
}
}
if (line.endsWith("\n") || line.endsWith("\r")) {
currentLineNumber++;
lastLineByteOffset = raf.getFilePointer();
}
}
return new FileMatchInfo(matches, currentLineNumber, lastLineByteOffset, matchedLines.toArray(new String[matchedLines.size()]), matchedLineNumbers.toArray(new Integer[matchedLines.size()]), matchedPatterns.toArray(new String[matchedPatterns.size()]));
} catch (IOException ioe) {
throw new FileSystemOperationException("Could not read file '" + fileName + "' seeking from byte " + searchFromPosition, ioe);
} finally {
IoUtils.closeStream(raf);
}
}
Aggregations