Skip to content

Commit

Permalink
Merge pull request dita-ot#4258 from dita-ot/feature/fix-conref-in-ke…
Browse files Browse the repository at this point in the history
…ydef-value

Fix conref in keydef value in preprocess2
  • Loading branch information
jelovirt authored Aug 5, 2023
2 parents e5c87cb + 63c758a commit a18e2c4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 17 deletions.
27 changes: 27 additions & 0 deletions src/main/java/org/dita/dost/module/reader/TopicReaderModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.streams.Predicates;
import org.dita.dost.exception.DITAOTException;
import org.dita.dost.exception.DITAOTXMLErrorHandler;
import org.dita.dost.exception.UncheckedDITAOTException;
Expand All @@ -36,6 +38,7 @@
import org.dita.dost.reader.SubjectSchemeReader;
import org.dita.dost.util.Configuration;
import org.dita.dost.util.Job.FileInfo;
import org.dita.dost.util.URLUtils;
import org.dita.dost.writer.DebugFilter;
import org.dita.dost.writer.NormalizeFilter;
import org.dita.dost.writer.ProfilingFilter;
Expand All @@ -53,6 +56,7 @@
public final class TopicReaderModule extends AbstractReaderModule {

static final QName QNAME_HREF = new QName(ATTRIBUTE_NAME_HREF);
static final QName QNAME_CONREF = new QName(ATTRIBUTE_NAME_CONREF);
static final QName QNAME_SCOPE = new QName(ATTRIBUTE_NAME_SCOPE);
static final QName QNAME_FORMAT = new QName(ATTRIBUTE_NAME_FORMAT);
static final QName QNAME_CLASS = new QName(ATTRIBUTE_NAME_CLASS);
Expand Down Expand Up @@ -212,6 +216,25 @@ private List<Reference> getStartDocuments(final FileInfo startFileInfo) throws D
}
}
});
source
.select(descendant(Predicates.hasAttribute(ATTRIBUTE_NAME_CONREF)))
.forEach(xdmItem -> {
getConref(xdmItem)
.ifPresent(href -> {
FileInfo fi = job.getFileInfo(startFileInfo.src.resolve(href));
if (fi == null) {
fi = job.getFileInfo(tmp.resolve(href));
}
if (fi != null && fi.src != null) {
String format = xdmItem.getAttributeValue(QNAME_ORIG_FORMAT);
if (format == null) {
format = xdmItem.getAttributeValue(QNAME_FORMAT);
}
res.add(new Reference(fi.src, format));
conrefTargetSet.add(fi.src);
}
});
});
} catch (final IOException e) {
throw new DITAOTException(e);
}
Expand All @@ -234,6 +257,10 @@ private URI getHref(final XdmNode in) {
return stripFragment(href);
}

private Optional<URI> getConref(final XdmNode in) {
return Optional.ofNullable(toURI(in.getAttributeValue(QNAME_CONREF))).map(URLUtils::stripFragment);
}

