/* **********************************************************************
 * source:    MyMarshallingValidator
 * package:   com.me.my.xml.toys
 * author:    sisjo
 ************************************************************************/
package com.me.my.xml.toys;

import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventLocator;
import java.util.List;
import java.util.Iterator;
import java.util.logging.Logger;

/*************
 * class: MyMarshallingValidator
 * <p>
 * This is a class to help Jaxb Marshaller and Unmarshaller operations in a JaxB world
 */
public final class MyMarshallingValidator extends MyMarshallingBase implements ValidationEventHandler {
  private List<String>  myErrorList;

  /*******************
   * ctor
   */
  public MyMarshallingValidator() {
    this(null, null);
  }

  /*******************
   * ctor
   */
  public MyMarshallingValidator(List<String> el) {
    this(null, el);
  }

  /*******************
   * ctor
   */
  public MyMarshallingValidator(Logger logger, List<String> el) {
    super(logger);
    myErrorList = el;
  }

  /************
   * set the list
   * @param el
   */
  public void setErrorList(List<String> el) {
    myErrorList = el;
  }

  /************
   * get the list
   */
  public List<String> getErrorList() {
    return myErrorList;
  }

  /************
   * clear the list
   */
  public void clear() {
    if (myErrorList != null) {
      myErrorList.clear();
    }
  }

  /************
   * dump and clear the list
   * @param sPrefix
   */
  public void dump(String sPrefix) {
    if (myErrorList != null) {
      if (sPrefix == null) sPrefix = "";
      Iterator<String> it = myErrorList.iterator();
      while(it.hasNext()) {
          reportError(sPrefix + it.next());
      }
      myErrorList.clear();
    }
  }

  /**************
   * IF::handleEvent
   * @param event
   * @return
   */
  public boolean handleEvent(ValidationEvent event) {
    StringBuilder sb = null;
    int sev = ValidationEvent.ERROR;
    boolean ok = true;
    try {
      sb = new StringBuilder(100);
      sb.append("XmlValidationEvent");
      // severity
      sb.append(": ");
      sev = event.getSeverity();
      switch(sev) {
        case ValidationEvent.WARNING:     sb.append("Warning"); break;
        case ValidationEvent.ERROR:       sb.append("Error");   ok = false; break;
        case ValidationEvent.FATAL_ERROR: sb.append("Fatal");   ok = false; break;
      }
      // message
      sb.append(": ");
      String msg = event.getMessage();
      if (okStr(msg, true)) {
        sb.append("\'").append(msg).append("\'");
      }
      else {
        sb.append("NoMessage");
      }
      // location
      sb.append(" @");
      ValidationEventLocator vel = event.getLocator();
      if (vel != null) {
        Object obj = vel.getObject();
        int lin = vel.getLineNumber();
        int col = vel.getColumnNumber();
        int off = vel.getOffset();
        sb.append(" lin:").append(lin);
        sb.append(" col:").append(col);
        sb.append(" off:").append(off);
        sb.append(" @");
        if (obj != null) {
          sb.append(obj.getClass().getName());
        }
        else {
          sb.append("NoObject");
        }
      }
      else {
        sb.append("NoLocation");
      }
    }
    catch(Throwable th) {
      reportException("XmlValidation Exception", th);
      ok = false;
    }
    finally {
      if (sb!= null) {
        if (myErrorList != null) {
          myErrorList.add(sb.toString());
        }
        else {
          reportError(sb.toString());
        }
      }
    }
    return ok;
  }

} //endclass
