title: jaxb - how to know when eventhandler is called (during validation)
body: <p>I am parsing XML with JAXB and have created an event handler that will display errors if there were issues on validation.</p>
  
  <p>The event handler is called and prints out errors; How do I throw an exception if the event handler is called (after printout?)</p>
  
  <p>In the code, I have no idea when the event handler is called, it is simply called on a validation error; I need the ability to move a file to a /dir/ after the event handler returns.</p>
  
  <hr>
  
  <p>My event handler looks like this:</p>
  
  <pre><code>import base.helper.HelperBase;
  import org.springframework.stereotype.Component;
  
  import javax.xml.bind.ValidationEvent;
  import javax.xml.bind.ValidationEventHandler;
  import java.util.logging.Level;
  
  /**
   *
   */
  @Component
  public class MyValidationEventHandler extends HelperBase implements ValidationEventHandler {
  
      public boolean handleEvent(ValidationEvent event) {
          logger.log(Level.INFO, "\n---");
          System.out.println("EVENT");
          System.out.println("\nEVENT");
          System.out.println("SEVERITY:  " + event.getSeverity());
          System.out.println("MESSAGE:  " + event.getMessage());
          System.out.println("LINKED EXCEPTION:  " + event.getLinkedException());
          System.out.println("LOCATOR");
          System.out.println("    LINE NUMBER:  " + event.getLocator().getLineNumber());
          System.out.println("    COLUMN NUMBER:  " + event.getLocator().getColumnNumber());
          System.out.println("    OFFSET:  " + event.getLocator().getOffset());
          System.out.println("    OBJECT:  " + event.getLocator().getObject());
          System.out.println("    NODE:  " + event.getLocator().getNode());
          System.out.println("    URL:  " + event.getLocator().getURL());
          new Exception("fail");
          return true;
      }
  }
  </code></pre>
  
  <p>When processing, my code looks like this:</p>
  
  <pre><code>    private void processXmlFile(String file) throws Exception {
          // todo: test for file existence, get size, print stats
  
          try {
              logger.log(Level.INFO, "Processing: " + file);
              SchemaFactory sf = null;
              Schema schema = null;
  
              JAXBContext jctx = JAXBContext.newInstance("mypackage.jaxb");
              Unmarshaller unmarshaller = jctx.createUnmarshaller();
  
              if (validate) {
                  sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                  schema = sf.newSchema(new File(xsd));
                  unmarshaller.setSchema(schema);
                  eventHandler.setLogger(logger);
                  unmarshaller.setEventHandler(eventHandler);
              }
  
  
              JAXBElement&lt;MyType&gt; mytype = unmarshaller.unmarshal(new StreamSource(new File(file)), MyType.class);
              MyType ct = mytype.getValue();
  
          } catch (Exception e) { // if find a problem file, just move it out of the way and keep processing
              // if the event handler is called, I want to throw an exception and do something here.
              // move file to failed
              fileUtils.moveFile(config.getErrorDir(), file);
              // on an unmarshall failure, this exception is not thrown/caught because the event handler handles things and returns true
  
  
          }
      }
  </code></pre>