Search in sources :

Example 1 with FileMatchInfo

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;
}
Also used : IFileSystemOperations(com.axway.ats.core.filesystem.model.IFileSystemOperations) FileMatchInfo(com.axway.ats.common.filesystem.FileMatchInfo) Validator(com.axway.ats.core.validation.Validator) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 2 with FileMatchInfo

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);
    }
}
Also used : FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) Pattern(java.util.regex.Pattern) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) FileMatchInfo(com.axway.ats.common.filesystem.FileMatchInfo) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Aggregations

FileMatchInfo (com.axway.ats.common.filesystem.FileMatchInfo)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)1 FileDoesNotExistException (com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException)1 IFileSystemOperations (com.axway.ats.core.filesystem.model.IFileSystemOperations)1 Validator (com.axway.ats.core.validation.Validator)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)1