Skip to content

Commit

Permalink
Merge pull request #653 from vChavezB/copy-direct-resources
Browse files Browse the repository at this point in the history
Copy resources directly without zip
  • Loading branch information
dgarijo authored Dec 18, 2023
2 parents cbc2097 + 3ee9c26 commit 9bbbbd3
Show file tree
Hide file tree
Showing 16 changed files with 41,267 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/widoco/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public Configuration() {
// create a temporal folder with all LODE resources
tmpFolder = new File("tmp" + new Date().getTime());
tmpFolder.mkdir();
WidocoUtils.unZipIt(Constants.LODE_RESOURCES, tmpFolder.getName());
WidocoUtils.copyResourceDir(Constants.LODE_PATH, tmpFolder);
} catch (Exception ex) {
logger.error("Error while creating the temporal file for storing the intermediate Widoco files.");
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/widoco/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -1241,9 +1241,9 @@ public static String getProvenanceRDF(Configuration c) {
return provrdf;
}

public static final String LODE_RESOURCES = "/lode.zip";
public static final String OOPS_RESOURCES = "/oops.zip";
public static final String WEBVOWL_RESOURCES = "/webvowl_1.1.7_patched.zip";
public static final String LODE_PATH = "lode";
public static final String OOPS_PATH = "oops";
public static final String WEBVOWL_PATH = "webvowl_1.1.7_patched";

public static final String CONFIG_PATH = "config" + File.separator + "config.properties";

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/widoco/CreateOOPSEvalInThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void run() {
if (!evalFolder.exists())
evalFolder.mkdir();
evalResourcesFolder.mkdir();
WidocoUtils.unZipIt(Constants.OOPS_RESOURCES, evalResourcesFolder.getAbsolutePath());
WidocoUtils.copyResourceDir(Constants.OOPS_PATH, evalResourcesFolder);
//eval = new OOPSevaluation("", content);
eval = new OOPSevaluation(content);
evaluation = eval.printEvaluation();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/widoco/CreateResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void generateDocumentation(String outFolder, Configuration c, File
}
}

public static void generateSkeleton(String folderOut, Configuration c, Properties l) {
public static void generateSkeleton(String folderOut, Configuration c, Properties l) throws IOException {
// c.setTitle("Skeleton title");
c.setIncludeDiagram(false);
c.setPublishProvenance(false);
Expand Down Expand Up @@ -426,7 +426,7 @@ public static void saveDocument(String path, String textToWrite, Configuration c

}

private static void createFolderStructure(String s, Configuration c, Properties lang) {
private static void createFolderStructure(String s, Configuration c, Properties lang) throws IOException {
File f = new File(s);
if(!c.isIncludeAllSectionsInOneDocument()){
File sections = new File(s + File.separator + "sections");
Expand Down Expand Up @@ -484,7 +484,7 @@ private static void createFolderStructure(String s, Configuration c, Properties
if (c.isCreateWebVowlVisualization()) {
File webvowl = new File(s + File.separator + "webvowl");
webvowl.mkdir();
WidocoUtils.unZipIt(Constants.WEBVOWL_RESOURCES, webvowl.getAbsolutePath());
WidocoUtils.copyResourceDir(Constants.WEBVOWL_PATH, webvowl);
}
}

Expand Down
88 changes: 87 additions & 1 deletion src/main/java/widoco/WidocoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -32,6 +34,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.io.FileUtils;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.FileDocumentSource;
import org.semanticweb.owlapi.model.*;
Expand All @@ -44,7 +47,7 @@
public class WidocoUtils {

private static final Logger logger = LoggerFactory.getLogger(WidocoUtils.class);

public static final char JAR_SEPARATOR = '/';
/**
* Method that will download the ontology to document with Widoco.
*
Expand Down Expand Up @@ -198,6 +201,89 @@ public static void copyResourceFolder(String[] resources, String savePath) throw
}
}

public static void copyResourceDir(String resourceFolder, File destinationFolder) throws IOException {
// Determine if running from JAR or as source
logger.info("copyResourceFolder from "+resourceFolder+" to "+ destinationFolder);
URL resourceUrl = WidocoUtils.class.getClassLoader().getResource(resourceFolder);
if (!destinationFolder.exists())
destinationFolder.mkdirs();
if (resourceUrl == null || !resourceUrl.getProtocol().equals("file")) {
// Running from JAR, use getResourceAsStream
copyDirFromJar(resourceFolder, destinationFolder);

} else {
// Running from source, use Files.copy
copyDirFromSrc(resourceFolder, destinationFolder);
}

}
// inspired from
// https://github.com/TriggerReactor/TriggerReactor/blob/7e71958b27231032c04d09795122dfc1d80c51b1/core/src/main/java/io/github/wysohn/triggerreactor/tools/JarUtil.java
public static void copyDirFromJar(String folderName, File destFolder) throws IOException {

byte[] buffer = new byte[1024];
File fullPath = null;
String path = WidocoUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
try {
if (!path.startsWith("file"))
path = "file://" + path;

fullPath = new File(new URI(path));
} catch (URISyntaxException e) {
logger.error("URI syntax error");
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(fullPath));

ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (!entry.getName().startsWith(folderName + JAR_SEPARATOR))
continue;

String fileName = entry.getName();

// Remove the folderName from the fileName
fileName = fileName.substring((folderName + JAR_SEPARATOR).length());

File file = new File(destFolder + File.separator + fileName);

if (fileName.isEmpty() || fileName.charAt(fileName.length() - 1) == JAR_SEPARATOR) {
// Skip empty or directory entries
continue;
}

if (!file.getParentFile().exists())
file.getParentFile().mkdirs();

if (!file.exists())
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);

int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zis.closeEntry();
zis.close();
}

private static void copyDirFromSrc(String resourceFolder, File destinationFolder) throws IOException {
URL resource = WidocoUtils.class.getClassLoader().getResource(resourceFolder);

if (resource == null) {
throw new IllegalArgumentException("Resource not found: " + resourceFolder);
}
try {
File sourceFolder = new File(resource.toURI());
// Copy only the contents of the source folder to the destination folder
FileUtils.copyDirectory(sourceFolder, destinationFolder);
} catch (URISyntaxException e) {
throw new IOException("Error copying resources to the temp folder: " + e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Method used to copy the local files: styles, images, etc.
*
Expand Down
Binary file removed src/main/resources/webvowl_1.1.7_patched.zip
Binary file not shown.
Loading

0 comments on commit 9bbbbd3

Please sign in to comment.