001package org.galaxyproject.gxformat2.v19_09.utils;
002
003import java.util.ArrayList;
004import java.util.Arrays;
005import java.util.List;
006
007public class ValidationException extends RuntimeException {
008  private final List<ValidationException> children;
009  private String bullet = "";
010  private String currentMessage;
011
012  public ValidationException(final String message) {
013    this(message, (List<ValidationException>) null);
014  }
015
016  public ValidationException(final String message, final ValidationException child) {
017    this(message, Arrays.asList(child));
018  }
019
020  public ValidationException(final String message, final List<ValidationException> children_) {
021    super(message);
022    this.currentMessage = message;
023    final List<ValidationException> children = new ArrayList<ValidationException>();
024    if (children_ != null) {
025      for (final ValidationException child : children_) {
026        children.addAll(child.simplify());
027      }
028    }
029    this.children = children;
030  }
031
032  public ValidationException withBullet(final String bullet) {
033    this.bullet = bullet;
034    return this;
035  }
036
037  public List<ValidationException> simplify() {
038    if (getMessage().length() > 0) {
039      return Arrays.asList(this);
040    } else {
041      return this.children;
042    }
043  }
044
045  public String summary(final int level, final boolean withBullet) {
046    final int indentPerLevel = 2;
047    final String spaces = new String(new char[level * indentPerLevel]).replace("\0", " ");
048    final String bullet;
049    if (this.bullet.length() > 0 && withBullet) {
050      bullet = this.bullet;
051    } else {
052      bullet = "";
053    }
054    return spaces + bullet + this.currentMessage;
055  }
056
057  public String prettyStr(final Integer level_) {
058    Integer level = level_;
059    if (level == null) {
060      level = 0;
061    }
062    final List<String> parts = new ArrayList<String>();
063    int nextLevel;
064    if (this.currentMessage != null && this.currentMessage.length() > 0) {
065      parts.add(this.summary(level, true));
066      nextLevel = level + 1;
067    } else {
068      nextLevel = level;
069    }
070    for (final ValidationException child : this.children) {
071      parts.add(child.prettyStr(nextLevel));
072    }
073    final String ret = String.join("\n", parts);
074    return ret;
075  }
076
077  public String getMessage() {
078    return this.prettyStr(null);
079  }
080}