From 535ae35cb2973e59d537bcaad013172bfea1a8c9 Mon Sep 17 00:00:00 2001 From: Michele Santoro <10807610+michelu89@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:54:10 +0200 Subject: [PATCH] Fix copyright header after saving files (#80) --- .../eclipse/esmf/ame/utils/ModelUtils.java | 20 +++++++++++++++++-- .../eclipse/esmf/ame/utils/MigratorUtils.java | 4 +++- .../esmf/ame/services/ModelService.java | 7 +++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/aspect-model-editor-core/src/main/java/org/eclipse/esmf/ame/utils/ModelUtils.java b/aspect-model-editor-core/src/main/java/org/eclipse/esmf/ame/utils/ModelUtils.java index aa88e299..16f674cf 100644 --- a/aspect-model-editor-core/src/main/java/org/eclipse/esmf/ame/utils/ModelUtils.java +++ b/aspect-model-editor-core/src/main/java/org/eclipse/esmf/ame/utils/ModelUtils.java @@ -20,6 +20,7 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.Optional; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.apache.jena.rdf.model.Model; @@ -49,7 +50,7 @@ private ModelUtils() { * * @throws FileReadException If there are failures in the generation process due to violations in the model. */ - public static AspectContext getAspectContext( Try context ) { + public static AspectContext getAspectContext( final Try context ) { return context.recover( throwable -> { throw new FileReadException( throwable.getMessage() ); } ).get(); @@ -120,7 +121,7 @@ private static Try loadFromUrl( final URL url ) { * * @throws FileHandlingException If the file contains path informationĀ“s. */ - public static String sanitizeFileInformation( String fileInformation ) { + public static String sanitizeFileInformation( final String fileInformation ) { if ( fileInformation.contains( File.separator ) || fileInformation.contains( ".." ) ) { throw new FileHandlingException( "Invalid file information: The provided string must not contain directory separators or relative path components." ); @@ -128,4 +129,19 @@ public static String sanitizeFileInformation( String fileInformation ) { return new File( fileInformation ).getName(); } + + /** + * Extracts and concatenates all lines from the provided aspect model string that start with a "#". + * This method is typically used to gather all comment lines (which start with "#") from a model represented as a + * string. + * + * @param aspectModel The aspect model represented as a string. + * @return A string containing all lines from the aspect model that start with "#", each line separated by a newline. + */ + public static String getCopyRightHeader( final String aspectModel ) { + final String[] lines = aspectModel.split( "\\r?\\n" ); + return Arrays.stream( lines ) + .filter( line -> line.startsWith( "#" ) ) + .collect( Collectors.joining( "\n" ) ); + } } diff --git a/aspect-model-editor-migrator/src/main/java/org/eclipse/esmf/ame/utils/MigratorUtils.java b/aspect-model-editor-migrator/src/main/java/org/eclipse/esmf/ame/utils/MigratorUtils.java index fdc19236..d1cd9072 100644 --- a/aspect-model-editor-migrator/src/main/java/org/eclipse/esmf/ame/utils/MigratorUtils.java +++ b/aspect-model-editor-migrator/src/main/java/org/eclipse/esmf/ame/utils/MigratorUtils.java @@ -37,7 +37,9 @@ public static String migrateModel( final String aspectModel ) throws InvalidAspe final VersionedModel versionedModel = migratedFile.getOrElseThrow( error -> new InvalidAspectModelException( "Aspect Model cannot be migrated.", error ) ); - return ResolverUtils.getPrettyPrintedVersionedModel( versionedModel, + final String prettyPrintedVersionedModel = ResolverUtils.getPrettyPrintedVersionedModel( versionedModel, fileSystemStrategy.getAspectModelUrn().getUrn() ); + + return ModelUtils.getCopyRightHeader( aspectModel ) + "\n\n" + prettyPrintedVersionedModel; } } diff --git a/aspect-model-editor-service/src/main/java/org/eclipse/esmf/ame/services/ModelService.java b/aspect-model-editor-service/src/main/java/org/eclipse/esmf/ame/services/ModelService.java index 498b32c9..0cc7c344 100644 --- a/aspect-model-editor-service/src/main/java/org/eclipse/esmf/ame/services/ModelService.java +++ b/aspect-model-editor-service/src/main/java/org/eclipse/esmf/ame/services/ModelService.java @@ -69,7 +69,8 @@ public String saveModel( final Optional namespace, final Optional