@Override
List<XMLFilter> getProcessingPipe(final URI fileToParse) {
assert fileToParse.isAbsolute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.dita.dost.TestUtils;
import org.dita.dost.exception.DITAOTException;
import org.dita.dost.pipeline.PipelineHashIO;
import org.dita.dost.reader.GenListModuleReader;
import org.dita.dost.store.StreamStore;
Expand All @@ -27,26 +31,42 @@

public class TopicReaderModuleTest {

private final URI ditamap;
private final URI root;

@TempDir
public File tempDir;

private TopicReaderModule reader;
private Job job;

public TopicReaderModuleTest() throws URISyntaxException {
ditamap =
getClass()
.getClassLoader()
.getResource("org/dita/dost/module/reader/TopicReaderModuleTest/src/root.ditamap")
.toURI();
root = ditamap.resolve(".");
}

@BeforeEach
public void setUp() throws SAXException, IOException {
reader = new TopicReaderModule();
reader.setLogger(new TestUtils.TestLogger());
final Job job = new Job(tempDir, new StreamStore(tempDir, new XMLUtils()));
job.setInputFile(URI.create("file:///foo/bar/baz.ditamap"));
job.setInputMap(URI.create("baz.ditamap"));
job.setInputDir(URI.create("file:///foo/bar/"));
job = new Job(tempDir, new StreamStore(tempDir, new XMLUtils()));
job.setInputFile(ditamap);
job.setInputMap(root.relativize(ditamap));
job.setInputDir(root);
job.add(
new Job.FileInfo.Builder()
.src(URI.create("file:///foo/bar/baz.ditamap"))
.uri(URI.create("baz.ditamap"))
.src(ditamap)
.uri(root.relativize(ditamap))
.format(ATTR_FORMAT_VALUE_DITAMAP)
.isInput(true)
.build()
);
job.add(new Job.FileInfo.Builder().src(root.resolve("mysite.dita")).uri(URI.create("mysite.dita")).build());
job.add(new Job.FileInfo.Builder().src(root.resolve("myproduct.dita")).uri(URI.create("myproduct.dita")).build());
reader.setJob(job);
final PipelineHashIO input = new PipelineHashIO();
input.setAttribute(ANT_INVOKER_EXT_PARAM_DITADIR, tempDir.getAbsolutePath());
Expand All @@ -62,44 +82,56 @@ public void setUp() throws SAXException, IOException {

@Test
public void categorizeReferenceFileTopic() throws Exception {
reader.categorizeReferenceFile(new GenListModuleReader.Reference(URI.create("file:///foo/bar/baz.dita")));
reader.categorizeReferenceFile(new GenListModuleReader.Reference(root.resolve("baz.dita")));
assertEquals(0, reader.htmlSet.size());
assertEquals(1, reader.waitList.size());
}

@Test
public void categorizeReferenceFileDitamap() throws Exception {
reader.categorizeReferenceFile(
new GenListModuleReader.Reference(URI.create("file:///foo/bar/baz.ditamap"), ATTR_FORMAT_VALUE_DITAMAP)
);
reader.categorizeReferenceFile(new GenListModuleReader.Reference(ditamap, ATTR_FORMAT_VALUE_DITAMAP));
assertEquals(0, reader.htmlSet.size());
assertEquals(0, reader.waitList.size());
}

@Test
public void categorizeReferenceFileDitaval() throws Exception {
reader.categorizeReferenceFile(
new GenListModuleReader.Reference(URI.create("file:///foo/bar/baz.ditaval"), ATTR_FORMAT_VALUE_DITAVAL)
new GenListModuleReader.Reference(root.resolve("baz.ditaval"), ATTR_FORMAT_VALUE_DITAVAL)
);
assertEquals(0, reader.htmlSet.size());
assertEquals(0, reader.formatSet.size());
}

@Test
public void categorizeReferenceFileHtml() throws Exception {
reader.categorizeReferenceFile(
new GenListModuleReader.Reference(URI.create("file:///foo/bar/baz.html"), ATTR_FORMAT_VALUE_HTML)
);
reader.categorizeReferenceFile(new GenListModuleReader.Reference(root.resolve("baz.html"), ATTR_FORMAT_VALUE_HTML));
assertEquals(1, reader.htmlSet.size());
assertEquals(0, reader.formatSet.size());
}

@Test
public void categorizeReferenceFileImage() throws Exception {
reader.categorizeReferenceFile(
new GenListModuleReader.Reference(URI.create("file:///foo/bar/baz.jpg"), ATTR_FORMAT_VALUE_IMAGE)
);
reader.categorizeReferenceFile(new GenListModuleReader.Reference(root.resolve("baz.jpg"), ATTR_FORMAT_VALUE_IMAGE));
assertEquals(0, reader.htmlSet.size());
assertEquals(1, reader.formatSet.size());
}

@Test
public void readStartFile() throws DITAOTException {
job
.getFileInfo(fi -> fi.isInput)
.forEach(fi -> {
try {
Files.copy(Paths.get(fi.src), Paths.get(job.tempDirURI.resolve(fi.uri)));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
reader.readStartFile();

assertEquals(1, reader.nonConrefCopytoTargetSet.size());
assertEquals(1, reader.conrefTargetSet.size());
assertEquals(2, reader.waitList.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<map xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" class="- map/map "
domains="(map mapgroup-d) (topic abbrev-d) (topic delay-d) a(props deliveryTarget) (map ditavalref-d) (map glossref-d) (topic hazard-d) (topic hi-d) (topic indexing-d) (topic markup-d) (topic pr-d) (topic relmgmt-d) (topic sw-d) (topic ui-d) (topic ut-d) (topic markup-d xml-d) "
ditaarch:DITAArchVersion="1.3">
<title class="- topic/title ">Test Conref and Keyref</title>
<keydef class="+ map/topicref mapgroup-d/keydef " keys="product-keyword" processing-role="resource-only">
<topicmeta class="- map/topicmeta ">
<keywords class="- topic/keywords ">
<keyword class="- topic/keyword " conref="mysite.dita#mysite/keyword"/>
</keywords>
</topicmeta>
</keydef>
<topicref class="- map/topicref " href="myproduct.dita"/>
</map>

0 comments on commit a18e2c4

Please sign in to comment.