All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.enofex.naikan.model.validator.DefaultValidator Maven / Gradle / Ivy

Go to download

The Naikan Model Module for Naikan the software inventory management tool for dev teams driven by our CI/CD pipeline.

The newest version!
package com.enofex.naikan.model.validator;

import com.enofex.naikan.model.Bom;
import com.enofex.naikan.model.BomSchema;
import com.enofex.naikan.model.module.ObjectMapperFactory;
import com.enofex.naikan.model.serializer.SerializerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class DefaultValidator extends BomSchema implements Validator {

  private final ObjectMapper mapper;

  public DefaultValidator() {
    this.mapper = ObjectMapperFactory.newObjectMapper();
  }

  @Override
  public List validate(File file) {
    return validate(file, BomSchema.VERSION_LATEST);
  }

  @Override
  public List validate(File file, BomSchema.Version schemaVersion) {
    try {
      return validate(FileUtils.readFileToString(file, StandardCharsets.UTF_8.name()),
          schemaVersion);
    } catch (IOException e) {
      throw new ValidationException(e);
    }
  }

  @Override
  public List validate(byte[] bomBytes) {
    return validate(bomBytes, BomSchema.VERSION_LATEST);
  }

  @Override
  public List validate(byte[] bomBytes, BomSchema.Version schemaVersion) {
    return validate(new String(bomBytes), schemaVersion);
  }

  @Override
  public List validate(Reader reader) {
    return validate(reader, BomSchema.VERSION_LATEST);
  }

  @Override
  public List validate(Reader reader, BomSchema.Version schemaVersion) {
    try {
      return validate(IOUtils.toString(reader), schemaVersion);
    } catch (IOException e) {
      throw new ValidationException(e);
    }
  }

  @Override
  public List validate(InputStream inputStream) {
    return validate(inputStream, BomSchema.VERSION_LATEST);
  }

  @Override
  public List validate(InputStream inputStream,
      BomSchema.Version schemaVersion) {
    try {
      return validate(IOUtils.toString(inputStream, StandardCharsets.UTF_8), schemaVersion);
    } catch (IOException e) {
      throw new ValidationException(e);
    }
  }

  @Override
  public List validate(String bomString, BomSchema.Version schemaVersion) {
    try {
      return validate(this.mapper.readTree(bomString), schemaVersion);
    } catch (JsonProcessingException e) {
      throw new ValidationException(e);
    }
  }

  @Override
  public List validate(String bomString) {
    try {
      return validate(this.mapper.readTree(bomString), BomSchema.VERSION_LATEST);
    } catch (JsonProcessingException e) {
      throw new ValidationException(e);
    }
  }

  public List validate(JsonNode bomJson, BomSchema.Version schemaVersion) {
    try {
      return jsonSchema(schemaVersion, this.mapper)
          .validate(this.mapper.readTree(bomJson.toString()))
          .stream()
          .map(message -> new ValidationException(message.getMessage()))
          .toList();
    } catch (IOException e) {
      throw new ValidationException(e);
    }
  }

  @Override
  public boolean isValid(File file) {
    return validate(file).isEmpty();
  }

  @Override
  public boolean isValid(File file, BomSchema.Version schemaVersion) {
    return validate(file, schemaVersion).isEmpty();
  }

  @Override
  public boolean isValid(byte[] bomBytes) {
    return validate(bomBytes).isEmpty();
  }

  @Override
  public boolean isValid(byte[] bomBytes, BomSchema.Version schemaVersion) {
    return validate(bomBytes, schemaVersion).isEmpty();
  }

  @Override
  public boolean isValid(String bomString) {
    return validate(bomString).isEmpty();
  }

  @Override
  public boolean isValid(String bomString, BomSchema.Version schemaVersion) {
    return validate(bomString, schemaVersion).isEmpty();
  }

  @Override
  public boolean isValid(Bom bom) {
    return validate(SerializerFactory.newJsonSerializer().toString(bom)).isEmpty();
  }

  @Override
  public boolean isValid(Bom bom, Version schemaVersion) {
    return validate(SerializerFactory.newJsonSerializer().toString(bom), schemaVersion).isEmpty();
  }

  @Override
  public boolean isValid(Reader reader) {
    return validate(reader).isEmpty();
  }

  @Override
  public boolean isValid(Reader reader, BomSchema.Version schemaVersion) {
    return validate(reader, schemaVersion).isEmpty();
  }

  @Override
  public boolean isValid(InputStream inputStream) {
    return validate(inputStream).isEmpty();
  }

  @Override
  public boolean isValid(InputStream inputStream, BomSchema.Version schemaVersion) {
    return validate(inputStream, schemaVersion).isEmpty();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy