Skip to content

Commit

Permalink
Merge pull request workcraft#1290 from danilovesky/refinement-canonic…
Browse files Browse the repository at this point in the history
…al-path

Use canonical path for base dir of file references
  • Loading branch information
danilovesky authored Oct 27, 2021
2 parents 9ef1ba4 + 3079505 commit f1f4006
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion workcraft/WorkcraftCore/src/org/workcraft/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Info {
private static final String subtitle3 = "Return of the Hazard";
private static final String subtitle4 = "Revenge of the Timing Assumption";

private static final Version version = new Version(3, 3, 6, Status.ALPHA);
private static final Version version = new Version(3, 3, 6, Status.BETA);

private static final int startYear = 2006;
private static final int currentYear = Calendar.getInstance().get(Calendar.YEAR);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package org.workcraft.utils;

import java.io.File;
import java.io.IOException;

public class ExecutableUtils {

public static String getAbsoluteCommandPath(String toolName) {
return getAbsoluteCommandPath(new File(toolName));
}

public static String getAbsoluteCommandPath(File file) {
String result = null;
if (file != null) {
result = file.getPath();
if (file.exists()) {
result = FileUtils.getFullPath(file);
File toolFile = new File(toolName);
if (toolFile.exists()) {
try {
return toolFile.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
return toolFile.getPath();
}

}
33 changes: 26 additions & 7 deletions workcraft/WorkcraftCore/src/org/workcraft/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,24 @@ public static void openExternally(String fileName, String errorTitle) {
}

public static String getFullPath(File file) {
return file == null ? null : file.getAbsolutePath();
if (file == null) {
return null;
}
if (file.exists()) {
try {
return file.getCanonicalPath();
} catch (IOException e) {
}
}
return file.getAbsolutePath();
}

public static String getBasePath(File file) {
return file == null ? null : file.getAbsoluteFile().getParent();
try {
return file.getCanonicalFile().getParent();
} catch (IOException e) {
return null;
}
}

public static String stripBase(String path, String base) {
Expand Down Expand Up @@ -333,11 +346,17 @@ public static String appendFileSeparator(String path, String separator) {
}

public static File getFileByPathAndBase(String path, String base) {
File result = null;
if (path != null) {
result = new File(path);
if (!result.isAbsolute()) {
result = new File(base, path);
if (path == null) {
return null;
}
File result = new File(path);
if (!result.isAbsolute()) {
result = new File(base, path);
}
if (result.exists()) {
try {
return result.getCanonicalFile();
} catch (IOException e) {
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public static ModelEntry loadModel(File file) throws DeserialisationException {
CompatibilityManager cm = framework.getCompatibilityManager();
InputStream is = cm.process(file);
me = loadModel(is);
String base = FileUtils.getBasePath(file);
String base = file.getCanonicalFile().getParent();
adjustPropertyFilePaths(me.getVisualModel(), base, true);
} catch (OperationCancelledException e) {
} catch (OperationCancelledException | IOException e) {
// Operation cancelled by the user
}
} else {
Expand Down Expand Up @@ -324,7 +324,7 @@ private static InputStream getZipEntry(String name, byte[] bytes) throws IOExcep
public static Collection<Resource> loadResources(File file) throws DeserialisationException {
try (InputStream is = new FileInputStream(file)) {
Collection<Resource> resources = loadResources(is);
String base = FileUtils.getBasePath(file);
String base = file.getAbsoluteFile().getParent();
return adjustResourceFilePaths(resources, base, true);
} catch (IOException e) {
throw new DeserialisationException(e);
Expand Down Expand Up @@ -354,7 +354,7 @@ public static void saveModel(ModelEntry me, Collection<Resource> resources, File

try {
FileOutputStream os = new FileOutputStream(file);
String base = FileUtils.getBasePath(file);
String base = file.getCanonicalFile().getParent();
adjustPropertyFilePaths(me.getVisualModel(), base, false);
Collection<Resource> adjustedResources = adjustResourceFilePaths(resources, base, false);
saveModel(me, adjustedResources, os);
Expand Down

0 comments on commit f1f4006

Please sign in to comment.