Skip to content

Commit

Permalink
Merge pull request #3262 from dita-ot/feature/import-project
Browse files Browse the repository at this point in the history
Add support for including project files in project files
  • Loading branch information
jelovirt committed Mar 25, 2019
2 parents 094c5eb + 83ea4ef commit d0e6197
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 4 deletions.
17 changes: 16 additions & 1 deletion src/main/java/org/dita/dost/project/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ public class Project {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "deliverable")
public final List<Deliverable> deliverables;
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "include")
public final List<ProjectRef> includes;

@JsonCreator
public Project(@JsonProperty("deliverables") List<Deliverable> deliverables) {
public Project(@JsonProperty("deliverables") List<Deliverable> deliverables,
@JsonProperty("includes") List<ProjectRef> includes) {
this.deliverables = deliverables;
this.includes = includes;
}

public static class Deliverable {
Expand Down Expand Up @@ -173,4 +178,14 @@ public Param(
}
}
}

public static class ProjectRef {
@JacksonXmlProperty(isAttribute = true)
public final URI href;

@JsonCreator
public ProjectRef(@JsonProperty("href") URI href) {
this.href = href;
}
}
}
59 changes: 59 additions & 0 deletions src/test/java/org/dita/dost/project/ProjectFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of the DITA Open Toolkit project.
*
* Copyright 2019 Jarno Elovirta
*
* See the accompanying LICENSE file for applicable license.
*/

package org.dita.dost.project;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.google.common.collect.ImmutableSet;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class ProjectFactory {

private static final ObjectReader reader = new ObjectMapper().reader().forType(Project.class);

public static Project load(final URI file) throws IOException {
try {
return load(file, Collections.emptySet());
} catch (IOException e) {
throw new IOException("Failed to read project file: " + e.getMessage(), e);
}
}

private static Project load(final URI file, final Set<URI> processed) throws IOException {
if (processed.contains(file)) {
throw new RuntimeException("Recursive project file import: " + file);
}
final Project project = reader.readValue(file.toURL());
return resolve(project, file, ImmutableSet.<URI>builder().addAll(processed).add(file).build());
}

private static Project resolve(final Project project, final URI base, final Set<URI> processed) throws IOException {
if (project.includes == null || project.includes.isEmpty()) {
return project;
}
final List<Project.Deliverable> res = project.deliverables != null
? new ArrayList(project.deliverables)
: new ArrayList();
if (project.includes != null) {
for (final Project.ProjectRef projectRef : project.includes) {
final URI href = projectRef.href.isAbsolute() ? projectRef.href : base.resolve(projectRef.href);
final Project ref = load(href, processed);
res.addAll(ref.deliverables);
}
}
return new Project(res, project.includes);
}

}
42 changes: 39 additions & 3 deletions src/test/java/org/dita/dost/project/ProjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,51 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import static org.junit.Assert.*;

public class ProjectTest {

@Test
public void read() throws IOException, URISyntaxException {
final URI file = getClass().getClassLoader().getResource("org/dita/dost/project/simple.json").toURI();
final Project project = ProjectFactory.load(file);
assertEquals(1, project.deliverables.size());
assertNull(project.includes);
}

@Test
public void deserializeXmlSimple() throws IOException {
final ObjectMapper xmlMapper = new XmlMapper();
final InputStream in = getClass().getClassLoader().getResourceAsStream("org/dita/dost/project/simple.xml");
xmlMapper.readValue(in, Project.class);
final Project project = xmlMapper.readValue(in, Project.class);
assertNotNull(project.deliverables);
assertNull(project.includes);
}

@Test
public void deserializeJsonSimple() throws IOException {
final ObjectMapper xmlMapper = new ObjectMapper();
final InputStream in = getClass().getClassLoader().getResourceAsStream("org/dita/dost/project/simple.json");
xmlMapper.readValue(in, Project.class);
final Project project = xmlMapper.readValue(in, Project.class);
assertNotNull(project.deliverables);
assertNull(project.includes);
}

@Test
public void deserializeJsonRoot() throws IOException, URISyntaxException {
final URI input = getClass().getClassLoader().getResource("org/dita/dost/project/root.json").toURI();
final Project project = ProjectFactory.load(input);
assertEquals(2, project.deliverables.size());
assertEquals(2, project.includes.size());
}

@Test(expected = RuntimeException.class)
public void deserializeJsonRecursive() throws IOException, URISyntaxException {
final URI input = getClass().getClassLoader().getResource("org/dita/dost/project/recursive.json").toURI();
ProjectFactory.load(input);
}

@Test
Expand All @@ -54,6 +83,13 @@ public void serializeJsonSimple() throws IOException {
xmlMapper.writeValueAsString(project);
}

@Test
public void serializeJsonRoot() throws IOException {
final ObjectMapper xmlMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
final Project project = new Project(null, Arrays.asList(new Project.ProjectRef(URI.create("simple.json"))));
xmlMapper.writeValueAsString(project);
}

private Project getProject() {
return new Project(Arrays.asList(new Project.Deliverable(
"name",
Expand All @@ -70,6 +106,6 @@ private Project getProject() {
new Publication.Param("args.gen.task.lbl", "YES", null),
new Publication.Param("args.rellinks", "noparent", null)
))
)));
)), null);
}
}
43 changes: 43 additions & 0 deletions src/test/resources/org/dita/dost/project/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"deliverables": [
{
"name": "common-name",
"context": {
"name": "common-Site",
"id": "common-site",
"inputs": {
"inputs": [
{
"href": "site.ditamap"
}
]
},
"profiles": {
"ditavals": [
{
"href": "site.ditaval"
}
]
}
},
"output": "./site",
"publications": {
"transtype": "html5",
"id": "common-sitePub",
"name": "common-Site",
"params": [
{
"name": "args.gen.task.lbl",
"value": "YES",
"href": null
},
{
"name": "args.rellinks",
"value": "noparent",
"href": null
}
]
}
}
]
}
7 changes: 7 additions & 0 deletions src/test/resources/org/dita/dost/project/recursive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"includes": [
{
"href": "recursive.json"
}
]
}
10 changes: 10 additions & 0 deletions src/test/resources/org/dita/dost/project/root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"includes": [
{
"href": "common.json"
},
{
"href": "simple.json"
}
]
}

0 comments on commit d0e6197

Please sign in to comment.