1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/javax/xml/bind/helpers/DefaultValidationEventHandler.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,131 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package javax.xml.bind.helpers; 1.30 + 1.31 +import org.w3c.dom.Node; 1.32 + 1.33 +import javax.xml.bind.ValidationEvent; 1.34 +import javax.xml.bind.ValidationEventHandler; 1.35 +import javax.xml.bind.ValidationEventLocator; 1.36 +import java.net.URL; 1.37 + 1.38 +/** 1.39 + * <p> 1.40 + * JAXB 1.0 only default validation event handler. This is the default 1.41 + * handler for all objects created from a JAXBContext that is managing 1.42 + * schema-derived code generated by a JAXB 1.0 binding compiler. 1.43 + * 1.44 + * <p> 1.45 + * This handler causes the unmarshal and validate operations to fail on the first 1.46 + * error or fatal error. 1.47 + * 1.48 + * <p> 1.49 + * This handler is not the default handler for JAXB mapped classes following 1.50 + * JAXB 2.0 or later versions. Default validation event handling has changed 1.51 + * and is specified in {@link javax.xml.bind.Unmarshaller} and 1.52 + * {@link javax.xml.bind.Marshaller}. 1.53 + * 1.54 + * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul> 1.55 + * @see javax.xml.bind.Unmarshaller 1.56 + * @see javax.xml.bind.Validator 1.57 + * @see javax.xml.bind.ValidationEventHandler 1.58 + * @since JAXB1.0 1.59 + */ 1.60 +public class DefaultValidationEventHandler implements ValidationEventHandler { 1.61 + 1.62 + public boolean handleEvent( ValidationEvent event ) { 1.63 + 1.64 + if( event == null ) { 1.65 + throw new IllegalArgumentException(); 1.66 + } 1.67 + 1.68 + // calculate the severity prefix and return value 1.69 + String severity = null; 1.70 + boolean retVal = false; 1.71 + switch ( event.getSeverity() ) { 1.72 + case ValidationEvent.WARNING: 1.73 + severity = Messages.format( Messages.WARNING ); 1.74 + retVal = true; // continue after warnings 1.75 + break; 1.76 + case ValidationEvent.ERROR: 1.77 + severity = Messages.format( Messages.ERROR ); 1.78 + retVal = false; // terminate after errors 1.79 + break; 1.80 + case ValidationEvent.FATAL_ERROR: 1.81 + severity = Messages.format( Messages.FATAL_ERROR ); 1.82 + retVal = false; // terminate after fatal errors 1.83 + break; 1.84 + default: 1.85 + assert false : 1.86 + Messages.format( Messages.UNRECOGNIZED_SEVERITY, 1.87 + event.getSeverity() ); 1.88 + } 1.89 + 1.90 + // calculate the location message 1.91 + String location = getLocation( event ); 1.92 + 1.93 + System.out.println( 1.94 + Messages.format( Messages.SEVERITY_MESSAGE, 1.95 + severity, 1.96 + event.getMessage(), 1.97 + location ) ); 1.98 + 1.99 + // fail on the first error or fatal error 1.100 + return retVal; 1.101 + } 1.102 + 1.103 + /** 1.104 + * Calculate a location message for the event 1.105 + * 1.106 + */ 1.107 + private String getLocation(ValidationEvent event) { 1.108 + StringBuffer msg = new StringBuffer(); 1.109 + 1.110 + ValidationEventLocator locator = event.getLocator(); 1.111 + 1.112 + if( locator != null ) { 1.113 + 1.114 + URL url = locator.getURL(); 1.115 + Object obj = locator.getObject(); 1.116 + Node node = locator.getNode(); 1.117 + int line = locator.getLineNumber(); 1.118 + 1.119 + if( url!=null || line!=-1 ) { 1.120 + msg.append( "line " + line ); 1.121 + if( url!=null ) 1.122 + msg.append( " of " + url ); 1.123 + } else if( obj != null ) { 1.124 + msg.append( " obj: " + obj.toString() ); 1.125 + } else if( node != null ) { 1.126 + msg.append( " node: " + node.toString() ); 1.127 + } 1.128 + } else { 1.129 + msg.append( Messages.format( Messages.LOCATION_UNAVAILABLE ) ); 1.130 + } 1.131 + 1.132 + return msg.toString(); 1.133 + } 1.134 +}