Home » xml-commons-external-1.4.01-src » javax » xml » bind » helpers » [javadoc | source]

    1   /*
    2    * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   package javax.xml.bind.helpers;
   26   
   27   import org.w3c.dom.Node;
   28   
   29   import javax.xml.bind.ValidationEvent;
   30   import javax.xml.bind.ValidationEventHandler;
   31   import javax.xml.bind.ValidationEventLocator;
   32   import java.net.URL;
   33   
   34   /**
   35    * <p>
   36    * JAXB 1.0 only default validation event handler. This is the default
   37    * handler for all objects created from a JAXBContext that is managing
   38    * schema-derived code generated by a JAXB 1.0 binding compiler.
   39    *
   40    * <p>
   41    * This handler causes the unmarshal and validate operations to fail on the first
   42    * error or fatal error.
   43    *
   44    * <p>
   45    * This handler is not the default handler for JAXB mapped classes following
   46    * JAXB 2.0 or later versions. Default validation event handling has changed
   47    * and is specified in  {@link javax.xml.bind.Unmarshaller} and
   48    * {@link javax.xml.bind.Marshaller}.
   49    *
   50    * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
   51    * @version $Revision: 1.5 $
   52    * @see javax.xml.bind.Unmarshaller
   53    * @see javax.xml.bind.Validator
   54    * @see javax.xml.bind.ValidationEventHandler
   55    * @since JAXB1.0
   56    */
   57   public class DefaultValidationEventHandler implements ValidationEventHandler {
   58   
   59       public boolean handleEvent( ValidationEvent event ) {
   60   
   61           if( event == null ) {
   62               throw new IllegalArgumentException();
   63           }
   64   
   65           // calculate the severity prefix and return value
   66           String severity = null;
   67           boolean retVal = false;
   68           switch ( event.getSeverity() ) {
   69               case ValidationEvent.WARNING:
   70                   severity = Messages.format( Messages.WARNING );
   71                   retVal = true; // continue after warnings
   72                   break;
   73               case ValidationEvent.ERROR:
   74                   severity = Messages.format( Messages.ERROR );
   75                   retVal = false; // terminate after errors
   76                   break;
   77               case ValidationEvent.FATAL_ERROR:
   78                   severity = Messages.format( Messages.FATAL_ERROR );
   79                   retVal = false; // terminate after fatal errors
   80                   break;
   81               default:
   82                   assert false :
   83                       Messages.format( Messages.UNRECOGNIZED_SEVERITY,
   84                               event.getSeverity() );
   85           }
   86   
   87           // calculate the location message
   88           String location = getLocation( event );
   89   
   90           System.out.println(
   91               Messages.format( Messages.SEVERITY_MESSAGE,
   92                                severity,
   93                                event.getMessage(),
   94                                location ) );
   95   
   96           // fail on the first error or fatal error
   97           return retVal;
   98       }
   99   
  100       /**
  101        * Calculate a location message for the event
  102        *
  103        */
  104       private String getLocation(ValidationEvent event) {
  105           StringBuffer msg = new StringBuffer();
  106   
  107           ValidationEventLocator locator = event.getLocator();
  108   
  109           if( locator != null ) {
  110   
  111               URL url = locator.getURL();
  112               Object obj = locator.getObject();
  113               Node node = locator.getNode();
  114               int line = locator.getLineNumber();
  115   
  116               if( url!=null || line!=-1 ) {
  117                   msg.append( "line " + line );
  118                   if( url!=null )
  119                       msg.append( " of " + url );
  120               } else if( obj != null ) {
  121                   msg.append( " obj: " + obj.toString() );
  122               } else if( node != null ) {
  123                   msg.append( " node: " + node.toString() );
  124               }
  125           } else {
  126               msg.append( Messages.format( Messages.LOCATION_UNAVAILABLE ) );
  127           }
  128   
  129           return msg.toString();
  130       }
  131   }

Home » xml-commons-external-1.4.01-src » javax » xml » bind » helpers » [javadoc | source]