You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2026/10/20 19:18:49 UTC
(tika) branch main updated: keep ISArchiveParser directory and study name per parse (#3374)
This is an automated email from the ASF dual-hosted git repository.
tballison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git
The following commit(s) were added to refs/heads/main by this push:
new 8eb43984c3 keep ISArchiveParser directory and study name per parse (#3374)
8eb43984c3 is described below
commit 8eb43984c35b0659d8ce01e86fa3261ba6dd68be
Author: Naveed <mr...@gmail.com>
AuthorDate: Wed Oct 21 00:48:43 2026 +0530
keep ISArchiveParser directory and study name per parse (#3374)
---
.../apache/tika/parser/isatab/ISArchiveParser.java | 28 ++++++++++----------
.../tika/parser/isatab/ISArchiveParserTest.java | 30 ++++++++++++++++++++++
2 files changed, 45 insertions(+), 13 deletions(-)
diff --git a/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/main/java/org/apache/tika/parser/isatab/ISArchiveParser.java b/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/main/java/org/apache/tika/parser/isatab/ISArchiveParser.java
index c96a481900..0a363c1ad8 100644
--- a/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/main/java/org/apache/tika/parser/isatab/ISArchiveParser.java
+++ b/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/main/java/org/apache/tika/parser/isatab/ISArchiveParser.java
@@ -50,9 +50,7 @@ public class ISArchiveParser implements Parser {
private static String studyAssayFileNameField = ISATabUtils.ISATAB.key("Study Assay File Name");
private final Set<MediaType> SUPPORTED_TYPES =
Collections.singleton(MediaType.application("x-isatab"));
- private String location = null;
-
- private String studyFileName = null;
+ private final String location;
/**
* Default constructor.
@@ -85,12 +83,14 @@ public class ISArchiveParser implements Parser {
TemporaryResources tmp = null;
try {
- if (this.location == null) {
- this.location = tis.getFile().getParent() + File.separator;
+ // the parser is shared across parses: resolve the directory per document
+ String location = this.location;
+ if (location == null) {
+ location = tis.getFile().getParent() + File.separator;
}
// Use resource name from metadata if available, fall back to file name
String resourceName = metadata.get(TikaCoreProperties.RESOURCE_NAME_KEY);
- this.studyFileName = resourceName != null ? resourceName : tis.getFile().getName();
+ String studyFileName = resourceName != null ? resourceName : tis.getFile().getName();
File locationFile = new File(location);
String[] investigationList = locationFile.list((dir, name) -> name.matches("i_.+\\.txt"));
@@ -98,9 +98,9 @@ public class ISArchiveParser implements Parser {
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata, context);
xhtml.startDocument();
- parseInvestigation(investigationList, xhtml, metadata, context);
+ parseInvestigation(location, studyFileName, investigationList, xhtml, metadata, context);
parseStudy(tis, xhtml, metadata, context);
- parseAssay(xhtml, metadata, context);
+ parseAssay(location, xhtml, metadata, context);
xhtml.endDocument();
} finally {
@@ -110,7 +110,8 @@ public class ISArchiveParser implements Parser {
}
}
- private void parseInvestigation(String[] investigationList, XHTMLContentHandler xhtml,
+ private void parseInvestigation(String location, String studyFileName,
+ String[] investigationList, XHTMLContentHandler xhtml,
Metadata metadata, ParseContext context)
throws IOException, SAXException, TikaException {
if ((investigationList == null) || (investigationList.length == 0)) {
@@ -123,8 +124,8 @@ public class ISArchiveParser implements Parser {
}
String investigation = investigationList[0]; // TODO add to metadata?
- try (InputStream stream = TikaInputStream.get(new File(this.location + investigation).toPath())) {
- ISATabUtils.parseInvestigation(stream, xhtml, metadata, context, this.studyFileName);
+ try (InputStream stream = TikaInputStream.get(new File(location + investigation).toPath())) {
+ ISATabUtils.parseInvestigation(stream, xhtml, metadata, context, studyFileName);
}
xhtml.element("h1",
"INVESTIGATION " + metadata.get(ISATabUtils.ISATAB.key("Investigation Identifier")));
@@ -137,12 +138,13 @@ public class ISArchiveParser implements Parser {
ISATabUtils.parseStudy(tis, xhtml, metadata, context);
}
- private void parseAssay(XHTMLContentHandler xhtml, Metadata metadata, ParseContext context)
+ private void parseAssay(String location, XHTMLContentHandler xhtml, Metadata metadata,
+ ParseContext context)
throws IOException, SAXException, TikaException {
// location starts with "/C:" on windows, so build the directory Path from a File
// rather than Paths.get(). The assay file names come from the investigation file and
// are resolved within this directory.
- Path locationDir = new File(this.location).toPath();
+ Path locationDir = new File(location).toPath();
for (String assayFileName : metadata.getValues(studyAssayFileNameField)) {
xhtml.startElement("div");
xhtml.element("h3", "ASSAY " + assayFileName);
diff --git a/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/test/java/org/apache/tika/parser/isatab/ISArchiveParserTest.java b/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/test/java/org/apache/tika/parser/isatab/ISArchiveParserTest.java
index 862365a2d9..8d20ee20b9 100644
--- a/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/test/java/org/apache/tika/parser/isatab/ISArchiveParserTest.java
+++ b/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/test/java/org/apache/tika/parser/isatab/ISArchiveParserTest.java
@@ -108,4 +108,34 @@ public class ISArchiveParserTest extends TikaTest {
assertFalse(handler.toString().contains("OUTSIDE_DIRECTORY_CONTENT"),
"assay reader read a file outside the ISA-Tab directory");
}
+
+ @Test
+ public void testDirectoryNotCachedAcrossParses(@TempDir Path root) throws Exception {
+ Parser parser = new ISArchiveParser();
+ assertEquals("FIRST", parseInvestigationId(parser, writeArchive(root, "first", "FIRST")));
+ assertEquals("SECOND", parseInvestigationId(parser, writeArchive(root, "second", "SECOND")));
+ }
+
+ private static Path writeArchive(Path root, String dirName, String investigationId)
+ throws IOException {
+ Path isaDir = Files.createDirectory(root.resolve(dirName));
+ Files.write(isaDir.resolve("i_test.txt"),
+ ("INVESTIGATION\n"
+ + "Investigation Identifier\t\"" + investigationId + "\"\n"
+ + "STUDY\n"
+ + "Study File Name\t\"s_test.txt\"\n")
+ .getBytes(StandardCharsets.UTF_8));
+ Path study = isaDir.resolve("s_test.txt");
+ Files.write(study, "\"Source Name\"\n\"culture1\"\n".getBytes(StandardCharsets.UTF_8));
+ return study;
+ }
+
+ private static String parseInvestigationId(Parser parser, Path study) throws Exception {
+ Metadata metadata = new Metadata();
+ metadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, "s_test.txt");
+ try (TikaInputStream tis = TikaInputStream.get(study)) {
+ parser.parse(tis, new BodyContentHandler(), metadata, new ParseContext());
+ }
+ return metadata.get("isatab:Investigation Identifier");
+ }
}