forked from dita-ot/dita-ot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensibleAntInvoker.java
906 lines (797 loc) · 26.8 KB
/
ExtensibleAntInvoker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/*
* This file is part of the DITA Open Toolkit project.
*
* Copyright 2004, 2008 IBM Corporation
*
* See the accompanying LICENSE file for applicable license.
*/
package org.dita.dost.ant;
import static java.util.Arrays.asList;
import static org.dita.dost.util.Constants.*;
import static org.dita.dost.util.FileUtils.supportedImageExtensions;
import static org.dita.dost.util.URLUtils.toFile;
import com.google.common.collect.ImmutableSet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Location;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.*;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.Resources;
import org.dita.dost.exception.DITAOTException;
import org.dita.dost.log.DITAOTAntLogger;
import org.dita.dost.log.MessageUtils;
import org.dita.dost.module.AbstractPipelineModule;
import org.dita.dost.module.ModuleFactory;
import org.dita.dost.module.XmlFilterModule;
import org.dita.dost.module.XmlFilterModule.FilterPair;
import org.dita.dost.module.XsltModule;
import org.dita.dost.pipeline.PipelineHashIO;
import org.dita.dost.store.Store;
import org.dita.dost.store.StreamStore;
import org.dita.dost.util.Configuration.Mode;
import org.dita.dost.util.Constants;
import org.dita.dost.util.Job;
import org.dita.dost.util.Job.FileInfo;
import org.dita.dost.util.XMLUtils;
import org.dita.dost.writer.AbstractXMLFilter;
/**
* Ant task for executing pipeline modules.
*
* @author Deborah Pickett
*/
public final class ExtensibleAntInvoker extends Task {
private DITAOTAntLogger logger;
private final ModuleFactory factory = ModuleFactory.instance();
/**
* Pipeline attributes and parameters
*/
private final Map<String, String> attrs = new HashMap<>();
/**
* Nested params.
*/
private final ArrayList<ParamElem> pipelineParams;
/**
* Nested modules.
*/
private final ArrayList<ModuleElem> modules;
/**
* Temporary directory.
*/
private File tempDir;
private Mode processingMode;
/**
* Constructor.
*/
public ExtensibleAntInvoker() {
super();
pipelineParams = new ArrayList<>();
modules = new ArrayList<>();
}
/**
* Set message.
*
* @param m message
*/
public void setMessage(final String m) {
attrs.put("message", m);
}
/**
* Set input map.
*
* @param inputmap input map file, may be relative or absolute
*/
public void setInputmap(final String inputmap) {
attrs.put(ANT_INVOKER_PARAM_INPUTMAP, inputmap);
}
/**
* Set temporary directory.
*
* @param tempdir temporary directory
*/
public void setTempdir(final File tempdir) {
this.tempDir = tempdir.getAbsoluteFile();
attrs.put(ANT_INVOKER_PARAM_TEMPDIR, tempdir.getAbsolutePath());
}
/**
* Handle nested parameters. Add the key/value to the pipeline hash, unless
* the "if" attribute is set and refers to a unset property.
*
* @return parameter
*/
public ParamElem createParam() {
final ParamElem p = new ParamElem();
pipelineParams.add(p);
return p;
}
/**
* Handle nested module elements.
*
* @since 1.6
*/
public void addConfiguredModule(final ModuleElem m) {
modules.add(m);
}
public void addConfiguredXslt(final XsltElem xslt) {
modules.add(xslt);
}
public void addConfiguredSax(final SaxPipeElem filters) {
// filters.setProject(getProject());
modules.add(filters);
}
private void initialize() throws BuildException {
if (tempDir == null) {
tempDir = new File(this.getProject().getProperty(ANT_TEMP_DIR));
if (!tempDir.isAbsolute()) {
tempDir = new File(this.getProject().getBaseDir(), tempDir.getPath());
}
}
if (modules.isEmpty()) {
throw new BuildException("ModuleElem must be specified");
}
attrs.computeIfAbsent(ANT_INVOKER_PARAM_BASEDIR, k -> getProject().getBaseDir().getAbsolutePath());
for (final ParamElem p : pipelineParams) {
if (!p.isValid()) {
throw new BuildException("Incomplete parameter");
}
if (isValid(getProject(), getLocation(), p.getIf(), p.getUnless())) {
attrs.put(p.getName(), p.getValue());
}
}
logger = new DITAOTAntLogger(getProject());
logger.setTask(this);
String mode = getProject().getUserProperty(ANT_INVOKER_EXT_PARAM_PROCESSING_MODE);
if (mode == null) {
mode = getProject().getProperty(ANT_INVOKER_EXT_PARAM_PROCESSING_MODE);
}
if (mode == null) {
mode = Mode.LAX.name();
}
processingMode = Mode.valueOf(mode.toUpperCase());
}
/**
* Execution point of this invoker.
*
* @throws BuildException exception
*/
@Override
public void execute() throws BuildException {
initialize();
final Job job = getJob(getProject());
final XMLUtils xmlUtils = getXmlUtils();
try {
for (final ModuleElem m : modules) {
m.setProject(getProject());
m.setLocation(getLocation());
final PipelineHashIO pipelineInput = new PipelineHashIO();
for (final Map.Entry<String, String> e : attrs.entrySet()) {
pipelineInput.setAttribute(e.getKey(), e.getValue());
}
AbstractPipelineModule mod = getPipelineModule(m, pipelineInput);
if (pipelineInput.getAttribute(ANT_INVOKER_EXT_PARAM_PROCESSING_MODE) == null) {
String processingMode = Objects.requireNonNullElse(
getProject().getUserProperty(ANT_INVOKER_EXT_PARAM_PROCESSING_MODE),
getProject().getProperty(ANT_INVOKER_EXT_PARAM_PROCESSING_MODE)
);
if (processingMode != null) {
pipelineInput.setAttribute(ANT_INVOKER_EXT_PARAM_PROCESSING_MODE, processingMode);
}
}
long start = System.currentTimeMillis();
mod.setLogger(logger);
mod.setJob(job);
mod.setXmlUtils(xmlUtils);
mod.execute(pipelineInput);
long end = System.currentTimeMillis();
logger.debug("{0} processing took {1} ms", mod.getClass().getSimpleName(), end - start);
}
} catch (final DITAOTException e) {
throw new BuildException(e.getMessage(), e);
}
}
private AbstractPipelineModule getPipelineModule(final ModuleElem m, final PipelineHashIO pipelineInput)
throws DITAOTException {
if (m instanceof final XsltElem xm) {
if (xm.reloadstylesheet && xm.parallel) {
throw new BuildException("Both reloadstylesheet and parallel cannot be true");
}
if (xm.parallel && xm.xmlcatalog != null) {
throw new DITAOTException("Pipeline XSLT task with parallel=true cannot be used with Ant's xmlcatalog");
}
final XsltModule module = new XsltModule();
module.setStyle(toSource(xm.xslResource));
if (xm.in != null) {
module.setSource(xm.in);
module.setResult(xm.out);
} else if (!xm.fileInfoFilters.isEmpty()) {
module.setFileInfoFilter(combine(xm.fileInfoFilters));
module.setDestinationDir(xm.destDir != null ? xm.destDir : tempDir);
} else {
final Set<File> inc = readListFile(xm.includes, logger);
inc.removeAll(readListFile(xm.excludes, logger));
module.setIncludes(inc);
module.setDestinationDir(xm.destDir != null ? xm.destDir : xm.baseDir);
module.setSorceDir(xm.baseDir);
}
module.setFilenameParam(xm.filenameparameter);
module.setFiledirParam(xm.filedirparameter);
module.setReloadstylesheet(xm.reloadstylesheet);
module.setParallel(xm.parallel);
module.setProcessingMode(processingMode);
module.setXMLCatalog(xm.xmlcatalog);
if (xm.mapper != null) {
module.setMapper(xm.mapper.getImplementation());
}
if (xm.extension != null) {
module.setExtension(xm.extension);
}
for (final ParamElem p : m.params) {
if (!p.isValid()) {
throw new BuildException("Incomplete parameter");
}
if (isValid(getProject(), getLocation(), p.getIf(), p.getUnless())) {
module.setParam(p.getName(), p.getValue());
}
}
for (final OutputPropertyElem o : ((XsltElem) m).outputProperties) {
if (!o.isValid()) {
throw new BuildException("Incomplete outputproperty");
}
module.setOutputProperty(o.name, o.value);
}
return module;
} else if (m instanceof final SaxPipeElem fm) {
final XmlFilterModule module = new XmlFilterModule();
module.setParallel(fm.parallel);
module.setProcessingMode(processingMode);
final List<FileInfoFilterElem> predicates = new ArrayList<>(fm.getFormat());
predicates.addAll(m.fileInfoFilters);
module.setFileInfoFilter(combine(predicates));
module.setProcessingPipe(fm.getFilters());
return module;
} else {
for (final ParamElem p : m.params) {
if (!p.isValid()) {
throw new BuildException("Incomplete parameter");
}
if (isValid(getProject(), getLocation(), p.getIf(), p.getUnless())) {
pipelineInput.setAttribute(p.getName(), p.getValue());
}
}
final AbstractPipelineModule module = factory.createModule(m.getImplementation());
module.setProcessingPipe(m.getFilters());
if (!m.fileInfoFilters.isEmpty()) {
module.setFileInfoFilter(combine(m.fileInfoFilters));
}
module.setParallel(m.parallel);
module.setProcessingMode(processingMode);
return module;
}
}
private Source toSource(final Resource style) {
if (style instanceof FileResource) {
return new StreamSource(((FileResource) style).getFile());
} else {
throw new BuildException(String.format("%s not supported", style.getClass().toString()));
}
}
private static Predicate<FileInfo> combine(final Collection<FileInfoFilterElem> filters) {
if (filters.isEmpty()) {
return f -> true;
}
final List<Predicate<FileInfo>> res = filters.stream().map(FileInfoFilterElem::toFilter).toList();
return f -> {
for (final Predicate<FileInfo> filter : res) {
if (filter.test(f)) {
return true;
}
}
return false;
};
}
/**
* Get job configuration from Ant project reference or create new.
*
* @param project Ant project
* @return job configuration
*/
public static Job getJob(final Project project) {
File tempDir = toFile(project.getUserProperty(ANT_TEMP_DIR));
if (tempDir == null) {
tempDir = toFile(project.getProperty(ANT_TEMP_DIR));
}
if (tempDir == null) {
throw new IllegalStateException(String.format("Ant property %s not set", ANT_TEMP_DIR));
}
Job job = project.getReference(ANT_REFERENCE_JOB);
if (job != null) {
if (job.isStale()) {
project.log("Reload stale job configuration reference", Project.MSG_ERR);
try {
job = new Job(tempDir, job.getStore());
} catch (final IOException ioe) {
throw new BuildException(ioe);
}
project.addReference(ANT_REFERENCE_JOB, job);
}
} else {
XMLUtils xmlUtils = project.getReference(ANT_REFERENCE_XML_UTILS);
if (xmlUtils == null) {
project.log("XML utils not found from Ant project reference", Project.MSG_ERR);
xmlUtils = new XMLUtils();
xmlUtils.setLogger(new DITAOTAntLogger(project));
}
Store store = project.getReference(ANT_REFERENCE_STORE);
if (store == null) {
project.log("Store not found from Ant project reference", Project.MSG_ERR);
store = new StreamStore(tempDir, xmlUtils);
}
project.log("Job not found from Ant project reference", Project.MSG_VERBOSE);
try {
job = new Job(tempDir, store);
} catch (final IOException ioe) {
throw new BuildException(ioe);
}
project.addReference(ANT_REFERENCE_JOB, job);
}
return job;
}
@Deprecated
public static Job getJob(final File tempDir, final Project project) {
return getJob(project);
}
/**
* Get XML utils from Ant project reference or create new.
*
* @return XML utils
*/
public XMLUtils getXmlUtils() {
XMLUtils xmlUtils = getProject().getReference(ANT_REFERENCE_XML_UTILS);
if (xmlUtils == null) {
xmlUtils = new XMLUtils();
xmlUtils.setLogger(logger);
getProject().addReference(ANT_REFERENCE_XML_UTILS, xmlUtils);
}
return xmlUtils;
}
private Set<File> readListFile(final List<IncludesFileElem> includes, final DITAOTAntLogger logger) {
final Set<File> inc = new HashSet<>();
for (final IncludesFileElem i : includes) {
if (!isValid(getProject(), getLocation(), i.ifProperty, null)) {
continue;
}
try (BufferedReader r = new BufferedReader(new FileReader(i.file))) {
for (String l = r.readLine(); l != null; l = r.readLine()) {
inc.add(new File(l));
}
} catch (IOException e) {
logger.error("Failed to read includes file " + i.file + ": " + e.getMessage(), e);
}
}
return inc;
}
public static boolean isValid(
final Project project,
final Location location,
final String ifProperty,
final String unlessProperty
) {
if (ifProperty != null) {
final String msg = MessageUtils.getMessage("DOTA014W", "if", "if:set").setLocation(location).toString();
project.log(msg, Project.MSG_WARN);
}
if (unlessProperty != null) {
final String msg = MessageUtils.getMessage("DOTA014W", "unless", "unless:set").setLocation(location).toString();
project.log(msg, Project.MSG_WARN);
}
return (
(ifProperty == null || project.getProperties().containsKey(ifProperty)) &&
(unlessProperty == null || !project.getProperties().containsKey(unlessProperty))
);
}
/**
* Nested pipeline module element configuration.
*
* @since 1.6
*/
public static class ModuleElem {
public final List<XmlFilterElem> filters = new ArrayList<>();
public final List<ParamElem> params = new ArrayList<>();
private Class<? extends AbstractPipelineModule> cls;
public final Collection<FileInfoFilterElem> fileInfoFilters = new ArrayList<>();
private Project project;
private Location location;
protected boolean parallel;
public void setClass(final Class<? extends AbstractPipelineModule> cls) {
this.cls = cls;
}
public void addConfiguredParam(final ParamElem p) {
params.add(p);
}
public void addConfiguredDitaFileset(final FileInfoFilterElem fileInfoFilter) {
fileInfoFilters.add(fileInfoFilter);
}
public void addConfiguredFilter(final XmlFilterElem filter) {
filters.add(filter);
}
public List<FilterPair> getFilters() {
return filters
.stream()
.filter(f -> isValid(getProject(), getLocation(), f.getIf(), f.getUnless()))
.map(f -> {
final Map<String, String> params = f.params
.stream()
.filter(p -> {
if (!p.isValid()) {
throw new IllegalArgumentException(new BuildException("Incomplete parameter"));
}
return isValid(getProject(), getLocation(), p.getIf(), p.getUnless());
})
.collect(Collectors.toMap(ParamElem::getName, ParamElem::getValue));
final List<FileInfoFilterElem> predicates = new ArrayList<>(f.fileInfoFilters);
// predicates.addAll(getFormat());
// assert !predicates.isEmpty();
Predicate<FileInfo> fs = combine(predicates);
return new FilterPair(f.getImplementation(), fs, params);
})
.collect(Collectors.toList());
}
public Class<? extends AbstractPipelineModule> getImplementation() {
return cls;
}
public void setProject(final Project project) {
this.project = project;
}
public Project getProject() {
return project;
}
public void setLocation(final Location location) {
this.location = location;
}
public Location getLocation() {
return location;
}
public void setParallel(final boolean parallel) {
this.parallel = parallel;
}
}
/**
* Nested pipeline XSLT element configuration.
*
* @author jelovirt
*/
public static class XsltElem extends ModuleElem {
private File baseDir;
private File destDir;
private File in;
private File out;
private final List<IncludesFileElem> includes = new ArrayList<>();
private final List<IncludesFileElem> excludes = new ArrayList<>();
private final List<OutputPropertyElem> outputProperties = new ArrayList<>();
private Mapper mapper;
private String extension;
private String filenameparameter;
private String filedirparameter;
private XMLCatalog xmlcatalog;
private boolean reloadstylesheet;
private boolean parallel;
private Resource xslResource;
// Ant setters
public void setStyle(final File style) {
final FileResource fr = new FileResource();
fr.setProject(getProject());
fr.setFile(style);
this.xslResource = fr;
}
public void setBasedir(final File baseDir) {
this.baseDir = baseDir;
}
public void setDestdir(final File destDir) {
this.destDir = destDir;
}
public void setTaskname(final String taskname) {}
public void setClasspathref(final String classpath) {
// Ignore classpathref attribute
}
public void setExtension(final String extension) {
this.extension = extension;
}
public void setReloadstylesheet(final boolean reloadstylesheet) {
this.reloadstylesheet = reloadstylesheet;
}
public void setParallel(final boolean parallel) {
this.parallel = parallel;
}
public void setIn(final File in) {
this.in = in;
}
public void setOut(final File out) {
this.out = out;
}
public void setIncludesfile(final File includesfile) {
final IncludesFileElem i = new IncludesFileElem();
i.setName(includesfile);
includes.add(i);
}
public void setExcludesfile(final File excludesfile) {
final IncludesFileElem i = new IncludesFileElem();
i.setName(excludesfile);
excludes.add(i);
}
public void setFilenameparameter(final String filenameparameter) {
this.filenameparameter = filenameparameter;
}
public void setFiledirparameter(final String filedirparameter) {
this.filedirparameter = filedirparameter;
}
public void addConfiguredStyle(final Resources rc) {
if (rc.size() != 1) {
throw new BuildException("The style element must be specified with exactly one nested resource.");
} else {
this.xslResource = rc.iterator().next();
}
}
public void addConfiguredXmlcatalog(final XMLCatalog xmlcatalog) {
this.xmlcatalog = xmlcatalog;
}
public void addConfiguredMapper(final Mapper mapper) {
if (this.mapper != null) {
throw new BuildException("Cannot define more than one mapper");
} else {
this.mapper = mapper;
}
}
public void addConfiguredIncludesFile(final IncludesFileElem includesFile) {
includes.add(includesFile);
}
public void addConfiguredExcludesFile(final IncludesFileElem excludesFile) {
excludes.add(excludesFile);
}
public void addOutputProperty(final OutputPropertyElem outputProperty) {
outputProperties.add(outputProperty);
}
}
public static class StyleElem extends ConfElem {
// XXX This should be List<ResourceCollection>
private final List<FileSet> filesets = new ArrayList<>();
public void addFileset(final FileSet fileset) {
filesets.add(fileset);
}
}
public static class OutputPropertyElem extends ConfElem {
private String name;
private String value;
public void setName(final String name) {
this.name = name;
}
public void setValue(final String value) {
this.value = value;
}
public boolean isValid() {
return (name != null && value != null);
}
}
public static class IncludesFileElem extends ConfElem {
private File file;
public void setName(final File file) {
this.file = file;
}
}
public static class FileInfoFilterElem extends ConfElem {
private Set<String> formats = Collections.emptySet();
private Boolean hasConref;
private Boolean isInput;
private Boolean isInputResource;
private Boolean isResourceOnly;
public void setFormat(final String format) {
final ImmutableSet.Builder<String> builder = ImmutableSet.<String>builder().add(format);
if (format.equals(ATTR_FORMAT_VALUE_IMAGE)) {
supportedImageExtensions.stream().map(ext -> ext.substring(1)).forEach(builder::add);
}
this.formats = builder.build();
}
public void setConref(final boolean conref) {
this.hasConref = conref;
}
public void setInput(final boolean isInput) {
this.isInput = isInput;
}
public void setInputResource(final boolean isInputResource) {
this.isInputResource = isInputResource;
}
public void setProcessingRole(final String processingRole) {
this.isResourceOnly = processingRole.equals(Constants.ATTR_PROCESSING_ROLE_VALUE_RESOURCE_ONLY);
}
public Predicate<FileInfo> toFilter() {
return f ->
(formats.isEmpty() || formats.contains(f.format != null ? f.format : ATTR_FORMAT_VALUE_DITA)) &&
(hasConref == null || f.hasConref == hasConref) &&
(isInput == null || f.isInput == isInput) &&
(isInputResource == null || f.isInputResource == isInputResource) &&
(isResourceOnly == null || f.isResourceOnly == isResourceOnly);
}
}
/**
* Nested pipeline SAX filter pipe element configuration.
*
* @author jelovirt
*/
public static class SaxPipeElem extends ModuleElem {
private List<String> format;
// Ant setters
public void setFormat(final String format) {
this.format = Collections.singletonList(format);
}
@Override
public List<FilterPair> getFilters() {
return filters
.stream()
.filter(f -> isValid(getProject(), getLocation(), f.getIf(), f.getUnless()))
.map(f -> {
final Map<String, String> params = f.params
.stream()
.filter(p -> {
if (!p.isValid()) {
throw new IllegalArgumentException(new BuildException("Incomplete parameter"));
}
return isValid(getProject(), getLocation(), p.getIf(), p.getUnless());
})
.collect(Collectors.toMap(ParamElem::getName, ParamElem::getValue));
final List<FileInfoFilterElem> predicates = new ArrayList<>(f.fileInfoFilters);
predicates.addAll(getFormat());
assert !predicates.isEmpty();
Predicate<FileInfo> fs = combine(predicates);
return new FilterPair(f.getImplementation(), fs, params);
})
.collect(Collectors.toList());
}
public List<FileInfoFilterElem> getFormat() {
return (format != null ? format : asList(ATTR_FORMAT_VALUE_DITA, ATTR_FORMAT_VALUE_DITAMAP)).stream()
.map(f -> {
final FileInfoFilterElem ff = new FileInfoFilterElem();
ff.setFormat(f);
return ff;
})
.collect(Collectors.toList());
}
}
/**
* Nested pipeline SAX filter element configuration.
*/
public static class XmlFilterElem extends ConfElem {
public final List<FileInfoFilterElem> fileInfoFilters = new ArrayList<>();
public final List<ParamElem> params = new ArrayList<>();
private Class<? extends AbstractXMLFilter> cls;
public void setClass(final Class<? extends AbstractXMLFilter> cls) {
this.cls = cls;
}
public void addConfiguredParam(final ParamElem p) {
params.add(p);
}
public void addConfiguredDitaFileset(final FileInfoFilterElem fileInfoFilter) {
fileInfoFilters.add(fileInfoFilter);
}
public Class<? extends AbstractXMLFilter> getImplementation() {
if (cls == null) {
throw new IllegalArgumentException("class not defined");
}
return cls;
}
}
/**
* Nested parameters.
*/
public static class ParamElem extends ConfElem {
private String name;
private String value;
private final List<ResourceCollection> rcs = new ArrayList<>();
/**
* Get parameter name.
*
* @return parameter name, {@code null} if not set
*/
public String getName() {
return name;
}
/**
* Validate that all required attributes have been set.
*
* @return isValid {@code true} is valid object, otherwise {@code false}
*/
public boolean isValid() {
return (name != null && (value != null || !rcs.isEmpty()));
}
/**
* Set parameter name.
*
* @param s name
*/
public void setName(final String s) {
name = s;
}
/**
* Get parameter value.
* @return parameter value, {@code null} if not set
*/
public String getValue() {
if (!rcs.isEmpty()) {
return rcs
.stream()
.flatMap(ResourceCollection::stream)
.map(ParamElem::resourceToString)
.collect(Collectors.joining(File.pathSeparator));
} else {
return value;
}
}
private static String resourceToString(final Resource r) {
if (r instanceof FileResource) {
return ((FileResource) r).getFile().getAbsolutePath();
} else {
throw new BuildException(String.format("%s not supported as param value", r.getClass()));
}
}
/**
* Set parameter value.
*
* @param v parameter value
*/
public void setExpression(final String v) {
value = v;
}
/**
* Set parameter value.
*
* @param v parameter value
*/
public void setValue(final String v) {
value = v;
}
/**
* Set parameter file value.
*
* @param v parameter file value
*/
public void setLocation(final File v) {
value = v.getPath();
}
/**
* Add resource collection as value.
* @param res resource collection
*/
public void add(final ResourceCollection res) {
rcs.add(res);
}
}
/**
* Add if and unless attributes to element. Use <a href="https://ant.apache.org/manual/ifunless.html">Ant's if and unless attributes</a> instead.
*
* @deprecated since 3.0
*/
@Deprecated
public abstract static class ConfElem {
String ifProperty;
String unlessProperty;
public String getIf() {
return ifProperty;
}
public void setIf(final String p) {
ifProperty = p;
}
public String getUnless() {
return unlessProperty;
}
public void setUnless(final String p) {
unlessProperty = p;
}
}
}