Skip to content
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 @@ -24,68 +24,55 @@
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Objects;
import java.util.stream.Stream;

class ResourceManager {
private static final String RESOURCE_ZIP_XSD_DOCS = "/docs.zip";
private static final int BUFFER_SIZE = 4096;

private final File xsdTempDirectory;

ResourceManager() {
// load resources
String zipFilePath = getResourceAsFile(RESOURCE_ZIP_XSD_DOCS).toString();
xsdTempDirectory = createTempDirectory();
UnzipUtility.unzip(zipFilePath, xsdTempDirectory.toString());
UnzipUtility.unzip(
this.getClass().getResourceAsStream(RESOURCE_ZIP_XSD_DOCS),
xsdTempDirectory.toString()
);
}

File getXsdFile(String version) {
return new File(xsdTempDirectory + File.separator +
version + File.separator + "watchface.xsd");
}

private File getResourceAsFile(String resource) {
File file;
URL res = getClass().getResource(resource);
if (res == null) {
throw new RuntimeException("No resource File : " + resource);
}
if (res.getProtocol().equals("jar")) {
try {
file = File.createTempFile("dwf_temp", "tmp");
} catch (IOException e) {
throw new RuntimeException(e);
}
file.deleteOnExit();

try (InputStream input = getClass().getResourceAsStream(resource);
OutputStream output = Files.newOutputStream(file.toPath())) {
byte[] bytes = new byte[BUFFER_SIZE];
int read;
while ((read = Objects.requireNonNull(input).read(bytes)) != -1) {
output.write(bytes, 0, read);
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
} else { // for IDE
file = new File(res.getFile());
}

if (!file.exists()) {
throw new RuntimeException(
"Error: Cannot read Resource File " + file + "(" + resource + ")!");
}
return file;
}

private File createTempDirectory() {
try {
File file = new File(Files.createTempDirectory("validator").toString());
file.deleteOnExit();
return file;
Path dirPath = Files.createTempDirectory("validator");
deleteFileAndContentOnExit(dirPath);
return dirPath.toFile();
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}

private static void deleteFileAndContentOnExit(Path dirPath) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (Files.exists(dirPath)) {
try (Stream<Path> walk = Files.walk(dirPath)) {
walk.sorted(Comparator.reverseOrder()) // Reverse order to delete contents first
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException e) {
// Do nothing
}
});
} catch (IOException e) {
System.err.println("Failed to delete temp dir " + dirPath);
}
}
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnzipUtility {
private static final int BUFFER_SIZE = 4096;

public static void unzip(String zipFilePath, String destDirectory) {
public static void unzip(InputStream zipInputStream, String destDirectory) {
tryCreateDirectory(destDirectory);

try (FileInputStream fileIn = new FileInputStream(zipFilePath);
ZipInputStream zipIn = new ZipInputStream(fileIn)) {
try (ZipInputStream zipIn = new ZipInputStream(zipInputStream)) {
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
final String filePath = destDirectory + File.separator + entry.getName();
Expand Down
Loading