All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.teamscale.index.metadata.abap.AbapFileMetadataReader Maven / Gradle / Ivy

There is a newer version: 2025.1.0-rc2
Show newest version
package com.teamscale.index.metadata.abap;

import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import org.conqat.lib.commons.collections.CollectionUtils;
import org.conqat.lib.commons.collections.Pair;
import org.conqat.lib.commons.string.StringUtils;

import com.teamscale.commons.utils.ParameterFileUtils;
import com.teamscale.index.metadata.IFileMetadata;

import eu.cqse.check.framework.scanner.ELanguage;

/**
 * Reader for metadata files for ABAP code files/objects.
 */
public class AbapFileMetadataReader {

	/** File extension for metadata files in Abap projects. */
	public static final String ABAP_METADATA_FILE_EXTENSION = "metadata";
	/**
	 * The languages that can have Abap Metadata files. More precise: files of these
	 * languages can have Abap Metadata files next to them. The metadata file will
	 * have the same name as the code file, suffixed with ".metadata".
	 */
	public static final EnumSet LANGUAGES_WITH_ABAP_METADATA_FILES = EnumSet.of(ELanguage.ABAP,
			ELanguage.ABAP_DDIC, ELanguage.ABAP_CDS);
	private static final Set METADATA_FIELDS = CollectionUtils.asHashSet(
			AbapFileMetadata.CREATION_DATE_PROPERTY, AbapFileMetadata.LAST_UPDATE_DATE_TIME_PROPERTY,
			AbapFileMetadata.ORIGINATING_SYSTEM_PROPERTY, AbapFileMetadata.PROGRAM_TYPE_PROPERTY,
			AbapFileMetadata.AUTHORIZATION_GROUP_PROPERTY, AbapFileMetadata.IS_RFC_ENABLED_PROPERTY,
			AbapFileMetadata.IS_GENERATED_CODE_PROPERTY);

	/**
	 * Returns whether a given file is a metadata file.
	 */
	public static boolean isMetadataFile(String uniformPath) {
		return uniformPath.endsWith("." + ABAP_METADATA_FILE_EXTENSION);
	}

	/**
	 * Returns the file name from some metadata file.
	 */
	public static String getFileNameFromMetadataFile(String uniformPath) {
		return StringUtils.stripSuffix(uniformPath, "." + ABAP_METADATA_FILE_EXTENSION);
	}

	/**
	 * Parses the contents of some metadata file for a language and returns the
	 * appropriate {@link IFileMetadata}.
	 */
	public static AbapFileMetadata parseFileContents(String filename, String fileContents,
			Consumer errorReporter) {
		AbapFileMetadata fileMetadata = new AbapFileMetadata("", 0, 0, null, null, null, false, false);
		List lines = StringUtils.splitLinesAsList(fileContents);
		for (String line : lines) {
			Pair parameterNameAndValue = ParameterFileUtils.findParameterNameAndValue(line,
					METADATA_FIELDS);
			if (parameterNameAndValue != null) {
				AbapFileMetadataReader.saveFieldAndValueIntoMetadata(fileMetadata, parameterNameAndValue);
			} else {
				errorReporter.accept("Unknown parameter in file " + filename + " on line\\n" + line);
			}
		}
		return fileMetadata;
	}

	private static void saveFieldAndValueIntoMetadata(IFileMetadata metadata, Pair fieldNameAndValue) {
		String parameter = fieldNameAndValue.getFirst();
		switch (parameter) {
		case AbapFileMetadata.CREATION_DATE_PROPERTY:
			((AbapFileMetadata) metadata).setCreationDate(Long.parseLong(fieldNameAndValue.getSecond()));
			break;
		case AbapFileMetadata.LAST_UPDATE_DATE_TIME_PROPERTY:
			((AbapFileMetadata) metadata).setLastUpdateDateTime(Long.parseLong(fieldNameAndValue.getSecond()));
			break;
		case AbapFileMetadata.ORIGINATING_SYSTEM_PROPERTY:
			((AbapFileMetadata) metadata).setOriginatingSystem(fieldNameAndValue.getSecond());
			break;
		case AbapFileMetadata.AUTHORIZATION_GROUP_PROPERTY:
			((AbapFileMetadata) metadata).setAuthorizationGroup(fieldNameAndValue.getSecond());
			break;
		case AbapFileMetadata.PROGRAM_TYPE_PROPERTY:
			EAbapProgramType programType = EAbapProgramType.valueOf(fieldNameAndValue.getSecond());
			((AbapFileMetadata) metadata).setProgramType(programType);
			break;
		case AbapFileMetadata.IS_RFC_ENABLED_PROPERTY:
			((AbapFileMetadata) metadata).setRfcEnabled(Boolean.parseBoolean(fieldNameAndValue.getSecond()));
			break;
		case AbapFileMetadata.IS_GENERATED_CODE_PROPERTY:
			((AbapFileMetadata) metadata).setCodeIsGenerated(Boolean.parseBoolean(fieldNameAndValue.getSecond()));
			break;
		default:
			// fall-through intended
			break;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy