Skip to content

Commit

Permalink
Merge pull request #36 from DILCISBoard/feat/test-parsing
Browse files Browse the repository at this point in the history
FEAT: Structure file parsing
  • Loading branch information
carlwilson authored May 7, 2024
2 parents db8adb7 + 1434de8 commit 5468db0
Show file tree
Hide file tree
Showing 50 changed files with 3,887 additions and 1,027 deletions.
29 changes: 17 additions & 12 deletions src/main/java/eu/dilcis/csip/MetsProfileProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;

Expand All @@ -16,10 +17,11 @@
import eu.dilcis.csip.profile.MetsProfile;
import eu.dilcis.csip.profile.MetsProfileParser;
import eu.dilcis.csip.structure.ParseException;
import eu.dilcis.csip.structure.Source;
import eu.dilcis.csip.structure.SpecificationStructure;
import eu.dilcis.csip.structure.SpecificationStructure.Part;
import eu.dilcis.csip.structure.SpecificationStructure.Section;
import eu.dilcis.csip.structure.StructFileParser;
import eu.dilcis.csip.structure.Utilities;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -74,16 +76,22 @@ public Integer call() {
try {
final StructFileParser structParser = StructFileParser.parserInstance(this.processProfiles());
final SpecificationStructure specStructure = structParser.parseStructureFile(this.structureFile.toPath());
specStructure.serialiseSiteStructure(structParser.getProfiles());
for (Entry<Part, List<Section>> entry : specStructure.content.entrySet()) {
boolean isFirst = true;
for (Entry<Part, List<Source>> entry : specStructure.content.entrySet()) {
try (Writer writer = new FileWriter(this.destination.resolve(entry.getKey().getFileName()).toFile())) {
if (Part.BODY.equals(entry.getKey())) {
writer.write("!TOC\n\n");
}
for (Section section : entry.getValue()) {
try (FileReader reader = new FileReader(section.source.toFile())) {
reader.transferTo(writer);
for (Source section : entry.getValue()) {
if (Part.APPENDICES.equals(entry.getKey())) {
Map<String, Object> context = new java.util.HashMap<>();
context.put("heading", section.heading);
context.put("label", section.label);
context.put("isFirst", isFirst);
isFirst = false;
Utilities.serialiseToTemplate("eu/dilcis/csip/out/appendix_heading.mustache", context, writer);
}
section.serialise(writer, false);
writer.write("\n");
}
if (Part.BODY.equals(entry.getKey())) {
Expand All @@ -92,14 +100,11 @@ public Integer call() {
writer.flush();
}
}
specStructure.serialisePdfStructure(structParser.getProfiles());
for (Entry<Part, List<Section>> entry : specStructure.content.entrySet()) {
for (Entry<Part, List<Source>> entry : specStructure.content.entrySet()) {
try (Writer writer = new FileWriter(
this.destination.resolve("../pdf").resolve(entry.getKey().getFileName()).toFile())) {
for (Section section : entry.getValue()) {
try (FileReader reader = new FileReader(section.source.toFile())) {
reader.transferTo(writer);
}
for (Source section : entry.getValue()) {
section.serialise(writer, true);
writer.write("\n");
}
if (Part.BODY.equals(entry.getKey())) {
Expand Down
55 changes: 0 additions & 55 deletions src/main/java/eu/dilcis/csip/out/ExampleGenerator.java

This file was deleted.

84 changes: 0 additions & 84 deletions src/main/java/eu/dilcis/csip/out/XmlFragmentGenerator.java

This file was deleted.

37 changes: 32 additions & 5 deletions src/main/java/eu/dilcis/csip/profile/Appendix.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package eu.dilcis.csip.profile;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.xml.sax.Attributes;

public final class Appendix {
public static Appendix fromValues(final int number, final String label, final String content) {
public static Appendix fromValues(final int number, final String label, final List<String> content) {
return new Appendix(number, label, content);
}

public final int number;
public final String label;
public final List<String> content;

public final String content;

private Appendix(final int number, final String label, final String content) {
private Appendix(final int number, final String label, final List<String> content) {
this.number = number;
this.label = label;
this.content = content;
this.content = Collections.unmodifiableList(content);
}

@Override
Expand All @@ -32,4 +35,28 @@ public boolean equals(final Object obj) {
final Appendix other = (Appendix) obj;
return number == other.number && Objects.equals(label, other.label) && Objects.equals(content, other.content);
}

static final class Builder extends XmlFragmentGenerator {
private int number;
private String label;

Builder(Attributes attributes) {
this.number = Integer.valueOf(Utilities.getNumber(attributes));
this.label = (Utilities.getLabel(attributes));
}

Builder number(final int number) {
this.number = number;
return this;
}

Builder label(final String label) {
this.label = label;
return this;
}

Appendix build() {
return Appendix.fromValues(number, label, content);
}
}
}
45 changes: 45 additions & 0 deletions src/main/java/eu/dilcis/csip/profile/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package eu.dilcis.csip.profile;

import java.util.List;

import org.xml.sax.Attributes;

public final class Example {
final String id;
final String label;
final List<String> content;

private Example(String id, String label, List<String> content) {
this.id = id;
this.label = label;
this.content = content;
}

static Example fromValues(String id, String label, List<String> content) {
return new Example(id, label, content);
}

static class Builder extends XmlFragmentGenerator {
private String id;
private String label;

Builder(Attributes attributes) {
this.id = Utilities.getId(attributes);
this.label = (Utilities.getLabel(attributes));
}

Builder id(String id) {
this.id = id;
return this;
}

Builder label(String label) {
this.label = label;
return this;
}

Example build() {
return Example.fromValues(id, label, content);
}
}
}
40 changes: 36 additions & 4 deletions src/main/java/eu/dilcis/csip/profile/MetsProfile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.dilcis.csip.profile;

import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -24,8 +25,9 @@ private Details(final URI uri, final String title) {
}

public static MetsProfile fromValues(final Details details, final List<Details> relatedProfiles,
final List<Requirement> requirements) {
return new MetsProfile(details, relatedProfiles, requirements);
final List<Requirement> requirements, final Map<String, Example> examples,
final List<Appendix> appendices, final List<ExternalSchema> schemas, final List<ControlledVocabulary> vocabularies) {
return new MetsProfile(details, relatedProfiles, requirements, examples, appendices, schemas, vocabularies);
}

public final Details details;
Expand All @@ -34,29 +36,59 @@ public static MetsProfile fromValues(final Details details, final List<Details>

private final List<RequirementId> orderedRequirements;
private final Map<RequirementId, Requirement> requirements;
private final Map<String, Example> examples;
private final List<Appendix> appendices;
private final List<ExternalSchema> schemas;
private final List<ControlledVocabulary> vocabularies;

private MetsProfile(final Details details, final List<Details> relatedProfiles,
final List<Requirement> requirements) {
final List<Requirement> requirements, final Map<String, Example> examples, final List<Appendix> appendices,
final List<ExternalSchema> schemas, final List<ControlledVocabulary> vocabularies) {
this.details = details;
this.relatedProfiles = relatedProfiles;
this.orderedRequirements = requirements.stream().map(r -> r.id).collect(Collectors.toList());
this.requirements = requirements.stream().collect(Collectors.toMap(r -> r.id, r -> r));
this.examples = examples;
this.appendices = appendices;
this.schemas = schemas;
this.vocabularies = vocabularies;
}

public final List<Requirement> getRequirements() {
return orderedRequirements.stream().map(this.requirements::get).collect(Collectors.toList());
}

public final List<Requirement> getRequirementsBySection(final Section section) {
return orderedRequirements.stream().map(this.requirements::get).filter(r -> r.details.section.equals(section))
return orderedRequirements.stream().map(this.requirements::get).filter(r -> section.equals(r.details.section))
.collect(Collectors.toList());
}

public List<Section> getSections() {
return orderedRequirements.stream().map(this.requirements::get).map(r -> r.details.section)
.distinct().collect(Collectors.toList());
}

public final Requirement getRequirementById(final RequirementId id) {
return this.requirements.get(id);
}

public final Example getExampleById(final String id) {
return this.examples.get(id);
}

public final URI getUri() {
return details.uri;
}

public List<Appendix> getAppendices() {
return Collections.unmodifiableList(this.appendices);
}

public List<ExternalSchema> getSchema() {
return Collections.unmodifiableList(this.schemas);
}

public List<ControlledVocabulary> getVocabularies() {
return Collections.unmodifiableList(this.vocabularies);
}
}
Loading

0 comments on commit 5468db0

Please sign in to comment.