001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.oscal.lib.profile.resolver.policy;
007
008import com.vladsch.flexmark.util.sequence.BasedSequence;
009
010import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.InsertAnchorExtension;
011import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.InsertAnchorExtension.InsertAnchorNode;
012import gov.nist.secauto.metaschema.core.metapath.format.IPathFormatter;
013import gov.nist.secauto.metaschema.core.metapath.item.node.IModelNodeItem;
014import gov.nist.secauto.metaschema.core.util.CollectionUtil;
015import gov.nist.secauto.metaschema.core.util.CustomCollectors;
016import gov.nist.secauto.metaschema.core.util.ObjectUtils;
017import gov.nist.secauto.oscal.lib.profile.resolver.support.IEntityItem;
018
019import org.apache.logging.log4j.LogManager;
020import org.apache.logging.log4j.Logger;
021
022import java.util.List;
023import java.util.Locale;
024
025import edu.umd.cs.findbugs.annotations.NonNull;
026
027public class InsertReferencePolicy
028    extends AbstractCustomReferencePolicy<InsertAnchorNode> {
029  private static final Logger LOGGER = LogManager.getLogger(InsertReferencePolicy.class);
030
031  public InsertReferencePolicy() {
032    super(IIdentifierParser.IDENTITY_PARSER);
033  }
034
035  @Override
036  protected List<IEntityItem.ItemType> getEntityItemTypes(@NonNull InsertAnchorExtension.InsertAnchorNode insert) {
037    String type = insert.getType().toString();
038
039    if (!"param".equals(type)) {
040      throw new UnsupportedOperationException("unrecognized insert type: " + type);
041    }
042    return CollectionUtil.singletonList(IEntityItem.ItemType.PARAMETER);
043  }
044
045  @Override
046  public String getReferenceText(@NonNull InsertAnchorExtension.InsertAnchorNode insert) {
047    return insert.getIdReference().toString();
048  }
049
050  @Override
051  public void setReferenceText(@NonNull InsertAnchorExtension.InsertAnchorNode insert, @NonNull String newReference) {
052    insert.setIdReference(ObjectUtils.notNull(BasedSequence.of(newReference)));
053  }
054
055  @Override
056  protected boolean handleIndexMiss(
057      @NonNull IModelNodeItem<?, ?> contextItem,
058      @NonNull InsertAnchorExtension.InsertAnchorNode insert,
059      @NonNull List<IEntityItem.ItemType> itemTypes,
060      @NonNull String identifier,
061      @NonNull ReferenceCountingVisitor.Context visitorContext) {
062    if (LOGGER.isErrorEnabled()) {
063      LOGGER.atError().log(
064          "The '{}' insert at '{}' should reference a '{}' identified by '{}'."
065              + " The index did not contain the identifier.",
066          insert.getType().toString(),
067          contextItem.toPath(IPathFormatter.METAPATH_PATH_FORMATER),
068          itemTypes.stream()
069              .map(type -> type.name().toLowerCase(Locale.ROOT))
070              .collect(CustomCollectors.joiningWithOxfordComma("or")),
071          identifier);
072    }
073    return true;
074  }
075
076}