chapi.app.analyser.support.BaseAnalyser.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of chapi-application Show documentation
Show all versions of chapi-application Show documentation
Chapi is A common language meta information convertor, convert different languages to same meta-data model
package chapi.app.analyser.support
import chapi.app.analyser.config.ChapiConfig
import chapi.app.analyser.filter.FileFilter
import chapi.domain.core.CodeDataStruct
import java.io.BufferedReader
import java.io.File
import kotlin.streams.asStream
abstract class BaseAnalyser(private var config: ChapiConfig) {
abstract fun analysisByFiles(files: Array): Array
abstract fun analysisByFile(file: AbstractFile): List
fun analysisNodeByPath(path: String): Array {
val files = getFilesByPath(path)
return this.analysisByFiles(files)
}
fun readFileAsString(filepath: String): String {
val bufferedReader: BufferedReader = File(filepath).bufferedReader()
var text = bufferedReader.use { it.readText() }
// fix for Window issue
if (text.startsWith("\uFEFF")) {
text = text.substring(1);
}
return text
}
private fun getFilesByPath(path: String): Array {
val workspace = File(path)
return workspace.walk().asStream().parallel()
.filter {
FileFilter.filterByLanguage(it.absolutePath, config)
}
.map {
val abstractFile = AbstractFile.toAbstractFile(it, workspace)
abstractFile.content = readFileAsString(it.path)
abstractFile
}
.toArray { length -> arrayOfNulls(length) }
}
}