com.liferay.source.formatter.check.BaseUpgradeCheck Maven / Gradle / Ivy
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.source.formatter.check;
import com.liferay.petra.string.CharPool;
import com.liferay.source.formatter.check.util.JavaSourceUtil;
/**
* @author Nícolas Moura
*/
public abstract class BaseUpgradeCheck extends BaseFileCheck {
protected String addNewImports(String newContent) {
String[] newImports = getNewImports();
if (newImports != null) {
newContent = JavaSourceUtil.addImports(newContent, newImports);
}
return newContent;
}
protected String afterFormat(
String fileName, String absolutePath, String content,
String newContent) {
return addNewImports(newContent);
}
@Override
protected String doProcess(
String fileName, String absolutePath, String content)
throws Exception {
if (!isValidExtension(fileName)) {
return content;
}
String newContent = format(fileName, absolutePath, content);
if (!content.equals(newContent)) {
newContent = afterFormat(
fileName, absolutePath, content, newContent);
}
return newContent;
}
protected abstract String format(
String fileName, String absolutePath, String content)
throws Exception;
protected String[] getNewImports() {
return null;
}
protected String[] getValidExtensions() {
return new String[] {"java"};
}
protected boolean isValidExtension(String fileName) {
for (String extension : getValidExtensions()) {
if (fileName.endsWith(CharPool.PERIOD + extension)) {
return true;
}
}
return false;
}
}