Skip to content

Commit ac3d3a8

Browse files
authored
Merge pull request #593 from Systems-Modeling/ST6RI-794
ST6RI-794: Specify UTF-8 encoding to access index files.
2 parents affb8af + 5169ea0 commit ac3d3a8

5 files changed

Lines changed: 24 additions & 24 deletions

File tree

org.omg.kerml.xtext.ui/src/org/omg/kerml/xtext/ui/library/DynamicLibraryIndexProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.io.IOException;
2626
import java.io.InputStreamReader;
27+
import java.nio.charset.StandardCharsets;
2728
import java.util.Map;
2829
import java.util.WeakHashMap;
2930

@@ -85,7 +86,7 @@ public LibraryIndex getIndexFor(Resource resource) {
8586
LibraryIndex indexFromJson = LibraryIndex.EMPTY_INDEX;
8687

8788
if (indexFile.exists()) {
88-
try (var reader = new InputStreamReader(indexFile.getContents())){
89+
try (var reader = new InputStreamReader(indexFile.getContents(), StandardCharsets.UTF_8)) {
8990
indexFromJson = LibraryIndex.fromJson(reader);
9091
} catch (IOException e) {
9192
//NOOP, return empty index

org.omg.kerml.xtext/src/org/omg/kerml/xtext/library/LibraryIndex.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.io.Reader;
2727
import java.lang.reflect.Type;
28+
import java.nio.charset.StandardCharsets;
2829
import java.util.ArrayList;
2930
import java.util.Collection;
3031
import java.util.HashMap;
@@ -141,8 +142,8 @@ public String toJson(GsonBuilder builder) {
141142
private String createChecksum(Map<String, Set<String>> index) {
142143
HashCode hashedIndex = Hashing.sha256().hashObject(index, (from, into) -> {
143144
from.forEach((libFqn, shortNames) -> {
144-
into.putBytes(libFqn.getBytes());
145-
shortNames.forEach(shortName -> into.putBytes(shortName.getBytes()));
145+
into.putBytes(libFqn.getBytes(StandardCharsets.UTF_8));
146+
shortNames.forEach(shortName -> into.putBytes(shortName.getBytes(StandardCharsets.UTF_8)));
146147
});
147148
});
148149

org.omg.kerml.xtext/src/org/omg/kerml/xtext/library/PrecalculatedLibraryIndexProvider.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
package org.omg.kerml.xtext.library;
2424

2525
import java.io.File;
26+
import java.io.FileInputStream;
2627
import java.io.FileNotFoundException;
27-
import java.io.FileReader;
28-
import java.io.IOException;
29-
import java.util.Arrays;
28+
import java.io.InputStreamReader;
29+
import java.nio.charset.StandardCharsets;
3030

3131
import org.apache.log4j.Logger;
3232
import org.eclipse.emf.common.util.URI;
@@ -61,7 +61,7 @@ public class PrecalculatedLibraryIndexProvider implements ILibraryIndexProvider
6161
@Override
6262
public LibraryIndex getIndexFor(Resource resource) {
6363

64-
if (disabled) {
64+
if (disabled || resource == null) {
6565
//return empty index
6666
return LibraryIndex.EMPTY_INDEX;
6767
}
@@ -77,14 +77,13 @@ public LibraryIndex getIndexFor(Resource resource) {
7777
File indexFile = getIndexFile(resourceURI);
7878

7979
if (indexFile == null || !indexFile.exists()) return LibraryIndex.EMPTY_INDEX;
80-
81-
try (FileReader fileReader = new FileReader(indexFile)){
82-
83-
index = LibraryIndex.fromJson(fileReader);
84-
80+
81+
try (FileInputStream fis = new FileInputStream(indexFile);
82+
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
83+
index = LibraryIndex.fromJson(isr);
8584
} catch (FileNotFoundException e) {
8685
//NOOP, return empty index
87-
} catch (IOException e) {
86+
} catch (Exception e) {
8887
if (log.isDebugEnabled()) {
8988
log.debug(e.getMessage(), e);
9089
}
@@ -103,13 +102,12 @@ public boolean isIndexDisabled() {
103102
}
104103

105104
private File getIndexFile(URI uri) {
106-
var segments = Arrays.asList(uri.segments());
107-
if (segments.contains(LIBRARY_FOLDER)) {
108-
String pathString = uri.path();
109-
String indexPath = pathString.split(LIBRARY_FOLDER)[0] + LIBRARY_FOLDER + File.separator + LibraryIndex.FILE_NAME;
110-
return new File(indexPath);
111-
}
112-
return null;
105+
String fileString = uri.toFileString();
106+
if (fileString == null) return null;
107+
int idx = fileString.lastIndexOf(LIBRARY_FOLDER);
108+
if (idx < 0) return null;
109+
String parentPath = fileString.substring(0, idx + LIBRARY_FOLDER.length());
110+
return new File(parentPath, LibraryIndex.FILE_NAME);
113111
}
114112

115113
public static synchronized PrecalculatedLibraryIndexProvider getInstance() {

org.omg.sysml.interactive/src/org/omg/sysml/interactive/SysMLInteractiveLibraryIndexGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.File;
2626
import java.io.FileOutputStream;
2727
import java.io.IOException;
28+
import java.nio.charset.StandardCharsets;
2829

2930
import org.eclipse.emf.ecore.resource.ResourceSet;
3031
import org.eclipse.xtext.EcoreUtil2;
@@ -74,7 +75,7 @@ public static void main(String[] args) throws IOException {
7475

7576
System.out.println("Writing index");
7677
try (FileOutputStream fileStream = new FileOutputStream(indexFile, false)) {
77-
fileStream.write(json.getBytes());
78+
fileStream.write(json.getBytes(StandardCharsets.UTF_8));
7879
}
7980

8081
System.out.println("Done.");

org.omg.sysml.xtext.ui/src/org/omg/sysml/xtext/ui/handlers/GenerateLibraryIndex.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.io.ByteArrayInputStream;
2727
import java.io.IOException;
28+
import java.nio.charset.StandardCharsets;
2829
import java.util.Collection;
2930
import java.util.HashSet;
3031
import java.util.Set;
@@ -36,15 +37,13 @@
3637
import org.eclipse.core.resources.IFile;
3738
import org.eclipse.core.resources.IProject;
3839
import org.eclipse.core.resources.IResource;
39-
import org.eclipse.core.resources.IWorkspace;
4040
import org.eclipse.core.resources.IncrementalProjectBuilder;
4141
import org.eclipse.core.resources.WorkspaceJob;
4242
import org.eclipse.core.runtime.CoreException;
4343
import org.eclipse.core.runtime.IProgressMonitor;
4444
import org.eclipse.core.runtime.IStatus;
4545
import org.eclipse.core.runtime.Status;
4646
import org.eclipse.core.runtime.SubMonitor;
47-
import org.eclipse.core.runtime.jobs.ISchedulingRule;
4847
import org.eclipse.emf.common.util.URI;
4948
import org.eclipse.emf.ecore.resource.Resource;
5049
import org.eclipse.emf.ecore.resource.ResourceSet;
@@ -152,7 +151,7 @@ public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
152151
private void writeIndex(IProject project, String json, IProgressMonitor monitor) throws CoreException {
153152
IFile file = project.getFile(LibraryIndex.FILE_NAME);
154153

155-
try (var inputStream = new ByteArrayInputStream(json.getBytes())){
154+
try (var inputStream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))){
156155
if (file.exists()) {
157156
file.setContents(inputStream, IFile.FORCE, monitor);
158157
} else {

0 commit comments

Comments
 (0)