99import java .util .List ;
1010import java .util .Map ;
1111import java .util .Map .Entry ;
12+ import java .util .Set ;
1213import java .util .concurrent .Callable ;
1314
1415import org .xml .sax .SAXException ;
@@ -50,6 +51,37 @@ public static void main(final String[] args) {
5051 System .exit (exitCode );
5152 }
5253
54+ private static void serialiseProfile (final Set <Entry <Part , List <Source >>> entries , final boolean isPdf , final Path root )
55+ throws IOException {
56+ for (final Entry <Part , List <Source >> entry : entries ) {
57+ try (Writer writer = new FileWriter (root .resolve (entry .getKey ().getFileName ()).toFile ())) {
58+ serialisePart (entry .getKey (), entry .getValue (), isPdf , writer );
59+ }
60+ }
61+ }
62+
63+ private static void serialisePart (final Part part , final List <Source > sources , final boolean isPdf ,
64+ final Writer writer ) throws IOException {
65+ boolean isFirst = true ;
66+ for (final Source section : sources ) {
67+ if (Part .APPENDICES .equals (part )) {
68+ final Map <String , Object > context = new java .util .HashMap <>();
69+ context .put ("heading" , section .heading );
70+ context .put ("label" , section .label );
71+ context .put ("isFirst" , isFirst );
72+ isFirst = false ;
73+ Utilities .serialiseToTemplate ("eu/dilcis/csip/out/appendix_heading.mustache" , context ,
74+ writer );
75+ }
76+ section .serialise (writer , isPdf );
77+ writer .write ("\n " );
78+ }
79+ if (Part .BODY .equals (part )) {
80+ writer .write ("!INCLUDE \" appendices.md\" \n " );
81+ }
82+ writer .flush ();
83+ }
84+
5385 @ Parameters (paramLabel = "FILE" , arity = "1..*" , description = "A list of METS Profile documents to be processed." )
5486 private File [] metsProfiles ;
5587
@@ -75,54 +107,8 @@ public Integer call() {
75107 try {
76108 final StructFileParser structParser = StructFileParser .parserInstance (this .processProfiles ());
77109 final SpecificationStructure specStructure = structParser .parseStructureFile (this .structureFile .toPath ());
78- boolean isFirst = true ;
79- for (Entry <Part , List <Source >> entry : specStructure .content .entrySet ()) {
80- try (Writer writer = new FileWriter (this .destination .resolve (entry .getKey ().getFileName ()).toFile ())) {
81- if (Part .BODY .equals (entry .getKey ())) {
82- writer .write ("!TOC\n \n " );
83- }
84- for (Source section : entry .getValue ()) {
85- if (Part .APPENDICES .equals (entry .getKey ())) {
86- Map <String , Object > context = new java .util .HashMap <>();
87- context .put ("heading" , section .heading );
88- context .put ("label" , section .label );
89- context .put ("isFirst" , isFirst );
90- isFirst = false ;
91- Utilities .serialiseToTemplate ("eu/dilcis/csip/out/appendix_heading.mustache" , context ,
92- writer );
93- }
94- section .serialise (writer , false );
95- writer .write ("\n " );
96- }
97- if (Part .BODY .equals (entry .getKey ())) {
98- writer .write ("!INCLUDE \" appendices.md\" \n " );
99- }
100- writer .flush ();
101- }
102- }
103- for (Entry <Part , List <Source >> entry : specStructure .content .entrySet ()) {
104- isFirst = true ;
105- try (Writer writer = new FileWriter (
106- this .destination .resolve ("../pdf" ).resolve (entry .getKey ().getFileName ()).toFile ())) {
107- for (Source section : entry .getValue ()) {
108- if (Part .APPENDICES .equals (entry .getKey ())) {
109- Map <String , Object > context = new java .util .HashMap <>();
110- context .put ("heading" , section .heading );
111- context .put ("label" , section .label );
112- context .put ("isFirst" , isFirst );
113- isFirst = false ;
114- Utilities .serialiseToTemplate ("eu/dilcis/csip/out/appendix_heading.mustache" , context ,
115- writer );
116- }
117- section .serialise (writer , true );
118- writer .write ("\n " );
119- }
120- if (Part .BODY .equals (entry .getKey ())) {
121- writer .write ("!INCLUDE \" appendices.md\" \n " );
122- }
123- writer .flush ();
124- }
125- }
110+ serialiseProfile (specStructure .content .entrySet (), false , this .destination );
111+ serialiseProfile (specStructure .content .entrySet (), true , this .destination .resolve ("../pdf" ));
126112 } catch (SAXException | IOException excep ) {
127113 // Basic for now, print the stack trace and trhow it
128114 excep .printStackTrace ();
@@ -136,9 +122,9 @@ public Integer call() {
136122
137123 private List <MetsProfile > processProfiles () throws SAXException , IOException {
138124 final List <MetsProfile > profiles = new ArrayList <>();
139- final MetsProfileParser parser = MetsProfileParser .newInstance ();
140125 for (final File profileXmlFile : this .metsProfiles ) {
141126 try {
127+ final MetsProfileParser parser = MetsProfileParser .newInstance ();
142128 final MetsProfile profile = parser .processXmlProfile (profileXmlFile .toPath ());
143129 profiles .add (profile );
144130 } catch (SAXException | IOException excep ) {
0 commit comments