• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / ro-crate-java / #392

29 Apr 2025 06:44PM UTC coverage: 89.357%. First build
#392

Pull #233

github

Pfeil
fix: add newline at end of gradle.yml for consistency
Pull Request #233: Version 2.1.0

608 of 700 new or added lines in 28 files covered. (86.86%)

1889 of 2114 relevant lines covered (89.36%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.65
/src/main/java/edu/kit/datamanager/ro_crate/preview/PreviewGenerator.java
1
package edu.kit.datamanager.ro_crate.preview;
2

3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStreamReader;
6

7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9

10
/**
11
 * Class responsible for the generation of the human-readable representation of
12
 * the metadata. For this to work the nodejs library rochtml has to be
13
 * installed. This can be done using: npm install -g ro-crate-html-js or npm
14
 * install ro-crate-html-js
15
 */
16
public class PreviewGenerator {
×
17

18
    private static Logger LOG = LoggerFactory.getLogger(PreviewGenerator.class);
1✔
19
    private static final String command = "rochtml";
20

21
    public static boolean isRochtmlAvailable() {
22
        ProcessBuilder builder = new ProcessBuilder();
1✔
23
        // this is the equivalent of "rochtml dir/ro-crate-metadata.json"
24
        // check if we are running on windows or unix
25
        String os = System.getProperty("os.name").toLowerCase();
1✔
26
        if (os.contains("win")) {
1✔
NEW
27
            builder.command("cmd.exe", "/c", command);
×
28

29
        } else {
30
            builder.command("sh", "-c", command);
1✔
31
        }
32
        Process process;
33
        try {
34
            process = builder.start();
1✔
35
            int exitVal = process.waitFor();
1✔
36
            return exitVal == 0;
1✔
NEW
37
        } catch (InterruptedException | IOException ex) {
×
38
        }
NEW
39
        return false;
×
40
    }
41

42
    /**
43
     * The method that from the location of the crate generates the html file.
44
     *
45
     * @param location the location of the crate in the filesystem.
46
     */
47
    public static void generatePreview(String location) {
48
        if (!isRochtmlAvailable()) {
1✔
NEW
49
            LOG.error("rochtml is not available. Please install rochtml using npm install -g ro-crate-html-js");
×
NEW
50
            return;
×
51
        }
52
        ProcessBuilder builder = new ProcessBuilder();
1✔
53
        // this is the equivalent of "rochtml dir/ro-crate-metadata.json"
54
        // check if we are running on windows or unix
55
        String os = System.getProperty("os.name").toLowerCase();
1✔
56
        if (os.contains("win")) {
1✔
NEW
57
            builder.command("cmd.exe", "/c", command + " " + location + "/ro-crate-metadata.json");
×
58

59
        } else {
60
            builder.command("sh", "-c", command + " " + location + "/ro-crate-metadata.json");
1✔
61
        }
62

63
        Process process;
64
        try {
65
            process = builder.start();
1✔
66
            StringBuilder output = new StringBuilder();
1✔
67

68
            BufferedReader reader = new BufferedReader(
1✔
69
                    new InputStreamReader(process.getInputStream()));
1✔
70

71
            String line;
72
            while ((line = reader.readLine()) != null) {
1✔
NEW
73
                output.append(line).append("\n");
×
74
            }
75
            reader.close();
1✔
76
            int exitVal = process.waitFor();
1✔
77
            if (exitVal != 0) {
1✔
78
                //abnormal...
NEW
79
                throw new Exception("failed command");
×
80
            }
NEW
81
        } catch (Exception e) {
×
NEW
82
            LOG.error("Error while generating preview: {}", e.getMessage());
×
83
        }
1✔
84
    }
1✔
85
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc