1
2
3
4
5
6 package gov.nist.secauto.oscal.lib.profile.resolver.support;
7
8 import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem;
9 import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
10 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
11 import gov.nist.secauto.oscal.lib.OscalModelConstants;
12
13 import edu.umd.cs.findbugs.annotations.NonNull;
14
15
16
17
18
19
20
21
22
23
24
25 public abstract class AbstractCatalogVisitor<T, R> implements ICatalogVisitor<T, R> {
26
27 protected abstract R newDefaultResult(T state);
28
29 protected abstract R aggregateResults(R first, R second, T state);
30
31 protected R visitCatalog(@NonNull IDocumentNodeItem catalogDocument, T state) {
32 return catalogDocument.modelItems().reduce(
33 newDefaultResult(state),
34 (result, catalogOrGroup) -> visitGroupContainer(
35 ObjectUtils.requireNonNull((IAssemblyNodeItem) catalogOrGroup), result, state),
36 (result1, result2) -> aggregateResults(result1, result2, state));
37 }
38
39
40
41
42
43
44
45
46
47
48
49
50
51 protected R visitGroupContainer(
52 @NonNull IAssemblyNodeItem catalogOrGroup,
53 R initialResult,
54 T state) {
55 R result = catalogOrGroup.getModelItemsByName(OscalModelConstants.QNAME_GROUP).stream()
56 .map(groupItem -> {
57 return visitGroupItem(
58 ObjectUtils.requireNonNull((IAssemblyNodeItem) groupItem),
59 state);
60 })
61 .reduce(initialResult, (first, second) -> aggregateResults(first, second, state));
62 return visitControlContainer(catalogOrGroup, result, state);
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76 protected R visitGroupItem(@NonNull IAssemblyNodeItem group, T state) {
77 R childResult = visitGroupContainer(group, newDefaultResult(state), state);
78 return visitGroupInternal(group, childResult, state);
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92 protected R visitGroupInternal(
93 @NonNull IAssemblyNodeItem group,
94 R childResult,
95 T state) {
96 return visitGroup(group, childResult, state);
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111 protected R visitControlContainer(
112 @NonNull IAssemblyNodeItem catalogOrGroupOrControl,
113 R initialResult,
114 T state) {
115 return catalogOrGroupOrControl.getModelItemsByName(OscalModelConstants.QNAME_CONTROL).stream()
116 .map(control -> {
117 return visitControlItem(ObjectUtils.requireNonNull((IAssemblyNodeItem) control), state);
118 })
119 .reduce(initialResult, (first, second) -> aggregateResults(first, second, state));
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 protected R visitControlItem(@NonNull IAssemblyNodeItem control, T state) {
134 R childResult = visitControlContainer(control, newDefaultResult(state), state);
135 return visitControlInternal(control, childResult, state);
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149 protected R visitControlInternal(@NonNull IAssemblyNodeItem control, R childResult, T state) {
150 return visitControl(control, childResult, state);
151 }
152 }