Skip to content

Commit a759532

Browse files
authored
Add charset utf 8 with readstring for windows problems (#47)
1 parent af205f4 commit a759532

File tree

4 files changed

+36
-57
lines changed

4 files changed

+36
-57
lines changed

src/main/java/org/eclipse/esmf/ame/repository/strategy/LocalFolderResolverStrategy.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
import java.io.File;
1717
import java.io.IOException;
18+
import java.io.InputStream;
1819
import java.nio.channels.FileChannel;
20+
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
1922
import java.nio.file.FileSystem;
2023
import java.nio.file.Files;
2124
import java.nio.file.Path;
@@ -275,7 +278,7 @@ private List<AspectModelInformation> getListOfAspectModels( final List<String> f
275278
final String[] arg = transformToValidModelDirectory( path ).split( ":" );
276279
final String namespace = arg[0] + ":" + arg[1];
277280
final String fileName = arg[2];
278-
String aspectModel = Files.readString( importFileSystem.getPath( path ) );
281+
String aspectModel = LocalFolderResolverUtils.readString( importFileSystem.getPath( path ), StandardCharsets.UTF_8 );
279282
return new AspectModelInformation( namespace, fileName, aspectModel );
280283
} catch ( IOException e ) {
281284
throw new FileNotFoundException( "Cannot find in-memory file to create package information", e );

src/main/java/org/eclipse/esmf/ame/repository/strategy/utils/LocalFolderResolverUtils.java

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
import java.io.File;
1717
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.nio.charset.Charset;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
1822
import java.util.Objects;
1923

2024
import javax.annotation.Nonnull;
@@ -73,48 +77,17 @@ public static String buildFilePath( final String namespace, final String fileNam
7377
}
7478

7579
/**
76-
* This method will convert the given urn to AspectModelUrn.
80+
* Reads the content of a file located at the specified path using the provided character encoding.
7781
*
78-
* @param urn - urn of the aspect model.
79-
* @return AspectModelUrn.
82+
* @param path The path to the file to be read.
83+
* @param charset The character encoding to be used for decoding the file content.
84+
* @return The content of the file as a string decoded with the specified character encoding.
85+
* @throws IOException If an I/O error occurs while reading the file.
8086
*/
81-
public static AspectModelUrn convertToAspectModelUrn( final String urn ) {
82-
return AspectModelUrn.from( urn ).getOrElse( () -> {
83-
throw new InvalidAspectModelException(
84-
String.format( "The URN constructed from the input file path is invalid: %s", urn ) );
85-
} );
86-
}
87-
88-
/**
89-
* This method will delete the given directory and all of its contents.
90-
*
91-
* @param storagePath - path of the directory to be deleted.
92-
*/
93-
public static void deleteDirectory( final File storagePath ) {
94-
try {
95-
if ( storagePath.exists() && storagePath.isDirectory() ) {
96-
handleFiles( storagePath );
97-
FileUtils.forceDelete( storagePath );
98-
}
99-
} catch ( final IOException error ) {
100-
LOG.error( "Cannot delete exported package folder." );
101-
throw new FileNotFoundException( String.format( "Unable to delete folder on: %s", storagePath ), error );
102-
}
103-
}
104-
105-
/**
106-
* This method will unlock all files in the given directory.
107-
*
108-
* @param storagePath path of the directory to be deleted.
109-
* @throws IOException if an I/O error occurs.
110-
*/
111-
private static void handleFiles( final File storagePath ) throws IOException {
112-
for ( final File file : Objects.requireNonNull( storagePath.listFiles() ) ) {
113-
if ( file.isDirectory() ) {
114-
handleFiles( file );
115-
}
116-
117-
FileUtils.forceDelete( file );
87+
public static String readString( Path path, Charset charset) throws IOException {
88+
try ( InputStream inputStream = Files.newInputStream(path)) {
89+
byte[] bytes = inputStream.readAllBytes();
90+
return new String(bytes, charset);
11891
}
11992
}
12093
}

src/main/java/org/eclipse/esmf/ame/resolver/strategy/InMemoryStrategy.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.File;
1919
import java.io.FileNotFoundException;
2020
import java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
2122
import java.nio.file.FileSystem;
2223
import java.nio.file.Files;
2324
import java.nio.file.Path;
@@ -26,6 +27,7 @@
2627

2728
import org.apache.jena.rdf.model.Model;
2829
import org.apache.jena.riot.RiotException;
30+
import org.eclipse.esmf.ame.repository.strategy.utils.LocalFolderResolverUtils;
2931
import org.eclipse.esmf.aspectmodel.resolver.AspectModelResolver;
3032
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
3133

@@ -35,39 +37,39 @@ public class InMemoryStrategy extends ResolutionStrategy {
3537
public final FileSystem fileSystem;
3638

3739
public InMemoryStrategy( final String aspectModel, final Path processingRootPath, final FileSystem fileSystem )
38-
throws RiotException {
40+
throws RiotException {
3941
super( aspectModel, processingRootPath );
4042
this.fileSystem = fileSystem;
4143
}
4244

4345
protected Try<Model> getModelFromFileSystem( final AspectModelUrn aspectModelUrn, final Path rootPath ) {
4446
try ( Stream<Path> pathStream = Files.walk( rootPath ) ) {
4547
final String filePath =
46-
aspectModelUrn.getNamespace() + File.separator + aspectModelUrn.getVersion() + File.separator
47-
+ aspectModelUrn.getName() + ".ttl";
48+
aspectModelUrn.getNamespace() + File.separator + aspectModelUrn.getVersion() + File.separator
49+
+ aspectModelUrn.getName() + ".ttl";
4850

4951
final Path file = fileSystem.getPath( filePath );
5052

5153
if ( Files.exists( file ) ) {
52-
return Try.of( () -> loadTurtleFromString( Files.readString( file ) ) );
54+
return Try.of( () -> loadTurtleFromString( LocalFolderResolverUtils.readString( file, StandardCharsets.UTF_8 ) ) );
5355
}
5456

5557
LOG.warn( "Looking for {}, but no {}.ttl was found. Inspecting files in {}", aspectModelUrn.getName(),
56-
aspectModelUrn.getName(), filePath );
58+
aspectModelUrn.getName(), filePath );
5759

5860
Optional<Try<Model>> modelWithDefinition = pathStream
59-
.filter( Files::isRegularFile )
60-
.map( Path::toAbsolutePath )
61-
.map( Path::toString )
62-
.map( fileSystem::getPath )
63-
.map( aspectModelPath -> Try.of( () -> loadTurtleFromString( Files.readString( aspectModelPath ) ) ) )
64-
.filter( tryModel -> tryModel.map(
65-
model -> AspectModelResolver.containsDefinition( model, aspectModelUrn ) )
66-
.getOrElse( false ) )
67-
.findFirst();
61+
.filter( Files::isRegularFile )
62+
.map( Path::toAbsolutePath )
63+
.map( Path::toString )
64+
.map( fileSystem::getPath )
65+
.map( aspectModelPath -> Try.of( () -> loadTurtleFromString( LocalFolderResolverUtils.readString( aspectModelPath, StandardCharsets.UTF_8 ) ) ) )
66+
.filter( tryModel -> tryModel.map(
67+
model -> AspectModelResolver.containsDefinition( model, aspectModelUrn ) )
68+
.getOrElse( false ) )
69+
.findFirst();
6870

6971
return modelWithDefinition.orElse( Try.failure( new FileNotFoundException(
70-
"No model file containing " + aspectModelUrn + " could be found in directory" ) ) );
72+
"No model file containing " + aspectModelUrn + " could be found in directory" ) ) );
7173
} catch ( IOException exception ) {
7274
return Try.failure( exception );
7375
}

src/main/java/org/eclipse/esmf/ame/services/PackageService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.File;
1717
import java.io.IOException;
1818
import java.io.InputStream;
19+
import java.nio.charset.StandardCharsets;
1920
import java.nio.file.FileSystem;
2021
import java.nio.file.Files;
2122
import java.nio.file.Path;
@@ -172,7 +173,7 @@ public List<String> importAspectModelPackage( final List<AspectModelFiles> aspec
172173
try {
173174
final FolderStructure folderStructure = LocalFolderResolverUtils.extractFilePath( data.getNamespace() );
174175
folderStructure.setFileName( fileName );
175-
String aspectModel = Files.readString( importFileSystem.getPath( folderStructure.toString() ) );
176+
String aspectModel = LocalFolderResolverUtils.readString( importFileSystem.getPath( folderStructure.toString() ), StandardCharsets.UTF_8 );
176177
Optional<String> namespaceVersion = Optional.of(
177178
folderStructure.getFileRootPath() + File.separator + folderStructure.getVersion() );
178179

0 commit comments

Comments
 (0)