Skip to content

Commit 4a8ee92

Browse files
committed
TIKA-4734 -- address copilot feedback
1 parent a1560f0 commit 4a8ee92

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

tika-app/src/main/java/org/apache/tika/cli/TikaCLI.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,9 @@ private boolean testForAsync(String[] args) {
402402
}
403403
}
404404

405-
// Single .json file is a config file for async mode
406-
if (args.length == 1 && args[0].endsWith(".json")) {
405+
// Single .json file is a config file for async mode.
406+
// Reject option-style args like `--config=foo.json` so they fall through to process().
407+
if (args.length == 1 && !args[0].startsWith("-") && args[0].endsWith(".json")) {
407408
return true;
408409
}
409410

@@ -623,8 +624,12 @@ private void convertConfigXmlToJson(String inputPath) throws Exception {
623624

624625
Path xmlPath = Paths.get(inputPath.trim());
625626

626-
if (!Files.exists(xmlPath)) {
627-
System.err.println("Error: Input XML file not found: " + xmlPath);
627+
if (!Files.isRegularFile(xmlPath)) {
628+
System.err.println("Error: Input XML path is not a regular file: " + xmlPath);
629+
return;
630+
}
631+
if (!Files.isReadable(xmlPath)) {
632+
System.err.println("Error: Input XML file is not readable: " + xmlPath);
628633
return;
629634
}
630635

@@ -782,7 +787,7 @@ private void usage() {
782787
out.println(" -g or --gui Start the Apache Tika GUI");
783788
out.println();
784789
out.println(" --config=<tika-config.json>");
785-
out.println(" TikaConfig file (JSON as of Tika 4.x). Must be specified before -g, -s or -f !");
790+
out.println(" TikaConfig file (JSON as of Tika 4.x). Must be specified before -g or -f !");
786791
// TODO: TIKA-XXXX - Re-enable config dump options once JSON serialization is complete
787792
// These options are not yet implemented in 4.x due to the migration from XML to JSON config
788793
// out.println(" --dump-minimal-config Print minimal TikaConfig");

tika-app/src/test/java/org/apache/tika/cli/TikaCLITest.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.List;
3939
import java.util.Set;
4040

41+
import com.fasterxml.jackson.databind.JsonNode;
42+
import com.fasterxml.jackson.databind.ObjectMapper;
4143
import org.junit.jupiter.api.AfterEach;
4244
import org.junit.jupiter.api.BeforeEach;
4345
import org.junit.jupiter.api.Disabled;
@@ -772,11 +774,23 @@ public void testConvertConfigXmlToJson() throws Exception {
772774
String xmlPath = Paths.get(getClass().getResource("/xml-configs/tika-config-simple.xml").toURI()).toString();
773775
String content = getParamOutContent("--convert-config-xml-to-json=" + xmlPath);
774776

775-
// stdout should contain the converted JSON (and only the JSON)
776-
assertTrue(content.contains("\"parsers\""), "Expected JSON parsers section, got: " + content);
777-
assertTrue(content.contains("pdf-parser"), "Expected pdf-parser in output, got: " + content);
778-
assertTrue(content.contains("\"sortByPosition\" : true"), "Expected converted param, got: " + content);
779-
assertTrue(content.trim().startsWith("{"), "Output should be pure JSON, got: " + content);
777+
// stdout should be pure JSON; parse and assert on structure, not formatting
778+
JsonNode root = new ObjectMapper().readTree(content.trim());
779+
JsonNode parsers = root.get("parsers");
780+
assertNotNull(parsers, "Expected parsers section, got: " + content);
781+
assertTrue(parsers.isArray() && parsers.size() > 0, "Expected non-empty parsers array, got: " + content);
782+
783+
JsonNode pdfEntry = null;
784+
for (JsonNode entry : parsers) {
785+
if (entry.has("pdf-parser")) {
786+
pdfEntry = entry.get("pdf-parser");
787+
break;
788+
}
789+
}
790+
assertNotNull(pdfEntry, "Expected pdf-parser entry, got: " + content);
791+
JsonNode sortByPosition = pdfEntry.findValue("sortByPosition");
792+
assertNotNull(sortByPosition, "Expected sortByPosition under pdf-parser, got: " + content);
793+
assertTrue(sortByPosition.asBoolean(), "Expected sortByPosition=true, got: " + sortByPosition);
780794
}
781795

782796
/**

0 commit comments

Comments
 (0)