Skip to content

Commit 535ae35

Browse files
authored
Fix copyright header after saving files (#80)
1 parent 1d972b4 commit 535ae35

File tree

3 files changed

+26
-5
lines changed
  • aspect-model-editor-core/src/main/java/org/eclipse/esmf/ame/utils
  • aspect-model-editor-migrator/src/main/java/org/eclipse/esmf/ame/utils
  • aspect-model-editor-service/src/main/java/org/eclipse/esmf/ame/services

3 files changed

+26
-5
lines changed

aspect-model-editor-core/src/main/java/org/eclipse/esmf/ame/utils/ModelUtils.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.nio.file.Path;
2121
import java.util.Arrays;
2222
import java.util.Optional;
23+
import java.util.stream.Collectors;
2324

2425
import org.apache.commons.lang3.StringUtils;
2526
import org.apache.jena.rdf.model.Model;
@@ -49,7 +50,7 @@ private ModelUtils() {
4950
*
5051
* @throws FileReadException If there are failures in the generation process due to violations in the model.
5152
*/
52-
public static AspectContext getAspectContext( Try<AspectContext> context ) {
53+
public static AspectContext getAspectContext( final Try<AspectContext> context ) {
5354
return context.recover( throwable -> {
5455
throw new FileReadException( throwable.getMessage() );
5556
} ).get();
@@ -120,12 +121,27 @@ private static Try<Model> loadFromUrl( final URL url ) {
120121
*
121122
* @throws FileHandlingException If the file contains path information´s.
122123
*/
123-
public static String sanitizeFileInformation( String fileInformation ) {
124+
public static String sanitizeFileInformation( final String fileInformation ) {
124125
if ( fileInformation.contains( File.separator ) || fileInformation.contains( ".." ) ) {
125126
throw new FileHandlingException(
126127
"Invalid file information: The provided string must not contain directory separators or relative path components." );
127128
}
128129

129130
return new File( fileInformation ).getName();
130131
}
132+
133+
/**
134+
* Extracts and concatenates all lines from the provided aspect model string that start with a "#".
135+
* This method is typically used to gather all comment lines (which start with "#") from a model represented as a
136+
* string.
137+
*
138+
* @param aspectModel The aspect model represented as a string.
139+
* @return A string containing all lines from the aspect model that start with "#", each line separated by a newline.
140+
*/
141+
public static String getCopyRightHeader( final String aspectModel ) {
142+
final String[] lines = aspectModel.split( "\\r?\\n" );
143+
return Arrays.stream( lines )
144+
.filter( line -> line.startsWith( "#" ) )
145+
.collect( Collectors.joining( "\n" ) );
146+
}
131147
}

aspect-model-editor-migrator/src/main/java/org/eclipse/esmf/ame/utils/MigratorUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public static String migrateModel( final String aspectModel ) throws InvalidAspe
3737
final VersionedModel versionedModel = migratedFile.getOrElseThrow(
3838
error -> new InvalidAspectModelException( "Aspect Model cannot be migrated.", error ) );
3939

40-
return ResolverUtils.getPrettyPrintedVersionedModel( versionedModel,
40+
final String prettyPrintedVersionedModel = ResolverUtils.getPrettyPrintedVersionedModel( versionedModel,
4141
fileSystemStrategy.getAspectModelUrn().getUrn() );
42+
43+
return ModelUtils.getCopyRightHeader( aspectModel ) + "\n\n" + prettyPrintedVersionedModel;
4244
}
4345
}

aspect-model-editor-service/src/main/java/org/eclipse/esmf/ame/services/ModelService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public String saveModel( final Optional<String> namespace, final Optional<String
6969
final ModelResolverStrategy strategy = modelResolverRepository.getStrategy( LocalFolderResolverStrategy.class );
7070
final String prettyPrintedModel = ResolverUtils.getPrettyPrintedModel( aspectModel );
7171

72-
return strategy.saveModel( namespace, fileName, prettyPrintedModel );
72+
return strategy.saveModel( namespace, fileName,
73+
ModelUtils.getCopyRightHeader( aspectModel ) + "\n\n" + prettyPrintedModel );
7374
}
7475

7576
private void saveVersionedModel( final VersionedModel versionedModel, final String namespace,
@@ -163,6 +164,8 @@ private void namespaceFileInfo( final VersionedNamespaceFiles versionedNamespace
163164
}
164165

165166
public String getFormattedModel( final String aspectModel ) {
166-
return ResolverUtils.getPrettyPrintedModel( aspectModel );
167+
final String prettyPrintedModel = ResolverUtils.getPrettyPrintedModel( aspectModel );
168+
169+
return ModelUtils.getCopyRightHeader( aspectModel ) + "\n\n" + prettyPrintedModel;
167170
}
168171
}

0 commit comments

Comments
 (0)