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

org.jvnet.jaxbvalidation.event.ObjectValidationEvent Maven / Gradle / Ivy

package org.jvnet.jaxbvalidation.event;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventLocator;

import org.jvnet.jaxbcommons.locator.ObjectLocator;
import org.jvnet.jaxbvalidation.problem.Problem;

/**
 * Validation event.
 *
 * @author Aleksei Valikov
 */
public class ObjectValidationEvent implements ValidationEvent {
  /**
   * Event locator.
   */
  protected final ObjectLocator locator;

  /**
   * The problem.
   */
  protected final Problem problem;

  /**
   * Constructs a new validation event.
   *
   * @param locator locator (where).
   * @param problem problem (what).
   */
  public ObjectValidationEvent(
      final ObjectLocator locator,
      final Problem problem) {
    this.locator = locator;
    this.problem = problem;
  }

  /**
   * Validation events are always {@link ValidationEvent#WARNING}.
   *
   * @return Event severity.
   */
  public int getSeverity() {
    return ValidationEvent.WARNING;
  }

  public ValidationEventLocator getLocator() {
    return locator;
  }

  /**
   * Returns locator as a {@link ObjectLocator}.
   *
   * @return Locator as a {@link ObjectLocator}.
   */
  public ObjectLocator getValidationEventLocator() {
    return locator;
  }

  public Throwable getLinkedException() {
    return problem;
  }

  /**
   * Returns the linked problem.
   *
   * @return The problem.
   */
  public Problem getProblem() {
    return problem;
  }

  /**
   * Returns code of the message.
   *
   * @return Code of the message.
   */
  public String getMessageCode() {
    return getClass().getName();
  }

  /**
   * Returns event message.
   *
   * @param bundle resource bundle.
   * @return event message.
   */
  public String getMessage(final ResourceBundle bundle) {
    final Object[] messageParameters = new Object[]{
        getValidationEventLocator().getMessage(bundle),
        getProblem().getMessage(bundle) };
    try {
      return MessageFormat.format(bundle.getString(getMessageCode()), messageParameters);

    }
    catch (MissingResourceException mrex) {
      return MessageFormat.format("Location:\n{0}\nProblem:\n{1}", messageParameters);
    }
  }

  /**
   * Returns event message.
   *
   * @return Event message.
   */
  public String getMessage() {
    final ResourceBundle bundle = ResourceBundle.getBundle(getClass().getPackage().getName()
        + ".messages");
    final Object[] messageParameters = new Object[]{
        getValidationEventLocator().getMessage(),
        getProblem().getMessage() };
    try {
      return MessageFormat.format(bundle.getString(getMessageCode()), messageParameters);

    }
    catch (MissingResourceException mrex) {
      return MessageFormat.format("Location:\n{0}\nProblem:\n{1}", messageParameters);
    }
  }

  public int hashCode() {
    return getLinkedException().hashCode() + getLocator().hashCode() * 37;
  }

  public boolean equals(final Object obj) {
    boolean result = false;
    if (obj instanceof ValidationEvent) {
      final ValidationEvent event = (ValidationEvent) obj;
      result = (getSeverity() == event.getSeverity())
          && (getLocator().equals(event.getLocator()))
          && (getLinkedException().equals(event.getLinkedException()));
    }
    return result;
  }

  public String toString() {
    return getMessage();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy