Skip to content

Commit

Permalink
Fix copyright header after saving files (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelu89 authored Jun 14, 2024
1 parent 1d972b4 commit 535ae35
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AspectContext> context ) {
public static AspectContext getAspectContext( final Try<AspectContext> context ) {
return context.recover( throwable -> {
throw new FileReadException( throwable.getMessage() );
} ).get();
Expand Down Expand Up @@ -120,12 +121,27 @@ private static Try<Model> 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." );
}

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" ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public String saveModel( final Optional<String> namespace, final Optional<String
final ModelResolverStrategy strategy = modelResolverRepository.getStrategy( LocalFolderResolverStrategy.class );
final String prettyPrintedModel = ResolverUtils.getPrettyPrintedModel( aspectModel );

return strategy.saveModel( namespace, fileName, prettyPrintedModel );
return strategy.saveModel( namespace, fileName,
ModelUtils.getCopyRightHeader( aspectModel ) + "\n\n" + prettyPrintedModel );
}

private void saveVersionedModel( final VersionedModel versionedModel, final String namespace,
Expand Down Expand Up @@ -163,6 +164,8 @@ private void namespaceFileInfo( final VersionedNamespaceFiles versionedNamespace
}

public String getFormattedModel( final String aspectModel ) {
return ResolverUtils.getPrettyPrintedModel( aspectModel );
final String prettyPrintedModel = ResolverUtils.getPrettyPrintedModel( aspectModel );

return ModelUtils.getCopyRightHeader( aspectModel ) + "\n\n" + prettyPrintedModel;
}
}

0 comments on commit 535ae35

Please sign in to comment.