Save This Page
Home » openjdk-7 » 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    * @see javax.xml.bind.Unmarshaller
   52    * @see javax.xml.bind.Validator
   53    * @see javax.xml.bind.ValidationEventHandler
   54    * @since JAXB1.0
   55    */
   56   public class DefaultValidationEventHandler implements ValidationEventHandler {
   57   
   58       public boolean handleEvent( ValidationEvent event ) {
   59   
   60           if( event == null ) {
   61               throw new IllegalArgumentException();
   62           }
   63   
   64           // calculate the severity prefix and return value
   65           String severity = null;
   66           boolean retVal = false;
   67           switch ( event.getSeverity() ) {
   68               case ValidationEvent.WARNING:
   69                   severity = Messages.format( Messages.WARNING );
   70                   retVal = true; // continue after warnings
   71                   break;
   72               case ValidationEvent.ERROR:
   73                   severity = Messages.format( Messages.ERROR );
   74                   retVal = false; // terminate after errors
   75                   break;
   76               case ValidationEvent.FATAL_ERROR:
   77                   severity = Messages.format( Messages.FATAL_ERROR );
   78                   retVal = false; // terminate after fatal errors
   79                   break;
   80               default:
   81                   assert false :
   82                       Messages.format( Messages.UNRECOGNIZED_SEVERITY,
   83                               event.getSeverity() );
   84           }
   85   
   86           // calculate the location message
   87           String location = getLocation( event );
   88   
   89           System.out.println(
   90               Messages.format( Messages.SEVERITY_MESSAGE,
   91                                severity,
   92                                event.getMessage(),
   93                                location ) );
   94   
   95           // fail on the first error or fatal error
   96           return retVal;
   97       }
   98   
   99       /**
  100        * Calculate a location message for the event
  101        *
  102        */
  103       private String getLocation(ValidationEvent event) {
  104           StringBuffer msg = new StringBuffer();
  105   
  106           ValidationEventLocator locator = event.getLocator();
  107   
  108           if( locator != null ) {
  109   
  110               URL url = locator.getURL();
  111               Object obj = locator.getObject();
  112               Node node = locator.getNode();
  113               int line = locator.getLineNumber();
  114   
  115               if( url!=null || line!=-1 ) {
  116                   msg.append( "line " + line );
  117                   if( url!=null )
  118                       msg.append( " of " + url );
  119               } else if( obj != null ) {
  120                   msg.append( " obj: " + obj.toString() );
  121               } else if( node != null ) {
  122                   msg.append( " node: " + node.toString() );
  123               }
  124           } else {
  125               msg.append( Messages.format( Messages.LOCATION_UNAVAILABLE ) );
  126           }
  127   
  128           return msg.toString();
  129       }
  130   }

Save This Page
Home » openjdk-7 » javax » xml » bind » helpers » [javadoc | source]