Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for invalid encoding when loading Aspect Model files #690

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@

import static org.apache.commons.lang3.StringUtils.isBlank;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnmappableCharacterException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.esmf.aspectmodel.AspectModelFile;
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
Expand All @@ -53,6 +55,11 @@ public static RawAspectModelFile load( final File file ) {
try {
final RawAspectModelFile fromString = load( content( new FileInputStream( file ) ) );
return new RawAspectModelFile( fromString.sourceModel(), fromString.headerComment(), Optional.of( file.toURI() ) );
} catch ( final ModelResolutionException exception ) {
if ( exception.getMessage().startsWith( "Encountered invalid encoding" ) ) {
throw new ModelResolutionException( "Encountered invalid encoding in input file " + file, exception.getCause() );
}
throw exception;
} catch ( final FileNotFoundException exception ) {
throw new ModelResolutionException( "File not found: " + file, exception );
}
Expand All @@ -70,8 +77,16 @@ public static RawAspectModelFile load( final String rdfTurtle ) {
}

private static String content( final InputStream inputStream ) {
return new BufferedReader( new InputStreamReader( inputStream, StandardCharsets.UTF_8 ) ).lines()
.collect( Collectors.joining( "\n" ) );
try {
final byte[] bytes = inputStream.readAllBytes();
final CharsetDecoder charsetDecoder = StandardCharsets.UTF_8.newDecoder();
final CharBuffer decodedCharBuffer = charsetDecoder.decode( ByteBuffer.wrap( bytes ) );
return new String( bytes, StandardCharsets.UTF_8 );
} catch ( final MalformedInputException | UnmappableCharacterException exception ) {
throw new ModelResolutionException( "Encountered invalid encoding in input" );
} catch ( final IOException exception ) {
throw new RuntimeException( exception );
}
}

private static List<String> headerComment( final String content ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.esmf.aspectmodel.AspectModelFile;
import org.eclipse.esmf.aspectmodel.resolver.FileSystemStrategy;
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy;
import org.eclipse.esmf.aspectmodel.resolver.exceptions.ModelResolutionException;
import org.eclipse.esmf.metamodel.AbstractEntity;
import org.eclipse.esmf.metamodel.AspectModel;
import org.eclipse.esmf.metamodel.ComplexType;
Expand All @@ -56,6 +57,13 @@ void loadAspectModelWithoutCharacteristicDatatype() {
.hasMessage( "No datatype is defined on the Characteristic instance 'Characteristic1: '." );
}

@Test
void testFileWithInvalidEncoding() {
assertThatThrownBy( () -> TestResources.load( InvalidTestAspect.INVALID_ENCODING ) )
.isInstanceOf( ModelResolutionException.class )
.hasMessageContaining( "Encountered invalid encoding" );
}

@Test
void testOfAbstractEntityCyclomaticCreation() {
final Map<String, ComplexType> entities =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
import org.eclipse.esmf.aspectmodel.resolver.modelfile.MetaModelFile;
import org.eclipse.esmf.aspectmodel.shacl.fix.Fix;
import org.eclipse.esmf.aspectmodel.shacl.violation.DatatypeViolation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum InvalidTestAspect implements TestModel {
ASPECT_WITH_SAMM_NAMESPACE_FOR_CUSTOM_UNIT,
ASPECT_WITH_RECURSIVE_PROPERTY,
INVALID_SYNTAX,
INVALID_ENCODING,
MISSING_ASPECT_DECLARATION,
INVALID_EXAMPLE_VALUE_DATATYPE,
INVALID_PREFERRED_NAME_DATATYPE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
#
# See the AUTHORS file(s) distributed with this work for additional
# information regarding authorship.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .

:InvalidSyntax a samm:Aspect ;
samm:description "The following umlaut is ISO-8859 encoded: Ä"@en ;
samm:properties () ;
samm:operations () .
Loading