001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.oscal.lib.model.control;
028
029import gov.nist.secauto.metaschema.model.common.datatype.markup.MarkupLine;
030import gov.nist.secauto.metaschema.model.common.datatype.markup.MarkupMultiline;
031import gov.nist.secauto.metaschema.model.common.datatype.markup.flexmark.InsertAnchorExtension.InsertAnchorNode;
032import gov.nist.secauto.metaschema.model.common.util.CollectionUtil;
033import gov.nist.secauto.metaschema.model.common.util.ObjectUtils;
034import gov.nist.secauto.oscal.lib.model.ControlPart;
035import gov.nist.secauto.oscal.lib.model.Link;
036import gov.nist.secauto.oscal.lib.model.Property;
037
038import java.net.URI;
039import java.util.LinkedList;
040import java.util.List;
041import java.util.Objects;
042import java.util.function.Predicate;
043import java.util.stream.Stream;
044
045import edu.umd.cs.findbugs.annotations.NonNull;
046
047public abstract class AbstractPart implements IPart {
048
049  @Override
050  @NonNull
051  public Stream<InsertAnchorNode> getInserts(@NonNull Predicate<InsertAnchorNode> filter) {
052    MarkupMultiline prose = getProse();
053
054    @NonNull Stream<InsertAnchorNode> retval;
055    if (prose == null) {
056      retval = ObjectUtils.notNull(Stream.empty());
057    } else {
058      List<InsertAnchorNode> result = prose.getInserts(filter);
059      retval = ObjectUtils.notNull(result.stream());
060    }
061    return retval;
062  }
063
064  public Stream<IPart> getPartsRecursively() {
065    return Stream.concat(
066        Stream.of(this),
067        CollectionUtil.listOrEmpty(getParts()).stream()
068            .flatMap(part -> part.getPartsRecursively()));
069  }
070
071  @NonNull
072  public static Builder builder(@NonNull String name) {
073    return new Builder(name);
074  }
075
076  public static class Builder {
077    private String id;
078    @NonNull
079    private final String name;
080    private URI namespace;
081    private String clazz;
082    private MarkupMultiline prose;
083    private MarkupLine title;
084    private final List<Property> props = new LinkedList<>();
085    private final List<Link> links = new LinkedList<>();
086    private final List<ControlPart> parts = new LinkedList<>();
087
088    public Builder(@NonNull String name) {
089      this.name = Objects.requireNonNull(name);
090    }
091
092    @SuppressWarnings("PMD.ShortMethodName")
093    @NonNull
094    public Builder id(@NonNull String value) {
095      this.id = Objects.requireNonNull(value);
096      return this;
097    }
098
099    @NonNull
100    public Builder namespace(@NonNull URI value) {
101      this.namespace = Objects.requireNonNull(value);
102      return this;
103    }
104
105    @NonNull
106    public Builder clazz(@NonNull String value) {
107      this.clazz = Objects.requireNonNull(value);
108      return this;
109    }
110
111    @NonNull
112    public Builder title(@NonNull String markdown) {
113      return title(MarkupLine.fromMarkdown(Objects.requireNonNull(markdown)));
114    }
115
116    @NonNull
117    public Builder title(@NonNull MarkupLine value) {
118      this.title = Objects.requireNonNull(value);
119      return this;
120    }
121
122    @NonNull
123    public Builder prose(@NonNull String markdown) {
124      return prose(MarkupMultiline.fromMarkdown(Objects.requireNonNull(markdown)));
125    }
126
127    @NonNull
128    public Builder prose(@NonNull MarkupMultiline value) {
129      this.prose = Objects.requireNonNull(value);
130      return this;
131    }
132
133    @NonNull
134    public Builder prop(@NonNull Property value) {
135      this.props.add(Objects.requireNonNull(value));
136      return this;
137    }
138
139    @NonNull
140    public Builder link(@NonNull Link value) {
141      this.links.add(Objects.requireNonNull(value));
142      return this;
143    }
144
145    @NonNull
146    public Builder part(@NonNull ControlPart value) {
147      this.parts.add(Objects.requireNonNull(value));
148      return this;
149    }
150
151    @NonNull
152    public ControlPart build() {
153      ControlPart retval = new ControlPart();
154
155      retval.setName(name);
156
157      if (id != null) {
158        retval.setId(id);
159      }
160      if (namespace != null) {
161        retval.setNs(namespace);
162      }
163      if (clazz != null) {
164        retval.setClazz(clazz);
165      }
166      if (prose != null) {
167        retval.setProse(prose);
168      }
169      if (title != null) {
170        retval.setTitle(title);
171      }
172      if (!props.isEmpty()) {
173        retval.setProps(props);
174      }
175      if (!links.isEmpty()) {
176        retval.setLinks(links);
177      }
178      if (!parts.isEmpty()) {
179        retval.setParts(parts);
180      }
181      return retval;
182    }
183  }
184}