Skip to content

Commit

Permalink
Support Sugarcane data files by support differently named JSON files …
Browse files Browse the repository at this point in the history
…at the root of a Parchment ZIP-File. (#5)
  • Loading branch information
shartte authored Dec 30, 2023
1 parent 8005731 commit 14d40d0
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
6 changes: 0 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ project.version = gradleutils.version

println "Version ${project.version}"

allprojects {
java.toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

subprojects {
group = rootProject.group
version = rootProject.version
Expand Down
7 changes: 7 additions & 0 deletions parchment/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ dependencies {
implementation project(":api")
implementation 'org.parchmentmc.feather:io-gson:1.1.0'
implementation 'net.neoforged:srgutils:1.0.0'

testImplementation platform("org.junit:junit-bom:$junit_version")
testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ParchmentDatabase implements NamesAndDocsDatabase {
Expand All @@ -38,6 +39,10 @@ public NamesAndDocsForClass getClass(String className) {
public static ParchmentDatabase loadZip(Path parchmentFile) throws IOException {
try (var zf = new ZipFile(parchmentFile.toFile())) {
var parchmentJsonEntry = zf.getEntry("parchment.json");
if (parchmentJsonEntry == null) {
parchmentJsonEntry = findFallbackEntryJson(parchmentFile, zf);
}

if (parchmentJsonEntry == null || parchmentJsonEntry.isDirectory()) {
throw new FileNotFoundException("Could not locate parchment.json at the root of ZIP-File " + parchmentFile);
}
Expand All @@ -48,6 +53,23 @@ public static ParchmentDatabase loadZip(Path parchmentFile) throws IOException {
}
}

private static ZipEntry findFallbackEntryJson(Path parchmentFile, ZipFile zf) throws FileNotFoundException {
// Fall back to any JSON file in the root if there is only one.
var entries = zf.entries().asIterator();
ZipEntry parchmentJsonEntry = null;
while (entries.hasNext()) {
var entry = entries.next();
if (!entry.getName().contains("/") && entry.getName().endsWith(".json")) {
if (parchmentJsonEntry != null) {
throw new FileNotFoundException("Could not locate parchment.json at the root of ZIP-File " + parchmentFile
+ " and there are multiple other JSON files present in the root.");
}
parchmentJsonEntry = entry;
}
}
return parchmentJsonEntry;
}

public static ParchmentDatabase loadJson(Path parchmentFile) throws IOException {
try (var reader = Files.newBufferedReader(parchmentFile)) {
return loadJson(reader);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package net.neoforged.jst.parchment.namesanddocs.parchment;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ParchmentDatabaseTest {
// A minimal Parchment JSON to validate that it loaded *something*
@Language("JSON")
private static final String EXAMPLE_JSON = """
{
"version": "1.1.0",
"classes": [
{
"name": "TestClass"
}
]
}
""";

@TempDir
Path tempDir;

@Test
void testLoadFromJsonReader() {
var db = ParchmentDatabase.loadJson(new StringReader(EXAMPLE_JSON));
assertNotNull(db.getClass("TestClass"));
}

@Test
void testLoadFromJsonFile() throws IOException {
var tempFile = tempDir.resolve("test.json");
Files.writeString(tempFile, EXAMPLE_JSON);

var db = ParchmentDatabase.loadJson(tempFile);
assertNotNull(db.getClass("TestClass"));
}

@Test
void testLoadFromZipWithParchmentJson() throws IOException {
var tempFile = tempDir.resolve("temp.zip");
writeZip(tempFile, Map.of(
"unrelated/parchment.json", "INVALID",
"parchment.json", EXAMPLE_JSON
));

var db = ParchmentDatabase.loadZip(tempFile);
assertNotNull(db.getClass("TestClass"));
}

@Test
void testLoadFromZipWithOtherJson() throws IOException {
var tempFile = tempDir.resolve("temp.zip");
writeZip(tempFile, Map.of(
"unrelated/parchment.json", "INVALID",
"other.json", EXAMPLE_JSON
));

var db = ParchmentDatabase.loadZip(tempFile);
assertNotNull(db.getClass("TestClass"));
}

@Test
void testLoadFromZipWithMultipleOtherJson() throws IOException {
var tempFile = tempDir.resolve("temp.zip");
writeZip(tempFile, Map.of(
"unrelated/parchment.json", "INVALID",
"other.json", EXAMPLE_JSON,
"yet_another_json.json", EXAMPLE_JSON
));

assertThrows(FileNotFoundException.class, () -> ParchmentDatabase.loadZip(tempFile));
}

@Test
void testLoadFromEmptyZip() throws IOException {
var tempFile = tempDir.resolve("temp.zip");
writeZip(tempFile, Map.of());

assertThrows(FileNotFoundException.class, () -> ParchmentDatabase.loadZip(tempFile));
}

private static void writeZip(Path tempFile, Map<String, String> entries) throws IOException {
try (var zf = new ZipOutputStream(Files.newOutputStream(tempFile))) {
for (var entry : entries.entrySet()) {
zf.putNextEntry(new ZipEntry(entry.getKey()));
zf.write(entry.getValue().getBytes(StandardCharsets.UTF_8));
zf.closeEntry();
}
}
}
}

0 comments on commit 14d40d0

Please sign in to comment.